Skip to content

Commit 41e3b82

Browse files
committed
Arrays
1 parent 7aab4ee commit 41e3b82

File tree

4 files changed

+406
-0
lines changed

4 files changed

+406
-0
lines changed
+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package chapter_07;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 15. Write a program that manages a list of up to 10 players and their high
7+
* scores in the computer’s memory. Use two arrays to manage the list. One
8+
* array should store the player’s name and the other array should store the
9+
* player’s high score. Use the index of the arrays to correlate the name with
10+
* the score. In the next Programming Project, we ask you to do the same
11+
* thing, but using an array of a class. Your program should start with zero
12+
* players in the list and support the following features:
13+
* a. Add a new player and score. If it is one of the top 10 scores then add it
14+
* to the list of scores. The same name and score can appear multiple
15+
* times. For example, if Bill played 3 times and scored 100,100, and 99,
16+
* and Bob played once and scored 50, then the top scores would be Bill
17+
* 100, Bill 100, Bill 99, Bob 50.
18+
* b. Print the top 10 names and scores to the screen sorted by score (highest
19+
* at the top).
20+
* c. Allow the user to enter a player name and output that player’s highest
21+
* score if it is on the top 10 list or a message if the player’s name has not
22+
* been input or is not in the top 10.
23+
* d. Allow the user to enter a player name and remove the highest score for
24+
* that player from the list.
25+
* Create a menu system that allows the user to select which option to
26+
* invoke.
27+
*
28+
*
29+
*
30+
* @author Sharaf Qeshta
31+
* */
32+
public class Project_07_15
33+
{
34+
private static String[] names = new String[10];
35+
private static int[] scores = new int[10];
36+
private static Scanner scanner = new Scanner(System.in);
37+
38+
public static void main(String[] args)
39+
{
40+
while (true)
41+
start();
42+
}
43+
44+
public static void start()
45+
{
46+
printOptions();
47+
System.out.println("\nEnter a choice: ");
48+
int choice = scanner.nextInt();
49+
50+
switch (choice)
51+
{
52+
case 1 : addPlayer(); break;
53+
case 2 : printTop10(); break;
54+
case 3 : search(); break;
55+
case 4 : remove(); break;
56+
case 5 : System.exit(0);
57+
default:
58+
System.out.println("Invalid Choice");
59+
}
60+
}
61+
62+
public static void printOptions()
63+
{
64+
System.out.println("1. Add new player");
65+
System.out.println("2. Print top 10 players");
66+
System.out.println("3. Search for a player");
67+
System.out.println("4. Remove a player");
68+
System.out.println("5. Exit");
69+
}
70+
71+
public static void remove()
72+
{
73+
System.out.print("Enter player name: ");
74+
String playerName = scanner.next();
75+
76+
int index = -1;
77+
for (int i = 0; i < 10; i++)
78+
{
79+
if (names[i] == null)
80+
break;
81+
82+
if (names[i].equals(playerName))
83+
{
84+
index = i;
85+
break;
86+
}
87+
}
88+
89+
if (index == -1)
90+
{
91+
System.out.println("the name is not exist");
92+
return;
93+
}
94+
95+
96+
for (int i = index; i < scores.length-1; i++)
97+
{
98+
names[i] = names[i+1];
99+
scores[i] = scores[i+1];
100+
}
101+
102+
names[names.length-1] = null;
103+
scores[scores.length-1] = 0;
104+
}
105+
106+
public static void search()
107+
{
108+
System.out.print("Enter player name: ");
109+
String playerName = scanner.next();
110+
111+
for (int i = 0; i < 10; i++)
112+
{
113+
if (names[i] == null)
114+
{
115+
System.out.println(playerName + " is not exist");
116+
break;
117+
}
118+
119+
if (names[i].equals(playerName))
120+
{
121+
System.out.println(playerName + " highest score : " + scores[i]);
122+
return;
123+
}
124+
}
125+
System.out.println(playerName + " is not on the top ten");
126+
}
127+
128+
public static void printTop10()
129+
{
130+
for (int i = 0; i < 10; i++)
131+
{
132+
if (names[i] == null)
133+
break;
134+
System.out.println(names[i] + " : " + scores[i]);
135+
}
136+
}
137+
138+
public static void addPlayer()
139+
{
140+
System.out.print("Enter player name: ");
141+
String playerName = scanner.next();
142+
143+
System.out.print("Enter player score: ");
144+
int score = scanner.nextInt();
145+
146+
int index = 0;
147+
for (int i = scores.length-1; i > -1; i--)
148+
{
149+
if (score <= scores[i])
150+
{
151+
index = i + 1;
152+
break;
153+
}
154+
}
155+
156+
157+
for (int i = index; i < scores.length-1; i++)
158+
{
159+
String indexName = names[i+1];
160+
int indexScore = scores[i+1];
161+
162+
names[i+1] = names[index];
163+
scores[i+1] = scores[index];
164+
165+
names[index] = indexName;
166+
scores[index] = indexScore;
167+
}
168+
169+
if (index < scores.length)
170+
{
171+
names[index] = playerName;
172+
scores[index] = score;
173+
}
174+
}
175+
}

chapter_07/Project_07_16/Player.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package chapter_07;
2+
3+
public class Player
4+
{
5+
String name;
6+
int score;
7+
8+
public Player(String name, int score)
9+
{
10+
this.name = name;
11+
this.score = score;
12+
}
13+
}
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package chapter_07;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 16. Do Programming Project 15 except use a class named Player to store a
7+
* player’s name and score. Use a single array of type Player. Be sure to
8+
* include a constructor with this class that sets the name and score.
9+
*
10+
*
11+
* @author Sharaf Qeshta
12+
* */
13+
public class Project_07_16
14+
{
15+
private static Player[] players = new Player[10];
16+
private static Scanner scanner = new Scanner(System.in);
17+
18+
public static void main(String[] args)
19+
{
20+
while (true)
21+
start();
22+
}
23+
24+
public static void start()
25+
{
26+
printOptions();
27+
System.out.println("\nEnter a choice: ");
28+
int choice = scanner.nextInt();
29+
30+
switch (choice)
31+
{
32+
case 1 : addPlayer(); break;
33+
case 2 : printTop10(); break;
34+
case 3 : search(); break;
35+
case 4 : remove(); break;
36+
case 5 : System.exit(0);
37+
default:
38+
System.out.println("Invalid Choice");
39+
}
40+
}
41+
42+
public static void printOptions()
43+
{
44+
System.out.println("1. Add new player");
45+
System.out.println("2. Print top 10 players");
46+
System.out.println("3. Search for a player");
47+
System.out.println("4. Remove a player");
48+
System.out.println("5. Exit");
49+
}
50+
51+
public static void remove()
52+
{
53+
System.out.print("Enter player name: ");
54+
String playerName = scanner.next();
55+
56+
int index = -1;
57+
for (int i = 0; i < 10; i++)
58+
{
59+
if (players[i] == null)
60+
break;
61+
62+
if (players[i].name.equals(playerName))
63+
{
64+
index = i;
65+
break;
66+
}
67+
}
68+
69+
if (index == -1)
70+
{
71+
System.out.println("the name is not exist");
72+
return;
73+
}
74+
75+
76+
for (int i = index; i < players.length-1; i++)
77+
players[i] = players[i+1];
78+
79+
players[players.length-1] = null;
80+
}
81+
82+
public static void search()
83+
{
84+
System.out.print("Enter player name: ");
85+
String playerName = scanner.next();
86+
87+
for (int i = 0; i < 10; i++)
88+
{
89+
if (players[i] == null)
90+
{
91+
System.out.println(playerName + " is not exist");
92+
break;
93+
}
94+
95+
if (players[i].name.equals(playerName))
96+
{
97+
System.out.println(playerName + " highest score : " + players[i].score);
98+
return;
99+
}
100+
}
101+
System.out.println(playerName + " is not on the top ten");
102+
}
103+
104+
public static void printTop10()
105+
{
106+
for (int i = 0; i < 10; i++)
107+
{
108+
if (players[i] == null)
109+
break;
110+
System.out.println(players[i].name + " : " + players[i].score);
111+
}
112+
}
113+
114+
public static void addPlayer()
115+
{
116+
System.out.print("Enter player name: ");
117+
String playerName = scanner.next();
118+
119+
System.out.print("Enter player score: ");
120+
int score = scanner.nextInt();
121+
122+
int index = 0;
123+
for (int i = players.length-1; i > -1; i--)
124+
{
125+
if (players[i] == null)
126+
continue;
127+
128+
if (score <= players[i].score)
129+
{
130+
index = i + 1;
131+
break;
132+
}
133+
}
134+
135+
for (int i = index; i < players.length-1; i++)
136+
{
137+
Player indexPlayer = players[i+1];
138+
139+
players[i+1] = players[index];
140+
141+
players[index] = indexPlayer;
142+
}
143+
144+
if (index < players.length)
145+
{
146+
players[index] = new Player(playerName, score);
147+
}
148+
}
149+
}

0 commit comments

Comments
 (0)