Java Programming The Orlando Park District holds a miniOlymp
Java Programming:
The Orlando Park District holds a mini-Olympics each summer.
Create a class named Contestants with fields for a name, age, and street address.
Include code to check that the age of the contestant cannot be over 18 and under 14
If invalid make user enter new age.
Include a constructor that assigns parameter values to each field and a toString() method that returns a String containg all values.
Include an equals() method that determines two Contestants are equal if they have the same values in all three fields.
Create a child class called Event that has a field for eventType and a message containing the type of event.
Create an application with two arrays of at least eight Contestants each
one holds Contestants in the 100 meter dash, and the other holds Contestants in the high-jump competition.
Prompt the user for contestant values, only allow the two types of events.
After the data values are entered, display all values for Participants who are in both events.
Save files as Contestant.java, Event.java and TwoEventParticipants.java
Solution
//TwoEventParticipants.java
import java.util.Scanner;
public class TwoEventParticipants {
public static void main(String[] args) {
/*create two arrays of at least eight Contestants each
one holds Contestants in the 100 meter dash, and the other holds Contestants in the high-jump competition.*/
Event dashContestants[]=new Event[8];
Event highJumpContestants[]=new Event[8];
Scanner input=new Scanner(System.in);
//Prompt the user for contestant values, only allow the two types of events.
System.out.println(\"Enter dash Contestants: \");
for(int i=0;i<8;i++)
{
System.out.println(\"Contestant: \"+(i+1));
System.out.println(\"Name: \");
String Name=input.next();
System.out.println(\"Age: \");
int Age=input.nextInt();
input.nextLine();
System.out.println(\"Street Address \");
String Address=input.nextLine();
System.out.println(\"Event type: \"); // 100 meters dash
String type=input.nextLine();
System.out.println(\"message: \");
String msg=input.nextLine();
dashContestants[i]=new Event(type,msg,Name, Age, Address);
}
System.out.println(\"Enter high jump Contestants: \");
for(int i=0;i<8;i++)
{
System.out.println(\"Contestant: \"+(i+1));
System.out.println(\"Name: \");
String Name=input.next();
System.out.println(\"Age: \");
int Age=input.nextInt();
input.nextLine();
System.out.println(\"Street Address \");
String Address=input.nextLine();
System.out.println(\"Event type: \"); // high jump
String type=input.nextLine();
System.out.println(\"message: \");
String msg=input.nextLine();
highJumpContestants[i]=new Event(type,msg,Name, Age, Address);
}
//display all values for Participants who are in both events
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(dashContestants[i].equals(highJumpContestants[j]))
{
System.out.println(dashContestants[i].toString());
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
//Contestant.java
import java.util.Scanner;
//Contestants with fields for a name, age, and street address.
public class Contestant
{
String name;
int age;
String streetAddress;
//constructor that assigns parameter values to each field
public Contestant(String Name, int Age,String Address)
{
name=Name;
setAge(Age);
streetAddress=Address;
}
//function to check and set age
public void setAge(int Age)
{
/*
check that the age of the contestant cannot be over 18 and under 14
If invalid make user enter new age.
*/
while(Age>18 && Age<14)
{
System.out.println(\"Invalid age. Enter new age:\");
Scanner input=new Scanner(System.in);
Age=input.nextInt();
}
age=Age;
}
//equals() method that determines two Contestants are equal if they have the same values in all three fields.
public boolean equals(Contestant C)
{
if(name.equalsIgnoreCase(C.name)&& streetAddress.equalsIgnoreCase(C.streetAddress) && age==C.age)
return true;
else
return false;
}
//toString() method that returns a String containg all values.
public String toString()
{
return name+\" \"+age+\" \"+streetAddress;
}
}
/////////////////////////////////////////////////////
//Event.java
import java.util.Scanner;
// child class called Event that has a field for eventType and a message containing the type of event.
public class Event extends Contestant
{
String eventType;
String message;
public Event(String type, String msg,String Name, int Age,String Address)
{
super(Name,Age,Address);
while(!type.equalsIgnoreCase(\"100 meters dash\") && !type.equalsIgnoreCase(\"high jump\") )
{
System.out.println(\"Invalid event type. Enter proper type:\");
Scanner input=new Scanner(System.in);
type=input.next();
}
eventType=type;
message=msg;
}
public String getEventType()
{
return eventType;
}
public String getMessage()
{
return message;
}
}
///////////////////////////////////////////////////////////


