Question

In: Computer Science

Java Starter Code: import java.util.Scanner; public class GameScore { static Scanner keyboard = new Scanner(System.in); public...

Java

Starter Code:

import java.util.Scanner;

public class GameScore {

static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {
int team1[] = new int[4];
int team2[] = new int[4];

for (int qtr = 0; qtr < 4; qtr++) {
quarterScoring(team1, team2, qtr);
}

int team1Total = teamTotal(team1);
int team2Total = teamTotal(team2);

displayGameResults(team1, team2);

if (team1Total > team2Total) {
System.out.println("Team 1 has won the game!");
} else {
System.out.println("Team 2 has won the game!");
}
}

static int teamTotal(int[] team) {
// This method will add the points a team has scored during all four quarters of a game.
return 0;
}

static void quarterScoring(int[] team1, int[] team2, int qtr) {
// This method will continue to prompt user which team scored, and how many points are scored
// When this method finishes, the appropriate team's score for the quarter will be assigned
}

static void displayGameResults(int[] team1, int[] team2) {
// This method will display the scores for each team per quarter as well as the total score

}
}

Sample Output:
Quarter: 1
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
3
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter: 2
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
3
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
3
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
3
Which team scored? (1 or 2)
0
Invalid team - quarter has ended.
Quarter: 3
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter: 4
Which team scored? (1 or 2)
0
Invalid team - quarter has ended.

Team Scores by quarter:
Q1 Q2 Q3 Q4 Total
3 3 2 0 8
3 6 2 0 11

Team 2 has won the game!

Solutions

Expert Solution

Summary: The program will take inputs for the game on multiple quarters and display the final results along with winner. The total score is calulated by cobining the score in each quarter and the winner is found. The input validations are done before executing the program.



import java.util.Scanner;

public class GameScore {

   static Scanner sc = new Scanner(System.in);

   public static void main(String[] args) {
      // Driver code
      int team1[] = new int[4];
      int team2[] = new int[4];
      
      // Get the input from user
      for (int qtr = 0; qtr < 4; qtr++) {
         quarterScoring(team1, team2, qtr);
      }
      // Find the team total
      int team1Total = teamTotal(team1);
      int team2Total = teamTotal(team2);
      // Display the result
      displayGameResults(team1, team2);

      if (team1Total > team2Total) {
         System.out.println("Team 1 has won the game!");
      } else {
         System.out.println("Team 2 has won the game!");
      }
   }
   // This method will return the total score of a team in all quarters
   static int teamTotal(int[] team) {
      int score = 0;
      for (int qtr = 0; qtr < 4; qtr++) {
         score += team[qtr];
      }
      return score;
   }
   // This method will read the team score details until user enters an team number /score
   static void quarterScoring(int[] team1, int[] team2, int qtr) {
      int team1Total = 0;
      int team2Total = 0;
      System.out.println("Quarter " + (qtr + 1));
      while (true) {
         System.out.println("Which team scored? (1 or 2)");
         // Read the team number
         int team = sc.nextInt();
         // Validate the team number
         if (team != 1 && team != 2) {
            System.out.println("Invalid team - quarter has ended.");
            break;
         }
         System.out.println("How many points did they score (1, 2, or 3)?");
         int score = sc.nextInt();
         // If the score in invalid then exit
         if (score < 1 || score > 3) {
            break;
         } else if (team == 1) {
            // Add the score to the total of team in the quarter
            team1Total += score;
         } else if (team == 2) {
            team2Total += score;
         }
         team1[qtr] = team1Total;
         team2[qtr] = team2Total;
      }
   }
   // This method will print the output in a format
   static void displayGameResults(int[] team1, int[] team2) {
      int score = 0;
      System.out.println("Team Scores by quarter:");
      System.out.println("Q1 Q2 Q3 Q4 Total");
      
      // Print output for each quarter for team 1
      for (int qtr = 0; qtr < 4; qtr++) {
         System.out.print(team1[qtr] + "  ");
         score+=team1[qtr];
      }
      System.out.println(score);
      score = 0;
      // Print output for each quarter for team 2
      for (int qtr = 0; qtr < 4; qtr++) {
         System.out.print(team2[qtr]+ "  ");
         score+=team2[qtr];
      }
      System.out.println(score);

   }
}

Output

Quarter 1
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter 2
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter 3
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Quarter 4
Which team scored? (1 or 2)
1
How many points did they score (1, 2, or 3)?
1
Which team scored? (1 or 2)
2
How many points did they score (1, 2, or 3)?
2
Which team scored? (1 or 2)
3
Invalid team - quarter has ended.
Team Scores by quarter:
Q1 Q2 Q3 Q4 Total
1  1  1  1  4
2  2  2  2  8
Team 2 has won the game!

Related Solutions

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { static Scanner sc=new Scanner(System.in);...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { static Scanner sc=new Scanner(System.in); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { // TODO code application logic here System.out.print("Enter any String: "); String str = br.readLine(); System.out.print("\nEnter the Key: "); int key = sc.nextInt(); String encrypted = encrypt(str, key); System.out.println("\nEncrypted String is: " +encrypted); String decrypted = decrypt(encrypted, key); System.out.println("\nDecrypted String is: " +decrypted); System.out.println("\n"); } public static String encrypt(String str,...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new Scanner(System.in);           boolean gameOver = false; int colSize = 10; int rowSize= 10; String floorTile= "."; int playerX = 0; int playerY= 0; String playerTile="@"; int exitX= colSize-1; int exitY= rowSize-1; String exitTile="# "; int zombieX=5; int zombieY=5; // Defining Second Zombie int zombie2Y= 8; int zombie2X= 3; // Defining third zombie int zombie3Y= 1; int zombie3X= 7; String zombieTile="*"; String zombie2Tile="*";...
import java.util.Scanner; public class Grade { public static void main(String[] args) { Scanner scnr = new...
import java.util.Scanner; public class Grade { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Reading score from user System.out.println("Enter the student's score:"); double score = scnr.nextDouble(); System.out.print("Grade:"); // Checking if score is less than 60 if(score<60){ System.out.println("F"); } // Checking if score is less than 70 else if(score<70){ System.out.println("D"); } // Checking if score is less than 80 else if(score<80){ System.out.println("C"); } // Checking if score is less than 90 else if(score<90){ System.out.println("B"); } // Checking...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[]...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[] args) { char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char userAnswers[] = new char[correctAnswers.length]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < userAnswers.length; i++) { String answer = ""; System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1); do...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); SimpleDate d1 = new SimpleDate(); SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(), stdin.nextInt()); System.out.println(d1); System.out.println(d2); System.out.println(d1.before(d2)); System.out.println(d2.before(d1)); } } Implement SimpleDate class in chapter6.date package with the following attributes: day, (int type,  private) month, (int type,  private) year (int type,  private) The class should have the following methods: a constructor with three parameters: year, month, and day a constructor with no parameters which initialize the...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT