The following table shows the results for 16 football teams after six games in Malaysia Cup 2014. Whichever team finishes with the most points at the end of these games is the champion. Teams are awarded three points for a victory (V), one for a draw (D) and none for a loss (L).
Write a program to calculate and output the points for each team. Also find which team is the champion.
The teams and the number of games, victory (V), draw (D) and loss (L).
The points earn by each team and the champion (the team with highest point).
Sample Input
FeldaUnited 4 1 1
Pahang 3 1 2
LionsXII 1 2 3
JDTII 1 2 3
Kedah 3 1 2
Terengganu 2 3 1
Perak 2 2 2
Sarawak 1 2 3
JohorDT 5 1 0
Kelantan 3 0 3
ATM 1 2 3
PulauPinang 1 1 4
PDRM 4 2 0
Selangor 3 1 2
SimeDarby 2 0 4
T-Team 1 0 5
Sample output
FeldaUnited 13
Pahang 10
LionsXII 5
JDTII 5
Kedah 10
Terengganu 9
Perak 8
Sarawak 5
JohorDT 16
Kelantan 9
ATM 5
PulauPinang 4
PDRM 14
Selangor 10
SimeDarby 6
T-Team 3
Champion JohorDT
If you look carefully at the table, you know that you need to use 2D array with size 16 x 3.
Step 1. Start with simple java program structure
Let's call the class "MalaysiaCup" and add empty main function. It's completely valid program you can compile and run, but it doesn't print anything to the console yet.
public class MalaysiaCup{
public static void main(String[] args) {
}
}
Compile and run your program.
Step 2. Declare the first array
In this program, we will use two different arrays because the team names are string type while the score are integers.
First, let we declare for the name of the team. There are 16 teams in the given table. We can use one dimensional array to declare the teams. After declare the array, try print the array.
public class MalaysiaCup{
public static void main(String[] args) {
// declare the team as 1 dimensional array
String[] team = {"Felda United",
"Pahang",
"LionsXII",
"JDT11",
"Kedah",
"Terengganu",
"Perak",
"Sarawak",
"JohorDT",
"Kelantan",
"ATM",
"Pulau Pinang",
"PDRM",
"Selangor",
"SimeDarby",
"T-Team"};
// print the array to test and delete it if the output is correct
for (int row=0; row< team.length; row++){
System.out.println(team[row] + " ");
}
}
}
Compile and run your program.
Step 3. Declare the second array
Lets name our second array as board, type of integers because it will store integers numbers. Our array size is 16 x 3.
public class MalaysiaCup{
public static void main(String[] args) {
// declare the team as 1 dimensional array
String[] team = {"Felda United",
"Pahang",
"LionsXII",
"JDT11",
"Kedah",
"Terengganu",
"Perak",
"Sarawak",
"JohorDT",
"Kelantan",
"ATM",
"Pulau Pinang",
"PDRM",
"Selangor",
"SimeDarby",
"T-Team"};
// declare the array 2 dimensional
int[][] board = new int[16][3];
}
}
Compile and run your program.
Step 4. Prompt the user to input the data
import java.util.Scanner;
public class MalaysiaCup{
public static void main(String[] args) {
// declare the team as 1 dimensional array
String[] team = {"Felda United",
"Pahang",
"LionsXII",
"JDT11",
"Kedah",
"Terengganu",
"Perak",
"Sarawak",
"JohorDT",
"Kelantan",
"ATM",
"Pulau Pinang",
"PDRM",
"Selangor",
"SimeDarby",
"T-Team"};
// declare the array 2 dimensional
int[][] board = new int[16][3];
// declare the scanner object
Scanner input = new Scanner(System.in);
// enter the score
for (int row=0; row< board.length; row++){
System.out.print(team[row] + " ");
for (int col=0; col<board[row].length; col++) {
board[row][col] = input.nextInt();
}
}
// print the array to test and delete it if the output is correct
for (int row=0; row< board.length; row++){
System.out.print(team[row] + " ");
for (int col=0; col<board[row].length; col++) {
System.out.print(board[row][col] + " ");
}
System.out.println();
}
}
}
Compile and run your program.
Step 5. Calculate the points for each team and print the result
Step 6. find the champion and print the output.