In: Computer Science
Advanced Inheritance Concepts (Exercise 7)
The Cullerton Park 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 eight participants each—one holds participants in the mini-marathon, and the other holds participants in the diving competition. Prompt the user for participant values. After the data values are entered, display values for participants who are in both events.
Participant.java
public class Participant
{
// private variables here
public Participant(String n, int a, String add)
{
// constructor code here
}
public String getName()
{
// method code here
}
public int getAge()
{
// method code here
}
public String getAddress()
{
// method code here
}
public String toString()
{
// method code here
}
public boolean equals(Participant p)
{
// method code here
}
}
TwoEventParticipant.java
import java.util.*;
public class TwoEventParticipants
{
public static void main(String[] args)
{
Participant marathoners[] = new Participant[8];
Participant divers[] = new Participant[8];
int i, j;
String name;
int age;
String address;
Scanner input = new Scanner(System.in);
System.out.println("Enter mini-marathon participants");
for(i = 0; i < marathoners.length; ++i)
{
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextInt();
input.nextLine();
System.out.print("Enter address: ");
address = input.nextLine();
marathoners[i] = new Participant(name, age, address);
}
System.out.println("\nEnter diving participants");
for(i = 0; i < divers.length; ++i)
{
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextInt();
input.nextLine();
System.out.print("Enter address: ");
address = input.nextLine();
divers[i] = new Participant(name, age, address);
}
System.out.println("\nParticipants who are in both events:");
for(i = 0; i < marathoners.length; ++i)
for(j = 0; j < divers.length; ++j)
if(marathoners[i].equals(divers[j]))
System.out.println(marathoners[i].toString());
}
}
Possible Answer:
Participants who are in both events:
Participant_2
10
Apartment No. 2
Participant_6
13
Apartment No. 6
Participant_7
13
Apartment No. 7
Participant.java:
public class Participant
{
// private variables here
private String name;
private int age;
private String address;
public Participant(String n, int a, String
add)
{
// constructor code here
this.name=n;
this.age=a;
this.address=add;
}
public String getName()
{
// method code here
return this.name;
}
public int getAge()
{
// method code here
return this.age;
}
public String getAddress()
{
// method code here
return this.address;
}
public String toString()
{
// method code here
return "Name:"+this.getName()+",
Age:"+this.getAge()+", Address:"+this.getAddress();
}
public boolean equals(Participant p)
{
// method code here
return
this.getName().equals(p.getName())&&this.getAddress().equals(p.getAddress())&&(this.getAge()==p.getAge());
}
}
TwoEventParticipants.java:
import java.util.*;
public class TwoEventParticipants
{
public static void main(String[] args)
{
Participant marathoners[] = new Participant[8];
Participant divers[] = new Participant[8];
int i, j;
String name;
int age;
String address;
Scanner input = new Scanner(System.in);
System.out.println("Enter mini-marathon participants");
for(i = 0; i < marathoners.length; ++i)
{
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextInt();
input.nextLine();
System.out.print("Enter address: ");
address = input.nextLine();
marathoners[i] = new Participant(name, age, address);
}
System.out.println("\nEnter diving participants");
for(i = 0; i < divers.length; ++i)
{
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextInt();
input.nextLine();
System.out.print("Enter address: ");
address = input.nextLine();
divers[i] = new Participant(name, age, address);
}
System.out.println("\nParticipants who are in both events:");
for(i = 0; i < marathoners.length; ++i)
for(j = 0; j < divers.length; ++j)
if(marathoners[i].equals(divers[j]))
System.out.println(marathoners[i].toString());
}
}
sample i/o:
Explanation:
The driver program is not touched here. As per the request Participant class was filled with private data members,a parameterised Constructor ,getter methods, toString method, equals method. A sample i/o is presented for further understanding.
**please upvote if you like the answer