Question

In: Computer Science

JAVA - Complete the directions in the 5 numbered comments import java.util.Scanner; /** * This program...

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);
    }
}

Solutions

Expert Solution

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 .


Related Solutions

JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
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...
complete this code for me, this is java question. // Some comments omitted for brevity import...
complete this code for me, this is java question. // Some comments omitted for brevity import java.util.*; /* * A class to demonstrate an ArrayList of Player objects */ public class ListOfPlayers { // an ArrayList of Player objects private ArrayList players; public ListOfPlayers() { // creates an empty ArrayList of Players players = new ArrayList<>(); } public void add3Players() { // adds 3 Player objects players.add(new Player("David", 10)); players.add(new Player("Susan", 5)); players.add(new Player("Jack", 25)); }    public void displayPlayers()...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT