Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. 1. Write a complete Java program called StringCompare to accept two values of type String from the user.
  2. Compare the two Strings and output “The two Strings are equal” or “The two Strings are not equal” depending on the result.
  3. Finally, output the two Strings in lexicographic order. (2 points)
  4.  
  5. package stringcompare;
  6. import java.util.Scanner;
  7. public class StringCompare {
  8.  
  9. public static void main(String[] args) {
  10. Scanner scnr = new Scanner(System.in);
  11.  
  12. String string1 = "";
  13. String string2 = "";
  14.  
  15. System.out.print("Enter Value One: ");
  16. string1 = scnr.next();
  17.  
  18. System.out.print("Enter Value Two: ");
  19. string2 = scnr.next();
  20.  
  21. if (string1.compareTo(string2) == 0) {
  22. System.out.println("The two Strings are equal");
  23. }
  24. else {
  25. System.out.println("The two Strings are not equal");
  26. }
  27.  
  28.  
  29.  
  30. }
  31.  
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. 2. Write a complete Java program called ATM to simulate an ATM transaction.
  41. The program should start with an initial account balance, which you can set to any value.
  42. Prompt the user to enter a transaction type which can be Balance, Deposit or Withdrawal.
  43. If a balance is requested output “Your balance is X” where X is the initial balance.
  44. For a deposit or withdrawal prompt the user to enter the transaction amount, which should be a double.
  45. Add or subtract this transaction amount from the initial balance and then output “Your current balance is X” where X is the balance after the transaction.
  46. If the user attempts to withdraw more than the balance output “Insufficient funds”.
  47. Then output “Your balance is X” where X is the balance resulting from the transaction. (3 points)
  48.  
  49. package atm;
  50. import java.util.Scanner;
  51. public class ATM {
  52.  
  53. public static void main(String[] args) {
  54. Scanner scnr = new Scanner(System.in);
  55.  
  56. final double initialBalance = 10;
  57. double withdrawlMoney = 0.0;
  58. double depositMoney = 0.0;
  59. double withdrawlBalance = 0.0;
  60. double depositBalance = 0.0;
  61.  
  62. depositBalance = (initialBalance + withdrawlMoney);
  63. String typeTransaction = "";
  64.  
  65. System.out.println("Current Balance: $" + initialBalance);
  66. System.out.println("Transaction Type? (Balance, Deposit, Withdrawl): ");
  67. typeTransaction = scnr.next();
  68.  
  69. if (typeTransaction.equals("Balance")) {
  70. System.out.print("You Current Balance Is: $" + initialBalance);
  71. {
  72. {else if (withdrawlMoney > 10.0)
  73. System.out.println("Insufficient Funds");
  74. System.out.println("Your Balance is: " + initialBalance);
  75. }}
  76. else if (typeTransaction.equals("Withdrawl")) {
  77. System.out.println("Enter Withdrawl Amount: ");
  78. withdrawlMoney = scnr.nextDouble();
  79. System.out.println("Your Current Balance Is: $" + (initialBalance - withdrawlMoney));
  80. }
  81. else if (typeTransaction.equals("Deposit")) {
  82. System.out.println("Enter Deposit Amount: ");
  83. depositMoney = scnr.nextDouble();
  84. System.out.println("Your Current Balance is: $" + initialBalance + depositMoney);
  85. }
  86.  
  87. }
  88.  
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. 3. Write a complete Java program called IncomeTax to determine the income tax rate for a citizen.
  97. The user enters their marital status, single or married, and their annual income at the command line.
  98. The program decides the income tax rate and computes income tax based on the annual income.
  99. You must use nested if statements to find the tax rate.
  100. Output from your program should appear as follows:
  101. Based on an annual income of 25,000 your tax is 3,000.00.
  102. Your output string should be constructed using both String literals and the two variables for tax rate and income tax.
  103. (3 points)
  104.  
  105. package incometax;
  106. import java.util.Scanner;
  107. public class IncomeTax {
  108. public static void main(String[] args) {
  109. Scanner scnr = new Scanner(System.in);
  110.  
  111. int annualIncome = 0;
  112. double taxRate = 0.0;
  113. int payTax = 0;
  114. String maritalStatus = "";
  115.  
  116. System.out.println("Enter Annual Income: ");
  117. annualIncome = scnr.nextInt();
  118.  
  119. System.out.println("Enter Marital Status (Single, Married): ");
  120. maritalStatus = scnr.next();
  121.  
  122. if (annualIncome <= 30000 && maritalStatus.equals("Married")) {
  123. taxRate = 0.12;
  124. }
  125. else if (annualIncome <= 30000 && maritalStatus.equals("Single")){
  126. taxRate = 0.15;
  127. }
  128. else if (annualIncome > 30000 && maritalStatus.equals("Married")) {
  129. taxRate = .20;
  130. }
  131. else {
  132. taxRate = .25;
  133. }
  134. payTax = (int)(annualIncome * taxRate);
  135. System.out.println("Based on the Annual Income of: " + annualIncome + " your tax rate is " + payTax);
  136. }
  137.  
  138.  
  139. }
  140.  
  141. }
  142.  
  143.  
  144.  
  145.  
  146. 4. Write a complete Java program called GameControl.
  147.  
  148. This program must use a switch statement that tests a value provided by the user at the command line.
  149. If the value is 0, you will print to the screen "Up". If the value is 1, you will print to the screen "Down".
  150. If the value is 2, you will print to the screen "Left". If the value is 3, you will print to the screen "Right".
  151. If it is any other value, you will print to the screen "Error". (2 points)
  152.  
  153. package gamecontrol;
  154. import java.util.Scanner;
  155. public class GameControl {
  156.  
  157. public static void main(String[] args) {
  158. Scanner scnr = new Scanner(System.in);
  159. int userValue = 0;
  160.  
  161. System.out.print("Enter Value (0, 1, 2, 3): ");
  162. userValue = scnr.nextInt();
  163.  
  164. switch (userValue){
  165. case 0:
  166. System.out.println("Up");
  167. break;
  168. case 1:
  169. System.out.println("Down");
  170. break;
  171. case 2:
  172. System.out.println("Left");
  173. break;
  174. case 3:
  175. System.out.println("Right");
  176. break;
  177.  
  178. default:
  179. System.out.println("Error");
  180. break;
  181. }
  182.  
  183.  
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement