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

I\'m having a hard time writing these last two methods in java. ________________

ID: 3619534 • Letter: I

Question

I'm having a hard time writing these last two methods in java.
_________________________________________________________________________________________________________________________________________________________
Create a class named InputGetter. Use this starter code: InputGetter.java
• You must write the addHouse method. It must be public. It returns nothing and accepts no parameters. Here is my program's output. Make sure your addHouse method prompts the user in the same way. The blue text was printed by mainMenu(). The red text was printed by addHouse. Don't forget to use the information from the user to create a House object and tell the Listings object to add it.
What do you want to do?
Enter 1 to add a house.
Enter 2 to sort listings by number of bedrooms.
Enter 3 to quit.
1
How many bedrooms? 5
How many bathrooms? 3.5
How many square feet? 4000
What is the asking price? 575000
What do you want to do?
Enter 1 to add a house.
Enter 2 to sort listings by number of bedrooms.
Enter 3 to quit.
3

• You must write the sortByBedRms method which returns nothing and accepts no parameters. It must be public.
This method asks the user for min and max bedrooms. It tells the object named list to run that method you wrote in the Listings class. Then it prints out the return. Use a loop to print out the contents of the returned ArrayList<House>. I want the Houses all printed on separate lines. Here is my output. You must match my prompts for the testscript to work. Remember the separation of responsibilities between objects. This InputGetter object just prompts the user and gets input from the keyboard and gives it to the Listings object who does the actual sorting. The Listing object returns an ArrayList<House> to the InputGetter object. The InputGetter object is responsible for printing it.
What do you want to do?
Enter 1 to add a house.
Enter 2 to sort listings by number of bedrooms.
Enter 3 to quit.
1
How many bedrooms? 5
How many bathrooms? 3.5
How many square feet? 4000
What is the asking price? 575000
What do you want to do?
Enter 1 to add a house.
Enter 2 to sort listings by number of bedrooms.
Enter 3 to quit.
2
Give me the minimum and maximum number of bedrooms desired.
5
5
House [numBathRs=3.5, numBedRs=5, price=575000.0, sqft=4000]
What do you want to do?
Enter 1 to add a house.
Enter 2 to sort listings by number of bedrooms.
Enter 3 to quit.
2
Give me the minimum and maximum number of bedrooms desired.
4
5
House [numBathRs=2.5, numBedRs=4, price=300000.0, sqft=2000]
House [numBathRs=3.5, numBedRs=4, price=450000.0, sqft=3000]
House [numBathRs=2.5, numBedRs=4, price=500000.0, sqft=2600]
House [numBathRs=3.5, numBedRs=5, price=575000.0, sqft=4000]
What do you want to do?
Enter 1 to add a house.
Enter 2 to sort listings by number of bedrooms.
Enter 3 to quit.
3

• Create a PT3 class with this main method. Test your code with lots of different inputs.
public static void main(String[] str) {
InputGetter iggy = new InputGetter();
iggy.mainMenu();
}

import java.util.ArrayList;

public class Listings {
private ArrayList<House> list = new ArrayList<House>();

public Listings() {
// if you change these lines for testing purposes, put them back before you submit the code
list.add(new House(4, 2000, 2.5f, 300000));
list.add(new House(3, 2500, 3f, 260000));
list.add(new House(2, 1300, 1.5f, 190000));
list.add(new House(4, 3000, 3.5f, 450000));
list.add(new House(3, 2100, 2.5f, 300000));
list.add(new House(3, 2300, 2f, 270000));
list.add(new House(3, 1800, 2f, 250000));
list.add(new House(4, 2600, 2.5f, 500000));
list.add(new House(2, 1600, 2f, 195000));
}

public ArrayList<House> getList() {
return list;
}

public void setList(ArrayList<House> list) {
this.list = list;
}

public void addHouse(House h){
list.add(h);
}

// add sortBedRms method here

}
import java.util.ArrayList;
import java.util.Scanner;


public class InputGetter {
private Scanner reader = new Scanner(System.in);
private Listings list = new Listings();


public void mainMenu() {
int input;
while(true){
System.out.println("What do you want to do?");
System.out.println("Enter 1 to add a house.");
System.out.println("Enter 2 to sort listings by number of bedrooms.");
System.out.println("Enter 3 to quit.");
input = reader.nextInt();
if(input == 1) addHouse();
else if(input == 2) sortByBedRms();
else if(input == 3) break;
else System.out.println("Bad choice.");
}
}

// put your addHouse method here

// put your sortbyBedRms method here
}
____________________________________________________________________________________________________________________________________________________________

Explanation / Answer

well this looks like it was the easy one to write: public void addHouse(){ System.out.println("How many Bedrooms?"); int bedrooms = reader.nextInt(); System.out.println("How many bathrooms?"); /// this needs to be a float since float bathrooms = reader.nextFloat(); System.out.println("How many square feet?"); int sqfeet = reader.nextInt(); System.out.println("What is the asking price?"); int price = reader.nextInt(); list.getList().add(new House(bedrooms, sqfeet, bathrooms, price)); } I think everything should be self-explainable except maybe the last statement, which tells the Listings object to return a reference to the arrayList that holds the houses. You then just add a new house. (Note: I haven't done any java in a few months, and I've been doing alot of C++ instead. one hiccup this method might have is that the arrayList returned by getList() is actually a copy of the arraylist contained within the Listings object, which in that case you'd have to pass it by pointer. I think Java does automatically pass it by pointer/reference, so I think this method works) for the second method you actually need to write two methods, one in the Listings class, and one in the InputGetter class. The one in the Listing class should take two parameters both ints, (int min, int max) 1.first in the method make a new array list: ArrayList result = new ArrayList(); 2.run a for loop that looks something like this: for(int i = min; i
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