Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

he Cullerton Park District holds a mini-Olympics each summer. Create a class nam

ID: 3719521 • Letter: H

Question

he 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.

Note: This is in Java

Explanation / Answer

Participant.java

public class Participant {

//Declaring instance variables

private String name;

private int age;

private String address;

//Parameterized constructor

public Participant(String name, int age, String address) {

this.name = name;

this.age = age;

this.address = address;

}

// getters and setters

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 String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

//This method compares two Participant Objects

public boolean equals(Participant other) {

if (address == null) {

if (other.address != null)

return false;

} else if (!address.equals(other.address))

return false;

if (age != other.age)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

return "Participant [name=" + name + ", age=" + age + ", address="

+ address + "]";

}

}

______________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

//Declaring variables

String name,address;

int age;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Creating Participants Objects array

Participant mini_marathon[]=new Participant[8];

Participant diving_competition[]=new Participant[8];

//Getting the inputs entered by the user

System.out.println("__Enter the Mini-Marathon Participants Indo__");

for(int i=0;i<mini_marathon.length;i++)

{

System.out.print("Enter name of Participant#"+(i+1)+":");

name=sc.nextLine();

System.out.print("Enter age :");

age=sc.nextInt();

sc.nextLine();

System.out.print("Enter address :");

address=sc.nextLine();

mini_marathon[i]=new Participant(name, age, address);

}

//Getting the inputs entered by the user

System.out.println("__Enter the Diving Competition Participants Indo__");

for(int i=0;i<diving_competition.length;i++)

{

System.out.print("Enter name of Participant#"+(i+1)+":");

name=sc.nextLine();

System.out.print("Enter age :");

age=sc.nextInt();

sc.nextLine();

System.out.print("Enter address :");

address=sc.nextLine();

diving_competition[i]=new Participant(name, age, address);

}

  

//Comparing the Two Participants class objects

System.out.println(" __Displaying the Participants who participated in both competitions__");

for(int i=0;i<mini_marathon.length;i++)

{

for(int j=0;j<diving_competition.length;j++)

{

if(mini_marathon[i].equals(diving_competition[j]))

System.out.println(mini_marathon[i]);

}

}

}

}

_________________

output:

__Enter the Mini-Marathon Participants Indo__
Enter name of Participant#1:James
Enter age :23
Enter address :America
Enter name of Participant#2:Sachin
Enter age :30
Enter address :India
Enter name of Participant#3:Bob
Enter age :25
Enter address :Canada
Enter name of Participant#4:Billy
Enter age :27
Enter address :America
Enter name of Participant#5:Rahul
Enter age :26
Enter address :India
Enter name of Participant#6:Peter
Enter age :26
Enter address :China
Enter name of Participant#7:Simkox
Enter age :22
Enter address :South Africa
Enter name of Participant#8:Pattinson
Enter age :26
Enter address :Australia
__Enter the Diving Competition Participants Indo__
Enter name of Participant#1:Sachin
Enter age :30
Enter address :India
Enter name of Participant#2:Peter
Enter age :26
Enter address :China
Enter name of Participant#3:Simkox
Enter age :22
Enter address :South Africa
Enter name of Participant#4:Pointing
Enter age :26
Enter address :Australia
Enter name of Participant#5:Mark Waugh
Enter age :25
Enter address :Australia
Enter name of Participant#6:Clark
Enter age :23
Enter address :America
Enter name of Participant#7:Billy
Enter age :26
Enter address :Canada
Enter name of Participant#8:Bob
Enter age :25
Enter address :Canada

__Displaying the Participants who participated in both competitions__
Participant [name=Sachin, age=30, address=India]
Participant [name=Bob, age=25, address=Canada]
Participant [name=Peter, age=26, address=China]
Participant [name=Simkox, age=22, address=South Africa]

____________Thank You