Question

In: Computer Science

An international conference for Java programmers was held last month. The event organizers wish to distribute...

An international conference for Java programmers was held last month. The event organizers wish to distribute a series of prizes by post to the event attendees. For this they have decided that the winners will be those pairs of people whose ages add up to a given S number. During the event, all the attendees registered their full name and age in a computer system. For this reason, the organizers have a text-type file, where the names and ages of all the attendees are found, ordered in decreasing order with respect to ages. It is also known that the ages of all the attendees are different from each other (there are no repeated ages).

Example 1: Suppose we want to find the pairs of participants whose age adds up to S = 51, and we have the following series of participants:

Carlos Perez, 40

Karla Ortiz, 38

Jose Lopez, 35

Claudia Ruiz, 13

Luis Diaz, 11

So the output would be:

Carlos Perez (40), Luis Diaz (11)

Karla Ortiz (38), Claudia Ruíz (13)

Total Winners: 2

Example 2: Suppose we want to find the pairs of participants whose age adds up to S = 10, and we have the following series of participants:

Carlos Perez, 40

Karla Ortiz, 38

Jose Lopez, 35

Claudia Ruiz, 13

Luis Diaz, 11

So the output would be:

There are no winners.

Develop a Java program that allows organizers to find the winning pairs. Said program must comply with the following functionality: It contains a main class called Winner.java

It contains a class Participant.java that is used to represent objects in memory with the data of the participants (name and age).

The main method, of the main Winner.java class, receives the file with the data of the conference attendees via command line redirection. For example, java Winner <attendees01.csv. This file has the following format: The first line contains an integer that represents the sum S of ages sought. The following lines each contain the name and age of a participant, separated by a comma.

There is a DataReading method that receives as a parameter an object of the Scanner class that allows receiving data from standard input (System.in) and loading it into a data structure that allows solving the problem.

There is a search for Winners method that receives as a parameter an integer that represents the sum S of ages searched and prints on the screen: If there are winners, the data (name and age) of the pairs of people (one pair per line) using the following format: Name1 Surname1 (Age1), Name2 Surname2 (Age2) The total X of winning pairs using the format: "Total winners: X", or the message "There are no winners".

Solutions

Expert Solution

//IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, ELSE COMMENT TO CLEAR DOUBTS

CODE:

//Winner.java

import java.util.*;

import java.io.*;  

import java.util.Scanner;

class Participant{

    String name;

    int age;

    public Participant(String name,int age){

        this.name = name;

        this.age = age;

    }

    public String getName(){

        return name;

    }

    public int getAge(){

        return age;

    }

}

public class Winner{

    public static int getScanAge(Scanner s){

        System.out.print("\nEnter the age: ");

        int x = s.nextInt();

        return x;

    }

    public static void searchForWinners(int age,ArrayList<Participant> p){

        int count = 0;

        System.out.println("");

        for(int i = 0; i < p.size() - 1; i++){

            for(int j = i + 1; j < p.size(); j++){

                if((p.get(i).getAge() + p.get(j).getAge()) == age){

                    System.out.println(p.get(i).getName()+" ("+p.get(i).getAge()+") , "+

                    p.get(j).getName()+" ("+p.get(j).getAge()+")");

                    count += 1;

                }

            }

        }

        if(count == 0){

            System.out.println("There are no winners"+"\n");

        }

        else{

            System.out.println("\nTotal WINNERS: "+count+"\n");

        }

    }

    public static void main(String[] args){

        ArrayList<Participant> p = new ArrayList<Participant>();

        try {

        File file = new File(args[0]);

        FileReader fr = new FileReader(file);

        BufferedReader br = new BufferedReader(fr);

        String line = "";

        String[] tempArr;

        Participant tempP = null;

        line = br.readLine();

        while((line = br.readLine()) != null) {

           tempArr = line.split(",");

           tempP = new Participant(tempArr[0],Integer.parseInt(tempArr[1]));

           p.add(tempP);

        }

        br.close();

        }

        catch (IOException e)   

        {  

            e.printStackTrace();  

        }

        Scanner s = new Scanner(System.in);

        int age = getScanAge(s);

        searchForWinners(age,p);

    }

}

CODE PREVIEW:


Related Solutions

Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month...
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month and year entered as input parameter. Remember to invoke the isLeapYear method when appropriate. If the month entered as input parameter is 2 (February) and the year passed as input parameter is a leap year this method should return 29. private boolean isDateValid (int m, int d, int y) – return true if day, month, and year entered as input parameters form a valid...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT