1Enter the taxable income: $41000
2The income tax payable is: $2200.00
3Enter the taxable income: $62000
4The income tax payable is: $6600.00
5Enter the taxable income: $73123
6The income tax payable is: $9936.90
7Enter the taxable income: $84328
8The income tax payable is: $13298.40
9Enter the taxable income: $-1
10bye!
1int grid[][] = new int[12][8]; // a 12×8 grid of int
2grid[0][0] = 8;
3grid[1][1] = 5;
4System.out.println(grid.length); // 12
5System.out.println(grid[0].length); // 8
6System.out.println(grid[11].length); // 8
1import java.util.Scanner;
2/**
3 * Prompt user for an int, and print its equivalent hexadecimal number.
4 */
5public class Dec2Hex {
6 public static void main(String[] args) {
7 // Declare variables
8 int dec; // The input decimal number in "int"
9 String hexStr = ""; // The equivalent hex String, to accumulate from an empty String
10 int radix = 16; // Hex radix
11 char[] hexChars = // Use this array as lookup table for converting 0-15 to 0-9A-F
12 {'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'};
13 Scanner in = new Scanner(System.in);
14
15 // Prompt and read input as "int"
16 System.out.print("Enter a decimal number: ");
17 dec = in.nextInt();
18
19 // Repeated modulus/division and get the hex digits (0-15) in reverse order
20 while (dec > 0) {
21 int hexDigit = dec % radix; // 0-15
22 hexStr = hexChars[hexDigit] + hexStr; // Append in front of the hex string corresponds to reverse order
23 dec = dec / radix;
24 }
25 System.out.println("The equivalent hexadecimal number is " + hexStr);
26 in.close();
27 }
28}
1public class Array2DWithDifferentLength {
2 public static void main(String[] args) {
3 int[][] grid = {
4 {1, 2},
5 {3, 4, 5},
6 {6, 7, 8, 9}
7 };
8
9 // Print grid
10 for (int y = 0; y < grid.length; ++y) {
11 for (int x = 0; x < grid[y].length; ++x) {
12 System.out.printf("%2d", grid[y][x]);
13 }
14 System.out.println();
15 }
16
17 // Another 2D array
18 int[][] grid1 = new int[3][];
19 grid1[0] = new int[2];
20 grid1[1] = new int[3];
21 grid1[2] = new int[4];
22
23 // Print grid - all elements init to 0
24 for (int y = 0; y < grid1.length; ++y) {
25 for (int x = 0; x < grid1[y].length; ++x) {
26 System.out.printf("%2d", grid1[y][x]);
27 }
28 System.out.println();
29 }
30 }
31}
1import java.util.Scanner;
2/**
3 * Prompt user for the length and all the elements of an array; and print [a1, a2, ..., an]
4 */
5public class ReadPrintArray {
6 public static void main(String[] args) {
7 // Declare variables
8 int numItems;
9 int[] items; // Declare array name, to be allocated after numItems is known
10 Scanner in = new Scanner(System.in);
11
12 // Prompt for a non-negative integer for the number of items;
13 // and read the input as "int". No input validation.
14 System.out.print("Enter the number of items: ");
15 numItems = in.nextInt();
16
17 // Allocate the array
18 items = new int[numItems];
19
20 // Prompt and read the items into the "int" array, only if array length > 0
21 if (items.length > 0) {
22 System.out.print("Enter the value of all items (separated by space): ");
23 for (int i = 0; i < items.length; ++i) {
24 items[i] = in.nextInt();
25 }
26 }
27
28 // Print array contents, need to handle first item and subsequent items differently
29 System.out.print("The values are: [");
30 for (int i = 0; i < items.length; ++i) {
31 if (i == 0) {
32 // Print the first item without a leading commas
33 System.out.print(items[0]);
34 } else {
35 // Print the subsequent items with a leading commas
36 System.out.print(", " + items[i]);
37 }
38 }
39 System.out.println("]");
40 in.close();
41 }
42}
1/**
2 * Input via a Dialog box
3 */
4import javax.swing.JOptionPane; // Needed to use JOptionPane
5public class JOptionPaneTest {
6 public static void main(String[] args) {
7 String radiusStr;
8 double radius, area;
9 // Read input String from dialog box
10 radiusStr = JOptionPane.showInputDialog("Enter the radius of the circle");
11 radius = Double.parseDouble(radiusStr); // Convert String to double
12 area = radius*radius*Math.PI;
13 System.out.println("The area is " + area);
14 }
15}
1import java.util.Scanner;
2import java.util.Arrays; // for Arrays.toString()
3/**
4 * Print the horizontal and vertical histograms of grades.
5 */
6public class GradesHistograms {
7 public static void main(String[] args) {
8 // Declare variables
9 int numStudents;
10 int[] grades; // Declare array name, to be allocated after numStudents is known
11 int[] bins = new int[10]; // int array of 10 histogram bins for 0-9, 10-19, ..., 90-100
12 Scanner in = new Scanner(System.in);
13
14 // Prompt and read the number of students as "int"
15 System.out.print("Enter the number of students: ");
16 numStudents = in.nextInt();
17
18 // Allocate the array
19 grades = new int[numStudents];
20
21 // Prompt and read the grades into the int array "grades"
22 for (int i = 0; i < grades.length; ++i) {
23 System.out.print("Enter the grade for student " + (i + 1) + ": ");
24 grades[i] = in.nextInt();
25 }
26 // Print array for debugging
27 System.out.println(Arrays.toString(grades));
28
29 // Populate the histogram bins
30 for (int grade : grades) {
31 if (grade == 100) { // Need to handle 90-100 separately as it has 11 items.
32 ++bins[9];
33 } else {
34 ++bins[grade/10];
35 }
36 }
37 // Print array for debugging
38 System.out.println(Arrays.toString(bins));
39
40 // Print the horizontal histogram
41 // Rows are the histogram bins[0] to bins[9]
42 // Columns are the counts in each bins[i]
43 for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
44 // Print label
45 if (binIdx != 9) { // Need to handle 90-100 separately as it has 11 items
46 System.out.printf("%2d-%3d: ", binIdx*10, binIdx*10+9);
47 } else {
48 System.out.printf("%2d-%3d: ", 90, 100);
49 }
50 // Print columns of stars
51 for (int itemNo = 0; itemNo < bins[binIdx]; ++itemNo) { // one star per item
52 System.out.print("*");
53 }
54 System.out.println();
55 }
56
57 // Find the max value among the bins
58 int binMax = bins[0];
59 for (int binIdx = 1; binIdx < bins.length; ++binIdx) {
60 if (binMax < bins[binIdx]) binMax = bins[binIdx];
61 }
62
63 // Print the Vertical histogram
64 // Columns are the histogram bins[0] to bins[9]
65 // Rows are the levels from binMax down to 1
66 for (int level = binMax; level > 0; --level) {
67 for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
68 if (bins[binIdx] >= level) {
69 System.out.print(" * ");
70 } else {
71 System.out.print(" ");
72 }
73 }
74 System.out.println();
75 }
76 // Print label
77 for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
78 System.out.printf("%3d-%-3d", binIdx*10, (binIdx != 9) ? binIdx * 10 + 9 : 100);
79 // Use '-' flag for left-aligned
80 }
81 System.out.println();
82
83 in.close(); // Close the Scanner
84 }
85}
1Enter the taxable income: $41234
2The income tax payable is: $2246.80
3
4Enter the taxable income: $67891The income tax payable is: $8367.30
5
6Enter the taxable income: $85432
7The income tax payable is: $13629.60
8
9Enter the taxable income: $12345
10The income tax payable is: $0.00
1import java.util.Scanner; // For keyboard input
2/**
3 * 1. Prompt user for the taxable income in integer.
4 * 2. Read input as "int".
5 * 3. Compute the tax payable using nested-if in "double".
6 * 4. Print the values rounded to 2 decimal places.
7 * 5. Repeat until user enter -1.
8 */
9public class IncomeTaxCalculatorSentinel {
10 public static void main(String[] args) {
11 // Declare constants first (variables may use these constants)
12 final double TAX_RATE_ABOVE_20K = 0.1;
13 final double TAX_RATE_ABOVE_40K = 0.2;
14 final double TAX_RATE_ABOVE_60K = 0.3;
15 final int SENTINEL = -1; // Terminating value for input
16
17 // Declare variables
18 int taxableIncome;
19 double taxPayable;
20 Scanner in = new Scanner(System.in);
21
22 // Read the first input to "seed" the while loop
23 System.out.print("Enter the taxable income: $");
24 taxableIncome = in.nextInt();
25
26 while (taxableIncome != SENTINEL) {
27 // Compute tax payable in "double" using a nested-if to handle 4 cases
28 if (taxableIncome > 60000) {
29 taxPayable = 20000 * TAX_RATE_ABOVE_20K
30 + 20000 * TAX_RATE_ABOVE_40K
31 + (taxableIncome - 60000) * TAX_RATE_ABOVE_60K;
32 } else if (taxableIncome > 40000) {
33 taxPayable = 20000 * TAX_RATE_ABOVE_20K
34 + (taxableIncome - 40000) * TAX_RATE_ABOVE_40K;
35 } else if (taxableIncome > 20000) {
36 taxPayable = (taxableIncome - 20000) * TAX_RATE_ABOVE_20K;
37 } else {
38 taxPayable = 0;
39 }
40
41 // Print result rounded to 2 decimal places
42 System.out.printf("The income tax payable is: $%.2f%n", taxPayable);
43
44 // Read the next input
45 System.out.print("Enter the taxable income: $");
46 taxableIncome = in.nextInt();
47 // Repeat the loop body, only if the input is not the SENTINEL value.
48 // Take note that you need to repeat these two statements inside/outside the loop!
49 }
50 System.out.println("bye!");
51 in.close(); // Close Scanner
52 }
53}