0% found this document useful (0 votes)
6 views

Lab 4

Uploaded by

headcodfree
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab 4

Uploaded by

headcodfree
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 4: Course and CourseLab (inheritance)

A) Create a super (parent) class called Course. The Course class represents any
course that does not have a lab (without a lab). The class should include the
followings:
 Private attributes:
o courseCode (type string)
o examMark (type double)
o courseWorkMark (type double)
 A constructor that initializes the three attributes of the class.
 A public and static variable called noOfCourses (stands for number of
courses). The initial value for this variable is 0 and it increases with every
new course object created.
 A public method called finalMark () which returns the total sum of course’
marks. The total mark is computed as (examMark + courseWorkMark).
 A public and static method called getNoOfCourses(). it returns the total
number of courses created.
 An overriden method called toString(), which returns a string that represents
the Course object.

B) Extend the above Course code by creating a sub (child) class called CourseLab.
The CourseLab inherits the super class Course. The class should include the
followings:
 An attribute called labMark (type double), which represents the mark of the
lab.
 A constructor that initializes the attributes of the class. (Hint: don’t forget to
consider the super class’ constructor using the keyword: super).
 An overriden method called finalMark (), which returns the total sum of
course’ marks in this sub class. The total mark in this case is computed as
(examMark + courseWorkMark + labMark).
 An overriden method called toString(), which returns a string that represents
the CourseLab object.

C) Write an application with a main Method that creates objects from the classes
Course and CourseLab and displays the objects information. Your application code
should also display the total number of courses that have been created.
Answer:

package main;
class Course{
private String courseCode;
private double examMark;
public double courseWorkMark;

public static int noOfCourses = 0;

public Course(String cc, double exam, double cw){


courseCode = cc;
examMark = exam;
courseWorkMark = cw;
noOfCourses ++;
}
public double finalMark (){
return examMark + courseWorkMark;
}
public static int getNoOfCourses(){
return noOfCourses ;
}
@Override
public String toString(){
return "Course Info: " + courseCode + " ExamMark: " + examMark
+ " CourseWorkMark: " + courseWorkMark
+ " FinalMark: " + finalMark();
}
}// end Course

class CourseLab extends Course {


private double labMark;
public CourseLab(String cc, double exam, double cw, double lb){
super(cc, exam, cw) ;
labMark = lb;
}
@Override
public double finalMark() {
return super.finalMark() + labMark;
}
@Override
public String toString(){
return super.toString() + " LabMark: "+labMark;
}
}// end CourseLab

public class Main {


public static void main(String [] args) {
System.out.println("Course Application:");
Course javaCourse = new Course("CSC101", 30, 40);
System.out.println(javaCourse.toString());
CourseLab javaCourseLab = new CourseLab("CSC102", 30, 30,15);
System.out.println(javaCourseLab.toString());
System.out.println("Total number of courses created= "+
Course.getNoOfCourses());
}
}// end Main Class

You might also like