Skip to content

Commit 122a726

Browse files
committed
Edited AddressBook to be clearer and have better code style. Also removed methods because those aren't introduced until Ch. 10
1 parent acd842b commit 122a726

File tree

10 files changed

+134
-82
lines changed

10 files changed

+134
-82
lines changed
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
package com.codefortomorrow.beginner.chapter9.practice;
22

33
/*
4-
Store the address information (Number, Street, Apt, and Zip Code)
5-
for five individuals. If there is no apartment, the user may enter –
6-
and this shouldn’t be included in the printout. Use the data below.
7-
8-
23 Chamberlain Ave 08852
9-
451 Avenue Q Apt B 10101
10-
1 K St Apt P 23456
11-
1111 Pitt Ave 19146
12-
1313 Elm St Apt Z 08628
13-
14-
If you want to try a char array, you will need the Scanner command which is something like:
15-
char c = scan.next().charAt(0);
16-
17-
Allow a user to enter the information for an assignment completed by 6 students,
18-
including first name, last initial, student ID number, points earned, and points possible.
19-
Then display the information on 6 lines and include the grade as a percent on the assignment.
20-
21-
Alexandria R 009988602 50 50 100%
22-
*/
4+
* Create a program called AddressBook
5+
* which prompts the user to enter
6+
* 5 addresses.
7+
*
8+
* The program should prompt the user
9+
* for the house number (int), street (String),
10+
* apartment (char), and zip code (int).
11+
*
12+
* Note that there is no nextChar() method
13+
* in the Scanner class, so you will need to do
14+
* something like this:
15+
* Scanner input = new Scanner(System.in);
16+
* char apt = input.nextLine().charAt(0);
17+
*
18+
* It should then print each of the addresses. If
19+
* the user entered '-' for the apartment, the apartment
20+
* should not be printed (see sample output).
21+
*
22+
* The zip codes should always be printed so that
23+
* they are 5 digits long. (You may need to use
24+
* String formatting so that if the zip code
25+
* is not 5 digits, it will fill in 0s to the left
26+
* of the number until the entire zip code is 5 digits
27+
* long. You can read about that here:
28+
* https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left)
29+
*
30+
* Sample output: https://youtu.be/ms2tM-llTwo
31+
*/
2332

2433
public class AddressBook {
25-
public static void main(String[]args) {
34+
public static void main(String[] args) {
2635
// write code here
2736
}
2837
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codefortomorrow.beginner.chapter9.practice;
2+
3+
/*
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%
9+
*/
10+
11+
public class GradeBook {
12+
public static void main(String[] args) {
13+
// write code here
14+
}
15+
}
Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,82 @@
11
package com.codefortomorrow.beginner.chapter9.solutions;
22

3+
/*
4+
* Create a program called AddressBook
5+
* which prompts the user to enter
6+
* 5 addresses. The program should prompt the user
7+
* for the house number (int), street (String),
8+
* apartment (char), and zip code (int).
9+
*
10+
* Note that there is no nextChar() method
11+
* in the Scanner class, so you will need to do
12+
* something like this:
13+
* Scanner input = new Scanner(System.in);
14+
* char apt = input.nextLine().charAt(0);
15+
*
16+
* It should then print each of the addresses. If
17+
* the user entered '-' for the apartment, the apartment
18+
* should not be printed (see sample output).
19+
*
20+
* The zip codes should always be printed so that
21+
* they are 5 digits long. (You may need to use
22+
* String formatting so that if the zip code
23+
* is not 5 digits, it will fill in 0s to the left
24+
* of the number until the entire zip code is 5 digits
25+
* long. You can read about that here:
26+
* https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left)
27+
*
28+
* Sample output: https://youtu.be/ms2tM-llTwo
29+
*/
30+
331
import java.util.Scanner;
4-
public class AddressBook
5-
{
6-
public static void main(String[]args)
7-
{
8-
//1
9-
int[][] num = new int [1][5];
10-
String[][] street = new String [1][5];
11-
String[][] apt = new String [1][5];
12-
int[][] zip = new int [1][5];
13-
initintArray(num, "number");
14-
initstrArray(street, "street");
15-
initstrArray(apt, "apartment");
16-
initintArray(zip, "zipcode");
17-
System.out.println();
18-
for (int c = 0; c < num[0].length; c++)
19-
{
20-
if (apt[0][c].equals("-"))
21-
System.out.println(num[0][c] + " " + street[0][c] + " " + zip[0][c]);
22-
else
23-
System.out.println(num[0][c] + " " + street[0][c] + " Apt " + apt[0][c] + " " + zip[0][c]);
24-
}
25-
26-
//2
27-
String[][] fn = new String [1][6];
28-
String[][] li = new String [1][6];
29-
int[][] id = new int [1][6];
30-
int[][] pe = new int [1][6];
31-
int[][] pp = new int [1][6];
32-
double[][] g = new double [1][6];
33-
initstrArray(fn, "first name");
34-
initstrArray(li, "last initial");
35-
initintArray(id, "student ID");
36-
initintArray(pe, "points earned");
37-
initintArray(pp, "points possible");
38-
for (int c = 0; c < g[0].length; c++)
39-
{
40-
g[0][c] = (pe[0][c])/((double)(pp[0][c])) * 100;
41-
}
42-
System.out.println();
43-
for (int c = 0; c < fn[0].length; c++)
44-
{
45-
System.out.println(fn[0][c] + " " + li[0][c] + " " + id[0][c] + " " + pe[0][c] + " " + pp[0][c] + " " + (int)g[0][c] + "%");
46-
}
47-
48-
}
49-
public static void initintArray (int[][] n, String message)
50-
{
51-
Scanner reader = new Scanner(System.in);
52-
for (int x = 0; x < n[0].length; x++)
53-
{
54-
System.out.print("Please enter a " + message + " " + (x+1) + ": ");
55-
n[0][x] = reader.nextInt();
32+
33+
public class AddressBook {
34+
public static void main(String[] args) {
35+
final int NUMBER_OF_ADDRESSES = 5;
36+
37+
// arrays store the address info
38+
int[] number = new int[NUMBER_OF_ADDRESSES];
39+
String[] street = new String[NUMBER_OF_ADDRESSES];
40+
char[] apartment = new char[NUMBER_OF_ADDRESSES];
41+
int[] zipCode = new int[NUMBER_OF_ADDRESSES];
42+
43+
Scanner input = new Scanner(System.in);
44+
45+
// prompt user for each address and store it
46+
for (int i = 0; i < NUMBER_OF_ADDRESSES; i++) {
47+
System.out.println("Address #" + (i + 1));
48+
49+
System.out.print("Enter the house number: ");
50+
number[i] = input.nextInt();
51+
52+
input.nextLine(); // move cursor to next line
53+
54+
System.out.print("Enter the street: ");
55+
street[i] = input.nextLine();
56+
57+
System.out.print("Enter the apartment, or '-' if apartment is not applicable: ");
58+
apartment[i] = input.nextLine().charAt(0);
59+
60+
System.out.print("Enter the zip code: ");
61+
zipCode[i] = input.nextInt();
62+
63+
System.out.println();
5664
}
57-
58-
}
59-
public static void initstrArray (String[][] n, String message)
60-
{
61-
Scanner reader = new Scanner(System.in);
62-
for (int x = 0; x < n[0].length; x++)
63-
{
64-
System.out.print("Please enter a " + message + " " + (x+1) + ": ");
65-
n[0][x] = reader.nextLine();
65+
66+
System.out.println("Address Book\n");
67+
68+
// print out each address
69+
for (int i = 0; i < NUMBER_OF_ADDRESSES; i++) {
70+
System.out.print(number[i] + " " + street[i] + " ");
71+
72+
// if apartment is '-', don't print the apartment
73+
if (apartment[i] != '-') {
74+
System.out.print("Apt " + apartment[i] + " ");
75+
}
76+
77+
// pad zeroes to the left so that zip codes are
78+
// 5 digits long
79+
System.out.printf("%05d\n", zipCode[i]);
6680
}
67-
6881
}
6982
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codefortomorrow.beginner.chapter9.solutions;
2+
3+
/*
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%
9+
*/
10+
11+
public class GradeBook {
12+
public static void main(String[] args) {
13+
// write code here
14+
}
15+
}

0 commit comments

Comments
 (0)