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 where stationed, Surface Ship or Submarine, Date Commissioned, Rank
Include a toString() method in each class that will print out what type of class it is. Include methods
read each of the class variables (i.e., getRank()) and methods to change the value of each class
variable. Finally, develop methods so that all of the information in an object can be printed out.
Write a short main() program to test it out.
Important Note: Be sure to incorporate the principles of encapsulation (Chapter 8) in your
definitions.
Please find the required program along with its output. Please see the comments against each line to understand the step.
class Test {
public static void main(String[] args) {
Person person = new Person("person"," ","one","male"); //create a person object
MilitaryPerson militaryPerson = new MilitaryPerson("person"," ","two","male",true); //create a MilitaryPerson object
NavySailor navySailor = new NavySailor("person"," ","three","male",true,"StationOne","Submarine","12-07-2016","captain"); //create a NavySailor object
System.out.println(person); //print all objects, this will internally invoke each class's toString() method
System.out.println(militaryPerson);
System.out.println(navySailor);
}
}
class Person{
String FirstName;
String MiddleInitial;
String LastName;
String Gender;
public Person(String firstName, String middleInitial, String lastName, String gender) { //constructor
FirstName = firstName;
MiddleInitial = middleInitial;
LastName = lastName;
Gender = gender;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getMiddleInitial() {
return MiddleInitial;
}
public void setMiddleInitial(String middleInitial) {
MiddleInitial = middleInitial;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
@Override
public String toString() {
return "Person{" +
"FirstName='" + FirstName + '\'' +
", MiddleInitial='" + MiddleInitial + '\'' +
", LastName='" + LastName + '\'' +
", Gender='" + Gender + '\'' +
'}';
}
}
class MilitaryPerson extends Person { //constructor
boolean activeDutyOrReserve;
public MilitaryPerson(String firstName, String middleInitial, String lastName, String gender, boolean activeDutyOrReserve) {
super(firstName, middleInitial, lastName, gender);
this.activeDutyOrReserve = activeDutyOrReserve;
}
public boolean isActiveDutyOrReserve() {
return activeDutyOrReserve;
}
public void setActiveDutyOrReserve(boolean activeDutyOrReserve) {
this.activeDutyOrReserve = activeDutyOrReserve;
}
}
class NavySailor extends MilitaryPerson { //constructor
String Base;
String ShipType;
String CommissionedDate;
String Rank;
public NavySailor(String firstName, String middleInitial, String lastName, String gender, boolean activeDutyOrReserve, String base, String shipType, String commissionedDate, String rank) {
super(firstName, middleInitial, lastName, gender, activeDutyOrReserve);
Base = base;
ShipType = shipType;
CommissionedDate = commissionedDate;
Rank = rank;
}
public String getBase() {
return Base;
}
public void setBase(String base) {
Base = base;
}
public String getShipType() {
return ShipType;
}
public void setShipType(String shipType) {
ShipType = shipType;
}
public String getCommissionedDate() {
return CommissionedDate;
}
public void setCommissionedDate(String commissionedDate) {
CommissionedDate = commissionedDate;
}
public String getRank() {
return Rank;
}
public void setRank(String rank) {
Rank = rank;
}
@Override
public String toString() {
return "NavySailor{" +
"Base='" + Base + '\'' +
", ShipType='" + ShipType + '\'' +
", CommissionedDate='" + CommissionedDate + '\'' +
", Rank='" + Rank + '\'' +
"} " + super.toString();
}
}
class Officer extends NavySailor{
public Officer(String firstName, String middleInitial, String lastName, String gender, boolean activeDutyOrReserve, String base, String shipType, String commissionedDate, String rank) {
super(firstName, middleInitial, lastName, gender, activeDutyOrReserve, base, shipType, commissionedDate, rank);
}
@Override
public String toString() {
return "Officer{} " + super.toString();
}
}
class Enlisted extends NavySailor{
public Enlisted(String firstName, String middleInitial, String lastName, String gender, boolean activeDutyOrReserve, String base, String shipType, String commissionedDate, String rank) {
super(firstName, middleInitial, lastName, gender, activeDutyOrReserve, base, shipType, commissionedDate, rank);
}
@Override
public String toString() {
return "Enlisted{} " + super.toString();
}
}
--------------------------------------
OUTPUT:
Person{FirstName='person', MiddleInitial=' ', LastName='one',
Gender='male'}
Person{FirstName='person', MiddleInitial=' ', LastName='two',
Gender='male'}
NavySailor{Base='StationOne', ShipType='Submarine',
CommissionedDate='12-07-2016', Rank='captain'}
Person{FirstName='person', MiddleInitial=' ', LastName='three',
Gender='male'}
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...
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...
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...
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...
write java program and for the following (you can add more classes as you see necessary) as follows: a class called Coursewith courseCode, courseName,and creditHours; a superclass called FacultyMemberwith FacultyID, firstName, lastName, academicRank, and academicSpecialization; a course Convener subclass inherits from the FacultyMember class with specific member variables representing the coursesand members (Lecturers andTAs) whom he/she is responsible of; Lecturers andTAsare also subclasses that inherit from FacultyMemberclass with some specific member variables (i.e., maximumNumberOfCourses, quotaOfCreditHoursthey can take, and assignedCourses). Create...
JAVA please: This lab has four parts: 1. Write a Person class. 2. Write a Student and Employee subclass to Person. 3. Write a Faculty and Staff subclass to Employee. 4. Write a program that creates an object of all of the above classes. Task 1 – The Person Class First, let us make a simple class titled Person. A person should include: 1. The class variables/attributes/properties: a. Name, b. Address, c. Phone number, d. And email address. 2. An...
Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student and “PostGraduate” that inherits form “Graduate”. Write the class definition for the abstract class “Student”. The class definition should include private instance variables of type String to hold the student’s first name, a string for his/her major and an int to hold the number of units taken. Getter and setter methods for each of the variables should be included in the class definition. Also...
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()
Create a Java program for a school. Create a report containing information for a classroom. For each classroom, the report will contain: the room number, the teacher and subject to the class, and a list of students assigned to the class and their grade. Create a directory called “SchoolInfo". Create Displayable interface. The interface should declare one method Create Person (abstract) class String firstName String lastName. (other classes will implement this one) Create the Teacher class, name and subject Create...
Write in C++ 1. Use the following Person class (Person.cpp, Person.h). You will be writing Person objects to a random access file. --------------------- Person.cpp--------------------------------- // Class Person stores customer's credit information. #include <string> #include "Person.h" using namespace std; // default Person constructor Person::Person( int idValue, string lastNameValue, string firstNameValue, int AgeValue ) { setID( idValue ); setLastName( lastNameValue ); setFirstName( firstNameValue ); setAge( AgeValue ); } // end Person constructor // get id value int Person::getID() const { return id;...