In: Computer Science
JAVA ONLY - Complete the code
import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String output; double mostExpensive; do{ System.out.print("Enter the number of houses --> "); count = scan.nextInt(); }while(count < 1); /* 1. * create an array of HouseListing objects using the variable that holds * the user's response to the prompt above */ // for loop loads the array with HouseListing's constructor for (int i = 0; i < list.length; i++) { System.out.println("\n***HOUSE " + (i+1) + "***"); // Prompt for house listing # System.out.print("Enter house listing " + "number (Alphanumeric) #"+ (i+1) +": "); listNumber = scan.next(); // Clear buffer scan.nextLine(); // Prompt for house description System.out.print("Enter description for " + "house #" +(i+1) + ": "); listDesc = scan.nextLine(); // Prompt for house price System.out.print("Enter listing price for " + "house #" +(i+1) + ": "); listPrice = scan.nextDouble(); /* 2. * create a HouseListing object using the input values and store/load * the object in the array */ } /* 3. * Assign the 0th element of the array to the most expensive house think ... you can't assign an object to a price but the object has a method that may help you here ... */ output = "Listings:\n"; /* * loop below builds the output string */ for (int i = 0; i < list.length; i++) { output += list[i] + "\n"; /* 4. Using a control structure, find and the store the double varaible which holds the most expensive house (We don't need to listing #) JUST THE PRICE */ } // output System.out.println(output); System.out.println("The most expensive house on the market costs: $" + mostExpensive); } }
HI,
Below are classes..i completed the all 4 points which you asekd to do.
HouseListing.java
public class HouseListing
{
private int num;
private String listNumber;
private String listDesc ;
private double listPrice;
// constructor
public HouseListing(int num, String listNumber, String listDesc, double listPrice){
//System.out.println("arrgument constructor");
this.num = num;
this.listNumber = listNumber;
this.listDesc = listDesc;
this.listPrice = listPrice;
}
// Accessors
public int getNum()
{
return num;
}
public String getListNumber()
{
return listNumber;
}
public String getListDesc()
{
return listDesc;
}
public double getListPrice()
{
return listPrice;
}
// If not given something to compare by, compares by title.
// NOTE: The natural ordering given by this function is inconsistent with equals
// public double compareTo(HouseListing other) {
// double compareprice =((HouseListing)other).getListPrice();
// return this.listPrice-compareprice;
/// }
@Override
public String toString()
{
return listNumber + "\t" + listDesc + "\t"+listPrice ;
}
}
HouseListingDemo.java
import java.util.Scanner;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;
public class HouseListingDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
HouseListing[] list;
String listNumber, listDesc;
int count = 0;
double listPrice;
String output;
double mostExpensive=0;
do{
System.out.print("Enter the number of houses --> ");
count = scan.nextInt();
}while(count < 1);
/* 1.
* create an array of HouseListing objects using the variable that holds
* the user's response to the prompt above
*/
list = new HouseListing[count];
// for loop loads the array with HouseListing's constructor
for (int i = 0; i < list.length; i++)
{
System.out.println("\n***HOUSE " + (i+1) + "***");
// Prompt for house listing #
System.out.print("Enter house listing " +
"number (Alphanumeric) #"+ (i+1) +": ");
listNumber = scan.next();
// Clear buffer
scan.nextLine();
// Prompt for house description
System.out.print("Enter description for " +
"house #" +(i+1) + ": ");
listDesc = scan.nextLine();
// Prompt for house price
System.out.print("Enter listing price for "
+ "house #" +(i+1) + ": ");
listPrice = scan.nextDouble();
/* 2.
* create a HouseListing object using the input values and store/load
* the object in the array
*/
HouseListing hl = new HouseListing((i+1), listNumber, listDesc, listPrice);
list[i] = hl;
}
/* 3.
* Assign the 0th element of the array to the most expensive house
think ... you can't assign an object to a price but the object
has a method that may help you here ...
*/
//converted array in list to us the sort method with comparerator to get price in descending order
List<HouseListing> arr_list = Arrays.asList(list);
Collections.sort(arr_list, new Comparator<HouseListing>(){
@Override
public int compare(HouseListing h1, HouseListing h2) {
/* if (h1.getListPrice().compareTo(h2.getListPrice()) < 0) //this comes 1st
return -1;
else if (h1.getListPrice().compareTo(h2.getListPrice()) > 0) //temp comes 1st
return 1;
else //if both are equal
return 0; */
if (h2.getListPrice() < h1.getListPrice())
return -1;
else if(h2.getListPrice() > h1.getListPrice())
return 1;
else
return 0;
}
});
output = "Listings:\n";
/*
* loop below builds the output string
*/
for (int i = 0; i < list.length; i++) {
output += list[i] + "\n";
/* 4.
Using a control structure, find and the store the double varaible
which holds the most expensive house (We don't need to listing #)
JUST THE PRICE
*/
}
// after sorting we will get most expensive house always at index 0 of array list.
mostExpensive = ((HouseListing)list[0]).getListPrice();
System.out.println(output);
System.out.println("The most expensive house on the market costs: $"
+ mostExpensive);
}
}
Thanks