Question

Design class House to describe a house object by using houseID, town, and zip code of...

Design class House to describe a house object by using houseID, town, and zip code of String type   and price as integer. Write a program that reads a sequence of houseIDs, towns, zip codes and prices for up to 100 houses from an input file. Store the data in an array that has capacity to hold 100 house objects. Keep track of number of houses in variable count.   Assume that each line of input has houseID, town, zip,and price data in this order and separated by one space.

Design class HouseList with following static methods. All methods have two input parameters: an array named list of House type, and n its actual size (number of occupied positions in the array).

  • static void printList(House[] list, int n)                            // prints all n houses in the list
  • static House mostExpensive(House[] list, int n)             // returns the most expensive house
  • static int countZIP((House[] list, int n, String zip)          // returns the number of houses in given zip
  • static void printListReversed(House[] list, int n)            // prints houses from n-th down to the

                                                                                                // first house in the list.

  • static String zipOfCheapest(House[] list, int n)              // EXTRA CREDIT returns the zip code of the

// cheapest house in the list. If more than one

                                                                                              // house has the same cheapest price return

                                                                                                // zip code on any one of them.

Class Tester will have main method. In it read data from input file "inData.txt" into array houseList, and update variable count as you read houses from input file. Invoke each of the five methods. You should create an input file, and put data for 7 houses in it.

First three Houses in the input file should be:

H101 NewBritain 06050 165000

H103 Berlin 06037 279900

H105 Newington 06111 325000

and remaining four should be of your choice. Use only towns with one word names, or if town name has two words omit space between them. Input file should be in the same folder where all files from BlueJ for this program are located. Do not forget to append throws IOException to the main method header, and to use following import clauses in the Tester class :                        

import java.io.*;     

import java.util.*;

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * House.java
 */
public class House {
    // private attributes
    private String houseId, town, zip;
    private int price;

    // constructor for initializing the value
    public House(String houseId, String town, String zip, int price) {
        this.houseId = houseId;
        this.town = town;
        this.zip = zip;
        this.price = price;
    }

    // getters and setters
    public String getHouseId() {
        return houseId;
    }

    public void setHouseId(String houseId) {
        this.houseId = houseId;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return String.format("House ID: %s\nTown: %s\nZip: %s\nPrice: %d\n", houseId, town, zip, price);
    }
}
/**
 * HouseList.java
 */
public class HouseList {

    static void printList(House[] list, int n) {
        // looping through list and printing values
        for (int i = 0; i < n; i++) {
            System.out.println(list[i]);
        }
    }

    static House mostExpensive(House[] list, int n) {
        int expensiveAmount = Integer.MIN_VALUE;
        House expensiveHouse = null;
        // looping through house list and finding expensive house
        for (int i = 0; i < n; i++) {
            if (list[i].getPrice() > expensiveAmount) {
                expensiveAmount = list[i].getPrice();
                expensiveHouse = list[i];
            }
        }
        // returning the expensive house
        return expensiveHouse;
    }

    static int countZip(House[] list, int n, String zip) {
        int count = 0;
        // looping though house list and finding the count of given zip
        for (int i = 0; i < n; i++) {
            if (list[i].getZip().equalsIgnoreCase(zip))
                count++;
        }
        return count;
    }

    static void printListReversed(House[] list, int n) {
        // printing reversed house list
        for (int i = n - 1; i >= 0; i--) {
            System.out.println(list[i]);
        }

    }

    static String zipOfCheapest(House[] list, int n) {
        int lowestPrice = Integer.MAX_VALUE;
        String lowesPriceZip = null;
        for (int i = 0; i < n; i++) {
            if (lowestPrice > list[i].getPrice()) {
                lowestPrice = list[i].getPrice();
                lowesPriceZip = list[i].getZip();
            }
        }
        return lowesPriceZip;
    }
}
/**
 * Tester.java
 */

import java.io.*;
import java.util.Scanner;

public class Tester {
    public static void main(String[] args) throws IOException {
        final int MAX_HOUSE = 100;
        House list[] = new House[MAX_HOUSE];
        int actualCount = 0;
        String fileName = "inData.txt";
        Scanner fileReader = new Scanner(new File(fileName));
        while (fileReader.hasNextLine() && actualCount < MAX_HOUSE){
            String values[] = fileReader.nextLine().trim().split(" ");
            if(values.length>=4){
                list[actualCount++] = new House(values[0].trim(),
                                                values[1].trim(),
                                                values[2].trim(),
                                                Integer.valueOf(values[3].trim()));
            }

        }
        HouseList.printList(list, actualCount);
        System.out.println("--------------------------------------------------------");
        System.out.println("Most Expensive House\n"+HouseList.mostExpensive(list, actualCount));
        System.out.println("--------------------------------------------------------");
        System.out.println("Count of Zip 06111: "+HouseList.countZip(list, actualCount, "06111"));
        System.out.println("--------------------------------------------------------");
        System.out.println("\nReversed List\n");
        HouseList.printListReversed(list,actualCount);
        System.out.println("--------------------------------------------------------");
        System.out.println("Zip of cheapest house: "+HouseList.zipOfCheapest(list, actualCount));
        System.out.println("--------------------------------------------------------");
    }
}

//OUTPUT

House ID: H101
Town: NewBritain
Zip: 06050
Price: 165000

House ID: H103
Town: Berlin
Zip: 06037
Price: 279900

House ID: H105
Town: Newington
Zip: 06111
Price: 325000

--------------------------------------------------------
Most Expensive House
House ID: H105
Town: Newington
Zip: 06111
Price: 325000

--------------------------------------------------------
Count of Zip 06111: 1
--------------------------------------------------------

Reversed List

House ID: H105
Town: Newington
Zip: 06111
Price: 325000

House ID: H103
Town: Berlin
Zip: 06037
Price: 279900

House ID: H101
Town: NewBritain
Zip: 06050
Price: 165000

--------------------------------------------------------
Zip of cheapest house: 06050
--------------------------------------------------------

Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Design class House to describe a house object by using houseID, town, and zip code of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • Hey I really need some help asap!!!! I have to take the node class that is...

    Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...

  • Hey guys! Huge part of my grade !! My code works already but I need it...

    Hey guys! Huge part of my grade !! My code works already but I need it so it references to my node list class instead of my array, what would I need to change? My assignment  was to create a list from an assignment when we used an array, but I cant make it to work without the array, help! The professor said it was okay to either keep the array or to delete it altogether. package zipcode; import java.io.File; import...

  • Use BlueJ to write a program that reads a sequence of data for several car objects...

    Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program...

  • Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

    Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields. Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both...

  • LAB: Zip code and population (generic types)

    13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...

  • All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary...

    All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes river's name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles long and returns false otherwise. Class CTRivers describes collection of CT rivers. It has no data, and it provides the...

  • Class River describes river’s name and its length in miles. It provides accessor methods (getters) for...

    Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables and toString() method that returns String representation of the river. Class CTRivers describes collection of CT rivers. It has no data, and it provides the following service methods. None of the methods prints anything, except method printListRec, which prints all rivers. // Prints all rivers recursively. Print them is same order as they were in the list . List can be empy...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT