Question 1
Question 1
public Salesman(){
this.name = "";
this.id = "";
this.basicSalary = 0.0;
this.salesAmount = 0.0;
}
public Salesman(String name, String id, double basicSalary, double
salesAmount){
this.name = name;
this.id = id;
this.basicSalary = basicSalary;
this.salesAmount = salesAmount;
}
public void setName (String nm){
name = nm;
}
public void setID (String d){
id = d;
}
public void setBasicSalary (double bs){
basicSalary = bs;
}
public void setSalesAmount (double sa){
salesAmount = sa;
}
public String getName(){
return name;
}
public String getID(){
return id;
}
public double getBasicSalary(){
return basicSalary;
}
public double getSalesAmount(){
return salesAmount;
}
public double calculateNetSalary(){
double commission = 0.15 *salesAmount;
return basicSalary + commission ;
}
public String toString () {
return "Salesman Details:" +
"Name: " + name +
"ID: " + id +
"Basic Salary:" + basicSalary +
"Sales Amount: " + salesAmount;
}
}
Question 2
System.out.println("Square Details:");
System.out.print("Enter height: ");
double hi = scan.nextDouble();
System.out.print("Enter width: ");
double wd = scan.nextDouble();
Square arrSquare = new Square(hi, wd);
System.out.println("\nCube Details:");
System.out.print("Enter depth: ");
double dp = scan.nextDouble();
Cube arrCube = new Cube(hi, wd, dp);
scan.close();
}
}
Output:
Question 3
if (response.equals("NO")) {
break;
}
System.out.print("Enter name: ");
String name = scan.nextLine();
System.out.print("Enter category (VIP/REGULAR): ");
String category = scan.next().toUpperCase();
scan.nextLine();
System.out.print("Are you a member? (true/false): ");
boolean member = scan.nextBoolean();
scan.nextLine();
System.out.print("Enter price: ");
double price = scan.nextDouble();
scan.nextLine();
System.out.print("Enter service type (" +
(category.equals("VIP") ? "S/B" : "B/F") + "): ");
String service = scan.nextLine();
if (category.equals("VIP")) {
customers[i] = new VIP(name, member, price, service);
vipCount++;
totalIncome += ((VIP) customers[i]).calcNewPrice();
} else {
customers[i] = new Regular(name, member, price,
service);
regularCount++;
totalIncome += ((Regular) customers[i]).calcService();
}
i++;
}
System.out.println("\n--- Customer Details ---");
for (int j = 0; j < i; j++) {
customers[j].display();
}
System.out.println("\nTotal VIP Customers: " + vipCount);
System.out.println("Total REGULAR Customers: " + regularCount);
System.out.println("Total Income: RM" +
df.format(totalIncome));
scan.close();
}
}
Output