You are on page 1of 9

CSC435

OBJECT ORIENTED PROGRAMMING

FINAL ASSESSMENT JUL 2022

MOHD NIZAM B OSMAN

Prepared By :

NAME NURUL QISTINA BINTI OSMAN TALIB

MATRIC NO 2021816376

GROUP RCS2402A
Part B

Question 1

A)

I)
//Normal Constructor
public Employee (String id,String nm, double sa)
{
id = id;
name = nm;
salesAmount = sa;
}

Ii)
//Getter or accessor methods
public String getId() {return id;}
public String getName() {return name;}
public double getSalesAmount() {return salesAmount;}

Iii)
//Processor methods
public int calBonus() {

int bonus;

if (salesAmount<=1000)
bonus = 0;
else if (salesAmount <=2500)
bonus = 200;
else
bonus = 300;

return bonus;
}
B)

I)
//Normal Constructor
employee[i] = new Employee(id,nm,sa);
employee[I].id = 1002;
employee[I].nm = “ali”;
employee[I].sa= 1500;

ii)
for(int i=0 ; i<size ; i++){
System.out.println( employee[i].toString() +
"\n Bonus: RM " +String.format("%.2f",employee[i].calBonus()));
}

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

if(employee[i].calBonus() >0)
System.out.println("Congrats, you will receive RM%d ”
+employee[i].calBonus());

else
System.out.println("Work harder next month”);
}
Question 2

A)

I)

//Normal Constructor
public SmallItemDelivery(String cn, double dis, int as,char vt)
{
super(cn,dis,as);
vehicleType = vt ;
}

public LorryDelivery(String cn, double dis, int as, boolean ct, int nh)
{
super(cn,dis,as);
coldTruck = ct;
numHelper = nh;
}

Ii)
//Abstract method - Processor SmallItemDelivery
public double calcCost()
{
double fare = 0;

if(vehicleType.equalsIgnoreCase(‘M’)) {
if (getDistanceKM()<5)
fare = 5;
else
fare = getDistanceKM();
}
else {
if (getDistanceKM()<15)
fare = 2 * getDistanceKM();
else
fare = 1.50 * getDistanceKM();
}

if(getAdditionalStop()){
if(vehicleType.equalsIgnoreCase(‘M’))
fare = fare + getAdditionalStop();
else
fare = fare + (2 * getAdditionalStop());
}
return fare;
}
//Abstract method - Processor LorryDelivery
public double calcCost()
{
double fare = 0;

if( getDistanceKM()<=5)
fare = 170;
else(getDistanceKM()>5)
fare = 170 + ( 3* (getDistanceKM() -5) );

if(getcoldTruck())
fare = fare +120;

if(getAdditionalStop())
fare = fare + (10*getAdditionalStop());

if(getnumHelper()>0)
fare = fare + (156*getnumHelper());

return fare;
}

B)

I)
double costBelow = 0;
double costAbove = 0;

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


{
if (deliveries[i].getDistanceKM() < 50)
costBelow += deliveries[i].calcCost();

else
costAbove += deliveries[i].calcCost();
}

System.out.println("Total cost deliveries below 50KM = RM” +costBolow);


System.out.println("Total cost deliveries above 50KM = RM” +costAbove);

Ii)
double disS = 0;
double disL = 0;
double avgS = 0;
double avgL = 0;
for (int i=0; i<100;i++) {
if (deliveries[i] instanceof SmallItemDelivery)
disS+ =deliveries[i].getDistanceKM()

if (deliveries[i] instanceof LorryDelivery)


disL+= deliveries[i].getDistanceKM()

avgS = disS / 100;


avgL = disL/100;

System.out.println("Average distance for small item delivery = “ +avgS;


System.out.println("Average distance of lorry delivery service = ” +avgL);
Question 3

import java.io.*;
import java.util.StringTokenizer;

public class collegeApp{


public static void main(String[] args){
//STEP 1: Open File input and Output
try{
//File input
BufferedReader in = new BufferedReader (new
FileReader("D:\\college_application.txt"));
//File output successful_applications
PrintWriter outsuccessful = new PrintWriter(new
BufferedWriter(new FileWriter("D:\\successful_applications.txt")));
//File output waiting_list
PrintWriter outwaiting = new PrintWriter(new
BufferedWriter(new FileWriter("D:\\waiting_list.txt")));

//STEP 2: Read File Input

String inData = null;


while((inData = in.readLine())!= null){
StringTokenizer ca = new
StringTokenizer(inData,";");
String date = ca.nextToken();
String id = ca.nextToken();
String gender= ca.nextToken();
String name = ca.nextToken();
String faculty = ca.nextToken();
int semester = Integer.parseInt(ca.nextToken());
String add = ca.nextToken();
StringTokenizer ad = new
StringTokenizer(inData,",");
String street = ad.nextToken();
String postcode = ad.nextToken();
String city = ad.nextToken();
String state = ad.nextToken();

//STEP 3: Manipulation
int countS=0;
int countW=0;
if(semester>=5 &&
state.equalsIgnoreCase("Sabah") &&state.equalsIgnoreCase("Sarawak")){
countS++;

outsuccessful.println(date+";"+id+";"+gender+";"+name+";"+faculty+";"+
semester+";"+add);
}

else if(Integer.parseInt(date.substring(4,5))==01){
countW++;

outwaiting.println(date+";"+id+";"+gender+";"+name+";"+faculty+";"+se
mester+";"+add);
}
}//end while
//STEP 4: Close all file input and output
System.out.println("number of successful applications = "
+countS);
System.out.println("number of applications on the waiting
list = " +countW);
in.close();
outsuccessful.close();
outwaiting.close();
}//end try
//STEP 5: Catch
catch(FileNotFoundException fe){
System.out.println(fe.getMessage());
}

catch(IOException iox){
System.out.println(iox.getMessage());
}
catch (Exception e){
System.out.println("Problem: "+e.getMessage());
}
}//end main
}//end class

You might also like