Lab 6 (Question)
Lab 6 (Question)
Students of 5th Dec 2016, 8.20 am class Please read a notice that I upload in
Moodle (lab exercise section)
For ALL the questions in Lab 6, some parts of the codes have been given to expedite
the process.
Write TWO (2) programs in Circle.java and TestCircle.java. Both programs shall
A class called circle is designed as shown in the following class diagram. It contains:
Two private instance variables: radius (of type double) and color (of type String), with
Circle.java
1
LAB 6
public class Circle {
___________________________
// 1st constructor, which sets both radius and color to default
public Circle() {
radius = 1.0;
color = "red";
}
// 2nd constructor with given radius, but color default
public Circle(double r) {
radius = r;
color = "red";
}
public double ____________ {
return radius;
}
public double ____________ {
return radius*radius*Math.PI;
}
}
TestCircle.java
2
LAB 6
Circle c2 = new Circle(2.0);
System.out.println("The circle has radius of "
+ c2.getRadius() + " and area of " + c2.getArea());
}
}
classes are as shown in Figure 3. Create one more test file named Test.java that tests the
accessibility of methods in class Teacher. Assume the two subclasses inherit all methods from
3
LAB 6
Teacher
+ name
+ staffNumber
+ setName( String)
+ getName ( )
+ setStaffNo( String)
+ getStaffNo( )
Primary_School_Teacher Secondary_School_Teacher
+ printExamSubjects( ) + printMarks( )
Figure 3
//Teacher.java
____________________
PrimarySchoolTeacher.java
4
LAB 6
class PrimarySchoolTeacher extends Teacher {
public String printExamSubjects( ) {
return "Science, Mathematic";
}
}
SecondarySchoolTeacher.java
TestPrimary_Secondary.java
}}
Write a program that inputs length in centimeter and outputs the length in equivalent inch. Your
program must have two classes. The first class named CmtoInches, contains all the method
5
LAB 6
definitions. The second class named LengthConverter, contains the main method and call the
import java.util.*;
class LengthConverter {
//Get input
System.out.print("Enter the length in centimeter (CM): ");
cm = scanner.nextDouble();
converter.setCm(cm);
converter.getCm();
//Convert to inch
inch = converter.toInch();
}
}
6
LAB 6
this.cm = cm;
}
(a) Design MealCard class that keeps track of a student’s food purchases at the campus
cafeteria. A meal card is assigned to an individual student. When a meal card is first issued,
the balance is set to the number of points. If the student does not specify the number of
points, then the initial balance is set to 100 points. A student can purchase additional points
at any time during a semester. Every time food items are bought, points are deducted from
the balance. If the balance becomes negative, the purchase of food items is not allowed.
Create class Student that holds information such as name, age and address. Put both
(b) Then, write another program that tests the classes that are created in Question 4 (a). Define
this test program outside the myutil package. Create two meal card objects in the test
7
LAB 6
program and verify that all methods defined in the meal card class operate correctly. The
---End of Questions---