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 |
|
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 |
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).






Comments