Question

In: Computer Science

JAVA programming- please answer all prompts as apart of the same java project Enums enumeration values()...

JAVA programming- please answer all prompts as apart of the same java project

Enums

  • enumeration
  • values() and other utilities

Part A

Create an enumeration AptType representing several different types of apartment. Each type of apartment has a description, a size (square footage), and a rent amount. In the enumeration, provide public final instance variables for these. Also write a parameterized constructor that sets the values of these variables (doing this is just part of seeing up the enum) The apartment types are

description size (sq ft) rent ($/mo)
studio "Studio" 520 480
one bedroom "1 Bedroom" 810 720
two bedroom "2 Bedroom" 1120 1030
luxury "Luxury" 1720 1780

Create the possible values for the enumeration, one for each type of apartment, in this order. Give these brief all-caps names.

Part B

☑ Create a class Apartment. Each Apartment has an instance variable to store its apartment type (use your enumeration).   Also add a boolean indicating whether it is rented or not. Apartments should start off unrented.

Add a private static final variable building in Apartment, which is a 2-dimensional array of Apartments.

☑ Add a constant FLOORS, which is 10, and another APTSPERFLOOR, which is the number of types of apartment in the enum.   We only need one of each of these for the whole class, and they will be availble to other classes

☑ In the static block, set up building to be FLOORS x APTSPERFLOOR apartments in size, and fill in all the apartments so that there is one apartment with each type on each floor, in the same order as the types were defined. To do this, use the .values() method of the enumeration so you can get the appropriate type of apartment for the position on the floor without having to use a conditional.

☑ Add a static method rent(floor, num) which, if the apartment at that position in building is currently free, sets it as rented and (using the values from AptType) prints a message like

You are renting a 1120 square foot 2 Bedroom apartment for $1030 / month.  Welcome to our building.

If the apartment is already rented, print an apologetic message saying the apartment is taken.

If the number requested is not valid in building (e.g., 15th floor) print an error message.

Part C

☑ In a class with a main,check that you can choose an apartment of each type from the building and rent it, but if you try to rent it again, you get an apology. Do this without ever creating a new Apartment in main.

Solutions

Expert Solution

Java code to implement the above functionality is :

AptType.java


public enum AptType {
        STUDIO("Studio",520,480),ONE_BEDROOM("1 Bedroom",810,720),TWO_BEDROOM("2 Bedroom",1120,1030),LUXURY("Luxury",1720,1780);
        
         public final String description;
         public final int size;
         public final int rent;
        
         AptType(String description,int size,int rent) {
                this.description=description;
                this.size=size;
                this.rent=rent;         
        }
}
        


Apartment.java


public class Apartment {
   
        public AptType apartmentType;
        boolean isRented=false;
    private static final Apartment building[][];
    public static final int FLOORS = 10;
    public static final int  APTSPERFLOOR = AptType.values().length;
    
    static{
        building= new Apartment[FLOORS][APTSPERFLOOR];
        final AptType aptType[] = AptType.values();
        for(int i=0;i<FLOORS;i++){                   
                for(int j=0;j<APTSPERFLOOR;j++){
                        building[i][j]= new Apartment();     //creating new Apartment           
                        building[i][j].apartmentType=aptType[j]; //setting the apartment type
                }
        }
    }
    
    static void rent(int floor, int num){
       if(floor>=0 && floor<=9 && num>=0 && num <=3)        { //checking if aprtment number is valid or not
           Apartment a = building[floor][num];
           if(a.isRented==false){ //if apartment is available to rent
                   building[floor][num].isRented=true; //aprtment is rented
                   System.out.println("You are renting a "+a.apartmentType.size+" square foot "+a.apartmentType.description+" for $"+a.apartmentType.rent+"/ month.  Welcome to our building");
           }else{
                   System.out.println("Sorry,that apartement is already taken");
           }
       }else{
           System.out.println("You have selected invalid apartment");
       }
    }
    
    public static void main(String[] args){
        Apartment.rent(9, 3);
        Apartment.rent(4, 1);
        Apartment.rent(2, 2);
        Apartment.rent(6, 4);
        Apartment.rent(4, 1);
        Apartment.rent(12, 3);
    }
}

The output screenshot of the above program is :

If you have any queries regarding this answer, please reach out through the comment section.


Related Solutions

JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A...
JAVA programming Classwork- please answer all prompts as apart of one java programming project Part A Add to your project this class Position, which has x and y coordinates. Create an abstract class GameElt, which has a String name, an int health (keep it in the range 0 to 100) and a Position pos. For GameElt, include a default constructor that starts pos at (0, 0), and a parameterized constructor that takes x and y coordinates and a name. health...
JAVA code - please answer all prompts as apart of the same java program with Polymorphism...
JAVA code - please answer all prompts as apart of the same java program with Polymorphism Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT