Question
Assignment 7 (Chapter 8) Override the toString() method and equals() method inside your class you created from Assignment 5. [2 points] Create a Copy Constructor
Assignment 7 (Chapter 8) Override the toString() method and equals() method inside your class you created from Assignment 5. [2 points] Create a Copy Constructor in your class from Assignment 5. [1 point] Create a static field in your class called counter. It should increment everytime an object is created. [1 point] Create a static method that displays the counter value. Also call this method at the end of your main method. [1 point] Use the "this" reference in all your setter (mutator) methods and also use the "this" reference in your default constructor to call the parameterized constructor. [1 point] Demonstrate that the copy Constructor, toString, and equals methods work in your main method [1 point]
package classes.and.objects;
public class Military
{
private String firstName;
private String lastName;
private int age;
private double asvabScore;
private boolean eligibility;
public Military()
{
firstName ="Unknown";
lastName="Unknown";
age=0;
asvabScore=0;
}
public Military(String f, String l, int a, double s)
{
firstName=f;
lastName=l;
age=a;
asvabScore=s;
}
public void setFirstName(String f)
{
firstName=f;
}
public void setLastName(String l)
{
lastName=l;
}
public void setAge(int a)
{
age=a;
}
public void setAsvabScore(double s)
{
asvabScore=s;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getAge()
{
return age;
}
public double getAsvabScore()
{
return asvabScore;
}
public void display()
{
System.out.print(firstName+" ");
System.out.println(lastName);
System.out.print("Age: "+age);
System.out.println(", Test score: "+asvabScore);
}
public boolean testEligibility()
{
if(age>=18 && asvabScore>69)
{
eligibility=true;
}
return eligibility;
}
}
-------------------------------------------------------------------------------------------------------------------
package classes.and.objects;
import java.util.Scanner;
public class Demo {
public static void main(String[] args)
{
Military mark= new Military();
mark.setFirstName("Mark");
mark.setLastName("Cardenas");
mark.setAge(24);
mark.setAsvabScore(85);
mark.display();
System.out.println("Did he pass the initial requirement?: "+mark.testEligibility());
System.out.println("---------------------------------");
Military john= new Military("John", "Legend", 15, 75);
john.display();
System.out.println("Did he pass the initial requirement?: " +john.testEligibility());
System.out.println("---------------------------------");
@SuppressWarnings("resource")
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter your first name: ");
String firstN=keyboard.nextLine();
System.out.println("Enter your last name: ");
String lastN=keyboard.nextLine();
System.out.println("Enter your age: ");
int age1=keyboard.nextInt();
System.out.println("Enter your asvab test score: ");
double asvab=keyboard.nextDouble();
Military mil =new Military(firstN, lastN, age1, asvab);
System.out.println("-----------------------------------");
System.out.print(mil.getFirstName());
System.out.println(" "+mil.getLastName());
System.out.print("Age: "+mil.getAge());
System.out.println(", Test score: "+mil.getAsvabScore());
System.out.println("Did he/she pass the initial requirement?: " +mil.testEligibility());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started