0% found this document useful (0 votes)
3 views

Question 1

The document contains Java code for three main classes: Salesman, Square/Cube, and Customer/VIP/Regular, each demonstrating object-oriented principles. The Salesman class calculates net salary based on sales, while Square and Cube classes compute surface areas. The Customer classes manage customer details and pricing, with a SpaManagement class to handle customer bookings and income calculations.

Uploaded by

suouasashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Question 1

The document contains Java code for three main classes: Salesman, Square/Cube, and Customer/VIP/Regular, each demonstrating object-oriented principles. The Salesman class calculates net salary based on sales, while Square and Cube classes compute surface areas. The Customer classes manage customer details and pricing, with a SpaManagement class to handle customer bookings and income calculations.

Uploaded by

suouasashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Question 1

public class Salesman {


private String name;
private String id;
private double basicSalary;
private double salesAmount;

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

public class Square {


private double height;
private double width;

public Square(double height , double width){


this.height = height;
this.width = width;
}
public void setHeight (double hi){
height = hi;
}
public void setWidth (double wd){
width = wd;
}

public double getHeight(){


return height;
}
public double getWidth(){
return width;
}
public double computeSurfaceArea(){
return height* width;
}
}
public class Cube extends Square{
private double depth;

public Cube(double height , double width , double depth){


super(height,width);
this.depth = depth;
}
public void setDepth (double dp){
depth = dp;
}
public double getDepth(){
return depth;
}
public double computeSpaceArea(){
return super.computeSurfaceArea()* depth;
}
}
import java.util.*;
public class CalculateSurfaceArea {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

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

// Display Surface Areas


System.out.println("\nResults:");
System.out.println("Square Area: " +
arrSquare.computeSurfaceArea());
System.out.println("Cube Surface Area: " +
arrCube.computeSurfaceArea());

scan.close();
}
}

Output:
Question 3

public class Customer {


protected String name;
protected String customerCat;
protected boolean member;

public Customer(String name, String customerCat, boolean member) {


this.name = name;
this.customerCat = customerCat;
this.member = member;
}

public String getName(){


return name;
}
public String getCustomerCat(){
return customerCat;
}
public boolean isMember(){
return member;
}

public void display() {


System.out.println("Name: " + name + ", Category: " +
customerCat + ", Member: " + member);
}
}
public class VIP extends Customer {
private double price;
private String vipService;

public VIP(String name, boolean member, double price, String


vipService) {
super(name,"VIP", member);
this.price = price;
this.vipService = vipService;
}
public double calcNewPrice() {
double discount = price * 0.15;
double finalPrice = price - discount;
if (member) finalPrice -= 50;
return finalPrice;
}
public void display() {
super.display();
System.out.println("Service: " + vipService + ", Final Price: "
+ calcNewPrice());
}
}

public class Regular extends Customer {


private double price;
private String regularService;

public Regular(String name, boolean member, double price, String


regularService) {
super(name, "REGULAR", member);
this.price = price;
this.regularService = regularService;
}
public double calcService() {
double discount = price * 0.10;
double finalPrice = price - discount;
if (member) finalPrice -= 10;
return finalPrice;
}
public void display() {
super.display();
System.out.println("Service: " + regularService + ", Final
Price: " + calcService());
}
}
import java.util.*;
import java.text.*;

public class SpaManagement {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.00");
Customer[] customers = new Customer[10];
int vipCount = 0, regularCount = 0;
double totalIncome = 0;
int i = 0;

while (i < customers.length) {


System.out.print("Do you want to book a customer? (Yes/No):
");
String response = scan.next().toUpperCase();
scan.nextLine();

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

You might also like