Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//Chapter 12; Java programming; joyce farrell; 8th Ed // Exception Handling //Go

ID: 3679201 • Letter: #

Question

//Chapter 12; Java programming; joyce farrell; 8th Ed

//Exception Handling

//Got answer from experter but i dont like "how many create apartment object"

//non digit and digit for apartment number; street address; number of room; price

#6. Create an ApartmentException class whose constructor receives a String that holds a street address, an apartment number, a number of bedroom, and a rent value for an apartment. Save the file as ApartmentException.java.

Create an Apartment class construction, throw an ApartmentException if the apartment number does not consist of three digits, If the number of bedrooms is less than 1 or more than 4, or if the rent is less than $500 or over $2,500. Save the class as Apartment.java.

Write an applicaiton that establishes any array of at least six Apartment objects with valid and invalid values. Display an appropriate message when an Apartment object is created succesfully and when one is not. Save the file as ThrowApartmentException.java

Explanation / Answer

Appartment.java

package org.students;
import org.students.*;
public class Appartment
{
   String streetaddress;
   int appartmentno;
   int noofbedrooms;
   int rent;
   public Appartment(String streetaddress, int appartmentno, int noofbedrooms,
           int rent) {
   //System.out.println("** Appartemt constructor called **");
   String s = new Integer(appartmentno).toString();
   int len = s.length();
   if(len<3)
   {
       try {
           throw new AppartmentException(":: You have to enter 3 digit number.But you entered "+len+" digit number as appartment number ::");
       } catch (AppartmentException e) {
           System.out.println("* APPARTMENT OBJECT FAILED TO CREATE *");
           System.out.println("---------------------------------------------");
       }
   }
   else if(noofbedrooms<1 || noofbedrooms>4)
   {
       try {
           throw new AppartmentException(":: Didnt have required no of bed rooms ::"+noofbedrooms);
       } catch (AppartmentException e) {
           System.out.println("* APPARTMENT OBJECT FAILED TO CREATE *");
           System.out.println("---------------------------------------------");
       }
   }
   else if(rent<500 || rent>2500)
   {
       try {
           throw new AppartmentException(":: Didnt meet valid the rent number ::"+rent+"$");
       } catch (AppartmentException e) {
           System.out.println("* APPARTMENT OBJECT FAILED TO CREATE *");
           System.out.println("---------------------------------------------");
       }
   }
   else{
       this.streetaddress = streetaddress;
           this.appartmentno = appartmentno;
           this.noofbedrooms = noofbedrooms;
           this.rent = rent;
           System.out.println("* APPARTMENT OBJECT CREATED SUCCESSFULY *");
           System.out.println("---------------------------------------------");
   }

   }
  

}

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

AppartmentException.java

package org.students;

public class AppartmentException extends Throwable {
   public AppartmentException(String streetaddress,int appartmentno,int noofbedrooms,int rent)
   {
       System.out.println("** AppartmentException constructor called **");
   }
   public AppartmentException(String message)
   {
       System.out.println(message);
       //System.out.println("== AppartmentException constructor ==");
   }
}

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

ThrowsAppartmentException.java

package org.students;

public class ThrowsAppartmentException
{

   public static void main(String[] args) throws AppartmentException
   {

   Appartment appartment[]=
       {
           new Appartment("mount Street",123,3,2500),
           new Appartment("Carmel Street",972,2,300),
           new Appartment("Margaret Street",456,1,970),
   new Appartment("Temple Street",34,1,1100),
   new Appartment("Palace Street",768,3,3000),
   new Appartment("Church Road",491,1,300),
   new Appartment("Richmond Street",4,2,1300)
           };
  
  
   }

}

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