0% found this document useful (0 votes)
18 views14 pages

Lab Asg

Uploaded by

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

Lab Asg

Uploaded by

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

CSC186 – OBJECT ORIENTED

PROGRAMMING

NAME : UMAR ZIKRI BIN ABDUL GHANI


STUDENT ID : 2023483164 GROUP
: CDCS1102B
TITLE : LAB ASSIGNMENT 4
QUESTION 1
import java.io.File; import
java.io.FileNotFoundException; import
java.io.FileWriter; import
java.io.IOException; import
java.util.Scanner;

public class FTMSK {

public static void main(String[] args) {

try (Scanner scanner = new Scanner(new File("FTMSK.txt"))) {


int cs110MaleCount = 0; int cs111MaleCount = 0;

try (FileWriter cs110MaleWriter = new FileWriter("CS110Male.txt");


FileWriter cs111MaleWriter = new FileWriter("CS111Male.txt")) {

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(";");

String matricNumber = parts[0];


String studentName = parts[1];
String program = parts[2];
String part = parts[3];
String gender = parts[4];

if (program.equals("CS110") && gender.equals("M")) {


cs110MaleWriter.write(matricNumber + ";" + studentName + ";" + part + "\n");
cs110MaleCount++;
} else if (program.equals("CS111") && gender.equals("M")) {
cs111MaleWriter.write(matricNumber + ";" + studentName + ";" + part + "\n");
cs111MaleCount++;
}
}
System.out.println("Total records in CS110Male.txt: " + cs110MaleCount);
System.out.println("Total records in CS111Male.txt: " + cs111MaleCount);
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
}

SAMPLE INPUT
2021001;John Doe;CS110;3;M
2021002;Jane Doe;CS111;2;F
2021003;Richard Roe;CS110;1;M
2021004;Alice Smith;CS111;1;F
2021005;Bob Johnson;CS111;4;M

SAMPLE OUTPUT CS110Male.txt:


2021001;John Doe;3
2021003;Richard Roe;1

CS111Male.txt:
2021005;Bob Johnson;4

Console Output:
Total records in CS110Male.txt: 2
Total records in CS111Male.txt: 1
QUESTION 2
import java.io.File; import
java.io.FileNotFoundException; import
java.io.FileWriter; import
java.io.IOException; import
java.util.Scanner;

class Vehicle { private


String carType; private
String carPlate; private
double carPrice;

public Vehicle(String carType, String carPlate, double carPrice) {


this.carType = carType; this.carPlate = carPlate;
this.carPrice = carPrice;
}

public String getCarPlate() {


return carPlate;
}

public double getCarPrice() {


return carPrice;
}

@Override public String toString() {


return carType + ";" + carPlate + ";" + carPrice;
}
}

public class CarRecords {

public static void main(String[] args) {


Vehicle[] arrCar = new Vehicle[100]; // Assume max 100 cars
int carCount = 0;
try (Scanner scanner = new Scanner(new File("car.txt"))) {
while (scanner.hasNextLine()) { String line =
scanner.nextLine();
String[] parts = line.split(";");

if (parts.length == 3) { String carType = parts[0];


String carPlate = parts[1]; double carPrice =
Double.parseDouble(parts[2]); arrCar[carCount++] = new
Vehicle(carType, carPlate, carPrice);
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
return;
} catch (NumberFormatException e) {
System.out.println("Error parsing car price: " + e.getMessage());
return;
}

double totalSelangorPrice = 0.0;


double totalTerengganuPrice = 0.0;

try (FileWriter selangorWriter = new FileWriter("selangor.txt");


FileWriter terengganuWriter = new FileWriter("terengganu.txt")) {

for (int i = 0; i < carCount; i++) {


Vehicle car = arrCar[i];
String carPlate = car.getCarPlate();
double carPrice = car.getCarPrice();

if (carPlate.startsWith("B")) {
selangorWriter.write(car.toString() + "\n");
totalSelangorPrice += carPrice; } else if
(carPlate.startsWith("T")) {
terengganuWriter.write(car.toString() + "\n");
totalTerengganuPrice += carPrice;

}
}

selangorWriter.write("Total price: " + totalSelangorPrice + "\n");


terengganuWriter.write("Total price: " + totalTerengganuPrice + "\n");

} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
}
SAMPLE INPUT
Sedan;B1234XYZ;50000
SUV;T5678ABC;75000
Truck;B4321XYZ;60000
Coupe;T8765ABC;65000
Hatchback;B6789XYZ;30000

Sample Output:
Sedan;B1234XYZ;50000.0
Truck;B4321XYZ;60000.0
Hatchback;B6789XYZ;30000.0
Total price: 140000.0

terengganu.txt:
SUV;T5678ABC;75000.0
Coupe;T8765ABC;65000.0
Total price: 140000.0

Console Output:
Total records in selangor.txt: 3
Total records in terengganu.txt: 2
QUESTION 3
import java.io.File; import
java.io.FileNotFoundException; import
java.io.FileWriter; import
java.io.IOException; import
java.util.Scanner;

public class FoodDelivery {

public static void main(String[] args) {


int lateDeliveries = 0;

try (Scanner scanner = new Scanner(new File("deliveroo.txt"));


FileWriter earningsWriter = new FileWriter("earnings.txt")) {

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(";");

if (parts.length == 8) {
String date = parts[0];
String day = parts[1];
String startTime = parts[2];
String endTime = parts[3];
String restaurant = parts[4]; double
totalPrice = Double.parseDouble(parts[5]); double
distance = Double.parseDouble(parts[6]); double
priceMultiplier = Double.parseDouble(parts[7]);

double finalPrice = totalPrice * priceMultiplier;


double profit = (priceMultiplier * finalPrice) - finalPrice;

earningsWriter.write("Date: " + date + ", Profit: " + profit + "\n");

int startHour = Integer.parseInt(startTime.split(":")[0]);


if (startHour >= 21) { lateDeliveries++;

}
}
}

System.out.println("Number of late deliveries (9 PM or later): " + lateDeliveries);

} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Error parsing number: " + e.getMessage());
}
}
}

SAMPLE INPUT
02032023;Monday;08:00;09:00;PizzaHut;50;10;1.2
03032023;Tuesday;21:00;22:00;Dominos;60;15;1.3
04032023;Wednesday;22:00;23:00;KFC;40;5;1.1
05032023;Thursday;18:00;19:00;McDonalds;30;8;1.4

Sample Output:
Date: 02032023, Profit: 10.0
Date: 03032023, Profit: 18.0
Date: 04032023, Profit: 4.0
Date: 05032023, Profit: 12.0

Console Output:
Number of late deliveries (9 PM or later): 2
QUESTION 4
import java.io.File; import
java.io.FileNotFoundException; import
java.io.FileWriter; import
java.io.IOException; import
java.util.Scanner;

class Supermarket {
private String itemName;
private int quantity;
private double unitPrice;

public Supermarket(String itemName, int quantity, double unitPrice) {


this.itemName = itemName; this.quantity = quantity;
this.unitPrice = unitPrice;
}

public double getSubtotal() {


return quantity * unitPrice;
}

@Override public String toString() { return


itemName + ";" + quantity + ";" + unitPrice;
}
}

public class SupermarketSales {

public static void main(String[] args) {


Supermarket[] arrStore = new Supermarket[100]; // Assume max 100 items
int itemCount = 0; double totalSales = 0.0;

try (Scanner scanner = new Scanner(new File("20180221.txt"))) {


while (scanner.hasNextLine()) {

String line = scanner.nextLine();


String[] parts = line.split(";");
if (parts.length == 3) { String itemName = parts[0]; int
quantity = Integer.parseInt(parts[1]); double unitPrice =
Double.parseDouble(parts[2]); arrStore[itemCount++] = new
Supermarket(itemName, quantity, unitPrice);
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
return;
} catch (NumberFormatException e) {
System.out.println("Error parsing number: " + e.getMessage());
return;
}

try (FileWriter reportWriter = new FileWriter("reportSale.txt")) {


for (int i = 0; i < itemCount; i++) { Supermarket item = arrStore[i];
double subtotal = item.getSubtotal();
reportWriter.write(item.toString() + ";Subtotal=" + subtotal + "\n");
totalSales += subtotal;
}

reportWriter.write("Total Sales: " + totalSales + "\n");

} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
}

SAMPLE INPUT
Apple;5;1.20
Banana;10;0.80
Orange;8;1.50
Grapes;2;2.00

SAMPLE OUTPUT
Apple;5;1.20;Subtotal=6.0
Banana;10;0.80;Subtotal=8.0
Orange;8;1.50;Subtotal=12.0
Grapes;2;2.00;Subtotal=4.0
Total Sales: 30.0

QUESTION 5
import java.io.*; import
java.util.*; public class
Photocopy {
String lectName;
String staffID;
String facCode;
String assType; int
masterNum; int
copiesNum;

public Photocopy() {
}

public void setPhotocopy(String lectName, String staffID, String facCode, String assType, int
masterNum, int copiesNum) { this.lectName = lectName; this.staffID = staffID;
this.facCode = facCode; this.assType = assType; this.masterNum = masterNum;
this.copiesNum = copiesNum;
}

public String getLectName() {


return lectName;
}

public String getStaffID() {


return staffID;
}

public String getFacCode() {


return facCode;
}
public String getAssType() {
return assType;
}

public int getMasterNum() {


return masterNum;
}

public int getCopiesNum() {


return copiesNum;
}

public String toString() {


return "Photocopy{" +
"lectName='" + lectName + '\'' +
", staffID='" + staffID + '\'' +
", facCode='" + facCode + '\'' +
", assType='" + assType + '\'' +
", masterNum=" + masterNum +
", copiesNum=" + copiesNum +
'}';
}

public static void main(String[] args) {


try {
Photocopy[] copiesArr = new Photocopy[200]; // Array to store photocopy records
int count = 0; // Counter for the number of records in the array

File file = new File("photocopyInfo.txt");


Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) { String
line = scanner.nextLine();
String[] parts = line.split(",");

Photocopy photocopy = new Photocopy();


photocopy.setPhotocopy(parts[0].trim(), parts[1].trim(), parts[2].trim(), parts[3].trim(),
Integer.parseInt(parts[4].trim()), Integer.parseInt(parts[5].trim())); copiesArr[count] =
photocopy; count++;
}

scanner.close();
System.out.println("Photocopy services requested by lecturers of FHTM:");
for (int i = 0; i < count; i++) {
if (copiesArr[i].getFacCode().equals("FHTM")) {
System.out.println(copiesArr[i]);
}
}

PrintWriter writer = new PrintWriter("quizRequest.txt");


writer.println("Detail of request for Quiz"); for (int i = 0;
i < count; i++) {
if (copiesArr[i].getAssType().equals("quiz")) {
writer.println("Data" + (i + 1) + ":"); writer.println("Staff
ID: " + copiesArr[i].getStaffID() +
" Number of pages for master copy: " + copiesArr[i].getMasterNum() +
" Number of copies required: " + copiesArr[i].getCopiesNum() +
" Total: " + (copiesArr[i].getMasterNum() * copiesArr[i].getCopiesNum()));
}
}
writer.close();

} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}

SAMPLE INPUT
Notebook;2;3.00
Pen;10;0.50
Pencil;5;0.80
Eraser;4;0.30

SAMPLE OUTPUT
Notebook;2;3.00;Subtotal=6.0
Pen;10;0.50;Subtotal=5.0
Pencil;5;0.80;Subtotal=4.0
Eraser;4;0.30;Subtotal=1.2
Total Sales: 16.2

You might also like