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

Lab 3 Oop

The document describes three Java classes - Salesperson, Cloth, and AlumniSTJ. The Salesperson class models salespeople with attributes like name, ID, and sales. The Cloth class models cloth items with attributes like name, price, and length. The AlumniSTJ class models alumni with attributes like name, IC number, gender, batch year, employment status, and education level. All classes have methods to get/set attributes, calculate values like commission and payment, and output details.

Uploaded by

Khairil Hazmie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab 3 Oop

The document describes three Java classes - Salesperson, Cloth, and AlumniSTJ. The Salesperson class models salespeople with attributes like name, ID, and sales. The Cloth class models cloth items with attributes like name, price, and length. The AlumniSTJ class models alumni with attributes like name, IC number, gender, batch year, employment status, and education level. All classes have methods to get/set attributes, calculate values like commission and payment, and output details.

Uploaded by

Khairil Hazmie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

College of Computing Informatics & Media

Bachelor of Computer Science (Hons.) Netcentric Computing

CSC435

Object Oriented Programming

Task: Lab Assignment 3

Name Khairil Hazmie Bin Abdul Razak


Student ID 2022697564
Group RCDCS2513A
Lecturer Sir Nizam Bin Osman
1. Salesperson class

public class Salesperson


{

//Data Members
private String Name;
private int Id;
private double Sales;

//Method members

//Default Constructor
public Salesperson()
{
Name = "";
Id = 0;
Sales = 0;
}

//Normal Constructor
public Salesperson(String NM, int Ix, double SL)
{
Name = NM;
Id = Ix;
Sales = SL;
}

//Copy Constructor
public Salesperson(Salesperson s)
{
Name = s.Name;
Id = s.Id;
Sales = s.Sales;
}

//Mutator/Setter method
public void setName(String NM)
{
Name = NM;
}

public void setId(int Ix)


{
Id = Ix;
}

public void setSales(double SL)


{
Sales = SL;
}
//Retriever
public String getName()
{
return Name;
}

public int getId()


{
return Id;
}

public double getSales()


{
return Sales;
}

//Processor
public double CommissionRate()
{

double commission = 0;
if (Sales < 500) {
commission = 0.1* Sales;
}
else if (Sales >= 500 && Sales < 1000) {
commission = 0.15* Sales;
}
else if (Sales >= 1000 && Sales < 2000) {
commission = 0.2* Sales;
}
else if (Sales >= 2000) {
commission = 0.25* Sales;
}

return commission;
}

//Printer
public String toString()
{
return "\n\nName: " + Name + "\nId: " + Id + "\nSales: " +
Sales;
}
}
SalespersonApp main.

import java.util.*;

public class SalespersonApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in);

//Step 1 Declare array of object


System.out.print("Enter size of array: ");
int size = scan1.nextInt();
Salesperson sp[] = new Salesperson[size];

//Step 2 Create/ Instantiate array of object


for (int i=0; i<size ; i++)
sp[i] = new Salesperson();

//Step 3 Input

for (int i = 0 ; i < size; i++)


{

System.out.print("Enter name: ");


String NM = scan.nextLine();

System.out.print("Enter id: ");


int Ix = scan1.nextInt();

System.out.print("Enter sales: ");


double SL = scan1.nextDouble();

//Setter

sp[i].setName(NM);
sp[i].setId(Ix);
sp[i].setSales(SL);

//normal Constructor

sp[i] = new Salesperson(NM,Ix,SL);


}

// Step 5 Manipulation

//i) Print a slip for each salesperson


for (int i=0; i<size ; i++)
System.out.println(sp[i].toString() + "\nCommission: RM"
+sp[i].CommissionRate());

//ii)The total sale for all saleperson


double totSale = 0;

for (int i=0; i<size ; i++)


totSale += sp[i].getSales();
System.out.println("\nTotal Sales: RM" + totSale);

//iii) the maximum and minimum


double maxSale = sp[0].getSales();
double minSale = sp[0].getSales();

for(int i=0 ; i <size ; i++)


{
if(sp[i].getSales() >maxSale)
maxSale = sp[i].getSales();

if(sp[i].getSales() < minSale)


minSale = sp[i].getSales();
}

System.out.println("The maximum Sales: RM " + maxSale);


System.out.println("The minimum Sales: RM " + minSale);

//to calculate total commision and average

double totComm=0;
for(int i=0; i<size ; i++)
totComm += sp[i].CommissionRate();

double avgComm = totComm/size;

System.out.println("Total commission:RM " + totComm +


"\nAverage commission: RM " + avgComm);

}
}
• Input & Output

Figure 1: Example of input and ouput 1.

Figure 2: Example of input and output 2.


2. Cloth Class
public class Cloth
{

//Data Members
private String Name;
private double Price;
private double Length;

//Method members

//Default Constructor
public Cloth()
{
Name = "";
Price = 0.0;
Length = 0.0;
}

//Normal Constructor
public Cloth(String NM, double PR, double LG)
{
Name = NM;
Price = PR;
Length = LG;
}

//Copy Constructor
public Cloth(Cloth c)
{
Name = c.Name;
Price = c.Price;
Length = c.Length;
}

//Mutator/Setter method
public void setName(String NM)
{
Name = NM;
}

public void setPrice(double PR)


{
Price = PR;
}

public void setLength(double LG)


{
Length = LG;
}

//Retriever
public String getName()
{
return Name;
}

public double getPrice()


{
return Price;
}

public double getLength()


{
return Length;
}

//Processor
public double calcPayment()
{
double GST;
double totPrice;

GST = (Price * Length) * 0.06;


totPrice = GST + (Price * Length);

return totPrice;
}

//Printer
public String toString()
{
return "\n\nName: " + Name + "\nPrice per meter: " + Price +
"\nLength in meter: " + Length;
}

}
ClothApp main

import java.util.*;

public class ClothApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in);

//Step 1 Declare array of object


Cloth ct[] = new Cloth[100];

//Step 2 Create/ Instantiate array of object


for (int i=0; i<100 ; i++)
ct[i] = new Cloth();

//Step 3 Input

for (int i = 0 ; i < 100; i++)


{

System.out.print("\nEnter name: ");


String NM = scan.nextLine();

System.out.print("Enter Price: ");


double PR = scan1.nextDouble();

System.out.print("Enter Length: ");


double LG = scan1.nextDouble();

//Step 4 Store data onto object


//Setter

ct[i].setName(NM);
ct[i].setPrice(PR);
ct[i].setLength(LG);

//normal Constructor

ct[i] = new Cloth(NM,PR,LG);


}
// Step 5 Manipulation

//i) Print a slip for each customer and all customers

double totAll = 0.0;

for (int i=0; i<100 ; i++)


{
double totalPayment = ct[i].calcPayment();

totAll += totalPayment;

System.out.println(ct[i].toString());

System.out.println("Price needed to be paid: RM " +


totalPayment);
}

System.out.println("\nTotal price: RM " + totAll);

}
}
• Input & Output

Figure 3:Example of input and output 1.

Figure 4:Example of input and output 2.


3. AlumniSTJ class

public class AlumniSTJ


{

//Data Members
private String Name;
private String Ic;
private char Gender;
private int Batch;
private boolean Employment;
private String Education;

//Method members

//Default Constructor
public AlumniSTJ()
{
Name = "";
Ic = "";
Gender = '\0';
Batch = 0;
Employment = false;
Education = "";
}

//Normal Constructor
public AlumniSTJ(String NM, String Ix, char GD, int BA, boolean EM, String ED)
{
Name = NM;
Ic = Ix;
Gender = GD;
Batch = BA;
Employment = EM;
Education = ED;
}

//Copy Constructor
public AlumniSTJ(AlumniSTJ a)
{
Name = a.Name;
Ic = a.Ic;
Gender = a.Gender;
Batch = a.Batch;
Employment = a.Employment;
Education = a.Education;
}

//Mutator/Setter method
public void setName(String NM)
{
Name = NM;
}
public void setIc(String Ix)
{
Ic = Ix;
}
public void setGender(char GD)
{
Gender = GD;
}
public void setBatch(int BA)
{
Batch = BA;
}
public void setEmployment(boolean EM)
{
Employment = EM;
}
public void setEducation(String ED)
{
Education = ED;
}

//Retriever
public String getName()
{
return Name;
}
public String getIc()
{
return Ic;
}
public char getGender()
{
return Gender;
}
public int getBatch()
{
return Batch;
}
public boolean getEmployment()
{
return Employment;
}

public String getEducation()


{
return Education;
}

//Processor
public int calculateAge()
{
int age = 0;
String icString = String.valueOf(Ic);
int birthYear = Integer.parseInt(icString.substring(0, 2));

if (birthYear >= 00 && birthYear <= 15)


age = 2015 - (2000 + birthYear);

else if (birthYear <= 99)


age = 2015 - (1900 +birthYear);

return age;
}

public double calculateFees()


{
double fees = 0.0;

if (Employment == true)
{
fees = 30;
}
else if (Employment == false)
{
fees = 15;
}

return fees;
}

//Printer
public String toString()
{
return "\nName: " + Name + "\nIc number: " + Ic + "\nGender: " + Gender + "\nBatch :" +
Batch + "\nEmployment: " + Employment + "\nEducation Level : " + Education;
}
}

AlumniSTJApp main

import java.util.*;

public class AlumniSTJApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in);

//Step 1 Declare array of object


System.out.print("Enter size of array: ");
int size = scan1.nextInt();
AlumniSTJ al[] = new AlumniSTJ[size];

//Step 2 Create/ Instantiate array of object


for (int i=0; i<size ; i++)
al[i] = new AlumniSTJ();

//Step 3 Input
for (int i = 0 ; i < size; i++)
{
System.out.print("\nEnter name: ");
String NM = scan.nextLine();

System.out.print("Enter ic: ");


String Ix = scan.nextLine();

System.out.print("Enter gender: ");


char GD = scan.next().charAt(0);

System.out.print("Enter batch: ");


int BA = scan.nextInt();
scan.nextLine();

System.out.print("Enter employment: ");


boolean EM = scan.nextBoolean();
scan.nextLine();

System.out.print("Enter education: ");


String ED = scan.nextLine();

//Setter
al[i].setName(NM);
al[i].setIc(Ix);
al[i].setGender(GD);
al[i].setBatch(BA);
al[i].setEmployment(EM);
al[i].setEducation(ED);

//normal Constructor
al[i] = new AlumniSTJ(NM,Ix,GD,BA,EM,ED);
}

// Step 5 Manipulation

//i) Count and display the number of male and female members.

int countF = 0;
int countM = 0;
for (int i = 0; i< size; i++)
{

if(al[i].getGender() == 'F')
countF++;
else if(al[i].getGender() == 'M')
countM++;
}

System.out.println("\nThe number of females members: " + countF);


System.out.println("The number of males members: " + countM);

//ii) Count and display the number of employed and unemployed members.

int countEmp = 0;
int countUnEmp = 0;
for (int i = 0; i< size; i++)
{
if(al[i].getEmployment() == true )
countEmp++;
else if(al[i].getEmployment() == false)
countUnEmp++;
}

System.out.println("The number of employed members: " + countEmp);


System.out.println("The number of unemployed members: " +
countUnEmp);

//iii) Display the member information from SPM batch year 1995.

System.out.println("\nMember information from SPM batch year


1995.");

for (int i = 0; i< size; i++)


{
if (al[i].getBatch() == 1995)
{
System.out.println(al[i].toString());
}
}

//iv) Display the member information who age >= 50 years old.

System.out.println("\nMember information who age >= 50 years old.");

for (int i = 0; i< size; i++)


{
if (al[i].calculateAge() >= 50)
{
System.out.println(al[i].toString());
}
}

//v) Calculate and display the total of fee paid by the members.

double totalFees = 0;
for (int i = 0; i< size; i++)
{
double fees = al[i].calculateFees();
totalFees += fees;
}
System.out.println("\nTotal Fees: RM" + totalFees);

//vi) Find the oldest member and display the information.

int oldest = al[0].calculateAge();


int oldestMemberIndex = 0;

for (int i=0 ; i <size ; i++)


{
int age = al[i].calculateAge();
if(age > oldest)
oldest = age;
oldestMemberIndex = i;
}

System.out.println("\nOldest Member Information: ");


System.out.println(al[oldestMemberIndex].toString());
}
}
• Input & Output

Figure 5: Example of input and output for AlumniSTJ program.


4. Furniture class.

public class Furniture


{

//Data Members

private String furnitureType;


private String material;
private double pricePerUnit;
private int quantity;

//Method members

//Default Constructor

public Furniture()
{
furnitureType = "";
material = "";
pricePerUnit = 0.0;
quantity = 0;
}

//Normal Constructor
public Furniture(String FT, String MT, double PU, int QT)
{
furnitureType = FT;
material = MT;
pricePerUnit = PU;
quantity = QT;
}

//Copy Constructor
public Furniture(Furniture f)
{
furnitureType = f.furnitureType;
material = f.material;
pricePerUnit = f.pricePerUnit;
quantity = f.quantity;
}

//Mutator/Setter method
public void setfurnitureType(String FT)
{
furnitureType = FT;
}

public void setmaterial(String MT)


{
material = MT;
}

public void setpricePerUnit(double PU)


{
pricePerUnit = PU;
}

public void setquantity(int QT)


{
quantity = QT;
}

//Retriever
public String getfurnitureType()
{
return furnitureType;
}

public String getmaterial()


{
return material;
}

public double getpricePerUnit()


{
return pricePerUnit;
}

public int getquantity()


{
return quantity;
}

//Processor

public double calcPriceFurniture()


{
double totPrice = 0;
if ("Wood".equals(material))
{
totPrice = pricePerUnit * quantity * 0.80;
}
else if ("Rattan".equals(material))
{
totPrice = pricePerUnit * quantity * 0.85;
}
else if ("Metal".equals(material))
{
totPrice = pricePerUnit * quantity * 0.90;
}
else if ("Bamboo".equals(material))
{
totPrice = pricePerUnit * quantity * 0.95;
}
return totPrice;
}

//Printer
public String toString()
{
return "\nFurniture Type: " + furnitureType + "\nMaterial: " + material + "\nPrice Per Unit: " +
pricePerUnit + "\nQuantity:" + quantity;
}
}
Furniture main.

import java.util.*;

public class FurnitureApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in);

//Step 1 Declare array of object


System.out.print("Enter size of array: ");
int size = scan1.nextInt();
Furniture arrFurniture[] = new Furniture[size];

//Step 2 Create/ Instantiate array of object


for (int i=0; i<size ; i++)
arrFurniture[i] = new Furniture();

//Step 3 Input
for (int i = 0 ; i < size; i++)
{
System.out.print("\nEnter Furniture Type : ");
String FT = scan.nextLine();

System.out.print("Enter material: ");


String MT = scan.nextLine();

System.out.print("Enter Price Per Unit: ");


double PU = scan.nextDouble();

System.out.print("Enter quantity: ");


int QT = scan.nextInt();
scan.nextLine();

//Setter
arrFurniture[i].setfurnitureType(FT);
arrFurniture[i].setmaterial(MT);
arrFurniture[i].setpricePerUnit(PU);
arrFurniture[i].setquantity(QT);

//normal Constructor
arrFurniture[i] = new Furniture(FT,MT,PU,QT);
}

// Step 5 Manipulation
//i) Total sale of each type of material.
double totPriceWood = 0.0;
double totPriceRattan = 0.0;
double totPriceMetal = 0.0;
double totPriceBamboo = 0.0;
for(int i = 0; i<size; i++)
{
if(arrFurniture[i].getmaterial().equals("Wood"))
{
totPriceWood += arrFurniture[i].calcPriceFurniture();
}
else if(arrFurniture[i].getmaterial().equals("Rattan"))
{
totPriceRattan += arrFurniture[i].calcPriceFurniture();
}
else if(arrFurniture[i].getmaterial().equals("Metal"))
{
totPriceMetal += arrFurniture[i].calcPriceFurniture();
}
else if(arrFurniture[i].getmaterial().equals("Bamboo"))
{
totPriceBamboo += arrFurniture[i].calcPriceFurniture();
}
}
System.out.println("\nTotal price for wood material: RM " + totPriceWood);
System.out.println("\nTotal price for rattan material: RM " + totPriceRattan);
System.out.println("\nTotal price for metal material: RM " + totPriceMetal);
System.out.println("\nTotal price for bamboo material: RM " + totPriceBamboo);

//ii) Highest price of wood furniture.


double highestPrice = 0.0;
int indexName = 0;

for (int i=0; i<size ; i++)


{
if (arrFurniture[i].getmaterial().equals("Wood"))
{
if(arrFurniture[i].calcPriceFurniture() > highestPrice)
{
highestPrice = arrFurniture[i].calcPriceFurniture();
indexName = i;
}
}
}
System.out.println("\nWood material with highest price: " +
arrFurniture[indexName].toString());

}
}
• Input & Output

Figure 6: Example of input and output for Furniture program.


5. Laptop class.

public class Laptop


{

//Data Members

private String brand;


private double price;
private int RAM;
private int USBport;

//Method members

//Default Constructor

public Laptop()
{
brand = "";
price = 0.0;
RAM = 0;
USBport =0;
}

//Normal Constructor
public Laptop(String BR, double PR, int R, int USB)
{
brand = BR;
price = PR;
RAM = R;
USBport = USB;
}

//Copy Constructor
public Laptop(Laptop l)
{
brand = l.brand;
price = l.price;
RAM = l.RAM;
USBport = l.USBport;
}

//Mutator/Setter method
public void setbrand(String BR)
{
brand = BR;
}

public void setprice(double PR)


{
price = PR;
}

public void setRAM(int R)


{
RAM = R;
}

public void setUSBport(int USB)


{
USBport = USB;
}

//Retriever
public String getbrand()
{
return brand;
}

public double getprice()


{
return price;
}

public int getRAM(){


return RAM;
}

public int getUSBport(){


return USBport;
}

//Processor

public double upgradeRAM()


{
double totPrice = 0.0;

if (RAM == 8)
{
totPrice = price + 98.0;
}
else if (RAM == 16)
{
totPrice = price + 299.0;
}
else
{
totPrice = price;
}
return totPrice;

//Printer
public String toString()
{
return "\nBrand: " + brand + "\nNet Price: " + price + "\nRAM: " +
RAM + "\nUSB port:" + USBport;
}
}
Laptop main

import java.util.*;

public class LaptopApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in);

//Step 1 Declare array of object


Laptop Laptops[] = new Laptop[100];

//Step 2 Create/ Instantiate array of object


for (int i=0; i<100 ; i++)
Laptops[i] = new Laptop();

//Step 3 Input

for (int i = 0 ; i < 100; i++)


{

System.out.print("\nEnter Laptop Brand : ");


String BR = scan.nextLine();

System.out.print("Enter price: ");


double PR = scan.nextDouble();

System.out.print("Enter RAM: ");


int R = scan.nextInt();

System.out.print("Enter USB Port: ");


int USB = scan.nextInt();
scan.nextLine();

//Setter

Laptops[i].setbrand(BR);
Laptops[i].setprice(PR);
Laptops[i].setRAM(R);
Laptops[i].setUSBport(USB);

//normal Constructor

Laptops[i] = new Laptop(BR,PR,R,USB);


}

// Step 5 Manipulation

//i) Total price for Acer laptops.


double totPriceAcer = 0.0;

for(int i = 0; i<100; i++)


{
if(Laptops[i].getbrand().equals("Acer"))
{
totPriceAcer += Laptops[i].upgradeRAM();
}
}
System.out.println("\nTotal price for Acer laptops: RM " +
totPriceAcer);

//ii) Laptop that provides 4 USB ports.

for(int i = 0; i<100; i++)


{
if(Laptops[i].getUSBport() == 4)
{
System.out.println("\nLaptops brand that has 4 USB
ports: " + Laptops[i].getbrand());
}
}

}
}
• Input & Output

Figure 7:Example of input and output for Laptop program.


6. StudentVehicle class.

public class StudentVehicle


{

//Data Members

private String stickerNumber;


private String studentNumber;
private String studentProgram;
private String plateNumber;
private String vehicleType;

//Method members

//Default Constructor

public StudentVehicle()
{
stickerNumber = "";
studentNumber = "";
studentProgram = "";
plateNumber = "";
vehicleType = "";
}

//Normal Constructor
public StudentVehicle(String IN, String UN, String UP, String PN,String VT)
{
stickerNumber = IN;
studentNumber = UN;
studentProgram = UP;
plateNumber = PN;
vehicleType = VT;
}

//Copy Constructor
public StudentVehicle(StudentVehicle v)
{
stickerNumber = v.stickerNumber;
studentNumber = v.studentNumber;
studentProgram = v.studentProgram;
plateNumber = v.plateNumber;
vehicleType = v.vehicleType;
}

//Mutator/Setter method
public void setstickerNumber(String IN)
{
stickerNumber = IN;
}

public void setstudentNumber(String UN)


{
studentNumber = UN;
}

public void setstudentProgram(String UP)


{
studentProgram = UP;
}

public void setplateNumber(String PN)


{
plateNumber = PN;
}

public void setvehicleType(String VT)


{
vehicleType = VT;
}

//Retriever
public String getstickerNumber()
{
return stickerNumber;
}

public String getstudentNumber()


{
return studentNumber;
}

public String getstudentProgram()


{
return studentProgram;
}

public String getplateNumber()


{
return plateNumber;
}

public String getvehicleType()


{
return vehicleType;
}
//Processor
public double CalcStickerCharge()
{

double price = 0;

if ("motorcycle".equals(vehicleType))
{
price = 2;
}
else if ("car".equals(vehicleType))
{
price = 4;
}
return price;
}

//Printer
public String toString()
{
return "\nSticker Number: " + stickerNumber + "\nStudent Number: " +
studentNumber + "\nStudent Program: " + studentProgram + "\nPlate Number: " +
plateNumber + "\nVehicle Type: " + vehicleType;
}
}
StudentVehicle main.

import java.util.*;

public class StudentVehicleApp


{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in);

//Step 1 Declare array of object


System.out.print("Enter size of array: ");
int size = scan1.nextInt();
StudentVehicle studVeh[] = new StudentVehicle[size];

//Step 2 Create/ Instantiate array of object


for (int i=0; i<size ; i++)
studVeh[i] = new StudentVehicle();

//Step 3 Input

for (int i = 0 ; i < size; i++)


{
System.out.print("\nEnter sticker number: ");
String IN = scan.nextLine();

System.out.print("Enter student number: ");


String UN = scan.nextLine();

System.out.print("Enter student program: ");


String UP = scan.nextLine();

System.out.print("Enter plate number: ");


String PN = scan.nextLine();

System.out.print("Enter vehicle type: ");


String VT = scan.nextLine();

//Setter

studVeh[i].setstickerNumber(IN);
studVeh[i].setstudentNumber(UN);
studVeh[i].setstudentProgram(UP);
studVeh[i].setplateNumber(PN);
studVeh[i].setvehicleType(VT);

//normal Constructor
studVeh[i] = new StudentVehicle(IN,UN,UP,PN,VT);
}

// Step 5 Manipulation

//i) Count number of motorcycle


int countMotorcycle = 0;

for (int i=0; i<size ; i++)


{
if(studVeh[i].getvehicleType().equals("motorcycle"))
{
countMotorcycle ++;
}
}

System.out.println("\nNumber of students that use mototrcycle: " + countMotorcycle);

//ii) Total charge of sticker

double totCharge = 0.0;

for (int i = 0; i<size; i++)


{
totCharge += studVeh[i].CalcStickerCharge();
}
System.out.println("\nTotal charge for vehicle sticker: RM " + totCharge);
}
}
• Input & Output

Figure 8: Example of input and output 1 for student vehicle program.

Figure 9:Example of input and output 2 for student vehicle program.

You might also like