0% found this document useful (0 votes)
9 views

Copy_of_Classes_-_5_Classes_w__Additional_Methods_1

The document outlines the creation of five Java classes: Rectangle, Cat, Monster, Car, and BankAccount, each with specific instance variables, constructors, accessor and mutator methods, and additional functionality. Each class includes a toString method for formatted output. The document provides detailed specifications for the methods and behaviors expected from each class.

Uploaded by

mw2rhkscw5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Copy_of_Classes_-_5_Classes_w__Additional_Methods_1

The document outlines the creation of five Java classes: Rectangle, Cat, Monster, Car, and BankAccount, each with specific instance variables, constructors, accessor and mutator methods, and additional functionality. Each class includes a toString method for formatted output. The document provides detailed specifications for the methods and behaviors expected from each class.

Uploaded by

mw2rhkscw5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Classes - 5 Classes w/ Additional Methods

Class 1 - Rectangle.java:

Create a file named Rectangle.java


Write the Rectangle class.
It should have 2 private instance variables:
● int length
● int width

Write a default constructor and another initializer constructor that takes 2 parameters:
● public Rectangle( )
● public Rectangle( int len, int wid )

Write the accessor and mutator methods:


● int getLength()
● int getWidth()
● void setDimensions(int len, int wid)

Write the following Rectangle ( accessor ) methods:


○ int getPerimeter()
■ Returns the perimeter of the rectangle
○ int getArea()
■ Returns the area of the rectangle
○ double getDiagonal()
■ Returns the diagonal of the rectangle (hint: use pythagorean theorem)

Write the toString method that returns a string that is formatted like this:
Length: 13
Width: 7

Computer Science II K
Classes - 5 Classes w/ Additional Methods

Class 2 - Cat.java:

Create a file named Cat.java


Write the Cat class.
It should have 2 private instance variables:
● int age
● String color

Write a default constructor and 2 other initializer constructors that take 2 parameters:
● public Cat( )
● public Cat( int a, String c )
● public Cat( String c, int a )

Write the accessor and mutator methods:


● int getAge()
● String getColor()
● int setAge( int a ), return the old age
● String setColor( String c ), return the old color

Write the following Cat ( accessor ) methods:


○ boolean isLucky( )
■ Returns true if the color of the cat is “black”, otherwise it returns false.
○ int livesLeft( )
■ If the cat is lucky, this method returns 9 if the cat’s age is less than 2, if it is 2 it will return 8, if it
is 3 it will return 7, 4 returns 6, 5 returns 5, 6 returns 4, 7 returns 3, 8 returns 2, 9 or more
returns 1 or if the cat is not lucky then it will return 1 always.

Write the toString method that returns a string that is formatted like this:
This is a brown cat that is 7 years old.

Computer Science II K
Classes - 5 Classes w/ Additional Methods

Class 3 - Monster.java:
Create a file named Monster.java
Write the Monster class.
It should have 3 instance variables:
● String name
● String type
● int age

Write a default constructor and 3 more initializer constructors with parameters:


● public Monster( )
● public Monster( String n, String t, int a )
● public Monster( String n )
● public Monster( String n , String t )

Write the accessor and mutator methods:


● String getName(), String getType(), int getAge()
● void setName( String n ), int setAge(int a)

Write the following Monster ( accessor ) methods:


○ int scaryScale( )
■ This method will return a value between 0 and 100.
If the age is below 13 it will return a random number between 90 and 100.
If the age is greater than 12 and less than 51, return a random number between 75 and 100.
If the age is over 50 and less than 100 and the last letter of the name comes before the letter
“M” in the alphabet, it will return a random value between 25 and 75 .
If the age is over 100 and the first letter of the name comes after the letter “M” in the alphabet,
it will return a random number between 50 and 100.
If the age is over 212 then it will return 100.
At worst, return 0 as Russell says you are not scary.
(Note: name may be uppercase or lowercase, and you have to use the compareTo method)
○ Monter scariest( Monster m )
■ This method will return the scariest monster between this Monster and the Monster given.
The scarier monster is the monster that returns the highest number when scaryScale is called.

Write the toString method that returns a string that is formatted like this:
My name is Lenny, I am a vampire and am 212 years old.

Computer Science II K
Classes - 5 Classes w/ Additional Methods

Class 4 - Car.java:
Create a file named Car.java
Write the Car class.
It should have 4 instance variables:
● String make
● String model
● int year
● double cost

Write an initializer constructor with 4 parameters:


● public Car( String mk, String mo, int yr, double c )

Write the accessor for all instance variables and a mutator method for only the cost.

Write the following Car ( accessor ) methods:


○ boolean isClassic( )
■ This method will return true if the car is over 20 years old.
○ double sellingPrice( int mileage )
■ This method will return the new cost of the vehicle depending on the mileage and the year. If
the car has been driven an average of 6,000 miles per year then the selling price will be 80% of
the original cost, anything over 6,000 miles per year deducts 5% off the selling price per 1,000
mile increase in the average miles per year. If it is a classic car, then it will increase the selling
price of the vehicle by 10%, for each year over 20 years old regardless of the mileage.
○ boolean sell( double offer, int mileage )
■ This method will return true if the offer is no lower than 10% below the selling price of the car.

Write the toString method that returns a string that is formatted like this:
2014 Ford Mustang - $26450.85

Computer Science II K
Classes - 5 Classes w/ Additional Methods

Class 5 - BankAccount.java:
Create a file named BankAccount.java
Write the BankAccount class.
It should have 3 instance variables:
● final int ACCOUNT_NUMBER ← final variables can only be assigned one time.
● int pin
● double balance

Write an initializer constructor that takes a 4 digit PIN, assigns a random account number that is
between 100000 - 999999 and starts off with $0.

Write the accessor methods to get the balance and the account number.
● double getBalance( )
● int getAccountNumber( ) ← This should only return the last 3 digits of the account number, because of privacy concerns!!

Write the BankAccount ( mutator ) methods:


○ void changePIN( int old_pin, int new_pin )
■ This method will check if the old_pin is the correct pin, then change the pin to the new_pin.
○ void deposit( double amount )
■ This method will add an amount to the balance. If the amount is negative then it will do nothing!
○ double withdraw( int piNum, double amount )
■ This method will check if the pin is correct and then subtract the amount from the balance and
return the amount withdrawn. The balance cannot be negative. So if the amount is larger than
the balance return the balance and make the balance 0. If the pin is not correct it will not allow
you to withdraw any money.
○ void transfer( BankAccount account, int piNum, double amount )
■ This method will transfer the amount of money to the given bank account from this bank
account. (Hint: must use deposit and withdraw methods.)

Write the toString method that returns a string that is formatted like this:
Account #: 123456 (Balance: $212.13)

Computer Science II K

You might also like