You are on page 1of 4

Methods, Objects and Classes

A. Create a class named ApartmentLease with fields that hold tenant’s information
such as apartment number, tenant’s name, monthly rental amount, and how
many months the apartment will be rented.

Inside this class, include a constructor that initializes the apartment number to 0,
the name of tenant to “XXX”, monthly rental amount into 2000, and the number of
months the apartment will be rented to 12.

Also include methods to get and set each of the fields. Include a nonstatic
method named addCarFee() that adds Php500 to the monthly rent value and
calls a static method named explainCarPolicy() that explains the car fee. Save
the class as ApartmentLease.java.

*note: When you run this program, nothing will be displayed. You don’t have a
main method here.

B. Create a class named TestLease whose main() method declares four Lease
objects. Call a getData() method three times. Within the method, prompt a
user for values for each field for an ApartmentLease, and return a
ApartmentLease object to the main() method where it is assigned to one of
main()’s ApartmentLease objects.

DO NOT prompt the user for values for the fourth ApartmentLease object, but let
it continue to hold the default values. Then, in main(), pass one of the
ApartmentLease objects to a showValues() method that displays the data. Then
call the addCarFee() method using the passed ApartmentLease object and
confirm that the fee explanation statement is displayed. Next, call the
showValues() method for the ApartmentLease object again and confirm that the
car fee has been added to the rent. Finally, call the showValues() method with
each of the other three objects; confirm that two hold the values you supplied as
input and one holds the constructor default values. Save the application as
TestLease.java.

*note: Save the two programs in one folder.


Expected output:
1. Review the programs and DO IT using your computer.
2. Screenshot your 2 programs and your sample outputs.
Program 1: ApartmentLease.java
Data Fields
• variables you declare within a class but outside of
any method.
• The data fields are not preceded by the keyword
static. If the keyword static had been inserted, only
one value for each data field would be shared by all
ApartmentLease objects that are eventually
instantiated.
• Because the data fields are not preceded by static,
when you eventually create, or instantiate, objects
from the class, each ApartmentLease can have its
own unique data fields.
• A class’s private data can be changed or
manipulated only by a class’s own methods and not
by methods that belong to other classes.
Constructors
• Any constructor you write must have the same
name as the class it constructs, and constructors
cannot have a return type—not even void

Mutator methods
• Methods that set or change field values
• Often called setters, and they conventionally start
with the prefix set.
• Using these three-letter prefixes with your method
names is not required, but it is conventional

Accessor methods
• Methods that retrieve values
• Often called getters, and they conventionally start
with the prefix get.
• Using these three-letter prefixes with your method
names is not required, but it is conventional

Additional notes:
• You cannot use a nonstatic method without an object.
• The keyword static is used for classwide methods, but
not for methods that “belong” to objects.

This method will


be performed if
addCarFee
method will be
called
Program 2: TestLease.java

3 objects of ApartmentLease are declared.


*no memory space is allocated yet.

Declaration and allocation of memory in the computer.

getData method is being called


Calling of addCarFee method that
can be found in ApartmentLease
class (program1)

Return type is
ApartmentLease

Declaration and allocation of memory in the computer.

ApartmentLease object named temp is returned


Sample output:

500 pesos were added to the rent due to


AddCarFee method (for lease1 only)

You might also like