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 - 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...
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.
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.
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...
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...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT