Lab Asg
Lab Asg
PROGRAMMING
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(";");
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
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;
if (carPlate.startsWith("B")) {
selangorWriter.write(car.toString() + "\n");
totalSelangorPrice += carPrice; } else if
(carPlate.startsWith("T")) {
terengganuWriter.write(car.toString() + "\n");
totalTerengganuPrice += carPrice;
}
}
} 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;
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]);
}
}
}
} 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;
} 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;
}
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]);
}
}
} 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