Question: Java Programming This assignment will show you a very simple practical application with the Java programming language. You will create your own calculator which performs

Java Programming

This assignment will show you a very simple practical application with the Java programming language. You will create your own calculator which performs four basic arithmetic operations (addition, subtraction, multiplication, division), in addition to one special operator which will be discussed later in this document. In the process you will practice conditional statements.

import java.util.*;

import java.io.*;

public class calculator

{

private int currentValue;

private Scanner scan;

public Calculator(){

// TODO: Finish constructor by setting up Scanner and current value

} public void printDirections()

// TODO: Print directions

}

public void run(){

printDirections(); boolean keepGoing = true; System.out.println("Starting value: " + currentValue);

while(keepGoing){ //code we will see soon...

char operation = askForOperation(); if(operation == 'q' || operation == 'Q'){ //q is the quit value

keepGoing = false; //make the loop stop

} else {

executeOperation(operation); //do the required math

System.out.println("Updated Value: " + currentValue);

}

}

} public char askForOperation(){

// TODO: Ask the user for the operation (or q to quit) // Convert the string to a char answer (can assume good input)

}

// TODO: Finish this method

public void executeOperation(char operator){

int val = scan.nextInt(); // the operand

// TODO: match the operand and perform the calculation

} //Main is here for running the program, like the book does.

public static void main(String[] args) {

Calculator c = new Calculator();

c.run();

}

}

}

Step 3: Finish the constructor, setting up the Scanner object and the currentValue for the Calculator (it should start at 0.)

Step 4: Print a set of directions for the user of the calculator, heres one example. Feel free to be creative with your directions (it does not have to match this)

Step 5: Finish the executeOperation method. The method takes in the operator as a parameter, and gets the second number in a local variable called val. Now, use your newfound knowledge of if/else blocks to perform the right calculation, updating the instance variable (currentValue) with the newly calculated value. For the ^ operator, you will need to use the Math.pow method, and cast the result back to an int. For the ? operator, use the Math.random method as part of the calculation (and since that method returns a double, you will also need to cast the result back to an int. If the operator is not one of the known 7, then print a message that the operator was not recognized.

Final Product:

The image to the right shows an example of what your calculator should be able to do.

* Note that, due to integer rounding, sometimes performing an operation and its reverse operation back- to-back, you will not get the same result. For instance, if you start with 5000, and divide by 432, and then multiply by 432, you will not get the same result, since some precision was lost in the division.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!