ticket sales java program code example

Example 1: ticket sales java program

import java.util.Scanner;
/**
 * Prompt user for an int, and print its equivalent hexadecimal number.
 */
public class Dec2Hex {
   public static void main(String[] args) {
      // Declare variables
      int dec;              // The input decimal number in "int"
      String hexStr = "";   // The equivalent hex String, to accumulate from an empty String
      int radix = 16;       // Hex radix
      char[] hexChars =     // Use this array as lookup table for converting 0-15 to 0-9A-F
         {'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'};
      Scanner in = new Scanner(System.in);
   
      // Prompt and read input as "int"
      System.out.print("Enter a decimal number: ");
      dec = in.nextInt();
   
      // Repeated modulus/division and get the hex digits (0-15) in reverse order
      while (dec > 0) {
         int hexDigit = dec % radix;   // 0-15
         hexStr = hexChars[hexDigit] + hexStr;  // Append in front of the hex string corresponds to reverse order
         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];   // a 12×8 grid of int
grid[0][0] = 8;
grid[1][1] = 5;
System.out.println(grid.length);      // 12
System.out.println(grid[0].length);   // 8
System.out.println(grid[11].length);  // 8

Example 4: ticket sales java program

import java.util.Scanner;
/**
 * Prompt user for a hexadecimal string, and print its binary equivalent.
 */
public class Hex2Bin {
   public static void main(String[] args) {
      // Define variables
      String hexStr;     // The input hexadecimal String
      int hexStrLen;     // The length of hexStr
      char hexChar;      // Each char in the hexStr
      String binStr =""; // The equivalent binary String, to accumulate from an empty String
      // Lookup table for the binary sub-string corresponding to Hex digit '0' (index 0) to 'F' (index 15)
      String[] binStrs =
         {"0000","0001","0010","0011","0100","0101","0110","0111",
          "1000","1001","1010","1011","1100","1101","1110","1111"};
      Scanner in = new Scanner(System.in);

      // Prompt and read input as "String"
      System.out.print("Enter a Hexadecimal string: ");
      hexStr = in.next();
      hexStrLen = hexStr.length();

      // Process the string from the left (most-significant hex digit)
      for (int charIdx = 0; charIdx < hexStrLen; ++charIdx) {
         hexChar = hexStr.charAt(charIdx);
         if (hexChar >= '0' && hexChar <= '9') {
            binStr += binStrs[hexChar - '0'];  // index into the binStrs array and concatenate
         } 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;   // or System.exit(1);
         }
      }
      System.out.println("The equivalent binary for \"" + hexStr + "\" is \"" + binStr + "\"");
      in.close();
   }
}

Example 5: ticket sales java program

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();
   }
}

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;   // 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.
 * 5. Repeat until user enter -1.
 */
public class IncomeTaxCalculatorSentinel {
   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;
      final int SENTINEL = -1;    // Terminating value for input

      // Declare variables
      int taxableIncome;
      double taxPayable;
      Scanner in = new Scanner(System.in);

      // Read the first input to "seed" the while loop
      System.out.print("Enter the taxable income: $");
      taxableIncome = in.nextInt();

      while (taxableIncome != SENTINEL) {
         // Compute tax payable in "double" using a nested-if to handle 4 cases
         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;
         }

         // Print result rounded to 2 decimal places
         System.out.printf("The income tax payable is: $%.2f%n", taxPayable);

         // Read the next input
         System.out.print("Enter the taxable income: $");
         taxableIncome = in.nextInt();
         // Repeat the loop body, only if the input is not the SENTINEL value.
         // Take note that you need to repeat these two statements inside/outside the loop!
      }
      System.out.println("bye!");
      in.close();  // Close Scanner
   }
}

Example 8: ticket sales java program

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
   }
}

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;   // 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
   }
}

Tags:

Java Example