Lab 4

Don't forget to finish the quiz first.  You have 5 minutes from the time you open the quiz to complete it.

Lab assignment: Nesting loops to perform calculations

In this week's lab you must write a Java program to simulate a simple calculator.

You may start with the sample code provided at the bottom of the page.

This calculator will perform up to 3 functions on two inputs.

For the first point, print the product of the two inputted numbers, but you may only use the + operator.

For the second point, print the exponent of the two numbers.  That is, take the first number to the power of the second.  So if the entered numbers were 4 and 3, you would calculate 4^3, or 64.  Again, for this function, you are only allowed to use the + operator.  No other built-in functions, and definitely no * operator.

If you've gotten these two things working, you may attempt to go an extra step, adding the prime number functionality.  What this operation should do is print out either the first x prime numbers, or the xth prime number, where x is the first of the two numbers input.  Start counting prime numbers from 2, where 2 is the first prime number.

The execution of the program should look similar to the following:

Welcome to the calculator. Enter two integers.

Enter the two integers to be used: 7 5

The product of these two numbers is 7*5 = 35

The exponent of these two numbers is 7^5 = 16807

The first 7 prime numbers are:

2 3 5 7 11 13 17

Exiting program...

OR

Welcome to the calculator. Enter two integers.

Enter the two integers to be used: 12 3

The product of these two numbers is 12*3 = 36

The exponent of these two numbers is 12^3 = 1728

Prime number number 12 is: 37

Exiting program...

Keep in mind, there should be no functions other than main in your program.  For the first two calculations, you are limited to the + operation.  For calculating the primes, you are permitted to use /, %, and *, though you will likely not require all of them.  Note that a/b gives you the integer floor, or the truncated integer, assuming a and b are both integers.

Don't worry about comments, coding conventions, or error checking of the input for this submission, though if you have free time it does make the code nicer.

Do put the names and netIDs of both partners in a comment at the top, as well as the lab section you are in.

Be sure to take turns coding!  It's easy to get wrapped up in the coding and forget to give your partner their turn on the keyboard, and it's too easy to sit back and discuss the problem without writing the code yourself.

Submission:

1. You should work with a partner for this lab.  Only one of you needs to submit the program to Blackboard.  Both names must be present in the program you submit.

2. You have an hour from the start of your lab session to submit the lab to Blackboard.  Plan your time wisely, and ask questions if you are working but can not progress.