Question

How would you write the following program using switch statements instead of if-else statements (in java)...

How would you write the following program using switch statements instead of if-else statements (in java)

import java.util.*;

public class ATM

{

public static void main(String[] args)

{

Scanner input = new Scanner (System.in);

double deposit, withdrawal;

double balance = 6985.00; //The initial balance

int transaction;

System.out.println ("Welcome! Enter the number of your transaction.");

System.out.println ("Withdraw cash: 1");

System.out.println ("Make a Deposit: 2");

System.out.println ("Check your balance: 3");

System.out.println ("Exit: 4");

System.out.println ("***************");

System.out.print ("Enter Your Transaction Number ");

transaction = input.nextInt();

if (transaction == 1)

{

System.out.println ("Enter the withdrawal amount ");

withdrawal = input.nextDouble();

if (withdrawal > balance)

System.out.println ("Invalid withdrawal amount");

else

{

balance -= withdrawal;

System.out.println ("Your withdrawal is $" +

withdrawal + "Your new balance is $" + balance);

}

}

else if (transaction == 2)

{

System.out.println ("Enter the deposit amount ");

deposit =input.nextDouble();

balance +=deposit;

System.out.println ("Your deposit is $" +deposit + "Your new balance is $" + balance);

}

else if (transaction == 3)

System.out.println("Your total balance is $"+balance);

else if (transaction==4 ){

System.exit(0);

}

else

System.out.println("Wrong input");

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.*;

public class ATM{
  
public static void main(String[] args)

{

Scanner input = new Scanner (System.in);

double deposit, withdrawal;

double balance = 6985.00; //The initial balance

int transaction;

System.out.println ("Welcome! Enter the number of your transaction.");

System.out.println ("Withdraw cash: 1");

System.out.println ("Make a Deposit: 2");

System.out.println ("Check your balance: 3");

System.out.println ("Exit: 4");

System.out.println ("***************");

System.out.print ("Enter Your Transaction Number ");

transaction = input.nextInt();
switch(transaction){
case 1:System.out.println ("Enter the withdrawal amount ");
withdrawal = input.nextDouble();
switch(withdrawal > balance ? 1:0){ // to check for greater
case 1: System.out.println ("Invalid withdrawal amount");
break;
case 0: balance -= withdrawal;
System.out.println ("Your withdrawal is $" +
withdrawal + "Your new balance is $" + balance);
break;
}
break;
case 2: System.out.println ("Enter the deposit amount ");
deposit =input.nextDouble();
balance +=deposit;
System.out.println ("Your deposit is $" +deposit + "Your new balance is $" + balance);
break;
case 3: System.out.println("Your total balance is $"+balance);
break;
case 4: System.exit(0);
break;
default: System.out.println("Wrong input");
}

}

}

/* OUTPUT */

/* PLEASE UPVOTE */

Add a comment
Know the answer?
Add Answer to:
How would you write the following program using switch statements instead of if-else statements (in java)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a Java program to count all words in a string. User inputs a string...

    1. Write a Java program to count all words in a string. User inputs a string sentence and your program should count the number of words in that string. Test Data: Input the string: The quick brown fox jumps over the lazy dog. Expected Output: Number of words in the string: 9 2. Write a class called ATMTransaction. This class has a variable balance and account number. It has three methods, checkBalance, deposit and withdraws along with constructors getters and...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • please help me with this problem The following program uses if-else-if statements to determine a letter...

    please help me with this problem The following program uses if-else-if statements to determine a letter grade. Rewrite it using if statements with logical operators (&&, ||, !) to produce the same output when given the same inputs. import java.util.Scanner; public class GradeCalculator{ public static void main(String[] args){ int grade; Scanner input = new Scanner(System.in); System.out.println("Enter numeric grade out of 100: "); grade = input.nextInt(); if (grade >= 90){ System.out.println("Congratulations! You got an A!"); } else if (grade >= 80){...

  • This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying...

    This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and how can I fix this?? import java.util.Scanner; import java.io.*; public class Topic7Hw {    static Scanner sc = new Scanner (System.in);       public static void main(String[] args) throws IOException {               PrintWriter outputFile = new PrintWriter ("output.txt");               outputFile.println("hello");               final int MAX_NUM = 10;...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • NOTE: Use the Account class codes in the section below these questions. Modify the main method...

    NOTE: Use the Account class codes in the section below these questions. Modify the main method in the CheckingAccountDemo class: Take out all previous code. Declare an object array with 10 accounts. Create a for loop to ask user to input each account’s information (name, account number, and initial balance) from keyboard and then initialize for each account object. Create a while loop to allow one to work on depositing and withdrawing operations on any account till one want to...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT