Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Write a class encapsulating the concept of a home, assuming that it ha

ID: 3567573 • Letter: Q

Question

Question: 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 classshould 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.

I am not sure what the problem is asking or how to set up both the Home class and the client class.

Explanation / Answer

Client.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class Client {
  
   public static void main(String[] args) throws IOException {
BufferedReader bfr;
String line;
bfr=new BufferedReader(new InputStreamReader(System.in));
  
/* creating 5 house objects */
Home[] homes = new Home[]
       {
           new Home(4, 120, true),
           new Home(4, 100, true),
           new Home(2, 240, false),
           new Home(5, 100, true),
           new Home(1, 200, false)
       };
  
/*create text file homes.txt to store house details */
String fileName="homes.txt";
File file=new File(fileName);   
if(!file.exists()){
file.createNewFile();
}   
try{
bfr=new BufferedReader(new FileReader(file));

FileWriter fw=new FileWriter(file);
for(int i=0;i<5;i++){   
fw.append(String.valueOf(homes[i].numberOfRooms)+" ");
fw.append(String.valueOf(homes[i].squareFootage)+" ");
fw.append(String.valueOf(homes[i].hasBasement)+" ");
}

bfr.close();
fw.close();

}catch(FileNotFoundException fex){
fex.printStackTrace();
}
  
  
/*House detail fetch to an array list*/
ArrayList<Home> newHome = new ArrayList<Home>();
  
if(!file.exists()){
file.createNewFile();
}   
try{
bfr=new BufferedReader(new FileReader(file));

while((line=bfr.readLine())!=null){
  
   newHome.add(new Home
           (
                   Integer.parseInt(line),
                   Integer.parseInt(bfr.readLine()),
                   Boolean.parseBoolean(bfr.readLine())
           ));
}
bfr.close();

}catch(FileNotFoundException fex){
fex.printStackTrace();
}
  
/*print house detail*/
int i=0;
for (Home home : newHome) {
           System.out.println("House "+(++i)+" : "+home.toString());
       }

}
}

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

Home.java


public class Home {
   public int numberOfRooms;
   public int squareFootage;
   public boolean hasBasement;
  
   public Home(int n, int s, boolean b)
   {
       numberOfRooms = n;
       squareFootage = s;
       hasBasement = b;
   }
  
   public String toString() {
       String str = "It has "+numberOfRooms+" rooms and is of "+squareFootage+ " square footage with ";
      
       if(hasBasement)
           str+= "basement";
       else
           str+= "NO basement";
       return str;
      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote