create account

Object Oriented Programming in Java with IntelliJ Part 4 by dissgo

View this thread on: hive.blogpeakd.comecency.com
· @dissgo · (edited)
$30.54
Object Oriented Programming in Java with IntelliJ Part 4
#### What Will I Learn?
* You will learn abstract class and method in Java
* You will learn interface in Java

#### Requirements
* Intellij
* Basic knowledge about Java programming language

#### Difficulty
* Basic

#### Tutorial Contents
What is Java?
>Java is a computer programming language and computing platform.

What is IntelliJ?
>IntelliJ is an IDE created that make possible for us to code or develop something useful for people.

What is IDE?
>IDE is an Integrated Development Environment that means provide the basic tool for developers to code and test the software. Basicly, it will contain code editor, compiler and debugger.

What is OOP?
>OOP stands for Object Oriented Programming. OOP is a programming concept which applies object concept that the object is consisted of attribute and method.

### Section 1
**Step 1**
Install and run IntelliJ which can be downloaded at [here](http://www.jetbrains.com/idea/download/) or directly from the community at [here](http://www.jetbrains.org/display/IJOS/Download). Yes, it's officially built by community. That's why it's called as community edition from the official jetbrains web and it's free.
![1. Setup.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515221273/qmwt1cwbfu5zzb4j2yyz.png)
<center>
![1. Setup_2.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515221428/kddiixsjbzcz9ux0skav.png)
</center>
This is the old code from my [post](https://steemit.com/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-3). We'll start from this.
```
public class Score {
    private double Quiz,MidExam,LastExam;
    public Score(){
        Quiz=0;
        MidExam=0;
        LastExam=0;
    }
    public Score(double Q, double M, double L){
        if(Q>=0 && Q<=100)
            Quiz=Q;
        if(M>=0 && M<=100)
            MidExam=M;
        if(L>=0 && L<=100)
            LastExam=L;
    }
    public void setQuiz(double Quiz){
        if(Quiz>=0 && Quiz<=100)
            this.Quiz=Quiz;
    }
    public void setMidExam(double MidExam){
        if(MidExam>=0 && MidExam<=100)
            this.MidExam=MidExam;
    }
    public void setLastExam(double LastExam){
        if(LastExam>=0 && LastExam<=100)
            this.LastExam=LastExam;
    }
    public double getQuiz() {
        return this.Quiz;
    }
    public double getMidExam() {
        return this.MidExam;
    }
    public double getLastExam() {
        return this.LastExam;
    }
    public double getScore(){
        return 0.2*Quiz+0.3*MidExam+0.5*LastExam;
    }
}
```

But, before we continue coding, we have to know about the basic knowledge about abstract class and method. We can't create an instance of abstract class or method, because abstract doesn't have a complete implementation. Abstract is created as a function base of subclass. We should extend before we can use it.

**Step 2**
Add `abstract` for `Score`  class.
Before
```
public class Score {
```

After
```
public abstract class Score {
```

Complete Codes will be like this.
```
public abstract class Score {
    private double Quiz,MidExam,LastExam;
    public Score(){
        Quiz=0;
        MidExam=0;
        LastExam=0;
    }
    public Score(double Q, double M, double L){
        if(Q>=0 && Q<=100)
            Quiz=Q;
        if(M>=0 && M<=100)
            MidExam=M;
        if(L>=0 && L<=100)
            LastExam=L;
    }
    public void setQuiz(double Quiz){
        if(Quiz>=0 && Quiz<=100)
            this.Quiz=Quiz;
    }
    public void setMidExam(double MidExam){
        if(MidExam>=0 && MidExam<=100)
            this.MidExam=MidExam;
    }
    public void setLastExam(double LastExam){
        if(LastExam>=0 && LastExam<=100)
            this.LastExam=LastExam;
    }
    public double getQuiz() {
        return this.Quiz;
    }
    public double getMidExam() {
        return this.MidExam;
    }
    public double getLastExam() {
        return this.LastExam;
    }
    public double getScore(){
        return 0.2*Quiz+0.3*MidExam+0.5*LastExam;
    }
}
```

When we've done, we `extend Score2` class, but on my previous [post](https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-3) it should have been done, because we discussed about inheritance.

Complete Codes will be like this.
```
public class Score2 extends Score {
    private double Exercise;
    public Score2(){
        super();
        Exercise=0;
    }
    public Score2(double Q, double M, double L, double E){
        super(Q,M,L);
        if(E>=0 && E<=100)
            Exercise=E;
    }
    public void setExercise(double Exercise){
        if(Exercise>=0 && Exercise<=100)
            this.Exercise=Exercise;
    }
    public double getExercise() {
        return this.Exercise;
    }
    public double getScore(){
        return super.getScore()+0.1*Exercise;
    }
}
```

Now, we want to implement abstract method.
>Abstract method must be implemented in the subclass, because in the superclass abstract method is just declared without the statement. Only name of the method and its parameter.

**Step 3**
Write whatever abstract method you want to, but this is for example. Just add `abstract` to the following method without the statement, because this is in superclass or abstract class.
```
public abstract void sendReport(String to, String subject, String message);
```

Complete Codes will be like this.
```
public abstract class Score {
    private double Quiz,MidExam,LastExam;
    public Score(){
        Quiz=0;
        MidExam=0;
        LastExam=0;
    }
    public Score(double Q, double M, double L){
        if(Q>=0 && Q<=100)
            Quiz=Q;
        if(M>=0 && M<=100)
            MidExam=M;
        if(L>=0 && L<=100)
            LastExam=L;
    }
    public void setQuiz(double Quiz){
        if(Quiz>=0 && Quiz<=100)
            this.Quiz=Quiz;
    }
    public void setMidExam(double MidExam){
        if(MidExam>=0 && MidExam<=100)
            this.MidExam=MidExam;
    }
    public void setLastExam(double LastExam){
        if(LastExam>=0 && LastExam<=100)
            this.LastExam=LastExam;
    }
    public abstract void sendReport(String to, String subject, String message);
    public double getQuiz() {
        return this.Quiz;
    }
    public double getMidExam() {
        return this.MidExam;
    }
    public double getLastExam() {
        return this.LastExam;
    }
    public double getScore(){
        return 0.2*Quiz+0.3*MidExam+0.5*LastExam;
    }
}
```

**Step 4**
Now we can develop or implement the abstract method in the subclass, `Score2`. The method name is the same as what have been declared in `Score` class, but without abstract defined. Because, it's already extended and abstract can't have body or statement itself. So, to develop it, the code will be like this.

```
public void sendReport(String to, String subject, String message){
        System.out.println("Send Report\nTo: "+to+"\nSubject: "+subject+"\nMessage: "+message);
}
```

For Complete Codes, they will be like this.
```
public class Score2 extends Score {
    private double Exercise;
    public Score2(){
        super();
        Exercise=0;
    }
    public Score2(double Q, double M, double L, double E){
        super(Q,M,L);
        if(E>=0 && E<=100)
            Exercise=E;
    }
    public void setExercise(double Exercise){
        if(Exercise>=0 && Exercise<=100)
            this.Exercise=Exercise;
    }
    public void sendReport(String to, String subject, String message){
        System.out.println("Send Report\nTo: "+to+"\nSubject: "+subject+"\nMessage: "+message);
    }
    public double getExercise() {
        return this.Exercise;
    }
    public double getScore(){
        return super.getScore()+0.1*Exercise;
    }
}
```

**Step 5**
Back to the main or tester class. Write the tester to test how your abstract works or implemented. Fill out with the parameters you've defined before.
For Example :
```
s2.sendReport("dissgo@steem.id","Score","Your Quiz Score: "+s2.getQuiz()+"\nYour Mid Exam Score: "+s2.getMidExam()+"\nYour Last Exam Score: "+s2.getLastExam()+"\nYour Exercise Score: "+s2.getExercise());
```

<center>
The Final Result
![Final Result Abstract.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515815301/kdnt3c8bmn2mryoabyos.jpg)
</center>

### Section 2
Now, I want to tell you about interface in Java.
What is interface?
>Interface is a group of abstract methods or a group of methods with empty bodies. All methods in interface should be implemented, while in class, not all of them should be done.

**Step 1**
Create Interface by right-click on the package, **New>Class**
![New Interface.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515839080/xuhowfnftpkpytzh9new.png)

Select `Interface` on the `Kind` field.
![new interface_2](https://user-images.githubusercontent.com/1270785/34903225-a18e8b08-f85f-11e7-866d-614c19f2f833.png)

**Step 2**
Create new class again and name it as you want to.
![New Class Quiz.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515817545/naubxujshcpyyb65dwdu.png)

When you've done, implement your class to the interface we've created before.
```
public class Quiz implements FinalScore
```

Complete codes will be like this.
![New Class Quiz Implements.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515817572/dd4achyiyogfmqaufz32.png)

**Step 3**
Now, back to `FinalScore` interface which has been made. Declares whatever you want to implement. If I want to know final score for quiz, the complete codes will be like this.
```
public interface FinalScore {
    double getQuiz();
}
```

**Step 4**
Now, get back to `Quiz` class and write or develop the methods. For example, I want to know what's my total score for Quiz and what's the final score.  `totalquiz` is how much the quiz score in total and `return totalquiz/4;` is how we calculate the final quiz score, because we have 4 times a semester for one subject. So, the complete codes will be like this.
```
public class Quiz implements FinalScore {
    public double totalquiz;
    public double getQuiz(){
        return totalquiz/4;
    }
}
```

**Step 5**
Write for testing the interface you've implemented. As always, create new Object of your class you created before in this tester class first, because It will refer as an instance of every object created, `Quiz q=new Quiz();`. `q.totalquiz=340;` means we set the value for `totalquiz` which has been created before. `System.out.println("Total Score Quiz: "+q.totalquiz+"\nFinal Score Quiz: "+q.getQuiz());` is the output to be displayed.

Complete codes will be like this.
```
public class Main {
    public static void main(String[] args) {
        Quiz q=new Quiz();
        q.totalquiz=340;
        System.out.println("Total Score Quiz: "+q.totalquiz+"\nFinal Score Quiz: "+q.getQuiz());
    }
}
```

<center>
The Final Result
![Final Result Interface.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515819085/mopdaaocmiidko4aqgtd.jpg)
</center>

#### Curriculum
- [Object Oriented Programming in Java with Intellij Part 1](https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-1)
- [Object Oriented Programming in Java with Intellij Part 2](https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-2)
- [Object Oriented Programming in Java with Intellij Part 3](https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-3)
    

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-4">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , ,
properties (23)
authordissgo
permlinkobject-oriented-programming-in-java-with-intellij-part-4
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":2489216,"name":"intellij-community","full_name":"JetBrains/intellij-community","html_url":"https://github.com/JetBrains/intellij-community","fork":false,"owner":{"login":"JetBrains"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","tutorial","java","programming","opensource"],"users":["dissgo","steem.id"],"links":["http://www.jetbrains.com/idea/download/","http://www.jetbrains.org/display/IJOS/Download","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515221273/qmwt1cwbfu5zzb4j2yyz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515221428/kddiixsjbzcz9ux0skav.png","https://steemit.com/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-3","https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-3","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515815301/kdnt3c8bmn2mryoabyos.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515839080/xuhowfnftpkpytzh9new.png","https://user-images.githubusercontent.com/1270785/34903225-a18e8b08-f85f-11e7-866d-614c19f2f833.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515817545/naubxujshcpyyb65dwdu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515817572/dd4achyiyogfmqaufz32.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515819085/mopdaaocmiidko4aqgtd.jpg","https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-1","https://utopian.io/utopian-io/@dissgo/object-oriented-programming-in-java-with-intellij-part-2"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515221273/qmwt1cwbfu5zzb4j2yyz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515221428/kddiixsjbzcz9ux0skav.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515815301/kdnt3c8bmn2mryoabyos.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515839080/xuhowfnftpkpytzh9new.png","https://user-images.githubusercontent.com/1270785/34903225-a18e8b08-f85f-11e7-866d-614c19f2f833.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515817545/naubxujshcpyyb65dwdu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515817572/dd4achyiyogfmqaufz32.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1515819085/mopdaaocmiidko4aqgtd.jpg"],"moderator":{"account":"manishmike10","time":"2018-01-13T13:30:55.718Z","reviewed":true,"pending":false,"flagged":false}}
created2018-01-13 05:00:57
last_update2018-01-13 13:30:54
depth0
children4
last_payout2018-01-20 05:00:57
cashout_time1969-12-31 23:59:59
total_payout_value21.154 HBD
curator_payout_value9.384 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11,681
author_reputation3,860,675,847,142
root_title"Object Oriented Programming in Java with IntelliJ Part 4"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,153,899
net_rshares4,733,065,645,433
author_curate_reward""
vote details (8)
@manishmike10 ·
$0.15
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
👍  
properties (23)
authormanishmike10
permlinkre-dissgo-object-oriented-programming-in-java-with-intellij-part-4-20180113t133059561z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-13 13:31:00
last_update2018-01-13 13:31:00
depth1
children1
last_payout2018-01-20 13:31:00
cashout_time1969-12-31 23:59:59
total_payout_value0.154 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation20,399,732,899,016
root_title"Object Oriented Programming in Java with IntelliJ Part 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,229,766
net_rshares19,205,608,082
author_curate_reward""
vote details (1)
@dissgo ·
Thanks for approval @manishmike10
properties (22)
authordissgo
permlinkre-manishmike10-re-dissgo-object-oriented-programming-in-java-with-intellij-part-4-20180113t222135119z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["manishmike10"],"app":"steemit/0.1"}
created2018-01-13 22:21:36
last_update2018-01-13 22:21:36
depth2
children0
last_payout2018-01-20 22:21:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length33
author_reputation3,860,675,847,142
root_title"Object Oriented Programming in Java with IntelliJ Part 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,325,495
net_rshares0
@piotras ·
$0.17
Intellij is my favorit
👍  
properties (23)
authorpiotras
permlinkre-dissgo-object-oriented-programming-in-java-with-intellij-part-4-20180119t094153604z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-19 09:41:54
last_update2018-01-19 09:41:54
depth1
children0
last_payout2018-01-26 09:41:54
cashout_time1969-12-31 23:59:59
total_payout_value0.171 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation434,677,846,131
root_title"Object Oriented Programming in Java with IntelliJ Part 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,587,729
net_rshares17,440,040,187
author_curate_reward""
vote details (1)
@utopian-io ·
### Hey @dissgo I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-dissgo-object-oriented-programming-in-java-with-intellij-part-4-20180113t171118131z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-13 17:11:18
last_update2018-01-13 17:11:18
depth1
children0
last_payout2018-01-20 17:11:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,504
author_reputation152,955,367,999,756
root_title"Object Oriented Programming in Java with IntelliJ Part 4"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,272,957
net_rshares0