CS180 LAB 4

 

OVERVIEW

 

Today, you will implement a simple calculator in Java. The calculator will implement only a few arithmetic functions.

 

The objectives of this lab are as follows:

 

·         Utilize multiple methods to execute different functions

·         Use conditionals (if-statements) to determine different program execution scenarios

·         Get more experience using loops (for-loops/while-loops/do-while-loops) in Java

·         Get more practice with arithmetic operations in Java

 

SETUP

 

Please follow these steps before starting the lab:

 

> cd CS180

 

> mkdir lab04

 

This will create a new folder named “lab4” in your CS180 folder. Turn this folder in for submission.

 

After creation, please download Calculator.java into your lab04 folder.

 

Please ask your TA if you have any questions.

 

CALCULATOR

 

Your calculator will implement the following six arithmetic functions:

1.      Addition (x + y)

2.      Multiplication (x * y)

3.      Exponentiation (xy)

4.      Permutation (xy)

5.      Factorial (x!)

6.      Fibonacci (give the xth Fibonacci number)

 

Getting input

 

Before continuing any further, you should code the main method such that it can accept input from the computer. In this lab, the input will be structured in the following way: operation #1 #2.

 

Operation is the arithmetic function that will be performed, and as such will be any of the following choices:

·         +

·         *

·         pow

·         npr

·         fact

·         fib

#1 will be the first number that your arithmetic function will work with.

 

#2 will be the second number that your arithmetic function will work with. Remember some functions only need one number (i.e. fact will be used for the factorial function, and it only needs one number; as such #2 will not be entered)

 

Recall that to read input, you should utilize the Scanner. As a slight refresher, here is the usual way to use a Scanner:

 

Scanner scanner = new Scanner(System.in);

<….some code ….>

int a = scanner.nextInt();

scanner.nextLine(); //do not forget to add this line DIRECTLY after your nextInt() line

                   //try your program without this line and see if you still have the same functionality...

 

Fibonacci

 

To start you off, we have given you the code for Fibonacci. The Fibonacci number sequence is a special set of numbers in which any number is the sum of two numbers immediately before it. The first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13 ---. Obviously this is an infinite sequence, and showing the entire sequence of numbers on this lab prompt is slightly less than practical. We have implemented for you a sample solution method for finding the nth Fibonacci number. As you can see n is an argument to the method fib.

 

Since n is an argument to the method, the actual functionality of the method is dependent on the value of n. The method expects that n will be given to it from somewhere else in the program; in our lab, that “somewhere” will be the main method.

 

You are encouraged to try out the method by giving it various numbers and seeing how the method takes the n as an argument, iterates through various loops and other statements and then finally gives back the nth Fibonacci number (e.g. if n was 6, the method would spit out 5 taking as convention that 0 is the first Fibonacci number).

 

If you notice, at the end of the method, there is a word that says return. This is a special word in Java that takes the value of the variable after the return word, and gives it back to whatever called the method where the return is. In the example of fib, the method returns the value of the variable b. This value holds the value of the nth Fibonacci number.

 

Please get a good footing for methods, arguments and returns with this sample method as your guide.

 

For more on the Fibonacci number sequence, please visit this: http://en.wikipedia.org/wiki/Fibonacci_number.

 

Addition & Multiplication

 

Now that you have input in place and you have some idea about methods and parameter passing, let’s start on your first functions: addition and multiplication.

 

Both of these functions require two numbers in order to work properly (1+1 is valid…1+ is not). As in the Fibonacci example, you will have to pass parameters to an add method that will perform addition, and pass parameters to a multiply method to perform multiplication. Keep in mind, however, that these functions require two numbers whereas Fibonacci required only one.

 

Once you perform the arithmetic, you should give the sum/product back to whatever is calling the method using the return word. BE CAREFUL WHERE YOU USE THIS WORD! The return keyword will usually only work if it is at the END of the method.

 

Test your methods to see if they perform correctly.

 

For more on addition, please visit this: http://en.wikipedia.org/wiki/Addition.

 

For more on multiplication, please visit this: http://en.wikipedia.org/wiki/Multiplication.

 

Exponentiation

 

For ease of description, we will refer to this as “power” (you probably say “x to the power y” anyway; how many times have you heard “x exponentiated by y”?). This is again a function that requires two numbers: the base and the exponent. You should be comfortable with your code for multiplication before proceeding with this function.

 

Recall that raising a whole number to some power is merely repeated multiplication. If the number 3 were raised to the power 4, then the notation would be as follows: 34 = 81. However, this is equivalent to 3 x 3 x 3 x 3 = 81. If you have a working multiplication function, getting this to work will only require you to create a loop, and multiply a set number of times.

 

For more on power (exponentiation), please visit this: http://en.wikipedia.org/wiki/Exponentiation.

 

Factorial

 

The factorial is a mathematical function that continuously multiplies a non-negative integer by a decreasing amount until it reaches 0. It is a definition that zero factorial is equal to 1. The reasons for which are beyond the scope of this course. The factorial function is denoted by the exclamation mark “!” after a number. For example, 5! = 5 x 4 x 3 x 2 x 1 = 120. Note that we stop at one, because if we continued to zero, it would only multiply another one, making it irrelevant.

 

Notice that this function is similar to the Fibonacci in that it only needs one number. Also take note that factorial is actually just repeated multiplication just like exponentiation. If your multiplication method is functional and correct, you may use it to help you compute a factorial.

 

For more on factorial, please visit this: http://en.wikipedia.org/wiki/Factorial.

 

Permutation

 

This is a special mathematical function that is used primarily in combinatorics and statistics. Basically, it is the number of ways you can pick a set of items (balls, apples, books, etc.) by picking up a small subset of those items (e.g. picking 3 books from a set of 5). Remember that the order of picking does matter, if it did not, that would be a combination.

 

Permutations are given by the following formula:

 

n!/(n-r)!

 

In this notation, the exclamation mark is denoting the factorial function as mentioned earlier. The slash is the division symbol. Thus, the formula translates to: n factorial divided by the quantity n minus r the quantity factorial. For example, if n were 5 and r were 3, 5 npr 3 (the notation that we use in this lab for permutation) will be: 5 npr 3 = 60.

 

For more information on permutation, please visit this: http://en.wikipedia.org/wiki/Permutation.

 

Continuous asking of input

 

Once you are sure that you have your functions correctly coded, you should try and implement a loop into your code. This way, the program will continuously ask for input to perform mathematic operations. This is typical in the real world: there are not many calculators that perform only one operation in the entirety of their lifetime and then die.

 

Since the input will be of the form “operation #1 #2”, you can check to see if the “operation” string says “exit”. If so, you should stop your program; otherwise your program should continue until the input “exit” is reached, an error occurs or the power runs out.

 

EXAMPLE INPUT/OUTPUT

 

As you progress through the lab, or at the end, you can use these example inputs and outputs to test that you are creating a truthful calculator. Saying 1+1=3 is less than ideal in most situations.

 

INPUT: + 3 3

OUTPUT: 6

INPUT: * 4 6

OUTPUT: 24

INPUT: pow 2 6

OUTPUT: 64

INPUT: npr 6 2

OUTPUT: 30

INPUT: fact 4

OUTPUT: 24

INPUT: fib 8

OUTPUT: 21

INPUT: exit

 

Remember that your output does not need to be EXACTLY like the example, but the overall functionality should be the same. In other words, it should be “+ 3 4”, not “3 + 4” or “3 4 +”.

 

TURNIN

 

As you code, you should periodically turn in your code just so you are not strapped for time in the end. Also, once you are satisfied with your code, please follow these instructions to turn in:

 

> turnin –v –c cs180=XXX –p lab04 lab04

 

Here, XXX is referring to your lab section number. Please ask your TA for further information.