Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Write a complete Java program called StringCompare to accept two values of type String from the user.
- Compare the two Strings and output “The two Strings are equal” or “The two Strings are not equal” depending on the result.
- Finally, output the two Strings in lexicographic order. (2 points)
- package stringcompare;
- import java.util.Scanner;
- public class StringCompare {
- public static void main(String[] args) {
- Scanner scnr = new Scanner(System.in);
- String string1 = "";
- String string2 = "";
- System.out.print("Enter Value One: ");
- string1 = scnr.next();
- System.out.print("Enter Value Two: ");
- string2 = scnr.next();
- if (string1.compareTo(string2) == 0) {
- System.out.println("The two Strings are equal");
- }
- else {
- System.out.println("The two Strings are not equal");
- }
- }
- }
- 2. Write a complete Java program called ATM to simulate an ATM transaction.
- The program should start with an initial account balance, which you can set to any value.
- Prompt the user to enter a transaction type which can be Balance, Deposit or Withdrawal.
- If a balance is requested output “Your balance is X” where X is the initial balance.
- For a deposit or withdrawal prompt the user to enter the transaction amount, which should be a double.
- 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.
- If the user attempts to withdraw more than the balance output “Insufficient funds”.
- Then output “Your balance is X” where X is the balance resulting from the transaction. (3 points)
- package atm;
- import java.util.Scanner;
- public class ATM {
- public static void main(String[] args) {
- Scanner scnr = new Scanner(System.in);
- final double initialBalance = 10;
- double withdrawlMoney = 0.0;
- double depositMoney = 0.0;
- double withdrawlBalance = 0.0;
- double depositBalance = 0.0;
- depositBalance = (initialBalance + withdrawlMoney);
- String typeTransaction = "";
- System.out.println("Current Balance: $" + initialBalance);
- System.out.println("Transaction Type? (Balance, Deposit, Withdrawl): ");
- typeTransaction = scnr.next();
- if (typeTransaction.equals("Balance")) {
- System.out.print("You Current Balance Is: $" + initialBalance);
- {
- {else if (withdrawlMoney > 10.0)
- System.out.println("Insufficient Funds");
- System.out.println("Your Balance is: " + initialBalance);
- }}
- else if (typeTransaction.equals("Withdrawl")) {
- System.out.println("Enter Withdrawl Amount: ");
- withdrawlMoney = scnr.nextDouble();
- System.out.println("Your Current Balance Is: $" + (initialBalance - withdrawlMoney));
- }
- else if (typeTransaction.equals("Deposit")) {
- System.out.println("Enter Deposit Amount: ");
- depositMoney = scnr.nextDouble();
- System.out.println("Your Current Balance is: $" + initialBalance + depositMoney);
- }
- }
- }
- 3. Write a complete Java program called IncomeTax to determine the income tax rate for a citizen.
- The user enters their marital status, single or married, and their annual income at the command line.
- The program decides the income tax rate and computes income tax based on the annual income.
- You must use nested if statements to find the tax rate.
- Output from your program should appear as follows:
- Based on an annual income of 25,000 your tax is 3,000.00.
- Your output string should be constructed using both String literals and the two variables for tax rate and income tax.
- (3 points)
- package incometax;
- import java.util.Scanner;
- public class IncomeTax {
- public static void main(String[] args) {
- Scanner scnr = new Scanner(System.in);
- int annualIncome = 0;
- double taxRate = 0.0;
- int payTax = 0;
- String maritalStatus = "";
- System.out.println("Enter Annual Income: ");
- annualIncome = scnr.nextInt();
- System.out.println("Enter Marital Status (Single, Married): ");
- maritalStatus = scnr.next();
- if (annualIncome <= 30000 && maritalStatus.equals("Married")) {
- taxRate = 0.12;
- }
- else if (annualIncome <= 30000 && maritalStatus.equals("Single")){
- taxRate = 0.15;
- }
- else if (annualIncome > 30000 && maritalStatus.equals("Married")) {
- taxRate = .20;
- }
- else {
- taxRate = .25;
- }
- payTax = (int)(annualIncome * taxRate);
- System.out.println("Based on the Annual Income of: " + annualIncome + " your tax rate is " + payTax);
- }
- }
- }
- 4. Write a complete Java program called GameControl.
- This program must use a switch statement that tests a value provided by the user at the command line.
- If the value is 0, you will print to the screen "Up". If the value is 1, you will print to the screen "Down".
- If the value is 2, you will print to the screen "Left". If the value is 3, you will print to the screen "Right".
- If it is any other value, you will print to the screen "Error". (2 points)
- package gamecontrol;
- import java.util.Scanner;
- public class GameControl {
- public static void main(String[] args) {
- Scanner scnr = new Scanner(System.in);
- int userValue = 0;
- System.out.print("Enter Value (0, 1, 2, 3): ");
- userValue = scnr.nextInt();
- switch (userValue){
- case 0:
- System.out.println("Up");
- break;
- case 1:
- System.out.println("Down");
- break;
- case 2:
- System.out.println("Left");
- break;
- case 3:
- System.out.println("Right");
- break;
- default:
- System.out.println("Error");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement