Here is the Person Class Provided: Use the following files: Person.java Here is
ID: 3924675 • Letter: H
Question
Here is the Person Class Provided:
Use the following files:
Person.java
Here is the tester file:
InsruanceTester.java
I have done three of the methods i just need the last one public double monthly premium method completed. Here is my code:
public class Person
{
private String name;
private String gender;
private int age;
/**
* Consructs a Person object
* @param name the name of the person
* @param gender the gender of the person either
* m for male or f for female
* @param age the age of the person
*/
public Person(String name, String gender, int age)
{
this.name = name;
this.gender = gender;
this.age = age;
}
/**
* gets the age of this Person
* @return the age of this Person
*/
public int getAge()
{
return age;
}
/**
* gets the gender of this Person
* @return the gender of this Person
*/
public String getGender()
{
return gender;
}
/**
* gets the name of this Person
* @return the name of this Person
*/
public String getName()
{
return name;
}
/**
* Increases the age of this Person by 1 year
*/
public void birthday()
{
age = age + 1;
}
}
/**
* Models an Insurance client
*/
public class Insurance
{
private Person client;
/**
* Constructs an Insurance object with the given Person
* @param p the Person for this Insurance
*/
public Insurance(Person p)
{
client = p;
}
public int clientAge(){
return client.getAge();
}
public String clientGender(){
return client.getGender();
}
public void incrementAge(){
client.birthday();
}
}
Explanation / Answer
//Person.java
public class Person
{
private String name;
private String gender;
private int age;
/**
* Consructs a Person object
* @param name the name of the person
* @param gender the gender of the person either
* m for male or f for female
* @param age the age of the person
*/
public Person(String name, String gender, int age)
{
this.name = name;
this.gender = gender;
this.age = age;
}
/**
* gets the age of this Person
* @return the age of this Person
*/
public int getAge()
{
return age;
}
/**
* gets the gender of this Person
* @return the gender of this Person
*/
public String getGender()
{
return gender;
}
/**
* gets the name of this Person
* @return the name of this Person
*/
public String getName()
{
return name;
}
/**
* Increases the age of this Person by 1 year
*/
public void birthday()
{
age = age + 1;
}
}
--------------------------------------------------------------------------------------------------------------
/**
* Models an Insurance client
*/
public class Insurance
{
private Person client;
/**
* Constructs an Insurance object with the given Person
* @param p the Person for this Insurance
*/
public Insurance(Person p)
{
client = p;
}
public int clientAge(){
return client.getAge();
}
public String clientGender(){
return client.getGender();
}
public void incrementAge(){
client.birthday();
}
/**
* The method monthlyPremium that sets the insurance
* value based on gender and age
* */
public double monthlyPremium() {
double insurance=0;
//Checking if age of client object is <16
//return -1
if(client.getAge()<16)
{
insurance=-1;
return insurance;
}
//checking if client gender is m
if(client.getGender().equals("m"))
{
//check age and set insurance
if(client.getAge()<25)
insurance=85.50;
//age is between 25 and 85(exclusive)
if(client.getAge()>=25 && client.getAge()<85)
insurance=55.00;
//age is >=85
if(client.getAge()>=85)
insurance=92.00;
}
else
{
//check age and set insurance
if(client.getAge()<25)
insurance=79.25;
//age is between 25 and 85(exclusive)
if(client.getAge()>=25 && client.getAge()<85)
insurance=45.00;
//age is >=85
if(client.getAge()>=85)
insurance=90.00;
}
//return insurance
return insurance;
}//end of the monthlyPremium
}
--------------------------------------------------------------------------------------------------------------
/**
* Test the Insurance class
*/
//InsruanceTester.java
public class InsruanceTester
{
public static void main(String[] args)
{
//male 15
Insurance policy = new Insurance(
new Person("Carlos", "m", 15));
System.out.println("underage: " + policy.monthlyPremium());
System.out.println("Expected: -1.0");
//male 16
policy.incrementAge(); //increment to 16
System.out.println("male < 25: " + policy.monthlyPremium());
System.out.println("Expected: 85.5");
//male 25
policy = new Insurance(
new Person("Henry", "m", 25));
System.out.println("male 25 to 85: " + policy.monthlyPremium());
System.out.println("Expected: 55.0");
//male 85
policy = new Insurance(
new Person("Thong", "m", 85));
System.out.println("male 85: " + policy.monthlyPremium());
System.out.println("Expected: 92.0");
//female 15
policy = new Insurance(
new Person("Ashwanee", "f", 15));
System.out.println("underage: " + policy.monthlyPremium());
System.out.println("Expected: -1.0");
//female 16
policy.incrementAge();
System.out.println("female < 25: " + policy.monthlyPremium());
System.out.println("Expected: 79.25");
//female 25
policy = new Insurance(
new Person("Rebecca", "f", 25));
System.out.println("female 25 to 85: " + policy.monthlyPremium());
System.out.println("Expected: 45.0");
//female 85
policy = new Insurance(
new Person("Chen", "f", 85));
System.out.println("female 85: " + policy.monthlyPremium());
System.out.println("Expected: 90.0");
}
}
--------------------------------------------------------------------------------------------------------------
Sample Output:
underage: -1.0
Expected: -1.0
male < 25: 85.5
Expected: 85.5
male 25 to 85: 55.0
Expected: 55.0
male 85: 92.0
Expected: 92.0
underage: -1.0
Expected: -1.0
female < 25: 79.25
Expected: 79.25
female 25 to 85: 45.0
Expected: 45.0
female 85: 90.0
Expected: 90.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.