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
  • 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 =...

  • 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...

  • // 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...

  • Here is the finsihed java program, it just needs to be converted to MIPs assembly language:...

    Here is the finsihed java program, it just needs to be converted to MIPs assembly language: import java.util.*; public class LoveCS { public static void main(String[] args) { int timesMessagePrinted; int count = 0; int sum = 0; Scanner input = new Scanner(System.in); System.out.print("How many times should the message be printed?: "); timesMessagePrinted = input.nextInt(); for(int x = 1; x <= timesMessagePrinted; x++) { System.out.println(x +" I love Computer Science!!"); count++; sum += x; } System.out.print("Printed this message " +count...

  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

  • Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems....

    Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems. You have to do both of your java codes based on the two provided Java codes. Create one method for depositing and one for withdrawing. The deposit method should have one parameter to indicate the amount to be deposited. You must make sure the amount to be deposited is positive. The withdraw method should have one parameter to indicate the amount to be withdrawn...

  • Write a JAVA program that prompts the user for student grades and displays the highest and...

    Write a JAVA program that prompts the user for student grades and displays the highest and lowest grades in the class. The user should enter a character to stop providing values. Starter code: import java.util.Scanner; public class MaxMinGrades{   public static void main(String[] args){     Scanner input = new Scanner(System.in);     System.out.println("Enter as many student grades as you like. Enter a character to stop.");     double grade = input.nextDouble();     double minGrade = Double.MAX_VALUE;     double maxGrade = Double.MIN_VALUE;     while (Character.isDigit(grade)) {       if (grade == 0)...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

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
ADVERTISEMENT