Solve this using Java for an Intro To Java Class Provided fi
Solve this using Java for an Intro To Java Class.
Provided files:
Person.java
Starter File for solution:
/**
* 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;
}
}
And here is the tester file:
Solution
Hi,
I have implemented the required methods. Highlighted the code changes below.
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();
}
}
/**
* Test the Insurance class
*/
public class InsruanceTester
{
public static void main(String[] args)
{
Insurance policy = new Insurance(
new Person(\"Carlos\", \"m\", 15));
System.out.println(policy.clientAge());
System.out.println(\"Expected: 15\");
policy.incrementAge();
System.out.println(policy.clientAge());
System.out.println(\"Expected: 16\");
System.out.println(policy.clientGender());
System.out.println(\"Expected: m\");
policy = new Insurance(
new Person(\"Ashwanee\", \"f\", 25));
System.out.println(policy.clientAge());
System.out.println(\"Expected: 25\");
policy.incrementAge();
System.out.println(policy.clientAge());
System.out.println(\"Expected: 26\");
System.out.println(policy.clientGender());
System.out.println(\"Expected: f\");
}
}
Output:
15
Expected: 15
16
Expected: 16
m
Expected: m
25
Expected: 25
26
Expected: 26
f
Expected: f


