NEED HELP ON JAVA HWK/Computer Science

NEED HELP ON JAVA HWK/Computer Science

ITP 109: Introduction to Java Programming Assignment 04: Programming your own command-line calculator 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.

Procedure

Step 1: Open BlueJ and create a new project with the title “Assignment04-username” where username is your USC username (your email address without the @usc.edu)

Step 2: Create a new class in this project with the title “Calculator”, remove the template code from BlueJ and insert the following starter code into the Calculator.java

import java.util.*; import java.io.*; // Your Name // ITP 109, Fall 2017 // Homework XX // USC email 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 for the user } //The run method controls the loop for the calculator to keep going // You do not need to make in changes within this method 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, here’s 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.

Deliverables

A compressed Assignment04-username folder containing a file named Calculator.java. It must be submitted through Blackboard. Here are the instructions for submission:

1) Navigate to the ITP-109 directory/folder and find your Assignment04 project folder. 2) Create a compressed folder (.zip file) with all your code (includes everything in the folder)

a) On Macs, you can do this by simply right (or control) clicking and selecting “compress” b) On PCs, you have to have a program like winzip or 7-zip that will compress folders to .zip

files. 3) Rename the zip file: assignment04_lastname_firstname.zip 4) Upload zip file to Blackboard site for our course

Please note, if you do not follow the submission instructions, your submission will not be graded. Please make sure you have named your files appropriately.

Grading

Item Points

The Calculator Constructor is Complete & Correct 4

The printDirections method is Complete & Correct 4

The askForOperation method is Complete & Correct 6

The executeOperation Method is Complete & Correct (7 operators (+ 1 catch-all else), 2 points each. )

16

All Comments Present for Class Calculator 2

Program Compiles and Runs 2

Program Zipped & Submitted Properly on Blackboard 2

TOTAL 36

Remember that looking at, sharing, or copying anyone else’s code, is considered cheating. If you need help with something, ask in class, ask one of the instructors during office hours, ask a TA in office hours, ask on Piazza, or ask a peer to verbally explain a concept to you.

Order from us and get better grades. We are the service you have been looking for.