Question

In Java, Develop a class definition for a Person. Then develop a class definition for a...

In Java, Develop a class definition for a Person. Then develop a class definition for a Military Person that inherits the properties of the Person, and a Navy Sailor that inherits the properties of a Military Person.   At each level, include the following class variables:

  • Person: FirstName, MiddleInitial, LastName, Gender
  • Military Person: Active Duty or Reserve
  • Navy Sailor: Base where stationed, Surface Ship or Submarine, Date Commissioned, Rank

Include a toString() method in each class that will print out the variables of the class. Write a short main() program to test it out.

Important Note: Be sure to incorporate the principles of encapsulation (Chapter 8) in your definitions.

For each exercise, submit the source code and the screen output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Person
{
protected String FirstName, MiddleInitial, LastName, Gender;
public Person(String FirstName,String MiddleInitial,String LastName,String Gender)
{
this.FirstName=FirstName;
this.MiddleInitial=MiddleInitial;
this.LastName=LastName;
this.Gender=Gender;
}
public String toString()
{
return this.FirstName+" "+this.MiddleInitial+" "+this.LastName+" "+this.Gender;
}
}

class MilitaryPerson extends Person
{
boolean isActiveDuty;
public MilitaryPerson(String FirstName,String MiddleInitial,String LastName,String Gender,boolean isActiveDuty)
{
super(FirstName,MiddleInitial,LastName,Gender);
this.isActiveDuty=isActiveDuty;
}
public String toString()
{
return super.toString()+(this.isActiveDuty?" is active duty":"is reserve");
}
}
class NavySailor extends MilitaryPerson
{
String base,DateCommissioned;
boolean isSurface;
public NavySailor(String FirstName,String MiddleInitial,String LastName,String Gender,boolean isActiveDuty,String base,boolean isSurface,String DateCommissioned)
{
super(FirstName,MiddleInitial,LastName,Gender,isActiveDuty);
this.base=base;
this.isSurface=isSurface;
this.DateCommissioned=DateCommissioned;
}
public String toString()
{
return super.toString()+" based at "+this.base+(this.isSurface?" Surface Ship":" Submarine")+" commissioned on "+DateCommissioned;
}
  
}
public class Main
{
   public static void main(String[] args)
   {
       NavySailor n= new NavySailor("John","Arthur","Cotton","Male",true,"San Diego",true,"13 April 2009");
       System.out.println(n.toString());
   }
}


Add a comment
Know the answer?
Add Answer to:
In Java, Develop a class definition for a Person. Then develop a class definition for a...
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
  • In JAVA Develop a class definition for a Person. Then develop a class definition for a...

    In JAVA Develop a class definition for a Person. Then develop a class definition for a Military Person that inherits the properties of the Person, and a Navy Sailor that inherits the properties of a Military Person. Finally, create two classes; an Officer and an Enlisted that each can inherit the properties of a Navy Sailor. At each level, include the following class variables: Person: FirstName, MiddleInitial, LastName, Gender * Military Person: Active Duty or Reserve * Navy Sailor: Base...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • C++ Program 3 Create a class Person Include three(3) variables: firstName, lastName, YearOfBirth Write default and...

    C++ Program 3 Create a class Person Include three(3) variables: firstName, lastName, YearOfBirth Write default and parameterized constructors Write getters and setters for each variable. Declare 2 instances of the person class P1 and P2, one for both default and parm constructors Declare ptrPerson1 and ptrPerson2. Assign ptrPerson1 address of P1 Assign ptrPerson2 address of P2 Call all the getters and setters using the ARROW notation to test the class. Example:    ptrP1à getFirstName()

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • In Java Pls Class Design: Donut (Suggested Time Spent: < 15 minutes) The Donut class is intended to be an abstract a...

    In Java Pls Class Design: Donut (Suggested Time Spent: < 15 minutes) The Donut class is intended to be an abstract and simplified representation of a yummy edible donut Class Properties (MUST be private) Name of the donut Type of donut Price of the donut Class Invariants Donut name must not be empty (Default: "Classic") Type of donut can only be one of the following: "Plain," "Filled," or "Glazed" (Default: "Plain") Class Components Public Getter and Private Setter for name,...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

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