Collecting data from an array

Asked

Viewed 120 times

0

I’m having a workout for college but I’m lost in my logic and how to solve it. I have to collect the game results and set the score for each result. the winner takes 3 points, a draw guarantees 1 point and the defeat of no point.

I know I don’t have much yet but I wrote it here:

Note: The teacher asks you to use only two-dimensional string arrays!

(Define a two-dimensional array of Strings Representing the Results of Matches. and a four-element array of ints Representing Scores of Teams of Germany, Ireland,Poland and Scotland (in this order). The program Calculates total score for each team (3 points for a win, 1 for a draw, 0 for a defeat), puts them into the array and then prints it)

Obs2: this English only because I study and live in Poland and the course and in English.

public class Jogos {

  public static void main(String[] args) {

    String [][] resultados = new String[12][4];

    resultados [0][0]="Alemanha";
    resultados [0][1]=2;
    resultados [0][2]="Escocia";
    resultados [0][3]=1;

    resultados [1][0]="Polonia";
    resultados [1][1]=2;
    resultados [1][2]="Alemanha";
    resultados [1][3]=0;

    resultados [2][0]="Alemanha";
    resultados [2][1]=1;
    resultados [2][2]="Irlanda";
    resultados [2][3]=1;

    resultados [3][0]="Polonia";
    resultados [3][1]=2;
    resultados [3][2]="Escocia";
    resultados [3][3]=2;

    resultados [4][0]="Escocia";
    resultados [4][1]=1;
    resultados [4][2]="Irlanda";
    resultados [4][3]=0;

    resultados [5][0]="Irlanda";
    resultados [5][1]=1;
    resultados [5][2]="Polonia";
    resultados [5][3]=1;

    resultados [6][0]="Irlanda";
    resultados [6][1]=1;
    resultados [6][2]="Escocia";
    resultados [6][3]=1;

    resultados [7][0]="Alemanha";
    resultados [7][1]=3;
    resultados [7][2]="Polonia";
    resultados [7][3]=1;

    resultados [8][0]="Escocia";
    resultados [8][1]=2;
    resultados [8][2]="Alemanha";
    resultados [8][3]=3;

    resultados [9][0]="Irlanda";
    resultados [9][1]=1;
    resultados [9][2]="Alemanha";
    resultados [9][3]=0;

    resultados [10][0]="Escocia";
    resultados [10][1]=2;
    resultados [10][2]="Polonia";
    resultados [10][3]=2;

    resultados [11][0]="Polonia";
    resultados [11][1]=2;
    resultados [11][2]="Irlanda";
    resultados [11][3]=1;

    for (int i=0; i<resultados.length; i++){
      for (int j=0; j<resultados[i].length; j++){
        System.out.print(resultados[i][j] + " - ");
      }
      System.out.println();
    }
  }
}
  • This modeling is forced by exercise ? It is that it is not good at all.

  • he asks to define a two-dimensional matrix of String that represents the results of combinations in a tournament, listing the scores of each team in a list.

  • If it’s a two-dimensional matrix of String is not what you have in the code at this time. It has how to put the statement in the question too ? Or at least the part that matters it?

  • Define a two-dimensional array of Strings Representing the Results of Matches. and a four-element array of ints Representing Scores of Teams of Germany, Ireland, Poland and Scotland (in this order). The program Calculates total score for each team (3 points for a win, 1 for a draw, 0 for a defeat), puts them into the array and then prints it.

2 answers

1


If you can only use two-dimensional arrays of Strings your solution is on the right path, but missing the calculations of the points. If it is all a String then it will be necessary to interpret the numerical value of the goals with Integer.parseInt to know the result of each game.

Also missing is an array for the scores as stated, which refers to the teams: Germany, Ireland, Poland and Scotland in this exact order.

A possible solution would be:

public static int obterPosicao(String equipe){
    switch (equipe){
    case "Alemanha": return 0;
    case "Irlanda": return 1;
    case "Polonia": return 2;
    case "Escocia": return 3;
    }
    return -1;
}

public static void main(String[] args) {
    String[][] resultados = {
            {"Alemanha","2","Escocia","1"},
            {"Polonia","2","Alemanha","0"},
            {"Alemanha","1","Irlanda","1"},
            {"Polonia","2","Escocia","2"},
            {"Escocia","1","Irlanda","0"},
            {"Irlanda","1","Polonia","1"},
            {"Irlanda","1","Escocia","1"},
            {"Alemanha","3","Polonia","1"},
            {"Escocia","2","Alemanha","3"},
            {"Irlanda","1","Alemanha","0"},
            {"Escocia","2","Polonia","2"},
            {"Polonia","2","Irlanda","1"}       
    };

    int[] pontuacoes = new int[4];
    String[] nomes = {"Alemanha","Irlanda","Polonia","Escocia"};

    for (int i = 0; i < resultados.length; ++i){
        int golos1 = Integer.parseInt(resultados[i][1]);//posicao 1 é golos de equipe1
        int golos2 = Integer.parseInt(resultados[i][3]);//posicao 3 é golos de equipe2

        int score1 = 1, score2 = 1; //assume empate por defeito     
        if (golos1 > golos2){ //ajusta caso não seja
            score1 = 3;
            score2 = 0;
        }
        else if (golos1 < golos2){
            score1 = 0;
            score2 = 3;
        }

        //obtem a posição da equipa no vetor pontuacoes com base no nome e ajusta score
        pontuacoes[obterPosicao(resultados[i][0])] += score1; 
        pontuacoes[obterPosicao(resultados[i][2])] += score2;
    }

    for (int i = 0; i < pontuacoes.length; ++i){
        System.out.println("Pontuação para " + nomes[i] + " é " + pontuacoes[i]);
    }
}

Example in Ideone

I have chosen to simplify a few things, most notably the creation of results. I also created an array of team names in the scores to be simple to write the final results on the screen.

Certainly it would give to elaborate enough in the solution using Hashmaps for example, but I suspect it was a solution of this nature that your teacher was waiting for.

  • Thank you Isac, I changed a few things to suit my reality here and put it all in English! this working perfectly!

0

Suggestion:

Set objects and their properties first.

The team:

package stackoverflow;

import java.util.Objects;

public class Team {

private String name;

public Team(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public int hashCode() {
    int hash = 7;
    hash = 17 * hash + Objects.hashCode(this.name);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Team other = (Team) obj;
    if (!Objects.equals(this.name, other.name)) {
        return false;
    }
    return true;
}

}

The game:

package stackoverflow;

import java.util.Objects;

public class Game {

    private Team home;
    private Team visitor;

    private Integer homeScore;
    private Integer visitorScore;

    public static Game getGameInstance(Team home, Integer homeScore, Team visitor, Integer visitorScore) {
        Game game = new Game();
        game.setHome(home);
        game.setVisitor(visitor);
        game.setHomeScore(homeScore);
        game.setVisitorScore(visitorScore);
        return game;
    }

    public Team getHome() {
        return home;
    }

    public Integer getHomePoints(){
        return 
                homeScore.equals(visitorScore) ? Result.DRAW :
                (homeScore > visitorScore ? Result.WIN : Result.LOSS)  ;
    }

    public Integer getVisitorPoints(){
        return 
                homeScore.equals(visitorScore) ? Result.DRAW :
                (homeScore < visitorScore ? Result.WIN : Result.LOSS)  ;
    }


    public void setHome(Team home) {
        this.home = home;
    }

    public Team getVisitor() {
        return visitor;
    }

    public void setVisitor(Team visitor) {
        this.visitor = visitor;
    }


    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + Objects.hashCode(this.home);
        hash = 53 * hash + Objects.hashCode(this.visitor);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Game other = (Game) obj;
        if (!Objects.equals(this.home, other.home)) {
            return false;
        }
        if (!Objects.equals(this.visitor, other.visitor)) {
            return false;
        }
        return true;
    }

    public Integer getHomeScore() {
        return homeScore;
    }

    public void setHomeScore(Integer homeScore) {
        this.homeScore = homeScore;
    }

    public Integer getVisitorScore() {
        return visitorScore;
    }

    public void setVisitorScore(Integer visitorScore) {
        this.visitorScore = visitorScore;
    }



}

The scores:

package stackoverflow;

public class Result {
    public static final Integer DRAW = 1;
    public static final Integer WIN = 3;
    public static final Integer LOSS = 0;

}

Then put the results and calculate:

package stackoverflow;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


public class CalculaResultado {

    public static void main(String[] args) {

        Set<Game> gameSet = new HashSet<>();

        final Team alemanha = new Team("Alemanha");
        final Team escocia = new Team("Escócia");
        final Team polonia = new Team("Polônia");
        final Team irlanda = new Team("Irlanda");

        Game game1 = Game.getGameInstance(alemanha, 2, escocia, 1);
        Game game2 = Game.getGameInstance(polonia, 2, alemanha, 0);
        Game game3 = Game.getGameInstance(alemanha, 1, irlanda, 1);
        Game game4 = Game.getGameInstance(polonia, 2, escocia, 2);
        Game game5 = Game.getGameInstance(escocia, 1, irlanda, 0);
        Game game6 = Game.getGameInstance(irlanda, 1, polonia, 1);
        Game game7 = Game.getGameInstance(irlanda, 1, escocia, 1);
        Game game8 = Game.getGameInstance(alemanha, 3, polonia, 1 );
        Game game9 = Game.getGameInstance(escocia, 2, alemanha, 3);
        Game game10 = Game.getGameInstance(irlanda, 1, alemanha, 0);
        Game game11 = Game.getGameInstance(escocia, 2, polonia, 2);
        Game game12 = Game.getGameInstance(polonia, 2, irlanda, 1);


        gameSet.add(game1);
        gameSet.add(game2);
        gameSet.add(game3);
        gameSet.add(game4);
        gameSet.add(game5);
        gameSet.add(game6);
        gameSet.add(game7);
        gameSet.add(game8);
        gameSet.add(game9);
        gameSet.add(game10);
        gameSet.add(game11);
        gameSet.add(game12);

        Map<Team, Integer> points = new HashMap<>();

        for (Game game : gameSet) {

            if ( !points.containsKey( game.getHome() ) ){
                points.put(game.getHome(), 0);
            }

            if ( !points.containsKey(game.getVisitor())){
                points.put(game.getVisitor(), 0);
            }

            points.put(game.getHome(), points.get(game.getHome()) + game.getHomePoints()  );
            points.put(game.getVisitor(), points.get(game.getVisitor()) + game.getVisitorPoints()  );

        }

        for (Map.Entry<Team, Integer> teamResult : points.entrySet()) {
            System.out.println( String.format("Pontuação %s : %d", teamResult.getKey().getName(), teamResult.getValue() ) );
        }
    }



}

RESULT:

Poland score : 9
Ireland Score : 6
Scotland Score : 6
Score Germany : 10

  • Good answer, but it is not very nice to ask in the reply for it to be accepted. What is recommended is to let the author decide ;)

  • Hello Carlos, thank you so much for the help! but this way of solution is not what the teacher wants, so I can not use! I had already seen about hashmap, but I can not use in this case! because the teacher asks exactly this.... Defines a two-dimensional array of Strings Representing the Results of Matches. and a four-element array of ints Representing Scores of Teams of Germany, Ireland,Poland and Scotland (in this order). The program Calculates total score for each team (3 points for a win, 1 for a draw, 0 for a defeat), puts them into the array and then prints it

  • @Articuno , you’re right. I edited.

  • @igorcamargo , I could have done so, if I had detailed the question better. There were no restrictions. So if you want to get the answer, consider editing your question.

  • @Carloscariello Sorry the mistake was all mine! I am relatively new to the forum and I am very stressed to be back to being 36 years old (matematica is killing me)! I’m sorry I made you write a solution that can’t be used!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.