Java Program Write a class encapsulating the concept of a home, assuming that it
ID: 3810579 • Letter: J
Question
Java Program
Write a class encapsulating the concept of a home, assuming that it has the following attributes: the number of rooms, the square footage, and whether it has a basement. Write a client program that creates five Home objects, writes them to a file as objects, then reads them from the file as objects, outputs a description of each object using the toString method (which the Home class should override), and outputs the number of Home objects. When reading the objects, you should assume that you do not know the number of objects in the file.Explanation / Answer
//Home.java
import java.io.Serializable;
//Home class implements Serializable for
// Saving object into file
public class Home implements Serializable{
//Attributes
int noOfRooms;
float sqFoot;
boolean basement;
//Getters and Setters
public int getNoOfRooms() {
return noOfRooms;
}
public void setNoOfRooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
}
public float getSqFoot() {
return sqFoot;
}
public void setSqFoot(float sqFoot) {
this.sqFoot = sqFoot;
}
public boolean isBasement() {
return basement;
}
public void setBasement(boolean basement) {
this.basement = basement;
}
//Overriding toString() method
@Override
public String toString(){
String bs="";
if(this.isBasement()){
bs="";
}else{
bs="no";
}
return "The home having "+this.getNoOfRooms()+" rooms "+"with "+this.getSqFoot()+" square feet having "+bs+" basement";
}
}
//HomeClient.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class HomeClient {
public static void main(String[] args) throws IOException {
//Array of Five Home Objects
Home home[] = new Home[5];
//ArrayList of type Home Objects
ArrayList<Home> list = new ArrayList<Home>();
Scanner s = new Scanner(System.in);
//Reading Home Objects and Store into List
for(int i=0;i<home.length;i++){
System.out.println("Enter Home-"+(i+1)+" details:");
home[i] = new Home();
System.out.println("Enter no. of rooms:");
home[i].setNoOfRooms(s.nextInt());
System.out.println("Enter the footage:");
home[i].setSqFoot(s.nextFloat());
System.out.println("Room has basement?:");
home[i].setBasement(s.nextBoolean());
//Adding to ArrayList Object
list.add(home[i]);
}
//FileInputStream object to Store objects into File
FileOutputStream fos = new FileOutputStream("D:\Home.txt");
// Storing as a Object for OutputStream
ObjectOutputStream oos = new ObjectOutputStream(fos);
//Writing Object into OutputStream
oos.writeObject(list);
System.out.println("Home Objects are stored in file..");
//Closing Streams
oos.close();fos.close();
}
}
//ReadHome.java
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
public class ReadHome {
public static void main(String[] args) {
try{
FileInputStream fis = new FileInputStream("D:\Home.txt");
//Creating ObjectOuputStream for retrieving objects
ObjectInputStream ois = new ObjectInputStream(fis);
//Reading Objects from File of type ArrayList
ArrayList<Home> list = (ArrayList<Home>) ois.readObject();
System.out.println("No.of Home objects are:"+list.size());
//Iterating Home Objects
Iterator<Home> itr = list.iterator();
while(itr.hasNext()){
Home home = itr.next();
String h = home.toString(); //Calling toString()
System.out.println(h);// Printing Home Objects
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Output for HomeClient:
Storing Home Objects into file:
Enter Home-1 details:
Enter no. of rooms:
3
Enter the footage:
1000.56
Room has basement?:
true
Enter Home-2 details:
Enter no. of rooms:
2
Enter the footage:
650.56
Room has basement?:
true
Enter Home-3 details:
Enter no. of rooms:
6
Enter the footage:
2500.78
Room has basement?:
true
Enter Home-4 details:
Enter no. of rooms:
4
Enter the footage:
1680.56
Room has basement?:
false
Enter Home-5 details:
Enter no. of rooms:
1
Enter the footage:
700.43
Room has basement?:
true
Home Objects are stored in file..
Output for Reading Home Objects:
Retriveing Home Objects from File and printing
No.of Home objects are:5
The home having 3 rooms with 1000.56 square feet having basement
The home having 2 rooms with 650.56 square feet having basement
The home having 6 rooms with 2500.78 square feet having basement
The home having 4 rooms with 1680.56 square feet having no basement
The home having 1 rooms with 700.43 square feet having basement
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.