Exercise User Defined Class

 

1.           What will be the output from the following code?

 


class QuestionOne {

     int count;

    

     public void init(){

           count = 1;

     }

     public void increment() {

           count = count + 1;

     }

     public int getCount() {

           return count;

     }

}

 


    public class Q1Main {

        public static void main (String [] arg) {

            QuestionOne q1;

            q1 = new QuestionOne (1);

            q1.increment();

            q1.increment();

            System.out.println(q1.getCount());

        }

    }



2.           Read and analyse the following class:

class Staff {

     private String name, staffID;

     private double salary;

     private int workingDay;

     public void setStaffInfo(String nm, String id, int wDay){

           name=nm;

stafID=id;

workingDay=wDay;

     }

     public void calculateSalary(){

           salary = workingDay * 35.0;

     }

     public double getSalary(){

           return salary;

     }

     public String getName(){

           return name;

     }

     public String getStaffID(){

           return staffID;

     }

}//end class

 

 

           

 
      class Staff {

     private String name, staffID;

     private double salary;

     private int workingDay;

     public void setStaffInfo(String nm, String id, int wDay){

           name=nm;

stafID=id;

workingDay=wDay;

}

     public void calculateSalary(){

           salary = workingDay * 35.0;

     }

     public double getSalary(){

           return salary;

     }

     public String getName(){

           return name;

     }

     public String getStaffID(){

           return staffID;

     }

}//end class


 a)     Draw a UML class diagram for Staff class.


           

Staff

-name:String

-staffID:String

-salary:double

- workingDay:int

+setStaffInfo(String nm, String id, int wDay):void

+calculateSalary():void

+getSalary():double

+getName():String

+getStaffID():String


b) By using the above class, complete the following class TestStaff that accepts name, staff id and working perday as inputs from the user and displays the name, staff ID and salary of the staff

 

 import java.util.Scanner;

class TestStaff {

    static Scanner console = new Scanner(System.in);

    public static void main(String arg[]){

        

        String name, staffID;

        double salary;

        int workingDay;

        

        System.out.print("Input your name:     ");

        name = console.nextLine();

        System.out.print("Input your Staff ID:     ");

        staffID = console.nextLine();

        System.out.print("Input your working days:     ");

        workingDay = console.nextInt();

        System.out.println("");

        

        Staff x = new Staff();

        x.setStaffInfo(name, staffID, workingDay);

        x.calculateSalary();

        System.out.println("===== STAFF INFO =====");

        System.out.println("Staff name: " + x.getName());

        System.out.println("Staff ID  :" + x.getStaffID());

        System.out.printf("Salary    : RM %.2f\n" , x.getSalary());

    }        

}

    class Staff {

private String name, staffID;

private double salary;

private int workingDay;

public void setStaffInfo(String nm, String id, int wDay){

name=nm;

                staffID=id;

                workingDay=wDay;

}

public void calculateSalary(){

salary = workingDay * 35.0;

}

public double getSalary(){

return salary;

}

public String getName(){

return name;

}

public String getStaffID(){

return staffID;

}

}//end class

 


 

a)     Draw a UML class diagram for Staff class.



b)     By using the above class, complete the following class TestStaff that accepts name, staff id and working perday as inputs from the user and displays the name, staff ID and salary of the staff.

 


3.       Understand and analyze the problem below:

 

Staffs at MyFC earn the basic hourly wage of RM8.00.  They will receive a commission on the sales they generate while tending the counter.  The commission is based on the following formula:

 

Sales Volume

Commission

RM150.00 to RM300.00

RM301.00 to RM500.00

Above RM500.00

5% of total sale

10% of total sale

15% of total sale

 

Based on above scenario:

 

·       Write a Java program that accepts staff’s information (including name and staffID), total hours work and total sales for that particular month and then displays total salary that he/she earned. You are required to create 2 classes named MyFCStaff and TestMyFCStaff. Declare appropriate data members for the class MyFCStaff and include the following methods as well:

ü  Constructor - to initialize name, staffID, total hours work and total sales for that particular month with values that are received through the parameters of the method.

ü  calculateCommission() - calculates the commission based on the sales volume.

ü  calculateSalary() - calculates the total salary

ü  displaySalary() - displays the output similar as shown below

 

Staff Name        : Ali

StaffID              : MyFC1001

Hours Work      : 200

Total Sale         : RM 4500.00

Total Salary      : RM 2275.00

 

·       The class TestMyFCStaff will contain the main method that consists of the statements to read the user input and invoke the methods of the class MyFCStaff.

 

4.           Modify the class MyFCStaff by replacing the displaySalary() method with the method toString() that returns all required information to be displayed as the output as shown in the above. You should then include printing statements (S.O.P) in the main method. Please ensure that you should get the same expected output as in the Question (3).

 

package javaapplication21;
import java.util.Scanner;

public class TestMyFCStaff {
    public static void main(String[]args){
        String name, staffID;
        double t_hoursWork, t_sales;
        Scanner sc = new Scanner (System.in);
        
        System.out.print("Input your name: ");
        name = sc.nextLine();
        System.out.print("Input your Staff ID: ");
        staffID = sc.nextLine();
        System.out.print("Input your working hours: ");
        t_hoursWork = sc.nextDouble();
        System.out.print("Input your total sales: ");
        t_sales = sc.nextDouble();
        System.out.println("");
        
        MyFCStaff x = new MyFCStaff (name, staffID, t_hoursWork, t_sales);
        x.displaySalary();
    }
}
    
    class MyFCStaff {
   String name, staffID;
    double t_hoursWork, t_sales, t_salary, commission;
    
    public MyFCStaff(String name, String staffID, double t_hoursWork, 
           double t_sales){
        this.name = name;
        this.staffID = staffID;
        this.t_hoursWork = t_hoursWork;
        this.t_sales = t_sales;
    }
    
    public double calculateCommission(){
        double comission=0;
      if (t_sales>500)
          commission=0.15*t_sales;
     else
     if (t_sales>=301&&t_sales<=500)
         commission=0.10*t_sales;
    else
     if (t_sales>=150&&t_sales<=300)
        commission=0.05*t_sales;
      return commission;

     
    }
    
    public double calculateSalary(){
        t_salary = t_hoursWork*8 + calculateCommission();
        return t_salary;
    }
    
    void displaySalary(){
        String x = "%-15s%-2s%s\n";
        String y = "%-15s%-5s%.2f\n";
        
        System.out.printf(x, "Staff Name",":", name);
        System.out.printf(x, "StaffID", ":", staffID);
        System.out.printf("%-15s%-2s%.0f\n", "Hours Work", ":", t_hoursWork);
        System.out.printf(y,"Total Sale", ": RM " , t_sales);
        System.out.printf(y,"Total Salary", ": RM " , calculateSalary());
        
    }
}
 



Comments

Popular posts from this blog

Exercise UML

Exercise 1 Revision String

Exercise OOP Concept