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

Lab 4

المعمل
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lab 4

المعمل
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Laboratory Manual

Computer Science II
)GEIT 1412(

Lab # 4
Objectives: In this lab, the following topics will be covered

1. Inheritance (in Java).


2. Overriding, and Hiding.

Task 1 :
Inheritance (in Java):
Inheritance is an important object-oriented concept that allows classes to be reused in
order to define similar, but distinct, classes. In this lab we walk through the
development of a class hierarchy and a program that makes use of several classes in
the hierarchy. We begin by looking at an example of inheritance hierarchy

Student
Name
Id
gpa

getName()
getGpa()
getId()
setName()
print()

Undergrad Graduate
year
thesisTitle
setYear()
getYear() setThesisTitle()
print() getThesisTitle()
print()

The class Student is the parent class. Note that all the variables are private and hence
the child classes can only use them through accessor and mutator methods. Also note
the use of overloaded constructors.
public class Student{
private String name;
private int id;
private double gpa;

public Student(int id, String name, double gpa) {


this.id = id;
this.name = name;
this.gpa = gpa;
}

public Student(int id, double gpa){


this(id, "", gpa);
}

public String getName(){


return name;
}
public int getId() {
return id;
}
public double getGPA(){
return gpa;
}

public void setName(String newName){


this.name = newName;
}
The class Undergrad extends the Student class.

public class Undergrad extends Student


{
private String year;
public Undergrad(int id, String name, double gpa, String year)
{
super(id, name, gpa);
this.year = year;
}

public String getYear() {


return year;
}

public void setYear(String newYear) {


this.year = newYear;
}

public String toString(){

The class Graduate extends the Student class too.

public class Graduate extends Student


{
private String thesisTitle;
public Graduate(int id, String name, double gpa, String thesisTitle)
{
super(id, name, gpa);
this.thesisTitle = thesisTitle;
}

public String getthesisTitle() {


return thesisTitle;
}

public void setThesisTitle(String newthesisTitle) {


this.thesisTitle = newthesisTitle;
}

public String toString() {


return "Graduate " +super.toString()+"\nThesis: "+thesisTitle;

TestStudents is a driver class to test the above classes


public class TestStudents
{
public static void main(String[] args)
{
Student s1 = new Student(97000, "Sameer", 3.51);
Student s2 = new Student(98000, 3.22);
Undergrad u1 = new Undergrad(99000, "Shahid", 2.91, "Junior");
Graduate g1 = new Graduate(200000, "Mubin", 3.57,
"Algorithms and Complexity");

System.out.println(s1);
System.out.println(s2);
System.out.println(u1);
System.out.println(g1);
Student:
ID: 97000
Name: Sameer
GPA: 3.51
Student:
ID: 98000
Name:
GPA: 3.22
Output Undergraduate Student:
ID: 99000
Name: Shahid
GPA: 2.91
Year: Junior
Graduate Student:
ID: 200000
Name: Mubin
GPA: 3.57

Exercise 1:

Consider a superclass PurchaseItem which models customer’s purchases. This class


has:
- two private instance variables name (String) and unitPrice (double).
- One constructor to initialize the instance variables.
- A default constructor to initialize name to “no item”, and unitPrice to 0. use this()
- A method getPrice that returns the unitPrice.
- Accessor and mutator methods.
- A print method to return the name of the item followed by @ symbol, then the
unitPrice.
Consider two subclasses WeighedItem and CountedItem.
WeighedItem has an additional instance variable weight (double) in Kg while
CountedItem has an additional variable quantity (int) both private.
- Write an appropriate constructor for each of the classes making use of the
constructor of the superclass in defining those of the subclasses.
- Override getPrice method that returns the price of the purchasedItem based on its
unit price and weight (WeighedItem), or quantity (CountedItem). Make use of
getPrice of the superclass
- Override also toString method for each class making use of the toString method of
the superclass in defining those of the subclasses.
toString should return something that can be printed on the receipt.
For example

Banana @ 3.00 1.37Kg 4.11 SR (in case of WeighedItem class)


Pens @ 4.5 10 units 45 SR (in case of CountedItem class)
Write an application class where you construct objects from the two subclasses and
print them on the screen.
Exercise 2:

Create a superclass Book. This class has:


- Three private instance variables name (String), author (String) and
yearOfPulication (int).
- One constructor to initialize the instance variables.
- Override toString method to return the name, author, year of publication separated
by (-).
Create two subclasses AudioBook and PaperBook.
AudioBook has an additional instance variable size (int), length (int), and artistName
(String) where as PaperBook has an additional variable publisher (String) and isbn
(String) all of them are private.
- Write an appropriate constructor for each of the classes making use of the
constructor of the superclass in defining those of the subclasses.
- Override toString method for each class making use of the toString method of the
superclass in defining those of the subclasses. toString should return something that
can be printed.

Create BookTest class with main method that will do the following:
Task 2

Overriding, Hiding

File Player.java contains a class that holds information about an athlete: name, team,
and uniform number. File ComparePlayers.java contains a skeletal program that uses
the Player class to read in information about two baseball players and determine
whether or not they are the same player.

1. Fill in the missing code in ComparePlayers so that it reads in two players and
prints “Same player” if they are the same, “Different players” if they are
different. Use the equal’s method, which Player inherits from the Object class,
to determine whether two players are the same. Are the results what you
expect?

2. The problem above is that as defined in the Object class, equals does an
address comparison. It says that two objects are the same if they live at the
same memory location, that is, if the variables that hold references to them are
aliases. The two Player objects in this program are not aliases, so even if they
contain exactly the same information they will be “not equal.” To make equals
compare the actual information in the object, you can override it with a
definition specific to the class. It might make sense to say that two players are
“equal” (the same player) if they are on the same team and have the same
uniform number.

o Use this strategy to define an equal’s method for the Player class. Your
method should take a Player object and return true if it is equal to the
current object, false otherwise
o Test your ComparePlayers program using your modified Player class.
It should give the results you would expect.

You might also like