Question

In: Computer Science

Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment...

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. Upon construction, throw an ApartmentException if any of the following occur: • The apartment number does not consist of 3 digits • The number of bedrooms is less than 1 or more than 4 • The rent is less than $500.00 or over $2500 Write a driver class that demonstrates creating valid Apartment Objects and Invalid Apartment Objects that throw the ApartmentException.

Solutions

Expert Solution

ApartmentException class:

public class ApartmentException extends Exception {
    public ApartmentException(String msg) {
        super(msg);
    }
}

Apartment class:

public class Apartment {

    private String streetAddress;
    private String apartmentNumber;
    private int numBedRooms;
    private double rentValue;

    public Apartment(String streetAddress, String apartmentNumber, int numBedRooms, double rentValue)
            throws ApartmentException {
        if ((apartmentNumber.length() != 3) || (numBedRooms < 1 || numBedRooms > 4)
                || (rentValue < 500 || rentValue > 2500)) {
            throw new ApartmentException(streetAddress + " " + apartmentNumber + " " + numBedRooms + " " + rentValue);
        }
        this.streetAddress = streetAddress;
        this.apartmentNumber = apartmentNumber;
        this.numBedRooms = numBedRooms;
        this.rentValue = rentValue;
    }
}

Main class:

public class Main {
    public static void main(String[] args) {
        // Create some valid and invalid Apartment objects
        try {
            Apartment a1 = new Apartment("Wall Street, NY", "112", 3, 2000);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
        try {
            Apartment a2 = new Apartment("Date Center, NY", "12", 4, 1000);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
        try {
            Apartment a3 = new Apartment("Downtown Road, NJ", "125", 5, 1500);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
        try {
            Apartment a4 = new Apartment("Oak City Road, TX", "412", 2, 5000);
        } catch (ApartmentException exp) {
            System.out.println(exp);
        }
    }
}

Output:

Kindly rate the answer and for any help just drop a comment


Related Solutions

Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s flavour and the number of scoops Create an IceCreamCone class with two fields - flavor and scoops The IceCreamCone constructor calls two data entry methods — getFlavor() and getScoops() The getScoops() method throws an IceCreamConeException when the scoop quantity exceeds 3 Write a program that establish three TceCreamCone objects and handles the Exception
Implement a class Address. An address has a house number, a street, an optional apartment number,...
Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code (all of these are strings). Create the getters and setters. Supply two constructors that accept all the address components except one includes an apartment number and one without (refer to the tester class below to see the proper order). Supply a print method that returns void and prints to the console the address with the street...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that...
Complete the definitions for the following prototypes in the Time class: Time(std::string) – a constructor that will take in a string with the format h:m:s and parse the h, m and s into separate int values that can be used to set the time of a new time object. int getSecond() const - return the value of the second void setSecond() - set the value of the second, making sure the new second value is valid before changing it. std::string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT