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...
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...
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...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT