In: Computer Science
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants.
2. Create a method to print the array.
3. Create a method to find the largest element in the array
4. Create a method to find the smallest element in the array
5. Create a method to find the sum of all elements in the array
import java.util.*;
import java.io.*;
public class SoccerDemo{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
try{
ArrayList<Player> players = new ArrayList<Player>();
Scanner fin = new Scanner(new File("Players.txt"));
int id, age;
String name;
double height, weight;
int i=0;
while(fin.hasNextLine()){
if(i==0){
i++;
continue;
}
id = fin.nextInt();
name = fin.next();
age = fin.nextInt();
height = fin.nextDouble();
weight = fin.nextDouble();
players.add(new Player(id, name, age, height, weight));
}
System.out.println("Successfully "+players.size()+" added to the players list");
System.out.println("=======================================");
System.out.println("Welcome to the Soccer Player Managemen and Visualization System");
System.out.println("1. Add details of a player");
System.out.println("2. Search details for a player");
System.out.println("3. Visualie student details");
System.out.println("4. Exit");
System.out.println("=======================================");
int option;
System.out.println("Please select an option from the above:");
option = in.nextInt();
switch(option){
case 1:
add_player(players);
break;
case 2:
search(players);
break;
case 3:
visulaize(players);
break;
case 4:
System.out.println("Good Bye!");
break;
default:
System.out.println("Invalid Option!");
}
}catch(FileNotFoundException e){
System.out.println("Unable to open file");
System.exit(0);
}
}
public static void add_player(ArrayList<Player> players){
Scanner in = new Scanner(System.in);
int id, age;
String name;
double height, weight;
System.out.println("Enter Player ID: ");
id = in.nextInt();
System.out.println("Enter Player Name: ");
name = in.next();
System.out.println("Enter Player Age: ");
age = in.nextInt();
System.out.println("Enter Player Height: ");
height = in.nextDouble();
System.out.println("Enter Player Weight: ");
weight = in.nextDouble();
players.add(new Player(id, name, age, height, weight));
System.out.println("Player successfully added to list");
}
public static void search(ArrayList<Player> players){
Scanner in = new Scanner(System.in);
System.out.println("Enter player ID to search: ");
int id = in.nextInt();
System.out.printf("%-5s %-15s %-5s %-5s %s\n", "ID", "Player Name", "Age", "Height", "Weight");
boolean found = false;
for(Player p: players){
if(p.PlayerID == id)
System.out.printf("%-5d %-15s %-5d %-5lf %-lf\n", p.PlayerID, p.PlayerName, p.Age, p.Height, p.Weight);
}
if(!found){
System.out.println("Player Not found");
}
}
public static void visulaize(ArrayList<Player> players){
System.out.printf("%-5s %-15s %-5s %-5s %s\n", "ID", "Player Name", "Age", "Height", "Weight");
for(Player p: players){
System.out.printf("%-5d %-15s %-5d %-5lf %-lf\n", p.PlayerID, p.PlayerName, p.Age, p.Height, p.Weight);
}
}
}