Exercise - User definedClass-3

TASK 1

 

Based on the following UML class diagram:

 



a)             Write a complete Student class.

b)             Next, write TestStudent class that invokes the methods of the Student class  and applies the concept of an array of objects.

c)              Sample run as given below:

User’s inputs

 

 Matric No : s1111
 Test 1 : 67
 Test 2 : 45
 
Matric No : s2222
 Test 1 : 67
 Test 2 : 98
 
Matric No : s3333
 Test 1 : 67
 Test 2 : 90
 

Program outputs

 

*****************************
      STUDENT INFORMATION
*****************************
Matric No   AverageMark
 s1111         56.0
 s2222         82.5
 s3333         78.5

a) 

public class Student {
    private String matricNo;
    private double test1;
    private double test2;
    private double averageMark;

    
  public Student(String matricNo, double test1, double test2){
      this.matricNo = matricNo;
      this.test1 = test1;
      this.test2 = test2;
  }  
  
  public String getStudentInfo(){
      return " " + matricNo + "\t\t" + averageMark;
      
     
  }
    
  public void calculateAverage(){
       averageMark = (test1 + test2 )/2;
  }  
    
}

b)

import java.util.*;

public class TestStudent {
    public static void main(String[] arg){	
	 Scanner read = new Scanner(System.in);
         Student[] arrStudent = new Student[3];
	 String matricNo;
         double test1;
	 double test2; 
         
         for (int i=0; i<arrStudent.length;i++){
            System.out.println("Matric Number: "); 
            matricNo = read.next();
            System.out.println("Test 1: "); 
            test1 = read.nextDouble();
            System.out.println("Test 2: "); 
            test2 = read.nextDouble();
            arrStudent[i]= new Student(matricNo, test1,test2);
         }
         
         System.out.println("*******************\n" + " Student Information \n" +
         "*******************\n");
	 
         for (int i=0; i<arrStudent.length;i++){
          arrStudent[i].calculateAverage();
          System.out.println(arrStudent[i].getStudentInfo()); 
         }
         }
   }

c) Output:
Matric Number: 
S1111
Test 1: 
67
Test 2: 
45
Matric Number: 
S2222
Test 1: 
67
Test 2: 
98
Matric Number: 
S3333
Test 1: 
67
Test 2: 
90
*******************
 Student Information 
*******************

 S1111		56.0
 S2222		82.5
 S3333		78.5
BUILD SUCCESSFUL (total time: 30 seconds)



TASK 2

A telecommunication provider, Maxcom, charges the following rates for telephone calls:

Rate Category

Rate per Minute (RM)

    1 (Daytime )

0.07

    2 (Evening)

0.12

    3 (Off-Peak)

0.05

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. 

 


Referring to the UML class diagram given above, write a complete program for both classes to obtain the result as shown in the example given below.

     

The example of the program running is as follows:

Enter the call duration (in minutes):

40

Enter Rate Category:  1.Daytime  2.Evening  3.Off-Peak

2

The amount you have to pay is = RM4.80

Do you want to continue? 1.Yes  2.No

1

Enter the call duration (in minutes):

30

Enter Rate Category:  1.Daytime  2.Evening  3.Off-Peak

3

The amount you have to pay is = RM1.50

Do you want to continue? 1.Yes  2.No

2
Thank you! See you again.
CallChargeCalculator Class


public class CallChargeCalculator {
    private double charge; 

public double computeCharge(int duration, int category){
        switch (category) {
            case 1:
                charge = duration * 0.07;
                return charge;
            case 2:
                charge = duration * 0.12;
                return charge;
            case 3:
                charge = duration * 0.05;
                return charge;
            default:
                break;
        }
        return 0;
  } 

  

}

MaxcommApp Class


import java.util.Scanner;


public class MaxcommApp {
    int act, duration, category;
    public static void main(String[] args){
    Scanner read = new Scanner(System.in);
    
    while (true){
    int act = 0, duration = 0, category = 0, charge =0;
    CallChargeCalculator calc = new CallChargeCalculator();
    
    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();
    System.out.println("The amount you have to pay is = RM" + calc.computeCharge(duration, category)); 
    
    System.out.println("Do you want to continue? 1.Yes  2.No");
    int choice = read.nextInt();

    if (choice == 2) {
    System.out.println("Thank you! See you again.");
    break;
    }
    
   }
    
  } 
}



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. Modify main method by adding a statement to invoke the latest overload method.


public class Average {
    public static void main(String[] args){
    int no1=4, no2 = 5, no3 =7, no4 = 11;
    System.out.println("Average for " + no1 + " and " + no2 + " is " + average(no1,no2));
    System.out.println("Average for " + no1 + " , " + no2 + " and " + no3 + " is " + average(no1,no2,no3));
    //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,int no4){
    return (double)(no1+no2+no3+no4)/4;
}

Comments

Popular posts from this blog

Exercise 3 - PredefinedClass

Exercise - User definedClass-2