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

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

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

  • 1. display a dialog box which contains the message "Input a value for size (>0)"; 2....

    1. display a dialog box which contains the message "Input a value for size (>0)"; 2. assume the input is a number, check if it is less than or equal to zero. If so, display the message "Size must be > 0" in another dialog box and go to 1; I got trouble in question 2. After I display the message of "Size must be >0", how can I repeat the step in question 1 and make it like a...

  • By editing the code below to include composition, enums, toString; must do the following: Prompt the...

    By editing the code below to include composition, enums, toString; must do the following: Prompt the user to enter their birth date and hire date (see Fig. 8.7, 8.8 and 8.9 examples) in addition to the previous user input Create a new class that validates the dates that are input (can copy date class from the book) Incorporate composition into your class with these dates Use enums to identify the employee status as fulltime (40 or more hours worked for...

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