Exercise - User definedClass-1
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();
System.out.println(q1.count);
q1.init();
System.out.println(q1.getCount());
q1.increment();
System.out.println(q1.count);
q1.increment();
System.out.println(q1.getCount());
}
}
Output: 0
1
2
3
Next, replace the init() method with a
constructor. Modify the main() method.
class QuestionOne {
int count;
// constructor initializes count to 1
public QuestionOne() {
count = 1;
}
public void increment() {
count = count + 1;
}
public int getCount() {
return count;
}
}
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
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.
a)
Staff |
-name: String -staffID: String -salary: double -workingDay: int |
+setStaffInfo(String, String, int) : void +calculateSalary() : void +getSalary() : double +getName() : String +getStaffID() : String
|
import java.util.Scanner;
class TestStaff {
static Scanner console = new Scanner(System.in);
public static void main(String args[]){
Staff staff = new Staff();
System.out.println("Enter staff name: ");
String name = console.nextLine();
System.out.println("Enter staff ID: ");
String staffID = console.nextLine();
System.out.print("Enter working per day: ");
int workingPerDay = console.nextInt();
staff.setStaffInfo(name, staffID, workingPerDay);
staff.calculateSalary();
System.out.println("Staff name: " + staff.getName());
System.out.println("Staff ID: " + staff.getStaffID());
System.out.println("Staff salary: " + staff.getSalary());
}
}
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.
MyFCStaff Class
public class MyFCStaff {
// Data members to store staff name, ID, hours worked, and sales
private String name, staffID;
private int hoursWorked;
private double sales, salary;
// Constructor to initialize staff information
public MyFCStaff(String name, String staffID, int hoursWorked, double sales) {
this.name = name;
this.staffID = staffID;
this.hoursWorked = hoursWorked;
this.sales = sales;
}
// Method to calculate the commission based on the sales volume
public void calculateCommission() {
if (sales >= 500) {
salary += 0.15 * sales;
} else if (sales >= 301) {
salary += 0.1 * sales;
} else if (sales >= 150) {
salary += 0.05 * sales;
}
}
// Method to calculate the total salary
public void calculateSalary() {
// Add the basic hourly wage to the salary
salary += 8 * hoursWorked;
// Calculate the commission
calculateCommission();
}
// Method to display the salary information
public void displaySalary() {
System.out.println("Staff Name: " + name);
System.out.println("StaffID: " + staffID);
System.out.println("Hours Work: " + hoursWorked);
System.out.println("Total Sale: RM " + sales);
System.out.println("Total Salary: RM " + salary);
}
}
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).
// Method to return the salary information as a string
public String toString() {
return "Staff Name: " + name + "\n" +
"StaffID: " + staffID + "\n" +
"Hours Work: " + hoursWorked + "\n" +
"Total Sale: RM " + sales + "\n" +
"Total Salary: RM " + salary;
}
// Display the salary information using the toString method
System.out.println(staff.toString());
Output:
Enter staff name:
Ali
Enter staff ID:
1001
Enter total hours worked:
200
Enter total sales:
4500
Staff Name: Ali
StaffID: 1001
Hours Work: 200
Total Sale: RM 4500.0
Total Salary: RM 2275.0
BUILD SUCCESSFUL (total time: 15 seconds)
Comments
Post a Comment