(Java) Look at the following Person.java code import java.util.Scanner; import java.io.*; class Person { public String name; public int age; public String gender; public void greeting() { System.out.println("Hi, my name is " + name + "!"); } public void greeting(String otherName) { System.out.println("Hi, " + otherName + "!"); } @Override public String toString() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Gender: " + gender); return ""; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setGender(String gender) { this.gender = gender; } public String getName() { return this.name; } public int getAge() { return this.age; } public String getGender() { return this.gender; } } Now, add two constructors to your class as specified below: The first constructor is a default constructor It takes in no parameters It sets the the name field to have a default value of "name unknown" It sets the age field to have a default value of 0 It sets the gender field to have a default value of "gender unknown" The second constructor overloads the first It takes in 3 parameters - a String for the name, an int for the age, a String for the gender It assigns name, age and gender the values passed in as parameters Copy and paste the new test file below into PersonTest.java (you can erase the old contents of the file): /** * PersonTest.java * @author * CIS 36B, Activity 8.1 */ import java.util.Scanner; public class PersonTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); String name, gender; int age; Person person; System.out.print("Welcome!\n\nEnter your name: "); name = input.nextLine(); System.out.print("Enter your age: "); age = input.nextInt(); System.out.print("Enter your gender: "); input.nextLine(); gender = input.nextLine(); person = //call 3-argument constructor here System.out.println("\nYour Summary:\n" + person); } } When you get the correct output (as shown below), upload Person.java and PersonTest.java to Canvas Sample Output (Note that user input may vary): Welcome! Enter your name: Antonia Li Enter your age: 29 Enter your gender: Female Your Summary: Name: Antonia Li Age: 29 Gender: Female
(Java)
Look at the following Person.java code
import java.util.Scanner;
import java.io.*;
class Person {
public String name;
public int age;
public String gender;
public void greeting() {
System.out.println("Hi, my name is " + name + "!");
}
public void greeting(String otherName) {
System.out.println("Hi, " + otherName + "!");
}
@Override
public String toString() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
return "";
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String getGender() {
return this.gender;
}
}
- Now, add two constructors to your class as specified below:
- The first constructor is a default constructor
- It takes in no parameters
- It sets the the name field to have a default value of "name unknown"
- It sets the age field to have a default value of 0
- It sets the gender field to have a default value of "gender unknown"
- The second constructor overloads the first
- It takes in 3 parameters - a String for the name, an int for the age, a String for the gender
- It assigns name, age and gender the values passed in as parameters
- Copy and paste the new test file below into PersonTest.java (you can erase the old contents of the file):
* PersonTest.java
* @author
* CIS 36B, Activity 8.1
*/
import java.util.Scanner;
public class PersonTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name, gender;
int age;
Person person;
System.out.print("Welcome!\n\nEnter your name: ");
name = input.nextLine();
System.out.print("Enter your age: ");
age = input.nextInt();
System.out.print("Enter your gender: ");
input.nextLine();
gender = input.nextLine();
person = //call 3-argument constructor here
System.out.println("\nYour Summary:\n" + person);
}
}
- When you get the correct output (as shown below), upload Person.java and PersonTest.java to Canvas
Enter your name: Antonia Li
Enter your age: 29
Enter your gender: Female
Your Summary:
Name: Antonia Li
Age: 29
Gender: Female
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 3 images