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

//Chapter 12; Java Programming; Joyce Farraell; 8th Ed //output CHAPTER 12 AREMA

ID: 3675975 • Letter: #

Question

//Chapter 12; Java Programming; Joyce Farraell; 8th Ed

//output

CHAPTER 12 AREMA CHAPTER 12 Exception Handling Create an ApartmentException class whose constructor receives a String that holds a street address, an apartment number, a number of bedrooms, and a rent value for an apartment. Save the file as ApartmentException.java. Create an Apartment class with those fields. The Apartment constructor requires values for each field. Upon 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 ar application that establishes an array of at least six Apartment objects with valid and invalid values. Display an appropriate message when an Apartment object is created successfully and when one is not. Save the file as ThrowApartmentException.java. 6.

Explanation / Answer

1) Enter atleast you have to create 6 objects.So you have to give ateast 6 to make the program run.Otherwise(if you give<6) program will exit.

2) Only for valid entries program will create Obect successfully.Other wise throws Exception by displaying the reason message to you.

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);

   }
}

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

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) throws AppartmentException {
       // System.out.println("** Appartemt constructor called **");
       String s = new Integer(appartmentno).toString();
       int len = s.length();
       if (len < 3) {
           throw new AppartmentException(
                   ":: You have to enter 3 digit number.But you entered "
                           + len + " digit number as appartment number ::");
       }
       if (noofbedrooms < 1 || noofbedrooms > 4) {
           throw new AppartmentException(
                   ":: Didnt have required no of bed rooms ::" + noofbedrooms);
       }
       if (rent < 500 || rent > 2500) {
           throw new AppartmentException(
                   ":: Didnt meet valid the rent number ::" + rent + "$");
       }

       this.streetaddress = streetaddress;
       this.appartmentno = appartmentno;
       this.noofbedrooms = noofbedrooms;
       this.rent = rent;
       System.out.println("* APPARTMENT OBJECT CREATED SUCCESSFULY *");
   }

}

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

ThrowAppartmentExceprion.java  (This Class Is having Main Method.Run this class)

package org.students;

import java.util.Scanner;

import org.students.*;

public class ThrowAppartmentExceprion {
   public static void main(String args[]) {
       int count = 0;
       System.out
               .print("Enter how many no of appartments obects you want to create Atleast 6 is recommended Enter Zero(0) to exit:");

       Scanner sc = new Scanner(System.in);
       int noofobjects = sc.nextInt();
       while (true) {

           if (noofobjects >= 6) {
               if (count < noofobjects) {
                   System.out.println(" ");
                   System.out.print("Enter the area name:");

                   String areaname = sc.next();
                   System.out.print("Enter appartment number:");
                   int apptno = sc.nextInt();
                   System.out.print("Enter no of rooms:");
                   int noofrooms = sc.nextInt();
                   System.out.print("Enter the the rent:");
                   int rent = sc.nextInt();

                   try {
                       Appartment a1 = new Appartment(areaname, apptno,
                               noofrooms, rent);
                       count++;
                   } catch (AppartmentException e) {
                       System.out
                               .println(" ** APPARTMENT OBJECT CREATION FAILED **");
                   }

               }
               continue;
           } else {
               System.out
                       .println(":: Program Exit ,As you didnt entered minimum number of 6 to create Appartment Objects ::");
               System.exit(0);
           }

       }
   }

}