My program should print out the name and age of all cats with claws who are over 3 years old. For the following input
Enter the name of Cat 1: Sam
Enter the age of Cat 1: 1
Enter the weight of Cat 1: 5
Enter the breed of Cat 1: fluffy1
Does the cat have claws? True or False?: True
Enter the name of Cat 2: Tom
Enter the age of Cat 2: 4
Enter the weight of Cat 2: 5
Enter the breed of Cat 2: fluffy2
Does the cat have claws? True or False?: True
Enter the name of Cat 3: Bob
Enter the age of Cat 3: 5
Enter the weight of Cat 3: 5
Enter the breed of Cat 3: fluffy3
Does the cat have claws? True or False?: False
The output shoud look like this.
The Cats over 3 with claws are:
Name: Tom
Age: 4 Years Old
However, the program finishes with no output. Here is the code I executed.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class MyClass {
private static Cat[] catArray = new Cat[3];
public static void main(String[] args) throws IOException {
for (int i=0;i<3;i++) {
Cat cat = new Cat();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Name of Cat " + (i+1) + ": ");
cat.setName(in.readLine());
System.out.print("Enter the Age of Cat " + (i+1) + ": ");
cat.setAge((Integer.parseInt(in.readLine())));
System.out.print("Enter the Weight of Cat " + (i+1) + ": ");
cat.setWeight((Double.parseDouble(in.readLine())));
System.out.print("Enter the Breed of Cat " + (i+1) + ": ");
cat.setBreed(in.readLine());
System.out.print("Does the cat have claws? True or False: ");
String hasClaws = in.readLine();
if(hasClaws.equals("False"))
cat.sethasClaws(false);
else
cat.sethasClaws(true);
catArray[i] = cat;
}
for(int j=0;j<3;j++) {
if((catArray[j].getAge()>=3) && (!catArray[j].hasClaws()) ) {
System.out.println("The cats over 3 with claws are: \n");
System.out.println("Name: " + catArray[j].getName());
System.out.println("Age: " + catArray[j].getAge() + " years old");
}
}
}
}
and also:
public class Cat {
private String name;
private int age;
private double weight;
private String breed;
private boolean hasClaws;
public Cat() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public boolean hasClaws() {
return hasClaws;
}
public void sethasClaws(boolean hasClaws) {
this.hasClaws = hasClaws;
}
}
I believe that the ! operator in (catArray[j].getAge()>=3) && (!catArray[j].hasClaws()) is messing you up. This equates to "show me all cats 3 or over, that don't have claws".
from: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
! Logical complement operator;
inverts the value of a boolean
Try this:
for(int j=0;j<3;j++) {
if((catArray[j].getAge()>=3) && (catArray[j].hasClaws()) ) {
System.out.println("The cats over 3 with claws are: \n");
System.out.println("Name: " + catArray[j].getName());
System.out.println("Age: " + catArray[j].getAge() + " years old");
}
}
Related
I am trying to do a program that will convert the input age of a dog into human years, but it doesn't work. Here are the instructions I had for the conversion of the dog years to human years:
A method inHumanYears which will return the age of a pet dog in human years. Here is how to calculate it:
15 human years equals the first year of a medium-sized dog's life.
Year two for a dog equals about nine years for a human.
And after that, each human year would be approximately five years for a dog.
Here a few examples:
a 4-month old dog = 0.25 (which is 1/4 of a year)*15 = 3.75 human years
a 5 years old = 15+9+(5-2) * 5 = 39 human years
So here is what my code looks so far:
import java.util.Scanner;
public class MyPet_1_lab7 {
// Implement the class MyPet_1 so that it contains 3 instance variables
private String breed;
private String name;
private int age;
private double inHumanYears;
// Default constructor
public MyPet_1_lab7()
{
this.breed = null;
this.name = null;
this.age = 0;
}
// Constructor with 3 parameters
public MyPet_1_lab7(String a_breed, String a_name, int an_age){
this.breed = a_breed;
this.name = a_name;
this.age = an_age;
this.inHumanYears = inHumanYears();
}
// Accessor methods for each instance variable
public String getBreed(){
return this.breed;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
//Mutator methods for each instance variable
public void setBreed(String a_breed){
this.breed = a_breed;
}
public void setName(String a_name){
this.name = a_name;
}
public void setAge(int an_age){
this.age = an_age;
this.inHumanYears = inHumanYears();
}
// toString method that will return the data in an object formated as per the output
public String toString(){
return (this.breed + " whose name is " + this.name + " and " + (double)this.age + " dog years (" + inHumanYears() + " human years old)");
}
public boolean equals(MyPet_1_lab7 a){
if ((this.breed.equals(a.getBreed())) && (this.age == a.getAge())){
return true;
}
else
return false;
}
public double inHumanYears(){
if ((double)age >= 2 ){
inHumanYears = (15 + 9 + (age - 2))*5;
return (inHumanYears);
}
else {
inHumanYears = age*15;
return (inHumanYears);
}
}
public double getHumanYears(){
double yearOneAge = age >=1 ? 1.0: age;
double yearTwoAge = age >=2 ? 1.0: age > 1? age-1.0: 0.0;
double yearThreeAge = age > 2 ? age-2.0: 0.0;
inHumanYears = yearOneAge * 15 + yearTwoAge*9 + yearThreeAge * 5;
return inHumanYears;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.print("What type of dog do you have? ");
String breed = keyboard.nextLine();
System.out.print("What is its name? ");
String name = keyboard.nextLine();
System.out.print("How old? ");
int age = keyboard.nextInt();
MyPet_1_lab7 dog= new MyPet_1_lab7();
System.out.println(dog);
MyPet_1_lab7 dog1 = new MyPet_1_lab7(breed,name,age);
System.out.println(dog1);
}
}
'''
The issue is your toString() method accesses a field you set with a method that has not been called. This
public String toString(){
return (this.breed + " whose name is " + this.name + " and "
+ this.age + " dog years (" + inHumanYears + " human years old)");
}
should be changed to invoke inHumanYears(). Like,
public String toString(){
return (this.breed + " whose name is " + this.name + " and "
+ this.age + " dog years (" + inHumanYears() + " human years old)");
}
Your method InHumanYears() is never called + your attribute inHumanYears is never assigned.
You must change your second constructor.
Before :
public MyPet_1_lab7(String a_breed, String a_name, int an_age){
this.breed = a_breed;
this.name = a_name;
this.age = an_age;
}
After :
public MyPet_1_lab7(String a_breed, String a_name, int an_age){
this.breed = a_breed;
this.name = a_name;
this.age = an_age;
this.inHumanYears = inHumanYears();
}
You must also called inHumanYears() in setAge() :
setAge(int age){
this.age = age;
this.inHumanYears = inHumanYears();
}
I obtain this execution (I think your calculation is incorrect):
toto whose name is tata and 3 dog years (125.0 human years old)
your algo seems wrong for the stated requirements.
15 human years equals the first year of a medium-sized dog's life.
Year two for a dog equals about nine years for a human.
And after that, each human year would be approximately five years
for a dog.
also you should change age to allow for decimal points. (if not age may be in months, like the example for your 4month old dog -> in which case you should divide everything by 12). It's usually better to name it ageMonth or ageYear to remove that ambiguity.
you can try breaking each component down.
note: the ?: are ternary operators. Think of it as a "short form" for the if-else statement
public double getHumanYears(){
double yearOneAge = age>=1 ? 1.0: age;
//if age>1, then year1Age=1, otherwise year1Age = age
double yearTwoAge = age>=2 ? 1.0: age>1? age-1.0: 0.0;
//if age>2, then year2Age=2, elseIf age>1, then year2Age = age-1, otherwise, year2Age is 0.
double yearThreeAge = age>2 ? age-2.0: 0.0;
//if age>2, then year3Age= age-2, otherwise, year3Age is 0.
//the formula will break down an age into 3 parts.
//yearOneAge: from 0.0 to 1.0.
//yearTwoAge: from 0.0 to 1.0.
//yearThreeAge: from 0.0 onwards.
//e.g. age=0.8years, year1=0.8, year2=0.0, year3=0.0
//e.g. age=1.5years, year1=1.0, year2=0.5, year3=0.0
//e.g. age=3.6years, year1=1.0, year2=1.0, year3=1.6
inHumanYears = yearOneAge * 15 + yearTwoAge * 9 + yearThreeAge * 5
return inHumanYears;
}
also like Quentin said, you forgot to call getHumanYears in your setter and constructor, or you can update the toString to call getHumanYears().
Ok, so thank you everybody for your time and your help. So here is what I did, I changed the private int age to a double. My friend told me that we didn't need to necessarily put it to an integer. The rest was just change everything to double and it solved all my problem for my output:
import java.util.Scanner;
public class MyPet_1_lab7 {
// Implement the class MyPet_1 so that it contains 3 instance variables
private String breed;
private String name;
private double age; // instead of private int age;
MyPet_1_lab7 dog1;
MyPet_1_lab7 dog2;
// Default constructor
public MyPet_1_lab7()
{
this.breed = null;
this.name = null;
this.age = 0;
}
// Constructor with 3 parameters
public MyPet_1_lab7(String a_breed, String a_name, double an_age){
this.breed = a_breed;
this.name = a_name;
this.age = an_age;
}
// Accessor methods for each instance variable
public String getBreed(){
return this.breed;
}
public String getName(){
return this.name;
}
public double getAge(){
return this.age;
}
//Mutator methods for each instance variable
public void setBreed(String breed2){
this.breed = breed2;
}
public void setName(String name2){
this.name = name2;
}
public void setAge(double age2){
this.age = age2;
}
// toString method that will return the data in an object formated as per the output
public String toString(){
return (this.breed + " whose name is " + this.name + " and " + this.age + " dog years (" + inHumanYears() + " human years old)");
}
public boolean equals(MyPet_1_lab7 a){
if ((this.breed.equals(a.getBreed())) && (this.age == a.getAge())){
return true;
}
else
return false;
}
double human_age = 0;
public double inHumanYears(){
if (this.age < 1)
human_age = (this.age) * 15;
if (this.age >=1 && this.age < 2)
human_age = 15 + (this.age - 1) * 9;
if (this.age >= 2 ){
human_age = 15 + 9 + (age - 2)*5;
}
return human_age;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.print("What type of dog do you have? ");
String breed = keyboard.nextLine();
System.out.print("What is its name? ");
String name = keyboard.nextLine();
System.out.print("How old? ");
double age = keyboard.nextDouble();
MyPet_1_lab7 dog= new MyPet_1_lab7();
System.out.println(dog);
MyPet_1_lab7 dog1 = new MyPet_1_lab7(breed,name,age);
System.out.println(dog1);
Scanner key = new Scanner(System.in);
System.out.println("\nLet's set up the 1st dog ... ");
System.out.print("\tWhat breed is it? ");
String breed1 = key.nextLine();
System.out.print("\tWhat is the dog's name? ");
String name1 = key.nextLine();
System.out.print("\tHow old is the dog in dog years (a double number)? ");
double age1 = key.nextDouble();
MyPet_1_lab7 dog2 = new MyPet_1_lab7 (breed1, name1, age1);
System.out.println("Dog1 is now a(n) " + dog2);
System.out.println("\nAre the 2 dogs the same breed and age?");
if ((breed.equals(breed1))&& age == age1)
System.out.println("Yes, they are the same breed and age");
else
System.out.println("No, they are not the same breed and/or age");
}
}
'''
For some reason, after I add a pet named "Oliver" the main menu prints out twice with the "invalid choice" line along with it. I just need another set of eyes to look at it because I have been looking at it for hours on end and been fixing little mistakes to no avail.
The code when ran looks like this:
/*Welcome to the pet store.Type the letter to make your selection
A. List the pets in the store.
B. Age up the pets
C. Add a new pet
D. Adopt a pet
E. Quit
C
Please type in a name
Oliver
Please type in an age
22
Oliver has just been added to the store!
Welcome to the pet store.Type the letter to make your selection
A. List the pets in the store.
B. Age up the pets
C. Add a new pet
D. Adopt a pet
E. Quit
Invalid choice
Welcome to the pet store.Type the letter to make your selection
A. List the pets in the store.
B. Age up the pets
C. Add a new pet
D. Adopt a pet
E. Quit*/
Here is my main class code:
private static void mainmenu(){
System.out.println("Welcome to the pet store.Type the letter to make
your selection");
System.out.println("A."+" " + "List the pets in the store.");
System.out.println("B."+" " + "Age up the pets");
System.out.println("C."+" " + "Add a new pet");
System.out.println("D."+" " + "Adopt a pet");
System.out.println("E."+" " + "Quit");
MainPets.Getuserinput();
}
public static String Getuserinput(){
userinput=scan.nextLine();
return userinput;
}
public static void main (String [] args){
int pet3age;
String pet3name;
Pet Pet1=new Pet("Fido",3);
Pet Pet2=new Pet("Furball",1);
Pet Pet3=null;
int userinputint;
MainPets.mainmenu();
while(userinput.equals("A")||userinput.equals("B")||userinput.equals("C")||userinput.equals("D")||userinput.equals("E")){
switch(userinput){
case "C":
if (Pet3!=null&&userinput.equals("C")){
System.out.println("Sorry the store is full");
}
if(Pet3==null){
System.out.println("Please type in a name");
pet3name=scan.nextLine();
System.out.println("Please type in an age");
pet3age=scan.nextInt();
Pet3=new Pet(pet3name,pet3age);
System.out.println(pet3name + " has just been added to the store!");
}
MainPets.mainmenu();
break;
}
}
while(!userinput.equals("A")||!userinput.equals("B")||!userinput.equals("C")||!userinput.equals("D")||!userinput.equals("E")){
System.out.println("Invalid choice");
MainPets.mainmenu();
}
Here is the class with all the methods:
public class Pet {
String Name;
String AdoptionStatus;
int Age;
public Pet() {}
public Pet(String Name, int Age) {
this.Name = Name;
this.Age = Age;
}
public void SetName(String namesetup) {
Name = namesetup;
}
public String GetName() {
return Name;
}
public int GetAge() {
return Age;
}
public int ageincrease() {
return Age++;
}
public String Getadoptionstatus() {
return AdoptionStatus;
}
public void Setadoptionstatustonotadopted(int petnumber) {
AdoptionStatus="not adopted";
}
public void Setadoptionstatustoadopted(int petnumber){
AdoptionStatus="adopted";
}
}
It looks like you're trying to use static as much as possible for practice of what it does?
Anyway, see below for a minimal example of which you can build on (i.e. it will let you enter 'C' as many times as you want to 'add' new pets).
static String petname, petage;
public static void main(String[] args) {
initialText();
String userinput = userInput();
while (userinput.equals("A") || userinput.equals("B") || userinput.equals("C") || userinput.equals("D") || userinput.equals("E")) {
if(userinput.equals("C")){
System.out.println("Please type in a name");
petname = userInput();
System.out.println("Please type in an age");
petage = userInput();
Pet p = new Pet(petname, petage);
System.out.println(petname + " has been added to the store.");
}
else{
System.out.println("Option not configured yet");
//TODO - the rest of the options
}
initialText();
userinput = userInput();
}
}
public static void initialText() {
System.out.println("Welcome to the pet store.Type the letter to make your selection");
System.out.println("A." + " " + "List the pets in the store.");
System.out.println("B." + " " + "Age up the pets");
System.out.println("C." + " " + "Add a new pet");
System.out.println("D." + " " + "Adopt a pet");
System.out.println("E." + " " + "Quit");
}
public static String userInput(){
Scanner s = new Scanner(System.in);
return s.nextLine();
}
It's by no means perfect, just knocked it together very quickly to give you a chance to work on it.
I am struggling to get this program to do exactly what the assignment asks. It throws a null pointer exception when trying to remove an added student. Also when I list the students, it shows null on everything.
-Code is fixed-
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Student> newStudents = new ArrayList<Student>();
System.out.println("Welcome to the Student Interface!.");
System.out.println("Please select a number from the options below \n");
while (true) {
// Give the user a list of their options
System.out.println("1: Add a student to the list.");
System.out.println("2: Remove a student from the list.");
System.out.println("3: Display all students in the list.");
System.out.println("0: Exit the student interface.");
// Get the user input
int userChoice = input.nextInt();
switch (userChoice) {
case 1:
addStudents(newStudents);
break;
case 2:
removeStudent(newStudents);
break;
case 3:
displayStudent(newStudents);
break;
case 0:
System.out.println("Thank you for using the student interface. See you again soon!");
System.exit(0);
}
}
}
public static void addStudents(ArrayList<Student> newStudents) {
Scanner input = new Scanner(System.in);
Student newStudent = new Student();
System.out.println("Please enter first name: ");
String First_Name = input.next();
newStudent.setFirst_Name(First_Name);
System.out.println("Please enter last name: ");
String Last_Name = input.next();
newStudent.setLast_Name(Last_Name);
System.out.println("Please enter major: ");
String Major = input.next();
newStudent.setMajor(Major);
System.out.println("Please enter GPA: ");
String GPA = input.next();
newStudent.setGPA(GPA);
System.out.println("Please enter UIN: ");
String UIN = input.next();
newStudent.setUIN(UIN);
System.out.println("Please enter NetID: ");
String NetID = input.next();
newStudent.setNetID(NetID);
System.out.println("Please enter Age: ");
String Age = input.next();
newStudent.setAge(Age);
System.out.println("Please enter Gender: ");
String Gender = input.next();
newStudent.setGender(Gender);
if (newStudents.size() <= 10) {
newStudents.add(newStudent);
System.out.println("Student added\n");
} else {
System.out.println("\n Student interface is full!");
}
}
private static void displayStudent(ArrayList<Student> newStudents) {
for (Student e : newStudents) {
System.out.println(e);
}
}
private static void removeStudent(ArrayList<Student> newStudents) {
Scanner input = new Scanner(System.in);
System.out.println("Please, enter the UIN to remove the Student: ");
String uin = input.nextLine();
for (Student e : newStudents) {
if (e.getUIN().equals(uin)) {
newStudents.remove(e);
System.out.println("Student removed");
break;
}
else {
System.out.println("Sorry, no such student with this " + uin + " " + "number exist");
}
}
}
Student Class:
package assignments;
public class Student{
private String First_Name;
private String Last_Name;
private String Major;
private String GPA;
private String UIN;
private String NetID;
private String Age;
private String Gender;
public String getFirstName()
{
return First_Name;
}
public void setFirst_Name(String value)
{
this.First_Name = value;
}
public String getLastName()
{
return Last_Name;
}
public void setLast_Name(String value)
{
Last_Name = value;
}
public String getMajor()
{
return Major;
}
public void setMajor(String value)
{
Major = value;
}
public String getGPA()
{
return GPA;
}
public void setGPA(String value)
{
GPA = value;
}
public String getUIN()
{
return UIN;
}
public void setUIN(String value)
{
UIN = value;
}
public String getNetID()
{
return NetID;
}
public void setNetID(String value)
{
NetID = value;
}
public String getAge()
{
return Age;
}
public void setAge(String value)
{
Age = value;
}
public String getGender()
{
return Gender;
}
public void setGender(String value)
{
Gender = value;
}
public String toString()
{
return "First Name: " + First_Name +
"\n Last Name: " + Last_Name +
"\n Major: " + Major +
"\n GPA: " +GPA+
"\n UIN: " + UIN +
"\n NetID: " + NetID+
"\n Age: " + Age+
"\n Gender: " + Gender;
}
public void createStudent(String first_Name2, String last_Name2, String major2, String gPA2, String uIN2, String netID2,
String age2, String gender2) {
first_Name2 = First_Name;
last_Name2 = Last_Name;
major2 = Major;
gPA2 = GPA;
uIN2 = UIN;
age2 = Age;
gender2 = Gender;
}
}
Your program runs about perfectly on my computer. Here’s an example run:
Welcome to the Student Interface!.
Please select a number from the options below
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
2
Please, enter the UIN to remove the Student:
13
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
1
Please enter first name:
o
Please enter last name:
v
Please enter major:
cs
Please enter GPA:
g
Please enter UIN:
79
Please enter NetID:
o
Please enter Age:
57
Please enter Gender:
m
Student added
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
3
Student [firstName=o, lastName=v, major=cs, gPA=g, uIN=79, netID=o, age=57, gender=m]
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
2
Please, enter the UIN to remove the Student:
79
Student removed
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
3
1: Add a student to the list.
2: Remove a student from the list.
3: Display all students in the list.
0: Exit the student interface.
0
Thank you for using the student interface. See you again soon!
Potential issues in your program include: You have two Scanner objects on System.in, you may want to share just one. You don’t use the variable student_added. In my two cases the program didn’t report back to the user whether a student was removed or not. Your two TODO comments are obsolete and should be removed.
One of two reasons you'd get a NullPointerException on the remove operation. Either the ArrayList is null, or the object you are attempting to remove is null. Check for those.
For step 6 in the println i know how to call the toString explicitly but how do i output student information from the current student in the array WITHOUT calling toString() explicitly or using any accessor methods?
import java.util.Scanner;
public class Students
{
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
Student[] students;
students = getStudents();
printStudents(students);
}
private static Student[] getStudents()
{
Student[] temp;
int how_many;
System.out.print("How many students? ");
how_many = input.nextInt();
purgeInputBuffer();
temp = new Student[input.nextInt()]; // Step 1 ???
for (int i = 0; i < temp.length; i++)
{
getStudent();
temp[i] = getStudent(); // Step 2
}
return temp; // Step 3
}
private static Student getStudent()
{
String name,
address,
major;
double gpa;
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter address: ");
address = input.nextLine();
System.out.print("Enter major: ");
major = input.nextLine();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
purgeInputBuffer();
return new Student(name, address, major, gpa); // Step 4
}
private static void printStudents(Student[] s)
{
System.out.println();
for (int i = 0; i < s.length; i++) // Step 5
{
System.out.println(______); // Step 6
}
}
private static void purgeInputBuffer()
{
// ----------------------------------------------------
// Purge input buffer by reading and ignoring remaining
// characters in input buffer including the newline
// ----------------------------------------------------
input.nextLine();
}
}
Declare your variables name, address, major, gpa as member variables.
Override the toString() method for this class.
public String toString() {
return "Name: " + name + "\n" + "Address: " + .... //whatever you want to print.
}
Whenever you need to print details of a student, pass it to the println method:
System.out.println(obj); //where obj is a Student object.
In your Stundent class override the toString() method as, for example,:
#Override
public String toString() {
return "Stundent [ Name: " + name + ", Address: " + address + ", Major: " + major + ", GPA: " + gpa + " ]";
}
And modify your printStudents() method as follows (your Step 6):
private static void printStudents(final Student[] s) {
System.out.println();
for (final Student student : s) {
System.out.println(student);
}
}
Then your output should look like:
Stundent [ Name: Name1, Address: Addr1, Major: Major1, GPA: 3.0 ]
Stundent [ Name: Name2, Address: Addr2, Major: Major2, GPA: 3.5 ]
Now you see the toString() method will be invoked implicitly.
The Cullerton Part District holds a mini-Olympics each summer. Create a class named Participant with fields for a name, age, and street address. Include a constructor that assigns parameter values to each field and a toString() method that returns a String containing all the values. Also include an equals() method that determines two Participants are equal if they have the same values in all three fields. Create an application with two arrays of at least 5 Participants each--one holds the Participants in the mini-marathon and the other holds Participants in the diving competition. Prompt the user for Participants who are in both events save the files as BC.java and ABC.java.
import javax.swing.JOptionPane;
import java.util.*;
public class ABC {
private static Participant mini[] = new Participant[2];
public static void main(String[] args) {
setParticipant();
displayDetail();
}
// BC p=new BC(name,age,add);
//displayDetails();
// System.out.println( p.toString());
public static void displayDetail() {
String name=null;
String add = null;
int age=0;
System.out.println("Name\tAdress\tAge");
BC p=new BC(name,age,add);
for (int x = 0; x < mini.length; x++) {
//Participant p1=mini[x];
System.out.println(p.toString());
}
}
public static String getName() {
Scanner sc = new Scanner(System.in);
String name;
System.out.print(" Participant name: ");
return name = sc.next();
}
// System.out.print(" Participant name: ");
// name = sc.next();
public static int getAge() {
int age;
System.out.print(" Enter age ");
Scanner sc=new Scanner(System.in);;
return age= sc.nextInt();
}
public static String getAdd() {
String add;
Scanner sc=new Scanner(System.in);;
System.out.print("Enter Address: ");
return add=sc.next();
}
public static void setParticipant(){
for (int x = 0; x < mini.length; x++) {
System.out.println("Enter loan details for customer " + (x + 1) + "...");
//Character loanType=getLoanType();
//String loanType=getLoanType();
String name=getName();
String add=getAdd();
int age=getAge();
System.out.println();
}
}
}
//another class
public class BC {
private String name;
private int age;
private String address;
public BC(String strName, int intAge, String strAddress) {
name = strName;
age = intAge;
address = strAddress;
}
#Override
public String toString() {
return "Participant [name=" + name + ", age=" + age + ", address=" + address + "]";
}
public boolean equals(Participant value){
boolean result;
if (name.equals(name) && age==value.age && address.equals(address))
result=true;
else
result=false;
return result;
}
}
outPut:
Enter loan details for customer 1...
Participant name: hddgg
Enter Address: 122
Enter age 12
Enter loan details for customer 2...
Participant name: ddjkjde
Enter Address: hdhhd23
Enter age 12
//Why I'm not getting right output
Name Adress Age
Participant [name=null, age=0, address=null]
Participant [name=null, age=0, address=null]
You are getting that output because of this method:
public static void displayDetail() {
String name=null;
String add = null;
int age=0;
System.out.println("Name\tAdress\tAge");
BC p=new BC(name,age,add);
for (int x = 0; x < mini.length; x++) {
//Participant p1=mini[x];
System.out.println(p.toString());
}
}
You are creating a BC with null for name and add and 0 for age. You are then printing it twice.