0% found this document useful (0 votes)
20 views12 pages

Lab Ass2 Report

calculus122333

Uploaded by

2024945687
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)
20 views12 pages

Lab Ass2 Report

calculus122333

Uploaded by

2024945687
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/ 12

OBJECT ORIENTED PROGRAMMING

(CSC186)

TITLE:
LAB ASSESSMENT 2

LECTURER NAME : SIR NIZAM BIN OSMAN

STUDENT NAME MATRIC GROUP


NUMBER
1 MUHAMMAD SAFUAN BIN NAZERI 2024945687 RCDCS1102B

Question 1
Source Code (Main)

import java.util.Scanner;
import java.text.*;

public class PersonalServiceApp {


public static void main (String []args){

Scanner scan = new Scanner (System.in);


DecimalFormat df = new DecimalFormat("###.00");

//STEP 1 : CREATE / INSTANTIATE AN OBJECT

PersonalService serviceObj = new PersonalService ();

//STEP 2 : INPUT

System.out.println("Enter price : RM ");


double pr = scan.nextDouble();

System.out.println("Enter service fee (%) : ");


double sf = scan.nextDouble();

System.out.println("Enter additional charge : RM ");


double ac = scan.nextDouble();

//STEP 3 : STORE ONTO OBJECT


//USING NORMAL CONSTRUCTOR

serviceObj = new PersonalService(pr,sf,ac);

//OR
//SETTER/MUTATOR
serviceObj.setPrice(pr);
serviceObj.setServiceFee(sf);
serviceObj.setAddCharge(ac);

//STEP : MANIPULATION
// b i)
System.out.println(serviceObj.ToString() + "\nTotal cost :RM " +
serviceObj.calcTotalCost());

//b ii)

2
serviceObj.setServiceFee (5);

//b iii)
double prodPrice = serviceObj.getPrice();

double dis = 0 ;

if (prodPrice >= 1000 && prodPrice < 5000)


dis = serviceObj.calcTotalCost() * 0.05;

else if (prodPrice >= 5000 && prodPrice < 10000)


dis = serviceObj.calcTotalCost() * 0.10;

else if (prodPrice >= 10000 && prodPrice < 20000)


dis = serviceObj.calcTotalCost() * 0.1;

else if (prodPrice >= 20000)


dis = serviceObj.calcTotalCost() * 0.20;

double totCosAfter = serviceObj.calcTotalCost() - dis;

//b iv)

System.out.println("\n\n*******************************");
System.out.println("Detail price : ");
System.out.println("Product price : "+serviceObj.getPrice());
System.out.println("Service Fee : "+serviceObj.getServiceFee()+"
% ");
System.out.println("Additional Charges :RM
"+serviceObj.getAddCharge());
System.out.println("Discoutnt : RM: "+dis);
System.out.println("Total price after discount : RM
"+df.format(totCosAfter));
System.out.println("\n\n*******************************");
}
}

3
Source code (Class)
public class PersonalService {

// DATA MEMBERS
private double price ;
private double serviceFee;
private double addCharge;

//METHOD MEMBERS

//DEFAULT CONSTRUCTOR
public PersonalService(){
price = 0;
serviceFee = 0;
addCharge = 0;
}

//NORMAL CONSTRUCTOR
public PersonalService(double price ,double serviceFee, double
addCharge){
this.price = price;
this.serviceFee= serviceFee;
this.addCharge = addCharge;
}

//COPY CONSTRUCTOR
public PersonalService(PersonalService ps){
price = ps.price;
addCharge = ps.addCharge;
serviceFee = ps.serviceFee;
}

//SETTER/MUTATOR

public void setPrice(double pr){


price = pr;

public void setServiceFee(double sf){


serviceFee = sf;
}

public void setAddCharge(double ac){

4
addCharge = ac;
}

//GETTER/ACCESSOR/RETRIEVER
public double getPrice(){
return price ;
}

public double getServiceFee(){


return serviceFee;
}
public double getAddCharge(){
return addCharge;
}

//PROCESSOR
public double calcTotalCost(){
return (price + (price * serviceFee/100) + addCharge);

//PRINTER
public String ToString(){
return "\n\nPrice: RM "+price+ "\nService Fee (%) : "
+serviceFee+ "\nAdditinal Charge : RM" + addCharge ;

}
}

Sample input

Enter price : RM 100

Enter service fee (%) : 10

Enter price : RM 10

Enter service fee (%) : 1

Enter price : RM 101

Enter service fee (%) : 1

5
Sample Output

Price: RM 101.0

Service Fee (%) : 1.0

Additinal Charge : RM7.0

Total cost :RM 109.01

*******************************

Detail price :

Product price : 101.0

Service Fee : 5.0 %

Additional Charges :RM 7.0

Discoutnt : RM: 0.0

Total price after discount : RM 113.05

6
Question 2

Source Code (Main)


import java.util.Scanner;

public class WardAdmissionApp {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


Scanner scanner2 = new Scanner(System.in);

String citizenType;
int wardClass;

System.out.print("\n Please enter patient name: ");


String name = scanner2.nextLine();

System.out.print(" \nPlease enter patient age : ");


int age = scanner.nextInt();

do {
System.out.print(" \nPlease enter ward class ): ");
wardClass = scanner.nextInt();
if (wardClass != 2) {
System.out.println("Sorry only Ward Class 2 allowed");
}
} while (wardClass != 2);

do {
System.out.print("\nEnter citizen type m / nm ): ");
citizenType = scanner2.nextLine();
if (!citizenType.equalsIgnoreCase("M")) {
System.out.println("Sorry , only Malaysian allowed ");
}
} while (!citizenType.equalsIgnoreCase("M"));

System.out.print("Please enter number of days: ");


int days = scanner.nextInt();

WardAdmission patient = new WardAdmission(name, age, wardClass,


citizenType, days);

7
double totalPrice = patient.calcTotalPrice();

patient.display(totalPrice);

scanner.close();
scanner2.close();
}
}

Source code (Class)


public class WardAdmission {

private String PatientName;


private int PatientAge;
private int WardClass;
private String CitizenType;
private int NumOfDay;

public WardAdmission () {

PatientName = " ";


PatientAge = 0;
WardClass = 0;
CitizenType = " ";
NumOfDay = 0;

public WardAdmission (String PatientName,int PatientAge,int


WardClass,String CitizenType,int NumOfDay){

this.PatientName=PatientName ;
this.PatientAge=PatientAge ;
this.WardClass=WardClass ;
8
this.CitizenType=CitizenType;
this.NumOfDay=NumOfDay ;

public WardAdmission (WardAdmission WA){


this.PatientName=WA.PatientName ;
this.PatientAge=WA.PatientAge ;
this.WardClass=WA.WardClass ;
this.CitizenType=WA.CitizenType;
this.NumOfDay=WA.NumOfDay ;

public void setPatientname(String PN) {


PatientName = PN ;
}

public void setPatientAge(int PA) {


PatientAge = PA ;
}

public void setWardClass(int WC) {


WardClass = WC ;
}

public void setCitizenType(String CT) {


CitizenType = CT ;
}

public void setNumOfDay(int NOD) {


NumOfDay = NOD ;
}

public String getPatientName(){


return PatientName ;
}

public int getPatientAge(){


return PatientAge;
}

9
public int getWardClass(){
return WardClass;
}

public String getCitizenType(){


return CitizenType;
}

public int getNumOfDay(){


return NumOfDay;
}

public double calcTotalPrice() {

double priceperday = 0;

if (WardClass == 1 && CitizenType.equalsIgnoreCase("M")){


priceperday = 200;
CitizenType = "Malaysian";}

else if (WardClass == 2 && CitizenType.equalsIgnoreCase("M")){


priceperday = 100;
CitizenType = "Malaysian";}

else if (WardClass == 3 && CitizenType.equalsIgnoreCase("M")){


priceperday = 50;
CitizenType = "Malaysian";}

else if (WardClass == 1 && CitizenType.equalsIgnoreCase("NM")){


priceperday = 400;
CitizenType = "non Malaysian";}

else if (WardClass == 2 && CitizenType.equalsIgnoreCase("NM")){


priceperday = 300;
CitizenType = "non Malaysian";}

else if (WardClass == 3 && CitizenType.equalsIgnoreCase("NM")){


priceperday = 150;
CitizenType = "non Malaysian";}

10
double TotalPrice = priceperday*NumOfDay;

if (PatientAge <= 6 || PatientAge >= 60) {


TotalPrice -= 0.2 * TotalPrice;
}

TotalPrice += 0.06 * TotalPrice;

return TotalPrice;
}

public void display(double TotalPrice) {

System.out.println(" \nSinaran hospital receipt\n ");


System.out.println(" ");
System.out.println("Patient name : " +PatientName);
System.out.println("Patient age : " +PatientAge);
System.out.println("Ward class : " +WardClass);
System.out.println("Citizen type : " +CitizenType);
System.out.println("Number of Day : " +NumOfDay);

System.out.println("total price : RM " +


String.format("%.2f", TotalPrice));

}
}

Sample input

Please enter patient name: wan

Please enter patient age : 19

Please enter ward class ): 2

Enter citizen type m / nm ): m

Please enter number of days: 2

11
Sample output

Sinaran hospital receipt

Patient name : wan

Patient age : 19

Ward class :2

Citizen type : Malaysian

Number of Day : 2

total price : RM 212.00

12

You might also like