Object Oriented Programming in Java with IntelliJ Part 3
Hello, everyone!
I'd like to share about what is and how to code object oriented programming in Java language with IntelliJ.
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.
Now, let's start.
Step 1
Install and run IntelliJ which can be downloaded at here or directly from the community at here. 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.


This is the old code from my post. We'll start from this.
private double Quiz,MidExam,LastExam;
public Score(){
Quiz=0;
MidExam=0;
LastExam=0;
}
public void setQuiz(double x){
if(x>=0 && x<=100)
Quiz=x;
}
public void setMidExam(double x){
if(x>=0 && x<=100)
MidExam=x;
}
public void setLastExam(double x){
if(x>=0 && x<=100)
LastExam=x;
}
public double getQuiz() {
return Quiz;
}
public double getMidExam() {
return MidExam;
}
public double getLastExam() {
return LastExam;
}
public double getScore(){
return 0.2*Quiz+0.3*MidExam+0.5*LastExam;
}
The Complete Code on Screenshot

Before we continue to the code, we should know that one of the main features is inheritance in OOP.
What is inheritance?
Inheritance is a class that get properties from another class, its attribute and its method. The top class is called as superclass or parent class and the class which is inherited from the top class is called as subclass or extended class.
Like in this case, Score is the superclass and Score2 is the subclass.
Step 2
Right-click on the package, then New>Class to create the extended class.

Name it as you want to, but to make it easier to remember, don't name randomly or further than the name of the superclass. Like, Score2.

Now, let's start the basic to extend the class from inheritance. Change the code in the class before to be like this. Score2 class will be able to use Score class's attributes and methods.
public class Score2 extends Score {
}
Now, let's start coding.
I want to add Exercise attribute to make the new one for counting the score.
private double Exercise;
Why private? Because to prevent the value to be directly set from another class. You can check my previous post about access level at here
Now, create the constructors. This is the constructor without parameter. You can set the value from the class directly itself. e.g : Exercise=100; or Exercise=0;, then the value will return 100 or 0.
public Score2(){
super();
Exercise=0;
}
This is the constructor with parameter. You can freely set the value when the constructor's referred as an instance of every object in the tester class later. About this code if(E>=0 && E<=100), I need to create the statement right here because it can be an exception or handler if the value is set lower/higher than it's supposed to be, but why don't code like that for super(Q,M,L);? Because it has been written in the class itself. So, I just call it here from the superclass.
public Score2(double Q, double M, double L, double E){
super(Q,M,L);
if(E>=0 && E<=100)
Exercise=E;
}
As always, we need to create getter and setter, but this won't be used if you use constructor with parameter.
What is setter?
Setter is the method to modify or set the value of the variable.
What is getter?
Getter is the method to get or view the value of the variable.
For example, I will create the getter and setter like this. Why we need to return or make the statement with this at this.Exercise=Exercise;? Because it declares that the Exercise attribute value in this class will be set with Exercise parameter. About this code if(Exercise>=0 && Exercise<=100), it's an exception or handler if the value is set lower/higher than it's supposed to be.
public void setExercise(double Exercise){
if(Exercise>=0 && Exercise<=100)
this.Exercise=Exercise;
}
public double getExercise() {
return this.Exercise;
}
The last for this class. Create the function which is the same as the superclass, but with additional.
super.getScore()+0.1*Exercise; means you call the function in the superclass which is created before and in this new class, you add the additional calculation with the new attribute and it becomes one complete function.
public double getScore(){
return super.getScore()+0.1*Exercise;
}
The Complete Code on Screenshot

Now, back to the superclass. You can modify the code to be like this. It's the same thing you've done with new subclass before.
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;
}
The Complete Code on Screenshot

Now, write the code in the tester class like this. 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. This one if you use the constructor without parameter.
Score2 s1;
s1=new Score2();
You need to set the value with the setter you created before. For Example, it will be completely like this.
Score2 s1;
s1=new Score2();
s1.setQuiz(60);
s1.setMidExam(80);
s1.setLastExam(90);
s1.setExercise(100);
And, this one if you use the constructor with parameter. You can set directly without using setter.
Score2 s2;
s2=new Score2(60,80,90,100);
Here is The Final Result

Posted on Utopian.io - Rewarding Open Source Contributors


Hey @dissgo I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
Your contribution cannot be approved yet. See the Utopian Rules. Please edit your contribution to reapply for approval.
We could use the code from my previous post.You may edit your post here, as shown below:
You can contact us on Discord.
[utopian-moderator]
Done @manishmike10. You can review again.
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thanks for approval @manishmike10
dissgo!! Thank you, your Post. i upvoted.^^
You're welcome :)