Professional Documents
Culture Documents
Lab Assignment 1 - Ziyad Zahidin 2021894702
Lab Assignment 1 - Ziyad Zahidin 2021894702
Attributes:
• id
• owner name
• house type
• area
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:
Answer
// 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;
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
if (data.length == 4) {
int id = Integer.parseInt(data[0]);
String ownerName = data[1];
String houseType = data[2];
double area = Double.parseDouble(data[3]);
// 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
------------------------------------------------------
Menu Selection
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;
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: ");
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;
}
}
}