Lab 3 Oop
Lab 3 Oop
Lab 3 Oop
CSC435
//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;
}
//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.*;
//Step 3 Input
//Setter
sp[i].setName(NM);
sp[i].setId(Ix);
sp[i].setSales(SL);
//normal Constructor
// Step 5 Manipulation
double totComm=0;
for(int i=0; i<size ; i++)
totComm += sp[i].CommissionRate();
}
}
• Input & Output
//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;
}
//Retriever
public String getName()
{
return Name;
}
//Processor
public double calcPayment()
{
double GST;
double totPrice;
return totPrice;
}
//Printer
public String toString()
{
return "\n\nName: " + Name + "\nPrice per meter: " + Price +
"\nLength in meter: " + Length;
}
}
ClothApp main
import java.util.*;
//Step 3 Input
ct[i].setName(NM);
ct[i].setPrice(PR);
ct[i].setLength(LG);
//normal Constructor
totAll += totalPayment;
System.out.println(ct[i].toString());
}
}
• Input & Output
//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;
}
//Processor
public int calculateAge()
{
int age = 0;
String icString = String.valueOf(Ic);
int birthYear = Integer.parseInt(icString.substring(0, 2));
return age;
}
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.*;
//Step 3 Input
for (int i = 0 ; i < size; i++)
{
System.out.print("\nEnter name: ");
String NM = 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++;
}
//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++;
}
//iii) Display the member information from SPM batch year 1995.
//iv) Display the member information who age >= 50 years old.
//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);
//Data Members
//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;
}
//Retriever
public String getfurnitureType()
{
return furnitureType;
}
//Processor
//Printer
public String toString()
{
return "\nFurniture Type: " + furnitureType + "\nMaterial: " + material + "\nPrice Per Unit: " +
pricePerUnit + "\nQuantity:" + quantity;
}
}
Furniture main.
import java.util.*;
//Step 3 Input
for (int i = 0 ; i < size; i++)
{
System.out.print("\nEnter Furniture Type : ");
String FT = 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);
}
}
• Input & Output
//Data Members
//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;
}
//Retriever
public String getbrand()
{
return brand;
}
//Processor
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.*;
//Step 3 Input
//Setter
Laptops[i].setbrand(BR);
Laptops[i].setprice(PR);
Laptops[i].setRAM(R);
Laptops[i].setUSBport(USB);
//normal Constructor
// Step 5 Manipulation
}
}
• Input & Output
//Data Members
//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;
}
//Retriever
public String getstickerNumber()
{
return stickerNumber;
}
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.*;
//Step 3 Input
//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