ticket sales java program
import java.util.Scanner; // For keyboard input
/**
* 1. Prompt user for the taxable income in integer.
* 2. Read input as "int".
* 3. Compute the tax payable using nested-if in "double".
* 4. Print the values rounded to 2 decimal places.
*/
public class IncomeTaxCalculator {
public static void main(String[] args) {
// Declare constants first (variables may use these constants)
final double TAX_RATE_ABOVE_20K = 0.1;
final double TAX_RATE_ABOVE_40K = 0.2;
final double TAX_RATE_ABOVE_60K = 0.3;
// Declare variables
int taxableIncome;
double taxPayable;
Scanner in = new Scanner(System.in);
// Prompt and read inputs as "int"
System.out.print("Enter the taxable income: $");
taxableIncome = in.nextInt();
// Compute tax payable in "double" using a nested-if to handle 4 cases
if (taxableIncome <= 20000) { // [0, 20000]
taxPayable = 0;
} else if (taxableIncome <= 40000) { // [20001, 40000]
taxPayable = (taxableIncome - 20000) * TAX_RATE_ABOVE_20K;
} else if (taxableIncome <= 60000) { // [40001, 60000]
taxPayable = 20000 * TAX_RATE_ABOVE_20K
+ (taxableIncome - 40000) * TAX_RATE_ABOVE_40K;
} else { // >=60001
taxPayable = 20000 * TAX_RATE_ABOVE_20K
+ 20000 * TAX_RATE_ABOVE_40K
+ (taxableIncome - 60000) * TAX_RATE_ABOVE_60K;
}
// Alternatively, you could use the following nested-if conditions
// but the above follows the table data
//if (taxableIncome > 60000) { // [60001, ]
// ......
//} else if (taxableIncome > 40000) { // [40001, 60000]
// ......
//} else if (taxableIncome > 20000) { // [20001, 40000]
// ......
//} else { // [0, 20000]
// ......
//}
// Print result rounded to 2 decimal places
System.out.printf("The income tax payable is: $%.2f%n", taxPayable);
in.close(); // Close Scanner
}
}
public class Array2DTest {
public static void main(String[] args) {
int[][] grid = new int[12][8]; // A 12x8 grid, in [row][col] or [y][x]
int numRows = grid.length; // 12
int numCols = grid[0].length; // 8
// Fill in grid
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
grid[row][col] = row*numCols + col + 1;
}
}
// Print grid
for (int row = 0; row < numRows; ++row) {
for (int col = 0; col < numCols; ++col) {
System.out.printf("%3d", grid[row][col]);
}
System.out.println();
}
}
}
/**
* Input via a Dialog box
*/
import javax.swing.JOptionPane; // Needed to use JOptionPane
public class JOptionPaneTest {
public static void main(String[] args) {
String radiusStr;
double radius, area;
// Read input String from dialog box
radiusStr = JOptionPane.showInputDialog("Enter the radius of the circle");
radius = Double.parseDouble(radiusStr); // Convert String to double
area = radius*radius*Math.PI;
System.out.println("The area is " + area);
}
}
import java.util.Scanner;
import java.util.Arrays; // for Arrays.toString()
/**
* Print the horizontal and vertical histograms of grades.
*/
public class GradesHistograms {
public static void main(String[] args) {
// Declare variables
int numStudents;
int[] grades; // Declare array name, to be allocated after numStudents is known
int[] bins = new int[10]; // int array of 10 histogram bins for 0-9, 10-19, ..., 90-100
Scanner in = new Scanner(System.in);
// Prompt and read the number of students as "int"
System.out.print("Enter the number of students: ");
numStudents = in.nextInt();
// Allocate the array
grades = new int[numStudents];
// Prompt and read the grades into the int array "grades"
for (int i = 0; i < grades.length; ++i) {
System.out.print("Enter the grade for student " + (i + 1) + ": ");
grades[i] = in.nextInt();
}
// Print array for debugging
System.out.println(Arrays.toString(grades));
// Populate the histogram bins
for (int grade : grades) {
if (grade == 100) { // Need to handle 90-100 separately as it has 11 items.
++bins[9];
} else {
++bins[grade/10];
}
}
// Print array for debugging
System.out.println(Arrays.toString(bins));
// Print the horizontal histogram
// Rows are the histogram bins[0] to bins[9]
// Columns are the counts in each bins[i]
for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
// Print label
if (binIdx != 9) { // Need to handle 90-100 separately as it has 11 items
System.out.printf("%2d-%3d: ", binIdx*10, binIdx*10+9);
} else {
System.out.printf("%2d-%3d: ", 90, 100);
}
// Print columns of stars
for (int itemNo = 0; itemNo < bins[binIdx]; ++itemNo) { // one star per item
System.out.print("*");
}
System.out.println();
}
// Find the max value among the bins
int binMax = bins[0];
for (int binIdx = 1; binIdx < bins.length; ++binIdx) {
if (binMax < bins[binIdx]) binMax = bins[binIdx];
}
// Print the Vertical histogram
// Columns are the histogram bins[0] to bins[9]
// Rows are the levels from binMax down to 1
for (int level = binMax; level > 0; --level) {
for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
if (bins[binIdx] >= level) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Print label
for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
System.out.printf("%3d-%-3d", binIdx*10, (binIdx != 9) ? binIdx * 10 + 9 : 100);
// Use '-' flag for left-aligned
}
System.out.println();
in.close(); // Close the Scanner
}
}
import java.util.Scanner;
/**
* Guess a secret number between 0 and 99.
*/
public class NumberGuess {
public static void main(String[] args) {
// Define variables
int secretNumber; // Secret number to be guessed
int numberIn; // The guessed number entered
int trialNumber = 0; // Number of trials so far
boolean done = false; // boolean flag for loop control
Scanner in = new Scanner(System.in);
// Set up the secret number: Math.random() generates a double in [0.0, 1.0)
secretNumber = (int)(Math.random()*100);
// Use a while-loop to repeatedly guess the number until it is correct
while (!done) {
++trialNumber;
System.out.print("Enter your guess (between 0 and 99): ");
numberIn = in.nextInt();
if (numberIn == secretNumber) {
System.out.println("Congratulation");
done = true;
} else if (numberIn < secretNumber) {
System.out.println("Try higher");
} else {
System.out.println("Try lower");
}
}
System.out.println("You got in " + trialNumber +" trials");
in.close();
}
}
Enter the grade for student 1: 98
Enter the grade for student 2: 100
Enter the grade for student 3: 9
Enter the grade for student 4: 3
Enter the grade for student 5: 56
Enter the grade for student 6: 58
Enter the grade for student 7: 59
Enter the grade for student 8: 87
0- 9: **
10- 19:
20- 29:
30- 39:
40- 49:
50- 59: ***
60- 69:
70- 79:
80- 89: *
90-100: **
*
* * *
* * * *
0-9 10-19 20-29 30-39 40-49 50-59 60-69 70-79 80-89 90-100