AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Program to find how many goals Manchester United scored

Write a program to work out how many wins Manchester United had, how many games they drew, and how many Manchester United lost.

CodeGround team is a die hard fan of Manchester United(#MANU) they want you to create a Java application to complete following task on the English premier league.

Set up a string variable to hold the following results

String results = "Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1, Manchester United 3 Fulham 1, Liverpool 2 Manchester United 1, Swansea 2 Manchester United 4";


Extend the program to work out how many goals Manchester United scored and how many they conceded.

Suppose a win gains you 3 points, a draw 1 point, and a loss no points. Have your program work out how many points in the total Manchester United have acquired.

The output for the above will be

Wins 3

Draws 1

Defeats 1

Goals Scored 10

Goals Conceded 6

Total Points 10

Sample Input:

5

ManchesterUnited 1 Chelsea 0

Arsenal 1 ManchesterUnited 1

ManchesterUnited 3 Fulham 1

Liverpool 2 ManchesterUnited 1

Swansea 2 ManchesterUnited 4

Sample Output:

Wins 3

Draws 1

Defeats 1

Goals Scored 10

Goals Conceded 6

Total Points 10

Hint : In the Input one of the two teams will always be "ManchesterUnited". The first line of input is the number of games followed by n lines in above format team1 goal team2 goal. The name of a team will be a one word name.

corejava x 351
programming x 167
Posted On : 2018-01-24 11:39:59.0
profile Xda Coder - anyforum.in Xda Coder
1300
up-rate
5
down-rate

Answers


import java.util.Scanner;

public class GoalsCalculator {
public static void main(String[] args) {
String teamName = "ManchesterUnited";
int goalsScored=0;
int wins=0;
int draws=0;
int defeats=0;
int goalsConceded=0;
int points=0;
String[] temp=null;
int teamGoals=0;
int otherTeamGoals=0;
String results=null;
Scanner input=new Scanner(System.in);
int n=Integer.valueOf(input.nextLine());
for(int k=0;k<n;k++){
results = input.nextLine().trim().replaceAll(teamName, "team");
for(String s:results.split(",")){
temp=s.trim().split(" ");
if(temp[0].equalsIgnoreCase("team")){
teamGoals = Integer.valueOf(temp[1]);
otherTeamGoals = Integer.valueOf(temp[3]);
}else{
teamGoals = Integer.valueOf(temp[3]);
otherTeamGoals = Integer.valueOf(temp[1]);
}
goalsScored = goalsScored+teamGoals;
goalsConceded = goalsConceded+Math.abs(teamGoals-otherTeamGoals);
if(teamGoals<otherTeamGoals){
defeats++;
}else if(teamGoals>otherTeamGoals){
wins++;
points=points+3;
}else{
draws++;
points=points+1;
}
}
}
System.out.println("Wins "+wins);
System.out.println("Draws "+draws);
System.out.println("Defeats "+defeats);
System.out.println("Goals Scored "+goalsScored);
System.out.println("Goals Conceded "+goalsConceded);
System.out.println("Total Points "+points);
}
}

Posted On : 2018-01-27 00:33:30
Satisfied : 0 Yes  6 No
profile Garima Gupta - anyforum.in Garima Gupta
596129025102
Reply This Thread
up-rate
-1
down-rate
Comments
Not Satisfying
profile Xda Coder - anyforum.in Xda Coder
13  0  0
Posted On :2018-01-29 20:54:49.0
Leave a Comment
Hi,
What´s the problem in above code. Is it not working?
profile Garima Gupta - anyforum.in Garima Gupta
596  1290  25102
Posted On :2018-01-29 23:08:23.0
Leave a Comment
not passing test cases.. lacking some logic in question just passed 33% of overall test case
profile Xda Coder - anyforum.in Xda Coder
13  0  0
Posted On :2018-01-30 19:35:14.0
Leave a Comment
Hi,
Please share the test Data.
profile Garima Gupta - anyforum.in Garima Gupta
596  1290  25102
Posted On :2018-01-30 22:35:40.0
Leave a Comment
can´t share it it´s on the server can you please create another version of this code?
profile Xda Coder - anyforum.in Xda Coder
13  0  0
Posted On :2018-01-31 19:44:23.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in