In: Computer Science
JAVA - Complete the directions in the 5 numbered comments 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); } }
CODE :
All the tasks are done .
NOTE:
Explanation is done in the comments for better visualisation we
have modified the output +=list[i] + "\n"; as This only prints the
object address not the contents because here list[i] is an array
object to invoke the object values to be able to see it.
If you want to see the contents of the objects we have to use as
done in the modified line only. If you just want to print the
address then use as output +=list[i] + "\n"; otherwise go with the
modified line as described in the comment section. Output is also
attached please check it.
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
*
*/
class HouseListing{
String listNumber;
String listDesc;
double listPrice;
HouseListing(){
this.listNumber="";
this.listDesc="";
this.listPrice=0;
}
HouseListing(String listNumber,String listDesc,double
listPrice){
this.listNumber=listNumber;
this.listDesc=listDesc;
this.listPrice=listPrice;
}
}
class Main extends HouseListing {
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
*/
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 house = new
HouseListing(listNumber,listDesc,listPrice);
list[i]=house;
}
/* 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 ...
*/
mostExpensive=list[0].listPrice;
output = "Listings:\n";
/*
* loop below builds the output string
*/
for (int i = 0; i < list.length; i++) {
//output +=list[i] + "\n"; // [ This only prints the object address
not the contents
// because here list[i] is an array object to
// invoke the object values to be able to see it.
// this line is modified for better output visualisation.
// because the above line only prints the output
// if you dont want to print the output completely and only
address
// comment the modified line. This is done only for your better
understanding.
output += "->" + list[i].listNumber +","+ list[i].listDesc +","
+ list[i].listPrice + "\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
*/
if(mostExpensive < list[i].listPrice){
mostExpensive=list[i].listPrice;
}
}
// output
System.out.println(output);
System.out.println("The most expensive house on the market costs:
$"
+ mostExpensive);
}
}
OUTPUT:
With modified line as described in the explanation .