In: Computer Science
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy() that explains the pet fee as folows: A pet fee of $10 is added to the monthly rent.
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 a Lease, and return a Lease object to the main() method where it is assigned to one of main()’s Lease objects. Do not prompt the user for values for the fourth Lease object, but let it continue to hold the default values. Then, in main(), pass one of the Lease objects to the showValues() method that displays the data. Then call the addPetFee() method using the passed Lease object and confirm that the fee explanation statement is displayed. Next, call the showValues() method for the Lease object again and confirm that the pet 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.
EDIT: This is for Java.
The program is given below: that contain class Lease with fields apartment_name, apartment_no, rent_amount, term_lease_month, default constructor, get set method for each field, addPetFee() method, explainPetPolicy() method. The program is tested by creating class TestLease.
import java.util.*;
class Lease{
String apartment_name;
int apartment_no;
double rent_amount,term_lease_month;
//default constructor
public Lease()
{
apartment_name="XXX";
apartment_no=0;
rent_amount=1000;
term_lease_month=12;
}
//get_apartment_name() method return apartment_name
public String get_apartment_name()
{
return apartment_name;
}
//get_apartment_no() method return apartment_no
public int get_apartment_no()
{
return apartment_no;
}
//get_rent_amount() method return rent_amount
public double get_rent_amount()
{
return rent_amount;
}
//get_term_lease_month() method return term_lease_month
public double get_term_lease_month()
{
return term_lease_month;
}
//set_apartment_name() method set apartment_name
public void set_apartment_name(String apartment_name)
{
this.apartment_name=apartment_name;
}
//set_apartment_no() method set apartment_no
public void set_apartment_no(int apartment_no)
{
this.apartment_no=apartment_no;
}
//set_rent_amount() method set rent_amount
public void set_rent_amount(double rent_amount)
{
this.rent_amount=rent_amount;
}
//set_term_lease_month() method set term_lease_month
public void set_term_lease_month(double term_lease_month)
{
this.term_lease_month=term_lease_month;
}
//addPetFee() method add $10 to monthly rent_amount
public void addPetFee()
{
this.rent_amount=this.rent_amount+10;
//call explainPetPolicy() method
explainPetPolicy();
}
//explainPetPolicy() method print statement regarding pet fee
public static void explainPetPolicy()
{
System.out.println("A pet fee of $10 is added to the monthly
rent.");
}
}
public class TestLease
{
//getData() method take each field of Lease from user & return
Lease Object
public static Lease getData()
{
Lease l=new Lease();
Scanner sc=new Scanner(System.in);
//take apartment_name from user
System.out.print("\nEnter apartment name: ");
l.apartment_name=sc.nextLine();
//take apartment_no from user
System.out.print("Enter apartment no: ");
l.apartment_no=sc.nextInt();
//take rent_amount from user
System.out.print("Enter monthly rent amount: ");
l.rent_amount=sc.nextDouble();
//take term_lease_month from user
System.out.print("Enter term of the lease in months: ");
l.term_lease_month=sc.nextDouble();
//return object
return l;
}
//showValues() method print data
public static void showValues(Lease l) {
System.out.println("\nApartment name: "+l.apartment_name);
System.out.println("Apartment no: "+l.apartment_no);
System.out.println("Monthly rent amount: "+l.rent_amount);
System.out.println("Term of the lease in months:
"+l.term_lease_month);
}
public static void main(String[] args) {
//decare 4 Lease object
Lease l1;
Lease l2;
Lease l3;
Lease l4;
//call getData() method
l1=getData();
l2=getData();
l3=getData();
//call showValues()
showValues(l1);
//call addPetFee() method
l1.addPetFee();
//call showValues()
showValues(l1);
l4=new Lease();
//call showValues()
showValues(l2);
showValues(l3);
showValues(l4);
}
}
The screenshot of code is given below:
Output: