Lab 6 (Question)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

LAB 6

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.

Question 1 Previous Final Examination

Write TWO (2) programs in Circle.java and TestCircle.java. Both programs shall

fullfill the descriptions that are described in this question.

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

default value of 1.0 and "red", respectively.

 Two overloaded constructors;

 Two public methods: getRadius() and getArea().

The output of the programs are:

The circle has radius of 1.0 and area of 3.141


The circle has radius of 2.0 and area of 12.566

*** NOTE: Assume Math.PI = 3.142

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

public class __________ {


public static void main(String[] args) {
Circle c1 = new Circle();
System.out.println("The circle has radius of "
+ ______getRadius() + " and area of " + _______getArea());

2
LAB 6
Circle c2 = new Circle(2.0);
System.out.println("The circle has radius of "
+ c2.getRadius() + " and area of " + c2.getArea());
}
}

Question 2 Previous Final Examination


Create 3 files named Teacher.java, Primary_School_Teacher.java and

Secondary_School_Teacher.java. The data members and methods in each of these

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

the superclass. You also have to show that method printExamSubjects()and

printMarks()can be accessed in Test.java. The method

printExamSubjects()returns the texts “Science Mathematics” and method

printMarks()returns the value of 200.

3
LAB 6
Teacher
+ name
+ staffNumber
+ setName( String)
+ getName ( )
+ setStaffNo( String)
+ getStaffNo( )

Primary_School_Teacher Secondary_School_Teacher

+ printExamSubjects( ) + printMarks( )

Figure 3

//Teacher.java

public class Teacher {

____________________

public void setName(String Name) {


name = Name;
}

public String getName( ) {


return name;
}

public void setStaffNo(String staffNo) {


staffNumber = staffNo;
}

public String getStaffNo( ) {


return staffNumber;
}
}

PrimarySchoolTeacher.java

4
LAB 6
class PrimarySchoolTeacher extends Teacher {
public String printExamSubjects( ) {
return "Science, Mathematic";
}
}

SecondarySchoolTeacher.java

class SecondarySchoolTeacher extends Teacher{


public int printMarks( ) {
return 200 ;
}
}

TestPrimary_Secondary.java

public class TestPrimary_Secondary {

public static void main(String[] args) {

SecondarySchoolTeacher second = new SecondarySchoolTeacher( );


second.setName("Teacher A");
second.setStaffNo("1234");

System.out.println(second.getName( ) + ": ");


System.out.println(second.getStaffNo( ));
System.out.println(second.printMarks( ));

PrimarySchoolTeacher primer = new PrimarySchoolTeacher( );


primer.setName("Teacher B");

System.out.println(primer.getName( ) + ": ");


System.out.println(primer.printExamSubjects( ));

}}

Question 3 Previous Final Examination

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

desired methods. The example of input and output as follows:

Enter the length in centimeter (CM): 12.5

12.5 cm is equivalent to 4.9212625 inches

Note: The bold text is the input.

import java.util.*;

class LengthConverter {

public static void main(String[] args) {


_______________________

scanner = new Scanner( System.in );


converter = new CmToInches();

//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();

//Display the result


System.out.println(cm + " cm is equivalent to " + inch + "
inches");

}
}

public class _______________ {


private double cm = 0;

public double toInch() {


return cm * 0.393701;
}

public void setCm(double cm) {

6
LAB 6
this.cm = cm;
}

public double getCm() {


return cm;
}
}

Question 4 Previous Final Examination

(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

classes in the myutil package.

(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

example of input and output as follows:

Student name: Sani Sudin


Student age: 19
Student address: Jalan UNITEN
Meal card 1 balance: 100
Meal card 2 balance: 50

Had used meal card 1 to purchase food worth 10-points.


Meal card 1 balance: 90

Purchased additional point. Add 15 points to meal card 2.


Meal card 2 balance: 65

---End of Questions---

You might also like