Writing Methods that Return a Value in Java In this lab, you complete a partially written Java program that includes a method that returns a value. The program is a simple calculator that prompts the user for two numbers and an operator ( +, −, *, /,or% ). The two numbers and the operator are passed to the method where the appropriate arithmetic operation is performed. The result is then returned to the main() method where the arithmetic operation and result are displayed. For example, if the user enters 3, 4, and *, the following is displayed: 3.00 * 4.00 = 12.00 The source code file provided for this lab includes the necessary variable declarations, and input and output statements. Comments are included in the file to help you write the remainder of the program. Instructions Write the Java statements as indicated by the comments. Execute the program Tasks Defined performOperation() Input / output tests Multiplication input / output test Division input / output test Subtraction input / output test Addition input / output test performOperation unit tests performOperation addition test performOperation multiplication test

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Writing Methods that Return a Value in Java

In this lab, you complete a partially written Java program that includes a method that returns a value. The program is a simple calculator that prompts the user for two numbers and an operator ( +, −, *, /,or% ). The two numbers and the operator are passed to the method where the appropriate arithmetic operation is performed. The result is then returned to the main() method where the arithmetic operation and result are displayed. For example, if the user enters 3, 4, and *, the following is displayed:

3.00 * 4.00 = 12.00

The source code file provided for this lab includes the necessary variable declarations, and input and output statements. Comments are included in the file to help you write the remainder of the program.

Instructions

  1. Write the Java statements as indicated by the comments.
  2. Execute the program

Tasks

  1. Defined performOperation()
  2. Input / output tests
    1. Multiplication input / output test
    2. Division input / output test
    3. Subtraction input / output test
    4. Addition input / output test
  3. performOperation unit tests
    1. performOperation addition test
    2. performOperation multiplication test

Use The Given Code

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

import java.util.Scanner;

public class Calculator
{
  public static void main(String args[]) 
  {
    double numberOne, numberTwo;                
    String numberOneString, numberTwoString;
    String operation;
    double result;
    Scanner input = new Scanner(System.in);
            
    System.out.println("Enter the first number: ");
    numberOneString = input.nextLine();
    numberOne = Double.parseDouble(numberOneString);
    System.out.println("Enter the second number: ");
    numberTwoString = input.nextLine();
    numberTwo = Double.parseDouble(numberTwoString); 
    System.out.println("Enter an operator (+.-.*,/,%): ");
    operation = input.nextLine();
    
    // 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 Calculator class.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education