Example 1: ticket sales java program
import java.util.Scanner;
public class Dec2Hex {
public static void main(String[] args) {
int dec;
String hexStr = "";
int radix = 16;
char[] hexChars =
{'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'};
Scanner in = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
dec = in.nextInt();
while (dec > 0) {
int hexDigit = dec % radix;
hexStr = hexChars[hexDigit] + hexStr;
dec = dec / radix;
}
System.out.println("The equivalent hexadecimal number is " + hexStr);
in.close();
}
}
Example 2: ticket sales java program
Enter the taxable income: $41000
The income tax payable is: $2200.00
Enter the taxable income: $62000
The income tax payable is: $6600.00
Enter the taxable income: $73123
The income tax payable is: $9936.90
Enter the taxable income: $84328
The income tax payable is: $13298.40
Enter the taxable income: $-1
bye!
Example 3: ticket sales java program
int grid[][] = new int[12][8];
grid[0][0] = 8;
grid[1][1] = 5;
System.out.println(grid.length);
System.out.println(grid[0].length);
System.out.println(grid[11].length);
Example 4: ticket sales java program
import java.util.Scanner;
public class Hex2Bin {
public static void main(String[] args) {
String hexStr;
int hexStrLen;
char hexChar;
String binStr ="";
String[] binStrs =
{"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"};
Scanner in = new Scanner(System.in);
System.out.print("Enter a Hexadecimal string: ");
hexStr = in.next();
hexStrLen = hexStr.length();
for (int charIdx = 0; charIdx < hexStrLen; ++charIdx) {
hexChar = hexStr.charAt(charIdx);
if (hexChar >= '0' && hexChar <= '9') {
binStr += binStrs[hexChar - '0'];
} else if (hexChar >= 'a' && hexChar <= 'f') {
binStr += binStrs[hexChar - 'a' + 10];
} else if (hexChar >= 'A' && hexChar <= 'F') {
binStr += binStrs[hexChar - 'A' + 10];
} else {
System.err.println("error: invalid hex string \"" + hexStr + "\"");
return;
}
}
System.out.println("The equivalent binary for \"" + hexStr + "\" is \"" + binStr + "\"");
in.close();
}
}
Example 5: ticket sales java program
import java.util.Scanner;
public class NumberGuess {
public static void main(String[] args) {
int secretNumber;
int numberIn;
int trialNumber = 0;
boolean done = false;
Scanner in = new Scanner(System.in);
secretNumber = (int)(Math.random()*100);
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();
}
}
Example 6: ticket sales java program
Enter a Hexadecimal string: 1bE3
The equivalent binary for "1bE3" is "0001101111100011"
Example 7: ticket sales java program
import java.util.Scanner;
public class IncomeTaxCalculatorSentinel {
public static void main(String[] args) {
final double TAX_RATE_ABOVE_20K = 0.1;
final double TAX_RATE_ABOVE_40K = 0.2;
final double TAX_RATE_ABOVE_60K = 0.3;
final int SENTINEL = -1;
int taxableIncome;
double taxPayable;
Scanner in = new Scanner(System.in);
System.out.print("Enter the taxable income: $");
taxableIncome = in.nextInt();
while (taxableIncome != SENTINEL) {
if (taxableIncome > 60000) {
taxPayable = 20000 * TAX_RATE_ABOVE_20K
+ 20000 * TAX_RATE_ABOVE_40K
+ (taxableIncome - 60000) * TAX_RATE_ABOVE_60K;
} else if (taxableIncome > 40000) {
taxPayable = 20000 * TAX_RATE_ABOVE_20K
+ (taxableIncome - 40000) * TAX_RATE_ABOVE_40K;
} else if (taxableIncome > 20000) {
taxPayable = (taxableIncome - 20000) * TAX_RATE_ABOVE_20K;
} else {
taxPayable = 0;
}
System.out.printf("The income tax payable is: $%.2f%n", taxPayable);
System.out.print("Enter the taxable income: $");
taxableIncome = in.nextInt();
}
System.out.println("bye!");
in.close();
}
}
Example 8: ticket sales java program
import java.util.Scanner;
import java.util.Arrays;
public class GradesHistograms {
public static void main(String[] args) {
int numStudents;
int[] grades;
int[] bins = new int[10];
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of students: ");
numStudents = in.nextInt();
grades = new int[numStudents];
for (int i = 0; i < grades.length; ++i) {
System.out.print("Enter the grade for student " + (i + 1) + ": ");
grades[i] = in.nextInt();
}
System.out.println(Arrays.toString(grades));
for (int grade : grades) {
if (grade == 100) {
++bins[9];
} else {
++bins[grade/10];
}
}
System.out.println(Arrays.toString(bins));
for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
if (binIdx != 9) {
System.out.printf("%2d-%3d: ", binIdx*10, binIdx*10+9);
} else {
System.out.printf("%2d-%3d: ", 90, 100);
}
for (int itemNo = 0; itemNo < bins[binIdx]; ++itemNo) {
System.out.print("*");
}
System.out.println();
}
int binMax = bins[0];
for (int binIdx = 1; binIdx < bins.length; ++binIdx) {
if (binMax < bins[binIdx]) binMax = bins[binIdx];
}
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();
}
for (int binIdx = 0; binIdx < bins.length; ++binIdx) {
System.out.printf("%3d-%-3d", binIdx*10, (binIdx != 9) ? binIdx * 10 + 9 : 100);
}
System.out.println();
in.close();
}
}
Example 9: ticket sales java program
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
Example 10: ticket sales java program
import java.util.Scanner;
public class IncomeTaxCalculator {
public static void main(String[] args) {
final double TAX_RATE_ABOVE_20K = 0.1;
final double TAX_RATE_ABOVE_40K = 0.2;
final double TAX_RATE_ABOVE_60K = 0.3;
int taxableIncome;
double taxPayable;
Scanner in = new Scanner(System.in);
System.out.print("Enter the taxable income: $");
taxableIncome = in.nextInt();
if (taxableIncome <= 20000) {
taxPayable = 0;
} else if (taxableIncome <= 40000) {
taxPayable = (taxableIncome - 20000) * TAX_RATE_ABOVE_20K;
} else if (taxableIncome <= 60000) {
taxPayable = 20000 * TAX_RATE_ABOVE_20K
+ (taxableIncome - 40000) * TAX_RATE_ABOVE_40K;
} else {
taxPayable = 20000 * TAX_RATE_ABOVE_20K
+ 20000 * TAX_RATE_ABOVE_40K
+ (taxableIncome - 60000) * TAX_RATE_ABOVE_60K;
}
System.out.printf("The income tax payable is: $%.2f%n", taxPayable);
in.close();
}
}