Alex

Alex

  • NA
  • 55
  • 16.4k

java...i don't know boolean type..Pls need some help

Aug 11 2013 7:45 AM

Take the Unit class from a previous exercise and add another instance variable called rented (type boolean) that stores whether the unit is rented or not. Ensure that the setters do not allow any illegal values (such as a negative value for numberOfRooms). If an illegal value is provided then the setter does nothing. Keep your original 4-arg constructor (use DEFAULT_RENTED as the final parameter in the call to the all-arg) and create an all-arg constructor to include the instance variable rented.

Also add the following method that uses a nested-if to return a String for the unit classification.

/**

* Determines the classification of the unit according to size

* (LARGE or SMALL) and quality (HIGH or LOW)

*    LARGE/HIGH -    more than 5 rooms and a rent of 300 or more

*    LARGE/LOW -     more than 5 rooms but a rent lower than 300

*    SMALL/HIGH -    3 rooms or less and a rent of 200 or more

*    SMALL/LOW -     3 rooms or less but a rent lower than 200

*    STANDARD -      all other cases

*

* @return one of the classifications listed above

*/

      public String calcUnitClassification(){


public class Unit {
   
    //constants
     public static final String DEFAULT_UNIT_NUMBER = "XXX";
     public static final int DEFAULT_NUMBER_0F_ROOMS = 0;
     public static final double DEFAULT_RENT_AMOUNT = 0;
     public static final double DEFAULT_MAINTENANCE_COST= 0;
   
     //instance variables
    private String unitNumber;
    private int numberOfRooms;
    private double rentAmount;
    private double maintenanceCost;
   
    public Unit(String unitNumber,int numberOfRooms,double rentAmount,double maintenanceCost){
   
     this.unitNumber = unitNumber;
     this.numberOfRooms = numberOfRooms;
     this.rentAmount = rentAmount;
     this.maintenanceCost = maintenanceCost;
    
    }
    public Unit(){
   this(DEFAULT_UNIT_NUMBER,DEFAULT_NUMBER_0F_ROOMS,DEFAULT_RENT_AMOUNT,DEFAULT_MAINTENANCE_COST);
      }
   
    public String getUnitNumber() {
        return unitNumber;
    }

    public void setUnitNumber(String unitNumber) {
        this.unitNumber = unitNumber;
    }

    public int getNumberOfRooms() {
        return numberOfRooms;
    }

    public void setNumberOfRooms(int numberOfRooms) {
        this.numberOfRooms = numberOfRooms;
    }

    public double getRentAmount() {
        return rentAmount;
    }

    public void setRentAmount(double rentAmount) {
        this.rentAmount = rentAmount;
    }

    public double getMaintenanceCost() {
        return maintenanceCost;
    }

    public void setMaintenanceCost(double maintenanceCost) {
        this.maintenanceCost = maintenanceCost;
    }
    public String toString(){
        return super.toString()+
                 "[" +
                 "\nNumber of Unit : " + this.unitNumber +
                 "\nRent Amount : " + this.rentAmount+
                   "\nNumber of Rooms:  " + this.numberOfRooms +
                   "\nMaintenance Cost : " + this.maintenanceCost+
                     "]";
    }

}



public class UnitTest {

   
    public static void main(String[] args) {
        System.out.println("no-arg constructor ");
        Unit townhouse = new Unit();
        System.out.println("Trees "+townhouse); //uses the toString
               
        townhouse.setUnitNumber("A65");
        townhouse.setNumberOfRooms(3);
        townhouse.setRentAmount(310);
        townhouse.setMaintenanceCost(56);
               
                System.out.println("Unit number: " +  townhouse.getUnitNumber());
                System.out.println("Number of rooms: " +  townhouse.getNumberOfRooms());
                System.out.println("Cost of Rent: " +  townhouse.getRentAmount());
                System.out.println("Maintenance Cost " +  townhouse.getMaintenanceCost());
             

                System.out.println("all-arg constructor roses");
               Unit appartment = new Unit("7A",3,410,23);
                System.out.println("Appartment " + appartment);//uses toString
              
   



    }

}

Answers (2)