Question

// Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % ) on two...

// Arithmetic2.java - This program performs arithm

// Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input:  Interactive.
// Output:  Result of arithmetic operation

import javax.swing.*;

public class Arithmetic2
{
        public static void main(String args[]) 
        {
                double numberOne, numberTwo;                    
                String numberOneString, numberTwoString;
                String operation;
                double result;  
                                                
                numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
                numberOne = Double.parseDouble(numberOneString); 
                numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
                numberTwo = Double.parseDouble(numberTwoString); 
                operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
                
                // Call performOperation method here            
                

                System.out.format("%.2f",numberOne);
                System.out.print(" " + operation + " ");
                System.out.format("%.2f", numberTwo);
                System.out.print(" = ");
                System.out.format("%.2f", result);
        
                System.exit(0);

        } // End of main() method.
        
        
        // Write performOperation method here.
        

} // End of Arithmetic2 class.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input: Interactive.
// Output: Result of arithmetic operation

import javax.swing.*;

public class Arithmetic2 {
   public static void main(String args[]) {
       double numberOne, numberTwo;
       String numberOneString, numberTwoString;
       String operation;
       double result = 0.0;

       numberOneString = JOptionPane
               .showInputDialog("Enter the first number: ");
       numberOne = Double.parseDouble(numberOneString);
       numberTwoString = JOptionPane
               .showInputDialog("Enter the second number: ");
       numberTwo = Double.parseDouble(numberTwoString);
       operation = JOptionPane
               .showInputDialog("Enter an operator (+.-.*,/,%): ");

       // Call performOperation method here
       result = performOperation(numberOne, numberTwo, operation);
       System.out.format("%.2f", numberOne);
       System.out.print(" " + operation + " ");
       System.out.format("%.2f", numberTwo);
       System.out.print(" = ");
       System.out.format("%.2f", result);

       System.exit(0);

   } // End of main() method.

   // Write performOperation method here.
   public static double performOperation(double numberOne, double numberTwo,
           String operation) {

       double result = 0.0;

       char op = operation.charAt(0);
       switch (op) {
       case '+':
           result = numberOne + numberTwo;
           break;
       case '-':
           result = numberOne - numberTwo;
           break;
       case '*':
           result = numberOne * numberTwo;
           break;
       case '/':
           result = numberOne / numberTwo;
           break;
       case '%':
           result = numberOne % numberTwo;
           break;

       default:
           break;
       }

       return result;

   }
} // End of Arithmetic2 class.

OUTPUT:

Input Input Enter the first number: Enter the second number: 3 4 OK Cancel OK Cancel 28 29 30 Input Enter an operator (+*,%):


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
// Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % ) on two...
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
  • In this lab, you complete a partially written Java program that includes methods that require multiple...

    In this lab, you complete a partially written Java program that includes methods that require multiple parameters (arguments). The program prompts the user for two numeric values. Both values should be passed to methods named calculateSum(), calculateDifference(), and calculateProduct(). The methods compute the sum of the two values, the difference between the two values, and the product of the two values. Each method should perform the appropriate computation and display the results. The source code file provided for this lab...

  • Complete a partially written C++ program that includes a function that return a value. The program...

    Complete a partially written C++ program that includes a function that return a value. The program is a simple calculator that prompts the user of 2 number and an operation (+, -, * or, /). The two number and the operator are passed to the function where the appropriate arithmetic operation is performed. The result is return to the main function () where the arithmetic operation and result are displayed. For example 3 * 4 = 12 The source code...

  • JAVA Please debug. Has 2 errors. Thanks ---------------------------------------- import javax.swing.*; public class UseCustomerAccount {     public...

    JAVA Please debug. Has 2 errors. Thanks ---------------------------------------- import javax.swing.*; public class UseCustomerAccount {     public static void main(String[] args)     {         int num;         double balance;         String input;                 input = JOptionPane.showInputDialog(null, "Enter an account number");         num = Integer.parseInt(input);         input = JOptionPane.showInputDialog(null, "Enter a balance due");         balance = Double.parseDouble(input);                 try         {             CustomerAccount ca = new CustomerAccount(num, balance);             JOptionPane.showMessageDialog(null, "Customer #" + num + " has a balance of...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • THIS CODE SHOULD BE MODIFIED TO READ SuperMarket.dat instead of Inputting and Outputting. SuperMarket.dat Monday 4...

    THIS CODE SHOULD BE MODIFIED TO READ SuperMarket.dat instead of Inputting and Outputting. SuperMarket.dat Monday 4 Monday 6 Monday 2 Tuesday 3 Tuesday 2 Wednesday 3 Wednesday 5 Wednesday 7 Thursday 5 Friday 4 Friday 3 Friday 4 Saturday 8 Saturday 8 Saturday 8 Sunday 0 QUESTION: Write the control break code, including the code for the dayChange() method, in the main() method. // SuperMarket.java - This program creates a report that lists weekly hours worked // by employees of...

  • Create a ComplexNo class for two imaginary numbers. Provide (in addition to add, subtract, multiply, and...

    Create a ComplexNo class for two imaginary numbers. Provide (in addition to add, subtract, multiply, and divide methods) a method for equals (which returns boolean), and toString( ) method (that returns a string representing the receiving object). Throw an exception if an attempt is made to divide by 0 (0 + 0i). Your implementation should return the results of each operation WITHOUT changing the operands. Exxample: Enter the first complex number's real part => 1.0 Enter the first complex number's...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

  • 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