Write a Java program with concepts used through chapter 11 (Introduction to Java
ID: 3774988 • Letter: W
Question
Write a Java program with concepts used through chapter 11 (Introduction to Java Programming) and meets the following requirements:- 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 and if invalid make the user enter a new age. 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 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.
Explanation / Answer
Hi,
Please see below the java files. Please comment for any feedbacks/queries.
Thanks,
Anita
Contestants.java
public class Contestants {
private String name;
private Integer age;
private String streetAddress;
public Contestants(){
}
public Contestants(String name, Integer age, String streetAddress){
this.name=name;
this.age=age;
this.streetAddress=streetAddress;
}
//Checks for validage limit
public boolean isValidAge(){
if(this.age<14 || this.age>18){
return false;
}
else{
return true;
}
}
public String toString(){
String retStr="";
retStr ="Name : "+this.getName()
+", Age : "+this.getAge().toString()
+", Address : "+ this.getStreetAddress();
return retStr;
}
public boolean equals(Contestants contestant){
if(this.getName().equalsIgnoreCase(contestant.getName())
&& this.getAge() == contestant.getAge()
&& this.getStreetAddress().equalsIgnoreCase(contestant.getStreetAddress())){
return true;
}
else{
return false;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
}
Event.java
public class Event extends Contestants {
public Event(String name, Integer age, String streetAddress, String event){
super(name,age,streetAddress);
this.eventType=event;
}
public Event(){
}
private String eventType;
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
}
OlympicApp.java
import java.util.Scanner;
public class OlympicApp {
public static void main(String [] args){
int noOfContestants =8;
Contestants[] dashArr= new Contestants[noOfContestants];
Contestants[] jumpArr= new Contestants[noOfContestants];
int dCounter =0;
int jCounter =0;
int counter=0;
while(counter<16){
Scanner scanner= new Scanner(System.in);
System.out.println("Enter the Event- D for 100meter Dash & H for High jump:");
String eventType = scanner.nextLine();
System.out.println("Enter Contestant details: ");
System.out.println("Name :");
String name = scanner.nextLine();
System.out.println("Age :");
String age = scanner.nextLine();
System.out.println("Street Address :");
String address = scanner.nextLine();
if(eventType.equalsIgnoreCase("D")&& dCounter<8){
Event event = new Event(name,new Integer(age),address,"100 Meter Dash");
if(!event.isValidAge()){
System.out.println("Invalid age");
continue;
}
dashArr[dCounter] = event;
dCounter++;
counter++;
}
else if(eventType.equalsIgnoreCase("H") && jCounter<8){
Event event = new Event(name,new Integer(age),address,"High Jump");
if(!event.isValidAge()){
System.out.println("Invalid age");
continue;
}
jumpArr[jCounter] = event;
jCounter++;
counter++;
}
else{
System.out.println("Invalid Event!!!");
continue;
}
}
System.out.println("Contestants for 100M dash");
printArray(dashArr);
System.out.println("Contestants for High Jump");
printArray(jumpArr);
}
public static void printArray(Contestants[] currArr){
for (Contestants contestants : currArr) {
String details= contestants.toString();
System.out.println(details);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.