Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CSC248 – Fundamentals of Data Structure

Academic Session Oct 2023 – Feb 2024


Lab Assignment – Review of OOP

Course Outcomes (CO) LO1 LO2 LO3


CO1
CO2 √ √ √
CO3

ZIYAD ZAHIDIN BIN YUSRI AMRI


2021894702
RCS1103B

1.1 Class Land has the following attributes and methods:

Attributes:
• id
• owner name
• house type
• area

i. Write the Land class and the following methods:

a) Default constructor.
b) Normal constructor that set all data with values given through the parameter.
c) Mutator/Setter method
d) Retriever method for each attribute.
e) Printer method using toString()defined method.
f) A processor method to calculate and return the tax amount. The tax of this
type of land depends on its area, and the type of the house built on the land
as shown in the following table:

House Type Description Tax rate (RM/m3)


T Terrace 10
S Semi-Detached 15
B Bungalow 20
C Condominium 30
Details of land

Answer

public class Land {


private int id;
private String ownerName;
private String houseType;
private double area;

// Default constructor
public Land() {
// Initialize attributes with default values
id = 0;
ownerName = "";
houseType = "";
area = 0.0;
}

// Normal constructor to set all data with values given through parameters
public Land(int id, String ownerName, String houseType, double area) {
this.id = id;
this.ownerName = ownerName;
this.houseType = houseType;
this.area = area;
}

// Mutator/Setter methods
public void setId(int id) {
this.id = id;
}

public void setOwnerName(String ownerName) {


this.ownerName = ownerName;
}

public void setHouseType(String houseType) {


this.houseType = houseType;
}
public void setArea(double area) {
this.area = area;
}

// Retriever methods for each attribute


public int getId() {
return id;
}

public String getOwnerName() {


return ownerName;
}

public String getHouseType() {


return houseType;
}

public double getArea() {


return area;
}

// Printer method using toString() defined method


@Override
public String toString() {
return "Land ID: " + id + "\nOwner Name: " + ownerName + "\nHouse
Type: " + houseType + "\nArea: " + area + " square meters";
}

// Processor method to calculate and return the tax amount


public double calculateTax() {
double taxRate = 0.0;

// Determine the tax rate based on the house type


switch (houseType) {
case "T":
taxRate = 10.0;
break;
case "S":
taxRate = 15.0;
break;
case "B":
taxRate = 20.0;
break;
case "C":
taxRate = 30.0;
break;
default:
System.out.println("Invalid house type");
}

// Calculate and return the tax amount


return taxRate * area;
}
}

ii. Write an application program that performs the following:

a) Declare array of object for Land objects.

b) Given the input file named customerData.txt that includes the


customers data such as id, owner name, house type and area. The following
input text fil includes all record of customer for the Land class:

Write a program that reads each record from customerData.txt and


store onto array of object Land.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class LandApplication {


public static void main(String[] args) {
// Declare an array of Land objects
Land[] landArray = new Land[100]; // Assuming a maximum of 100
records, you can adjust this size as needed

// Initialize the array with default Land objects


for (int i = 0; i < landArray.length; i++) {
landArray[i] = new Land();
}

// Read data from customerData.txt and store it in the Land array


try (BufferedReader br = new BufferedReader(new
FileReader("customerData.txt"))) {
String line;
int index = 0; // Index for the array

while ((line = br.readLine()) != null) {


// Split the line into fields (assuming fields are separated by
spaces or tabs)
String[] data = line.split("\\s+");

if (data.length == 4) {
int id = Integer.parseInt(data[0]);
String ownerName = data[1];
String houseType = data[2];
double area = Double.parseDouble(data[3]);

// Create a Land object and store it in the array


landArray[index] = new Land(id, ownerName, houseType,
area);
index++;

// You may want to add error handling for the case when the
array is full
} else {
System.out.println("Invalid data format: " + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}

// Now you have an array of Land objects containing the data from the
input file

// You can print the details of each Land object if needed


for (Land land : landArray) {
System.out.println(land.toString());
}
}
}

c) Display a menu selection to select the following process:

------------------------------------------------------
Menu Selection

1. Sorting using Bubble Sort 2. Sorting using Insertion


Sort 3. Searching using Binary Search

Your Option: XX

------------------------------------------------------

****Details explanation:
1-Sorting using Bubble Sort – Sort the list based on the tax price and display
the list
2.Sorting using Insertion Sort – Sort the list based on id and display the list
3.Searching using Binary Search-Search the item from the list based on id
and display the information detail.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class LandApplication {


public static void main(String[] args) {
// ... Previous code to read data from customerData.txt and store it in
the Land array ...

Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("------------------------------------------------------");
System.out.println("Menu Selection");
System.out.println("1. Sorting using Bubble Sort");
System.out.println("2. Sorting using Insertion Sort");
System.out.println("3. Searching using Binary Search");
System.out.println("4. Exit");
System.out.print("Your Option: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume the newline character

switch (choice) {
case 1:
// Sorting using Bubble Sort
bubbleSort(landArray);
displayLandArray(landArray);
break;
case 2:
// Sorting using Insertion Sort
insertionSort(landArray);
displayLandArray(landArray);
break;
case 3:
// Searching using Binary Search
System.out.print("Enter the ID to search: ");
int searchId = scanner.nextInt();
int result = binarySearch(landArray, searchId);
if (result != -1) {
System.out.println(landArray[result].toString());
} else {
System.out.println("Land with ID " + searchId + " not
found.");
}
break;
case 4:
// Exit the program
System.out.println("Exiting the program.");
System.exit(0);
default:
System.out.println("Invalid option. Please select a valid option
(1-4).");
break;
}
}
}

// Additional utility methods for sorting and searching


public static void bubbleSort(Land[] landArray) {
// Sort the array based on the tax price
int n = landArray.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (landArray[j].calculateTax() > landArray[j + 1].calculateTax()) {
// Swap elements
Land temp = landArray[j];
landArray[j] = landArray[j + 1];
landArray[j + 1] = temp;
}
}
}
}

public static void insertionSort(Land[] landArray) {


// Sort the array based on ID
int n = landArray.length;
for (int i = 1; i < n; i++) {
Land key = landArray[i];
int j = i - 1;
while (j >= 0 && landArray[j].getId() > key.getId()) {
landArray[j + 1] = landArray[j];
j = j - 1;
}
landArray[j + 1] = key;
}
}
public static int binarySearch(Land[] landArray, int searchId) {
int left = 0;
int right = landArray.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (landArray[mid].getId() == searchId) {
return mid;
}
if (landArray[mid].getId() < searchId) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // ID not found
}

public static void displayLandArray(Land[] landArray) {


// Display the information of all Land objects in the array
for (Land land : landArray) {
System.out.println(land.toString());
}
}
}

You might also like