Draw a structure chart for the program below : (java) public class HighScore{ // method readGameScore return
Question:
Draw a structure chart for the program below : (java)
public class HighScore{
// method readGameScore return a GameScore object public static GameScore readGameScore(Scanner sc){ GameScore game; // game score object String user; // this is user of game int id; // this is game id of the game int score; // this is the game score
System.out.print("Enter game user: "); user = sc.next(); System.out.print("Enter game ID: "); id = sc.nextInt();
do{ System.out.print("Enter game score: "); score = sc.nextInt(); if(score < 0){ System.out.println("Please enter a valid value. "); } } while(score < 0);
game = new GameScore(user, score, id); return game; }
// read type name public static int addGameScore(Scanner sc, GameScore[] gs, int numCount){ if(numCount < gs.length){ gs[numCount] = readGameScore(sc); numCount++; } else { System.out.println("Score is full."); } return numCount; }
// the average score for player name public static double averageUserScore(GameScore[] gs, int numCount, String name){ double score = 0; // the sum of name's scores int playerScores = 0; // number of game scores for name
for(int i = 0; i < numCount; i++){ score += gs[i].score(); playerScores++; }
if(playerScores > 0){ return score / playerScores; } else { return score; } }
// highest score public static double highestScore(GameScore[] gs, int numCount, String name){
int score = 0; for(int i = 0; i < numCount; i++){ if(gs[i].score()>score){ score = gs[i].score(); }
} return score; }
// display all objects public static void displaysAll(GameScore[] gs, int numCount){
for(int i = 0; i < numCount; i++){ System.out.println(gs[i]); } System.out.println(); }
// bulkAdd Method public static void bulkAdd(Scanner sc, GameScore[] gs, int numCount){
for(int i = 0; i < numCount; i++){ addGameScore(sc, gs, i); } }
public static void main(String[] args){ Scanner sc = new Scanner(System.in);
final int CAPACITY = 50; // the maximum number of allowes character GameScore[] gs = new GameScore[CAPACITY]; int count = 0; //count how many character the user has typed in int choice; /umber of choices being displayed to users String name; // Player name
System.out.println("Contact Manager"); System.out.println("Enter initial number of entries(< " + CAPACITY + "):");
count = sc.nextInt(); count = Math.min(count, CAPACITY); bulkAdd(sc, gs, count);
//Implementation of the menu do { System.out.println("1. Add another score"); System.out.println("2. Display all scores, one per line"); System.out.println("3. Display average score for a player"); System.out.println("4. Display highest score for a player"); System.out.println("5. Quit");
System.out.println("Enter option: "); choice = sc.nextInt();
switch(choice){ case 1: count = addGameScore(sc, gs, count); System.out.println(); break; case 2: displaysAll(gs, count); System.out.println(); break; case 3: System.out.println("Please enter name: "); name = sc.next(); System.out.println(name + " Average score: " + averageUserScore(gs, count, name) + "."); System.out.println(); break; case 4: System.out.println(" Please enter name: "); name = sc.next(); System.out.println(name + " Highest score: " + highestScore(gs, count, name) + "."); System.out.println(); break; } } while(choice != 5); }
}
///////////////////////////
public record GameScore(String user, int score, int id) {
public String toString() { String output = " game " + id + " : " + user + " scored " + score;
if (score > 999999) { output += " God Gamer!"; } else if(score < 100) { output += " Newb!"; }
return output; }
}
Structure charts provide an effective way of communicating the methods within a program. To draw a structure chart you would do the following (an example is shown below)
- Draw a box for the program's main() method at the top of the page
- Write the text main within the box; this now represents the main method
- Draw a box for each of the methods that main() calls. Position these below the box for main so that you can easily draw arrows from the bottom of main to the methods it calls
- Write the name of each method within the box
- Draw an arrow from main to the method's box
- Next to the line, draw smaller arrows to indicate the data that will be passed to, or returned from, the method.
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates