Exercise User Defined Class 3
TASK 1
a)
Write a complete Student class.
public class Student {
private String matricNo;
private double test1;
private double test2;
private double averageMark;
public
Student(String matric, double ts1, double ts2 ){
this.matricNo
= matric;
this.test1
= test1;
this.test2
= test2;
}
public
String getStudentInfo()
{
return
matricNo+"\t\t"+averageMark;
}
public void calculateAverage()
{
averageMark
= (test1 + test2)/2;
}
}
b)
Next, write TestStudent
class that invokes the methods of the Student class and applies the
concept of an array of objects.
public class TestStudent {
public
static void main (String [] args) {
Scanner
input = new Scanner(System.in);
String matricNo;
double test1,test2;
Student [] studDegree = new Student[3];
for
( int i = 0; i<3 ; x++){
System.out.print("Matric No: ");
matricNo = input.next();
System.out.print("Test 1: ");
test1 = input.nextDouble();
System.out.print("Test 2: ");
test2 = input.nextDouble();
studDegree[x] = new Student(matricNo, test1, test2);
}
System.out.println("*****************************");
System.out.printf("%24s\n","STUDENT INFORMATION");
System.out.println("*****************************");
System.out.println("Matric No\tAverageMark");
for
( int y=0; y <3; y++){
studDegree[y].calculateAverage();
System.out.println(studDegree[y].getStudentInfo());
}
}
TASK 2
You are assigned by Maxcom to write a program that can calculate the charge for a call made by the user. The user needs to enter the duration in minutes of the call and the rate category. The program will then display output containing the rate category, the duration and the calculated charge. Then, the program will ask whether the user want to continue with the next calculation or quit the program. If the user chooses to continue, the program will repeat again with a new calculation.
import java.util.Scanner;
public class MaxcomApp {
public
static void main(String[] args) {
int
duration, category;
Scanner read= new Scanner(System.in);
double charge = 0.0;
int
respond;
CallChargeCalculator ccc = new
CallChargeCalculator();
do {
System.out.println("Enter the call duration (in minutes):");
duration = read.nextInt();
System.out.println("Enter Rate Category: 1.Daytime
2.Evening 3.Off-Peak");
category = read.nextInt();
charge = ccc.computeCharge(duration, category);
System.out.printf("The amount you have to pay is = RM%.2f%n",
charge);
System.out.println("Do you want to continue? 1.Yes 2.No");
respond = read.nextInt();
System.out.println();
}
while (respond==1);
System.out.println("Thank you! See you again.");
}
}
class CallChargeCalculator {
double
charge;
public
double computeCharge(int duration, int category) {
double rate;
switch (category) {
case 1:
rate =0.07;
break;
case 2:
rate = 0.12;
break;
default:
rate = 0.05;
break;
}
charge = duration*rate;
return charge;
}
}
TASK 3
Implement a class Average which consists of the main method and average method. Inside the main method, call average method. Print the value return by the average method.
Next, overload the average method such that if three integers are provided as parameters, the method returns the average of all three. Modify main method by adding a statement to invoke the overload method. Then, create another overload average method to accept four integer parameters and return their average.
public class Average {
public static void main(String[] args) {
int no1 = 4, int no2 = 5, int no 3 = 7, int no 4 = 11;
System.out.printf("Average for %d and %d is %.2f\n",no1,no2,average(no1,no2));
System.out.printf("Average for %d, %d and %d is %.2f\n",no1,no2,no3, average(no1,no2,no3));
System.out.printf("Average for %d, %d, %d and %d is %.2f\n",no1,no2,no3, no4, average(no1,no2,no3,no4));
}
public static double average(int no1, int no2){
return (double)(no1+no2)/2;
}
public static double average(int no1, int no2, int no3){
return (double)(no1+no2+no3)/3;
}
public static double average(int no1, int no2, int no3, no4){
int no4;
return (double)(no1+no2+no3+no4)/4;
}
}



Comments