Question

Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

Solve this using Java for an Intro To Java Class.

Solve this using Java for an Intro To Java Class.

Provided files:

Person.java

/**
 * Models a person
 */
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;
    }
}

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:

/**
 * 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");
        
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • What is the final value of the count field? public class Person { private String name;...

    What is the final value of the count field? public class Person { private String name; private int age; private static int count=0; public Person(String name, int age) { this.name = name; this.age age; count++; } public static void main(String[] args) { Person a - new Person("John Doe". 35): Person b new Person("Jane Doe", 38): for(int i=0;i<5;i++) Person tempa

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** *...

    JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** * Write a description of class UniversityTester here. * * @author (your name) * @version (a version number or a date) */ public class ClassroomTester { public static void main(String[] args) { ArrayList<Double> grades1 = new ArrayList<>(); grades1.add(82.0); grades1.add(91.5); grades1.add(85.0); Student student1 = new Student("Srivani", grades1); ArrayList<Double> grades2 = new ArrayList<>(); grades2.add(95.0); grades2.add(87.0); grades2.add(99.0); grades2.add(100.0); Student student2 = new Student("Carlos", grades2); ArrayList<Double> grades3 = new...

  • public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);     

    public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);               private int minAge;               private Rating(int i)        {              minAge = i;        }               public int getMinAge()        {              return minAge;        }               public void setMinAge(int age)        {              minAge = age;        }               public String toString()        {              switch(this)              {              case GENERAL:                     return "G";              case PARENTALGUIDANCE:                     return "P";              case MATURE:                     return...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please...

    USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please do not change the DataSetDemo code. Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element. Skeleton Code: DataSet: /** Computes the maximum of a set of data values. */ public class DataSet { private int[] values; private int first; private int last; /** Constructs a DataSet object. @param...

  • A class called Author is designed as shown in the class diagram. (JAVA)

    in javaA class called Author is designed as shown in the class diagram. Author name: String email: String gender: char +Author (name :String, email: String, gender char) +getName ():String +getEmail() String +setEmail (email: String) :void +getGender(): char +toString ():String It contains  Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f),  One constructor to initialize the name, email and gender with the given values; public Author (String name, String email, char gender) f......)  public getters/setters: getName(), getEmail setEmail and getGender(); (There are no setters for name and gender,...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT