Question

In: Computer Science

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 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

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


Related Solutions

I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
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...
In this create a code that will drop a student by ID number //////////////////////////////////////////////////////////////////////// import java.util.Scanner;...
In this create a code that will drop a student by ID number //////////////////////////////////////////////////////////////////////// import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String [] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public void...
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()...
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...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public class CourseeCOM616 {        private String courseName;     private String[] studentsName = new String[1];     private String studentId;        private int numberOfStudents;        public CourseeCOM616(String courseName) {         this.courseName = courseName;     }     public String[] getStudentsName() {         return studentsName;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getStudentId() {         return studentId;    ...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT