Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Tutorial 5 Inheritance and Polymorphism: ! Name It With "Your ID - Name - Date", E.G. 20191963 - 20211108

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Tutorial 5 Inheritance and Polymorphism

NOTE: Put all the source code and answers into a word file, and upload the word file (just
ONE Word file! name it with “your ID_name_date”, e.g. 20191963****_***_20211108.doc).

Attempt ALL questions. Each question worth 2 marks, and 10 marks in total.

1. Complete the following program to make it able to read a list of scores

from console (input from the user), and then analyze the numbers of scores

that are not less than 60, maximum score, and the mean value.

//(1) import necessary classes from the library


class ReadScore {
private Scanner input;
public ReadScore() {
input = new Scanner(System.in);
}
public double[] readInput() {
double[] score;
System.out.print("Enter the number of the scores: ");
int size = input.nextInt();
score = new double[size];
System.out.println("Enter the scores: ");
for(int i=0; i<size; i++) {
score[i] = input.nextDouble();
}
return score;
}
}

class EstimateScore {
private int numberPass;
private double max, mean;
private double[] score;
public EstimateScore(double[] score) {
//(2) initialize the array score with the given value
}
public void calculatePassNumber() {
//(3) add statements here
//to calculate the number of scores that are not less than 60
}
public void calculateMax() {
//(4) add statements here to calculate the maximum score
}
public void calculateMean() {
//(5) add the statements here to calculate the mean value
}
public void displayResults() {
System.out.println("There're " + numberPass + " scores that are not less than
60");
System.out.println("The maximum score is " + max);
System.out.println("The mean value is " + mean);
}
}

public class Question1T5 {


public static void main(String[] args) {
//(6) add the statements here to test all the methods
}
}

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

2. Design two programs named BaseClass and SubClass. In BaseClass, define a


variable xVar (type: char, value: 65), and a method myPrint to print xVar. SubClass
is a subclass of BaseClass. In SubClass, define a variable yVar (type: int, value:
16) and another variable strVar (type: String, value: “java program!”). There is also a
method myPrint to print the value of yVar and strVar in SubClass, as well as an
additional method printAll, in which invoking myPrint method both from the base
class and sub class.
Write a test program. In the main method, declare an instance of SubClass named
bObj. Then use the object to call method printAll to output all the properties of that
object.

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)


 DISCUSSION AND CONCLUSION

3. Design a class named Person and its two subclasses named Student and
Employee. Make Faculty and Staff subclasses of Employee. A person has a name,
address, phone number, and email address. A student has a class status (freshman,
sophomore, junior, or senior). Define the status as a constant. An employee has an
office, salary, and date hired. Define a class named MyDate that contains the fields
year, month, and day. A faculty member has office hours and a rank. A staff
member has a title. Override the toString method in each class to display the class
name and the person’s name.
Draw the UML diagram for the classes. Implement the classes. Write a test program
that creates a Person, Student, Employee, Faculty, and Staff, and invokes their
toString() methods.

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

4. Write a Java application which contains a class called ArrayMethods. The


ArrayMethods class contains methods with the following headers:
public int duplicate(char[] array1, char[] array2)
This method copies each character from array array1 into the same location
of array array2. array2 must have already been instantiated and be of the
same or greater length as array1. The method returns the number of
elements copied.
public boolean same(char[] array1, char[] array2)
This method examines arrays array1 and array2 character by character and
returns false if a character in array1 is different than a character at the
corresponding location in array2 or if the arrays are of different length. The
method returns true if every character of array1 is the same as the
corresponding character in array2.
These are the only methods in the ArrayMethods class.
Then write the test class called TestArrayMethods, within the main() method,
there should be statements as following,
ArrayMethods am = new ArrayMethods();
char[] array1 = {'s','t','r','e','s','s'};
char[] array2;
boolean ok;
//initialize array2 with the size of array1.length, and copy elements
from array1 to array2 using method duplicate
//assign the return value from invoking same method with array1 and
array2 as the parameters, to variable ok
//display the two arrays, and value of ok

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

5. (The Triangle class) Design a class named Triangle that extends


GeometricObject. The class contains:

 Three double data fields named side1, side2, and side3 with default values 1.0
to denote three sides of the triangle.
 A no-arg constructor that creates a default triangle.

 A constructor that creates a triangle with the specified side1, side2, and side3.

 The accessor methods for all three data fields.

 A method named getArea() that returns the area of this triangle.

 A method named getPerimeter() that returns the perimeter of this triangle.

 A method named toString() that returns a string description for the triangle.

For the formula to compute the area of a triangle, see task3 in tutorial 1. And
GeometricObject class can be found on slides within the chapter (or in the textbook).
The toString() method is implemented as follows:

return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;

Draw the UML diagram for the classes Triangle and GeometricObject. Implement
the class. Write a test program that creates a Triangle object with sides 1, 1.5, 1, color
yellow and filled true, and displays the area, perimeter, color, and whether filled or
not.

 SOURCE CODE (TEXT)

 EXECUTION RESULTS (TEXT OR SNAPSHOTS)

 DISCUSSION AND CONCLUSION

You might also like