CPSC 150 Laboratory Lab 3 if-else and count-controlled loops At this stage you should be comfortable composing simple programs: reading input from the user, making simple calculations and producing program output. In this lab you will continue practicing Java with programs that use variables, assignments and expressions. To store the programs you will write in this lab, create a directory named “lab03” in your home directory. If Statements This exercise tests your basic knowledge of if statements. 1. Absolute value In this first step you will create a program that calculates the absolute value of a number. Write a program, named as Absolute.java, to prompt the user for a number and output the absolute value of the number. E.g. if the user enters 3.4, output: The absolute value of 3.4 is 3.4 E.g. if the user enters -4.66 output: The absolute value of -4.66 is 4.66 Hints: The algorithm should only have 3 basic steps: Prompt the user Read the number (double) CPSC 150 Laboratory Write an if-else that tests if the number is >=0. If so, print the number, otherwise print the number multiplied by -1. At this stage, if you are totally lost, don’t be concerned that you are unable to write the entire program. Start by focusing on the parts you CAN do. Go back to the previous lab and refer to the programs that read data from the user. Then proceed to the if-else and write the parts you understand: the test, the output statements, whatever you think you understand. Now ask your instructor for some help if needed. When you are finished, submit your program to Web-CAT. 2. Find the minimum of three numbers This program, named Minimum.java, is similar to the previous example, except you must read 3 numbers and write a more complicated test. If two of the numbers have the same value, it is arbitrary which one you print as the smallest, as you only print the value, not the name of the variable holding the value. Hints: There are three numbers, so there are three possible answers. In the previous case you used a two-way if (if-else) and this example demands a three-way if (if – else if – else).. Testing Test that your program calculates the minimum correctly by using the following values • 23.6 46.5 26.6 -> 23.6 • 46.5 46.5 56.6 -> 46.5 • 11 3 2 -> 2 Also try other values to see if they give correct results (your instructor will use his/her own). When you are finished, submit your program to Web-CAT. Lab 3A Lab 3B 3. Leap Year Most people assume that a leap year is determined by whether or not the year is evenly divisible by 4, but that is only partially correct. Go to http://en.wikipedia.org/wiki/Leap_year and look at the section on Gregorian calendar to determine the special cases. Then write a program, named LeapYear.java, that reads the year number and outputs either “IT IS A LEAP YEAR” or “IT IS NOT A LEAP YEAR”. Hints: Don’t forget that you can calculate the remainder by using the mod operation, %. Testing • Choose your test cases to include the examples on the web site. When you are finished, submit your program to Web-CAT. Count-controlled Loops There are many ways of writing a loop, and that presents a problem for the programmer when trying to determine the type of loop to use. In this exercise, we will investigate the use of one of the more common loops, a count controlled loop. This simply means that you create a counter that determines how many times a particular operation repeats. You have likely seen something like this before, but let’s try a simple one. A count-controlled loop looks something like this: int i=1; while (i<=6) Lab 3C CPSC 150 Laboratory { System.out.println(“Try Again”); i++; } EXAMPLE 1. Write a simple program and insert this code in the main routine. Guess what it will do before running it and then compile and execute it to see if your guess was correct. Modify the number 6 to be 35 and run it again. Did it behave as expected? Making a count controlled loop execute a particular number of times is not that hard and you can use this example as a starting point. If you have questions about the results, discuss this with your instructor. EXAMPLE 2. Try the next loop example, predicting what it will do before running the program. int i=1; int limit=4; int sum=0; while (i<=limit) { sum = sum + i; System.out.println(sum); i++; } If you have questions about the results, discuss this with your instructor. EXAMPLE 3. Change EXAMPLE 2, moving the output statement before the beginning of the loop and another copy after the end of the loop, deleting the println inside the loop. Run it again and compare your prediction with the result. If you have questions about the results, discuss this with your instructor. EXAMPLE 4. Can you see what this program calculates.. the value of sum at the end of the loop. It’s the sum of the values from 1..N. There is a formula that you can use to check your answers which states that the sum of the numbers from 1..N is Nx(N+1)/2 What does this suggest the sum of the first 100 integers (1..100) would be? Modify EXAMPLE 3 to input the first number and the last number of a sequence and then output the sum of the numbers from the first number to the last number (inclusive) . Name the program SumRange.java. When you are finished, submit your program to Web-CAT. 1. Factorial So let’s see if you can use the previous examples to write some loops of your own. Factorial is similar to summing, which is what you did in the previous example. Write a program to calculate factorial(n). E.g. 4! = 1x2x3x4 = 24. Hints: Like the above example, but you’re multiplying rather than adding. Testing Lab 3D CPSC 150 Laboratory • Testing here is not that tricky, so you make up some test cases. • Use integers. This will prove to be an issue at some point as the value of factorial grows rapidly and can exceed the value which an integer is capable of storing, but it will be tested with small values for now. • Name the program Factorial.java. When you are finished, submit your program to Web-CAT.Lab 3E