|
1 | 1 | package com.codefortomorrow.beginner.chapter9.solutions;
|
2 | 2 |
|
| 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 | + |
3 | 31 | 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(); |
56 | 64 | }
|
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]); |
66 | 80 | }
|
67 |
| - |
68 | 81 | }
|
69 | 82 | }
|
0 commit comments