Java 8th Edition Joyce Farrell Creating class name lease with feels that whole a
ID: 3878354 • Letter: J
Question
Java 8th Edition Joyce Farrell
Creating class name lease with feels that whole an apartment tenant name, apartment number, monthly rent amount, in terms of the lease in months. Include a constructor that initializes the name to”XXX”, the apartment number 20, the rent to 1000, and the term to 12. Also methods to get and set each of the fields. Include a nonstatic method name addPeteFee() that asked $10 to the monthly rent value and call a static method named explainPetPolicy() that explains the pet fee. Say the class as Lease.java. Create a class name TestLease who main () method declares for lease objects. Calls a getData() method three times. Within the method, prompt the user for values for each feel for a Lease, and return a lease object to the main() method where it is assigned to one of the 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 the main() past one of the Lease objects to showValues() method that displays the data. Then call the addPetFee() method using the passed Lease object and confirmed that the fee explanation statement is displayed. Next, call the showValues() method for the lease object again and confirmed that the pet fee has been added to the rent. Finally call for showValues() method with each of the other three objects; confirm that two holes the values you supplied as input and one holes the constructor values. Say the application as testLease.java.
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//Lease.java
public class Lease {
private String tenantName;
private int apartmentNumber;
private double monthlyRent;
private int terms;
/**
* Default constructor to initialize data members with default values
*/
public Lease() {
tenantName = "XXX";
apartmentNumber = 20;
monthlyRent = 1000;
terms = 12;
}
/**
* getters and setters
*/
public String getTenantName() {
return tenantName;
}
public void setTenantName(String tenantName) {
this.tenantName = tenantName;
}
public int getApartmentNumber() {
return apartmentNumber;
}
public void setApartmentNumber(int apartmentNumber) {
this.apartmentNumber = apartmentNumber;
}
public double getMonthlyRent() {
return monthlyRent;
}
public void setMonthlyRent(double monthlyRent) {
this.monthlyRent = monthlyRent;
}
public int getTerms() {
return terms;
}
public void setTerms(int terms) {
this.terms = terms;
}
/**
* method to add pet fee to the rent
*/
public void addPetFee() {
monthlyRent += 10;
explainPetPolicy();
}
/**
* methods to explain the pet policy
*/
static void explainPetPolicy() {
System.out.println("Tenants with pets need to pay $10 as pet fee....");
}
}
//TestLease.java
import java.util.Scanner;
public class TestLease {
static Scanner scanner;
public static void main(String[] args) {
scanner=new Scanner(System.in);
/**
* Declaring 4 objects of Lease
*/
Lease ob1,ob2,ob3,ob4;
/**
* getting user input for 3 objects
*/
ob1=getData();
ob2=getData();
ob3=getData();
/**
* using default values for the fourth one
*/
ob4=new Lease();
/**
* Displaying details of first object
*/
showValues(ob1);
/**
* Adding pet fee and displaying pet policy for ob1
*/
ob1.addPetFee();
/**
* displaying updated rent and details
*/
showValues(ob1);
/**
* displaying details of the remaining objects
*/
showValues(ob2);
showValues(ob3);
showValues(ob4);
}
/**
* method to ask the user to enter lease details
* and returns a Lease object based on input data
*/
static Lease getData(){
Lease object=new Lease();
try{
System.out.println("Enter tenant name");
String name=scanner.nextLine();
System.out.println("Enter apartment number");
int number=Integer.parseInt(scanner.nextLine());
System.out.println("Enter rent");
double rent=Double.parseDouble(scanner.nextLine());
System.out.println("Enter terms (months)");
int terms=Integer.parseInt(scanner.nextLine());
/**
* Assigning values to the object, not checking for any negative
* values as it is not mentioned in the question
*/
object.setTenantName(name);
object.setApartmentNumber(number);
object.setMonthlyRent(rent);
object.setTerms(terms);
return object;
}catch (Exception e) {
System.out.println("Invalid input, try again");
return getData();
}
}
/**
* method to display a lease object values
*/
static void showValues(Lease object){
System.out.println("Tenant Name: "+object.getTenantName());
System.out.println("Apartment Number: "+object.getApartmentNumber());
System.out.println("Monthly Rent: "+object.getMonthlyRent());
System.out.println("Terms: "+object.getTerms()+" months");
}
}
/*OUTPUT*/
Enter tenant name
Alice
Enter apartment number
123
Enter rent
1200
Enter terms (months)
12
Enter tenant name
Bob
Enter apartment number
124
Enter rent
1100
Enter terms (months)
24
Enter tenant name
Chris
Enter apartment number
125
Enter rent
1500
Enter terms (months)
6
Tenant Name: Alice
Apartment Number: 123
Monthly Rent: 1200.0
Terms: 12 months
Tenants with pets need to pay $10 as pet fee....
Tenant Name: Alice
Apartment Number: 123
Monthly Rent: 1210.0
Terms: 12 months
Tenant Name: Bob
Apartment Number: 124
Monthly Rent: 1100.0
Terms: 24 months
Tenant Name: Chris
Apartment Number: 125
Monthly Rent: 1500.0
Terms: 6 months
Tenant Name: XXX
Apartment Number: 20
Monthly Rent: 1000.0
Terms: 12 months
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.