Question

In: Computer Science

I need the output of the code like this in java First we create a new...

I need the output of the code like this in java

First we create a new building
and display the result:

This building has no apartments.

Press enter to continue.........................

Now we add some apartments to the building
and display the result:

This building has the following apartments:

Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable

Unit 2 2 Bedroom
Rent $400 per month
Currently available

Unit 3 4 Bedroom
Rent $1000 per month
Currently unavailable

Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable

Unit 5 5 Bedroom
Rent $10000 per month
Currently available

Press enter to continue.........................

Now we try to add another apartment to the building
Cannot add another apartment the building is full.

Now we create a new building and add some apartments
then we collect the rent from all the occupied apartments
The total rent collected was: $2500

Finally we use the getUnits method to get a copy of the 3rd unit
from the building and change its rent to $10then we display
the building again to show that the apartment has not changed.

This building has the following apartments:

Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable

Unit 2 2 Bedroom
Rent $400 per month
Currently available

Unit 3 4 Bedroom
Rent $1000 per month
Currently unavailable

Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable

Unit 5 5 Bedroom
Rent $10000 per month
Currently available

Unit 6 3 Bedroom
Rent $550 per month
Currently unavailable

Press enter to end the program...................

and now I have to make a new class Building.java to show output like this given above and given below is the main java file

package lab06;

import properties.Building;
import properties.Apartment;
import util1228.Utilities;

//Tests the class Building

public class Lab06Driver {
    public static void main(String[] args) {
        
        //testing constructor and toString
        System.out.println("\nFirst we create a new building");
        Building b=new Building(5);
        System.out.println("and display the result:\n");
        System.out.printf("%s\n\n", b);
        Utilities.pause("Press enter to continue.........................");

        //testing addUnit
        System.out.println("\nNow we add some apartments to the building");
        b.addUnit(new Apartment(1,3,450,true));
        b.addUnit(new Apartment(2,2,400,false));
        b.addUnit(new Apartment(3,4,1000,true));
        b.addUnit(new Apartment(4,1,500,true));
        b.addUnit(new Apartment(5,5,10000,false));     
        System.out.println("and display the result:\n");
        System.out.printf("%s\n", b);
        Utilities.pause("Press enter to continue.........................");

        System.out.println("\nNow we try to add another "
                + "apartment to the building");
        b.addUnit(new Apartment(6,3,550,true));
        
        
        
        //testing collectRent
        System.out.println("\nNow we create a new building "
                + "and add some apartments");
        b=new Building(10);
        b.addUnit(new Apartment(1,3,450,true));
        b.addUnit(new Apartment(2,2,400,false));
        b.addUnit(new Apartment(3,4,1000,true));
        b.addUnit(new Apartment(4,1,500,true));
        b.addUnit(new Apartment(5,5,10000,false)); 
        b.addUnit(new Apartment(6,3,550,true));
        System.out.println("then we collect the rent from "
                + "all the occupied apartments");
        System.out.println("The total rent collected was: $"+b.collectRent());
        
        //testing getUnits
        System.out.print("\nFinally we use the getUnits method to get a copy "
                + "of the 3rd unit \nfrom the building and change its rent to "
                + "$10");
        b.getUnits()[2].setRent(10);
        System.out.println("then we display \nthe building again to show that "
                + "the apartment has not changed.\n");
        System.out.printf("%s\n", b);
        Utilities.pause("Press enter to end the program...................");
    }
}

Solutions

Expert Solution

Lab06Driver .java

----------------------------

package com.faisal.com.laraib;

public class Lab06Driver {
public static void main(String[] args) {
  
//testing constructor and toString
System.out.println("\nFirst we create a new building");
Building b=new Building(5);
System.out.println("and display the result:\n");
// System.out.printf("%s\n\n", b.displayApartment());
b.displayApartment();
Utilities.pause("Press enter to continue.........................");

//testing addUnit
System.out.println("\nNow we add some apartments to the building");
b.addUnit(new Apartment(1,3,450,true));
b.addUnit(new Apartment(2,2,400,false));
b.addUnit(new Apartment(3,4,1000,true));
b.addUnit(new Apartment(4,1,500,true));
b.addUnit(new Apartment(5,5,10000,false));   
System.out.println("and display the result:\n");
// System.out.printf("%s\n", b);
b.displayApartment();
Utilities.pause("Press enter to continue.........................");

System.out.println("\nNow we try to add another "
+ "apartment to the building");
b.addUnit(new Apartment(6,3,550,true));
  
  
  
//testing collectRent
System.out.println("\nNow we create a new building "
+ "and add some apartments");
b=new Building(10);
b.addUnit(new Apartment(1,3,450,true));
b.addUnit(new Apartment(2,2,400,false));
b.addUnit(new Apartment(3,4,1000,true));
b.addUnit(new Apartment(4,1,500,true));
b.addUnit(new Apartment(5,5,10000,false));
b.addUnit(new Apartment(6,3,550,true));
System.out.println("then we collect the rent from "
+ "all the occupied apartments");
System.out.println("The total rent collected was: $"+b.collectRent());
// b.displayApartment();
  
//testing getUnits
System.out.print("\nFinally we use the getUnits method to get a copy "
+ "of the 3rd unit \nfrom the building and change its rent to "
+ "$10");
b.getUnits(3,10);
System.out.println("then we display \nthe building again to show that "
+ "the apartment has not changed.\n");
// System.out.printf("%s\n", b);
b.displayApartment();
Utilities.pause("Press enter to end the program...................");
}
}
-------------------------------

Building.java

-----------------------------

package com.faisal.com.laraib;

import java.util.ArrayList;

public class Building {
ArrayList<Apartment> apartmentsList=null;
int numberApartment;
  
  
   public Building(int n) {
       numberApartment=n;
       apartmentsList=new ArrayList<Apartment>(n);
   }
   public void addUnit(Apartment apartment) {
  
       if(numberApartment>=apartmentsList.size()+1) {
           apartmentsList.add(apartment);
       }
       else {
           System.out.println("Cannot add another apartment the building is full.");
       }
   }
   public int collectRent() {
       int totalRent=0;
       for(Apartment apartment:apartmentsList) {
           if(apartment.isAvailabel()) {
               totalRent=totalRent+apartment.getRent();
           }
       }
       return totalRent;
   }
  
   public void getUnits(int unit, int newRent) {
      
       for(Apartment apartment:apartmentsList) {
           if(unit==apartment.getFloorNumber()) {
               apartment.setRent(newRent);
           }
       }
   }
  
   public void displayApartment() {
       if(apartmentsList.size()==0) {
           System.out.println("This building has no apartments.");
       }else {
           for(Apartment apartment:apartmentsList) {
              
               System.out.println("Unit "+apartment.getFloorNumber()+" "+apartment.getFlatNumber()+" Bedroom");
               System.out.println("Rent $"+apartment.getRent()+" per month");
               if(apartment.isAvailabel()) {
                   System.out.println("Currently unavailable");
               }
               else {
                   System.out.println("Currently available");
               }
               System.out.println();
           }
      
       }
      
   }
   public int getNumberApartment() {
       return numberApartment;
   }
   public void setNumberApartment(int numberApartment) {
       this.numberApartment = numberApartment;
   }
  
}
------------------------

Apartment.java

package com.faisal.com.laraib;

public class Apartment {
  
   int floorNumber;
   int flatNumber;
   int rent;
   boolean availabel;
   public Apartment(int floorNumber, int flatNumber, int rent, boolean availabel) {
       super();
       this.floorNumber = floorNumber;
       this.flatNumber = flatNumber;
       this.rent = rent;
       this.availabel = availabel;
   }
   public int getFloorNumber() {
       return floorNumber;
   }
   public void setFloorNumber(int floorNumber) {
       this.floorNumber = floorNumber;
   }
   public int getFlatNumber() {
       return flatNumber;
   }
   public void setFlatNumber(int flatNumber) {
       this.flatNumber = flatNumber;
   }
   public int getRent() {
       return rent;
   }
   public void setRent(int rent) {
       this.rent = rent;
   }
   public boolean isAvailabel() {
       return availabel;
   }
   public void setAvailabel(boolean availabel) {
       this.availabel = availabel;
   }
  
  
  

}
---------------------------

Utilities.java

-------------------------

package com.faisal.com.laraib;

public class Utilities {

   public static void pause(String msg) {
       System.out.println(msg);
      
   }

}
------------------------------

SAMPLE OUTPUT:


First we create a new building
and display the result:

This building has no apartments.
Press enter to continue.........................

Now we add some apartments to the building
and display the result:

Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable

Unit 2 2 Bedroom
Rent $400 per month
Currently available

Unit 3 4 Bedroom
Rent $1000 per month
Currently unavailable

Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable

Unit 5 5 Bedroom
Rent $10000 per month
Currently available

Press enter to continue.........................

Now we try to add another apartment to the building
Cannot add another apartment the building is full.

Now we create a new building and add some apartments
then we collect the rent from all the occupied apartments
The total rent collected was: $2500

Finally we use the getUnits method to get a copy of the 3rd unit
from the building and change its rent to $10then we display
the building again to show that the apartment has not changed.

Unit 1 3 Bedroom
Rent $450 per month
Currently unavailable

Unit 2 2 Bedroom
Rent $400 per month
Currently available

Unit 3 4 Bedroom
Rent $10 per month
Currently unavailable

Unit 4 1 Bedroom
Rent $500 per month
Currently unavailable

Unit 5 5 Bedroom
Rent $10000 per month
Currently available

Unit 6 3 Bedroom
Rent $550 per month
Currently unavailable

Press enter to end the program...................
-----------------------------------------------------------------

SUMMARY:

I have provided the solution as per your requirement, i hope you're satisfied with the way i have approached. please dont hesitate to give me a Like if you like it, i appreciate your like. If you have any queries you can shoot them any time in comments section. I will be glad to help you.
Thank you :)


Related Solutions

Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
I need to create a code in C++ that first has a menu animation of game...
I need to create a code in C++ that first has a menu animation of game Pacman, a score label in the map, and a bar that have the lives of pacman in the map.
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to...
Create a new Java program named AllAboutMe (For JAVA we use blue J) Write code to have the program print your name, favorite color, and three hobbies to a new text file called “AllAboutMe” using PrintStream. Submit code.
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an error message if the input cannot be converted I can give an thumbs up! :)
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT