Throughout history, computers have been used to process data, and to make math problems such as cryptography easier for humans. While their use today has extended far beyond math, the goal of a computer is to make our lives easier, and the goal of the computer scientist is to use technology to improve the world around them.
Going back to the simple uses of a computer, you are going to build a simple program that uses both methods and basic operations to answer arithmetic questions. While simple, it is important to pay attention to how we are using the Divide-Conquer-Glue approach breaking up each part of the program into small simple pieces. It would be simple to add another math operation.
In this lab you will learn:
- Basic Operations
- More System.out.print/println
- Using methods to divide a problem into smaller parts.
You will notice we provided two methods for you. main and run. You should not modify anything in the run method, but we encourage you to read through it. The run method is the ‘glue’ in the Divide-Conquer-Glue strategy.
What’s in a Name (Step 0)
For all labs, you will want to modify your name and email by the author line
/* * @author YOU NAME <br> * YOUR EMAIL<br> */
You should do this now!
Run Your Program (Step 0.5)
To run your program, make sure you are in the ‘Develop Mode’ and click run. This won’t appear to do anything, but you should type 0 to end the program. One should never write an entire program and then run it. They should instead run frequently and without reservation making sure the lines you type are indeed correct.
Menu Options (Step 1)
Many programs have menu’s of actions to perform. However, the only way to know the options is to tell your client what they are allowed to do. Locate the printOptions TODO comment. This method simply prints several options, and that is it.
You first must write the method signature for this method below the TODO comment. The method is called printOptions() and it is a private static method that does not return anything. If a method does not return anything what is the keyword you should use?
The options you should print are as follows:
Menu Options 1) Add 2) Subtract 3) Multiply 4) Divide 0) Exit Select a menu option:
However, before you print the Menu Options you should print two blank lines. Additionally, the Select a menu option line SHOULD NOT print a newline after it. It should also have a space at the end. Do you think you would use System.out.println or System.out.print?
After you have completed the printOptions() method uncomment run() in the main method. run() will call the printOptions() method so you will be able to see your menu you just wrote.
REMINDER: You should be running your code as you work - just to see what is happening.
Splash Screen (Step 2)
Splash screens show up in many games and in applications. You just have to open up a copy of any mobile game such as Pokemon Go to see a splash screen. These are used to load background ‘assets’ into the memory of the computer, so the program can access them quickly. In your case, we are simply using splash screens so you can practice System.out.println.
First - find the Splash Screen method.
In this method, you will write a series of System.out.println to generate exactly the following in your console / output.
************************* * Welcome * * to * * Computer 1 * *************************
For reference, their are 25 stars in the first line and last line
Add Numbers (Step 3)
Find the add method. This method has already been written for you but you should make sure that you understand what is happening within the method before completing the following methods.
The method signature for the method is:
private static double add(double num1, double num2)
The keyword private means that the method can only be accessed from within this class. The keyword static means that you do not have to create an instance of the class in order to call the add method from a separate method. The keyword double is the return type. So you must either return a double or a variable of type double in this method. add is the name of the method. double num1 and double num2 are the parameters. They are variable of type double that can be accessed anywhere within the add method.
Inside the method the following occurs:
double rtn; rtn = num1+num2; return rtn;
First a variable of type double called rtn is declared. That variable is then assigned to the solution of adding the two parameters (num1 and num2) together. rtn is then returned.
Hint: Use this method as a reference while completing the following methods.
Subtract Numbers (Step 4)
Find the subtract method. You will notice the following method signature.
private static double subtract(double num1, double num2)
num1 and num2 are parameters or variables that are ‘passed’ into the method. You can use them. The purpose of the subtract method is to subtract num2 from num1, and then return the result.
If 5 and 5 are passed in, the result returned should be 0.
If 10 and 20 are passed in, the result returned should be -10.
If 2 and 1 are passed in, the result returned should be 1,.
Once this method is written, you can test the program by running it.
To get you started, you can copy and past the following into your Predefine program input (optional) box
2 5 5 2 10 20 2 2 1 0
That will run every test above, and then exit the program. You should change it and run your own tests. Remember, programming is like playing a musical instrument, you only learn by practicing.
Multiply Numbers (Step 5)
Find the multiply method. You should multiply num1 and num2 together, and return the result. Remember, to run and test it.
Divide Numbers (Step 6)
Find the divide method. You should divide num1 by num2, and return the result.
If you wanted to write a method that ‘squares’ a number, and only takes in one number - how would you go about doing that?
Turning In (Step 7)
Make sure you click through the canvas link to the assignment if you haven’t already. This triggers the linking process, so canvas can get an updated score. As with the previous assignment, you only have five changes to ‘submit for grading’, so it is important to run the program testing it for various cases before you click submit for grading.