Question

In: Computer Science

Advanced Inheritance Concepts (Exercise 7) The Cullerton Park District holds a mini-Olympics each summer. Create a...

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

Solutions

Expert Solution

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


Related Solutions

7. What are the different forms of Inheritance? Give an example for each.
7. What are the different forms of Inheritance? Give an example for each.
Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers...
Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers of a provided array (arr), accounting for non-integer values in arr. The output should return an integer. Notes Remember your data types? If the element is an integer (8), it should be added. If the element is a string with a number value ("8"), it should be added. If the element is not a number, or if it is a string with a non-number...
Exercise #7 Explain the concepts of Rival, NonRival, Exclusive and NonExclusive in regards to Economics. What...
Exercise #7 Explain the concepts of Rival, NonRival, Exclusive and NonExclusive in regards to Economics. What is the nonexclusive aspect of a public good? Please use the Matrix. Show the relationships between rival goods, nonrival goods, exclusive goods and nonexclusive goods with private goods, public goods, regulated monopoly, and tragedy of the commons. (8 points)   Give examples from common social practice or class to illustrate your answer (you must use the concept and definition of the Coase Solution and Transaction...
2. Discuss the McKinsey's 7 S's and how each of these strategy concepts/dimensions plays into the...
2. Discuss the McKinsey's 7 S's and how each of these strategy concepts/dimensions plays into the formation and operation of a real estate brokerage. Provide detailed examples of how different brokerages may make different choices in each of these areas and how those choices may affect their company performance. Write a minimum of 500 words.
Exercise 7: Experimental Variables Determine the variables tested in the each of the following experiments. If...
Exercise 7: Experimental Variables Determine the variables tested in the each of the following experiments. If applicable, determine and identify any positive or negative controls. Observations 1. A study is being done to test the effects of habitat space on the size of fish populations. Different sized aquariums are set up with six goldfish in each one. Over a period of six months, the fish are fed the same type and amount of food. The aquariums are equally maintained and...
Exercise 5-11 Missing Data; Basic CVP Concepts [LO5-1, LO5-9] Fill in the missing amounts in each...
Exercise 5-11 Missing Data; Basic CVP Concepts [LO5-1, LO5-9] Fill in the missing amounts in each of the eight case situations below. Each case is independent of the others. (Hint: One way to find the missing amounts would be to prepare a contribution format income statement for each case, enter the known data, and then compute the missing items.) Required: a. Assume that only one product is being sold in each of the four following case situations: b. Assume that...
Exercise 5-11 Missing Data; Basic CVP Concepts [LO5-1, LO5-9] Fill in the missing amounts in each...
Exercise 5-11 Missing Data; Basic CVP Concepts [LO5-1, LO5-9] Fill in the missing amounts in each of the eight case situations below. Each case is independent of the others. (Hint: One way to find the missing amounts would be to prepare a contribution format income statement for each case, enter the known data, and then compute the missing items.) Required: a. Assume that only one product is being sold in each of the four following case situations: b. Assume that...
Exercise 7-10 (Video) Stahl Inc. produces three separate products from a common process costing $100,900. Each...
Exercise 7-10 (Video) Stahl Inc. produces three separate products from a common process costing $100,900. Each of the products can be sold at the split-off point or can be processed further and then sold for a higher price. Shown below are cost and selling price data for a recent period. Sales Value at Split-Off Point Cost to Process Further Sales Value after Further Processing Product 10 $59,300 $100,400 $189,600 Product 12 15,900 29,100 34,800 Product 14 54,500 149,900 214,400 Determine...
Exercise 7-10 (Video) Stahl Inc. produces three separate products from a common process costing $100,900. Each...
Exercise 7-10 (Video) Stahl Inc. produces three separate products from a common process costing $100,900. Each of the products can be sold at the split-off point or can be processed further and then sold for a higher price. Shown below are cost and selling price data for a recent period. Sales Value at Split-Off Point Cost to Process Further Sales Value after Further Processing Product 10 $59,300 $100,400 $189,600 Product 12 15,900 29,100 34,800 Product 14 54,500 149,900 214,400 Determine...
Exercise 5-10 (Static) Future and present value [LO5-3, 5-7, 5-8] Answer each of the following independent...
Exercise 5-10 (Static) Future and present value [LO5-3, 5-7, 5-8] Answer each of the following independent questions. Alex Meir recently won a lottery and has the option of receiving one of the following three prizes: (1) $64,000 cash immediately, (2) $20,000 cash immediately and a six-period annuity of $8,000 beginning one year from today, or (3) a six-period annuity of $13,000 beginning one year from today. (FV of $1, PV of $1, FVA of $1, PVA of $1, FVAD of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT