Java Basics 110– Java Class Inheritance
STEPS
1) Open Startup Project.
1-1 ) Continue from the previous tutorial, or create a new Java Application project using Project/Package Name exams and Main Class ExamDetails.
1-2 ) Your Main Class should contain the following codes.
package exams;
public class ExamDetails {
public static void main(String[] args) {
StudentResults aStudent = new StudentResults();
String sName = aStudent.fullName("Bill Gates");
String exam = aStudent.examName("VB");
String score = aStudent.examScore(30);
String grade = aStudent.examGrade(30);
System.out.println(sName);
System.out.println(exam);
System.out.println(score);
System.out.println(grade);
}
}
class StudentResults {
private String Full_Name;
private String Exam_Name;
private String Exam_Score;
private String Exam_Grade;
StudentResults() {
Full_Name = "No name given";
Exam_Name = "Unknown";
Exam_Score = "No Score";
Exam_Grade = "Unknown";
}
String fullName(String aName) {
Full_Name = aName;
return Full_Name;
}
String examName(String examCode) {
if (examCode.equals("VB")) {
Exam_Name = "Visual Basic.NET";
} else if (examCode.equals("JV")) {
Exam_Name = "Java";
} else if (examCode.equals("C#")) {
Exam_Name = "C# .NET";
} else if (examCode.equals("PH")) {
Exam_Name = "PHP";
} else {
Exam_Name = "No Exam Selected";
}
return Exam_Name;
}
String examScore(int aScore) {
Exam_Score = aScore + " out of 50";
return Exam_Score;
}
private String getGrade(int aScore) {
String examGrade = "";
if (aScore >= 0 && aScore <= 10) {
examGrade = "E";
} else if (aScore >= 11 && aScore <= 20) {
examGrade = "D";
} else if (aScore >= 21 && aScore <= 30) {
examGrade = "C";
} else if (aScore >= 31 && aScore <= 40) {
examGrade = "B";
} else if (aScore >= 41) {
examGrade = "A";
}
return "Grade is " + examGrade;
}
String examGrade(int aScore) {
Exam_Grade = this.getGrade(aScore);
return Exam_Grade;
}
}
|
2) Create new Java Class.
2-1) Create New Java Class.
2-2) Type the name “Certificates”.
2-3) Replace the NetBeans-generated code with the following codes:
package exams;
public class Certificates {
private String certificate;
}
|
3) Change Class to Sub Class
3-1) Modify the Class declaration for Certificates as follows:
package exams;
public class Certificates extends StudentResults{
private String certificate;
}
|
4) Add Constructor
4-1) Insert constructor codes.
package exams;
public class Certificates extends StudentResults{
private String certificate;
Certificates(){
super();
certificate="No certificate awarded";
}
}
|
5) Modify ExamDetails Class.
5-1) Comment all codes in the Main Method.
Select all codes and click Comment button.
5-2) Add new object declaration from Certificates after the codes.
Public static void main(String[] args) {
// StudentResults aStudent = new StudentResults();
// String sName = aStudent.fullName("Bill Gates");
// String exam = aStudent.examName("VB");
// String score = aStudent.examScore(30);
// String grade = aStudent.examGrade(30);
// System.out.println(sName);
// System.out.println(exam);
// System.out.println(score);
// System.out.println(grade);
Certificates c1 = new Certificates();
}
|
5-3) Test the object properties with value assignment and output statement.
public static void main(String[] args) {
// StudentResults aStudent = new StudentResults();
// String sName = aStudent.fullName("Bill Gates");
// String exam = aStudent.examName("VB");
// String score = aStudent.examScore(30);
// String grade = aStudent.examGrade(30);
// System.out.println(sName);
// System.out.println(exam);
// System.out.println(score);
// System.out.println(grade);
Certificates c1 = new Certificates();
String exam = c1.examName("VB");
System.out.println(exam);
}
|
5-4) Output.
6) Add Methods to Sub Class.
6-1) Define new method to sub class.
package exams;
public class Certificates extends StudentResults {
private String certificate;
Certificates() {
super();
certificate = "No certificate awarded";
}
String certificateAwarded(int aScore) {
String aGrade = examGrade(aScore);
if (aGrade.equals("Grade is A")) {
this.certificate = "Certificate of Excellence";
} else if (aGrade.equals("Grade is B")) {
this.certificate = "Certificate of Achievement";
} else if (aGrade.equals("Grade is C")) {
this.certificate = "Certificate of Achievement";
} else {
this.certificate = "No Certificate Awarded";
}
return this.certificate;
}
|
7) Add codes to ExamDetails Class.
public class ExamDetails {
public static void main(String[] args) {
// StudentResults aStudent = new StudentResults();
// String sName = aStudent.fullName("Bill Gates");
// String exam = aStudent.examName("VB");
// String score = aStudent.examScore(30);
// String grade = aStudent.examGrade(30);
// System.out.println(sName);
// System.out.println(exam);
// System.out.println(score);
// System.out.println(grade);
Certificates c1 = new Certificates();
String exam = c1.examName("VB");
String award = c1.certificateAwarded(50);
System.out.println( exam + " " + award );
}
|
8) Final Output.
REFERENCE
http://www.homeandlearn.co.uk/java/java_inheritance.html
No comments:
Post a Comment