Skip to content

Commit 414e0a5

Browse files
committed
Edited Grade Book (used to be in the Address Book program, but I separated them). Finished instructions and solution.
1 parent 122a726 commit 414e0a5

File tree

7 files changed

+88
-11
lines changed

7 files changed

+88
-11
lines changed

src/com/codefortomorrow/beginner/chapter9/practice/AddressBook.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
* https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left)
2929
*
3030
* Sample output: https://youtu.be/ms2tM-llTwo
31+
*
32+
* Hint: You may encounter a problem when using Scanner.
33+
* Read the following article for a solution:
34+
* https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
3135
*/
3236

3337
public class AddressBook {

src/com/codefortomorrow/beginner/chapter9/practice/GradeBook.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package com.codefortomorrow.beginner.chapter9.practice;
22

33
/*
4-
* Allow a user to enter the information for an assignment completed by 6 students,
5-
including first name, last initial, student ID number, points earned, and points possible.
6-
Then display the information on 6 lines and include the grade as a percent on the assignment.
7-
8-
Alexandria R 009988602 50 50 100%
4+
* Create a program called GradeBook
5+
* which prompts the user to enter
6+
* information for 6 students.
7+
*
8+
* The program should ask the user for
9+
* the student's first name (String), last initial (char),
10+
* student ID number (long), points earned (int),
11+
* and points possible (int).
12+
*
13+
* It should then print all of that data (see
14+
* sample output).
15+
*
16+
* Sample output: https://youtu.be/2KC8Zy6EIwc
17+
*
18+
* Hint: You may encounter a problem when using Scanner.
19+
* Read the following article for a solution:
20+
* https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
921
*/
1022

1123
public class GradeBook {

src/com/codefortomorrow/beginner/chapter9/solutions/AddressBook.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
* https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left)
2727
*
2828
* Sample output: https://youtu.be/ms2tM-llTwo
29+
*
30+
* Hint: You may encounter a problem when using Scanner.
31+
* Read the following article for a solution:
32+
* https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
2933
*/
3034

3135
import java.util.Scanner;
Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,72 @@
11
package com.codefortomorrow.beginner.chapter9.solutions;
22

33
/*
4-
* Allow a user to enter the information for an assignment completed by 6 students,
5-
including first name, last initial, student ID number, points earned, and points possible.
6-
Then display the information on 6 lines and include the grade as a percent on the assignment.
7-
8-
Alexandria R 009988602 50 50 100%
4+
* Create a program called GradeBook
5+
* which prompts the user to enter
6+
* information for 6 students.
7+
*
8+
* The program should ask the user for
9+
* the student's first name (String), last initial (char),
10+
* student ID number (long), points earned (int),
11+
* and points possible (int).
12+
*
13+
* It should then print all of that data (see
14+
* sample output).
15+
*
16+
* Sample output: https://youtu.be/2KC8Zy6EIwc
17+
*
18+
* Hint: You may encounter a problem when using Scanner.
19+
* Read the following article for a solution:
20+
* https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
921
*/
1022

23+
import java.util.Scanner;
24+
1125
public class GradeBook {
1226
public static void main(String[] args) {
13-
// write code here
27+
final int NUMBER_OF_STUDENTS = 6;
28+
29+
String[] firstName = new String[NUMBER_OF_STUDENTS];
30+
char[] lastInitial = new char[NUMBER_OF_STUDENTS];
31+
long[] studentIDNumber = new long[NUMBER_OF_STUDENTS];
32+
int[] pointsEarned = new int[NUMBER_OF_STUDENTS];
33+
int[] pointsPossible = new int[NUMBER_OF_STUDENTS];
34+
double[] percent = new double[NUMBER_OF_STUDENTS];
35+
36+
Scanner input = new Scanner(System.in);
37+
38+
for (int i = 0; i < NUMBER_OF_STUDENTS; i++) {
39+
System.out.println("Student #" + (i + 1));
40+
41+
System.out.print("Enter first name: ");
42+
firstName[i] = input.nextLine();
43+
44+
System.out.print("Enter last initial: ");
45+
lastInitial[i] = input.nextLine().charAt(0);
46+
47+
System.out.print("Enter student ID number: ");
48+
studentIDNumber[i] = input.nextLong();
49+
50+
System.out.print("Enter points earned: ");
51+
pointsEarned[i] = input.nextInt();
52+
53+
System.out.print("Enter points possible: ");
54+
pointsPossible[i] = input.nextInt();
55+
56+
input.nextLine(); // move cursor to next line
57+
58+
// calculate percent
59+
percent[i] = ((double) pointsEarned[i] / pointsPossible[i]) * 100;
60+
61+
System.out.println();
62+
}
63+
64+
// display grade book
65+
System.out.print("Grade Book\n");
66+
67+
for (int i = 0; i < NUMBER_OF_STUDENTS; i++) {
68+
System.out.printf("%s %s \t %d \t %d / %d \t %.2f%% \n", firstName[i], lastInitial[i],
69+
studentIDNumber[i], pointsEarned[i], pointsPossible[i], percent[i]);
70+
}
1471
}
1572
}

0 commit comments

Comments
 (0)