COMP 110 (Fall '10) First Java Program: Computing X raised to the Y Power due date: Wed Nov. 10 by 11:59 pm ------------------------------------------------------------------------ GOALS The goals of this program are: 1) to get you to create a simple, yet non-trivial, Java program and get it to execute 2) to get you to understand obtaining information off the run arguments line when the program starts up (by using the "args" argument array in method "public static void main") 3) to get you to understand input from the keyboard during program execution 4) to get you to write a second method (function) that will be called from main during execution Write a Java program that is able to compute X ^ Y (X raised to the Y power). The program will get the two numbers X and Y from the user (typed at the keyboard) and will print out the result. The program will repeat this several times; the number of times to repeat it will be given as an integer on the command line at initiation of program execution. STRUCTURE Your program will be a single class... let's call this class javaProgOne. This class definition will obviously contain a public static void main (String[] args) function (it won't run without one), In addition, it will conatain a second fuction we will call "power". This second function will use an iterative algorithm (an algorithm that uses a loop to accumulate the result), to compute X ^ Y; it will not simply call a math method to do it for you. Remember our goal is to learn how to express algorithms clearly. Obviously once you are an accomplished programmer and understand well how to form and express algorithms well, you would compute X ^ Y with a math function call or an operator like ^. INPUTS from the run arguments line: get an integer that tells you how many times we will be computing an X ^ Y result. For example, if the user put the integer "5" on the command line, then you will go through a loop that asks the use to give an X, Y pair at the keyboard 5 times and then stops from the keyboard: you will have the user type two numbers X will be of type double Y will be of type integer (positive integer) data checking -- there is no need to check to make sure the user types input that looks like numbers... you may assume that for both the command line number and the keyboard numbers... in other words, you may assume the user will not type "hi there" when you ask for a number -- check to make sure the integer on the run arguments line is greater than 0 -- check to make sure the Y number (typed at the keyboard) is greater than or equal to 0 -- check to make sure the Y number is an integer -- the X number can be any real number, positive or negative, so no checking needed BASIC ALGORITHM We can compute X ^ Y with a simple looping (iterative) algorithm. For example, to compute 2 ^ 4 you need to compute 2.0 * 2.0 * 2.0 * 2.0 * = 16.0 Similarly, 3.2 ^ 3 is 3.2 * 3.2 * 3.2 and similarly, 5.0 ^ 8 is 5.0 * 5.0 * 5.0 * 5.0 * 5.0 * 5.0 * 5.0 * 5.0 This is the base X multipled by itself Y times. So you will set up a loop that will go from 0 up to Y and in the loop you will accumulate the result using multiplication. SUPPORTING FUNCTION Write a function named "power" to do this algorithm. The declaration will look like this public static double power (double X, int Y) { // code in here to implement the above algorithm // and then return the result as a double } Note that this shows you that Java functions can have more than one parameter. This one has two parameters... a double and an int. FINAL THOUGHTS Remember to go to the Build menu and turn on the command line argument window in jGraps before you run the program. Remember to use good programming style... use named constants where appropriate, choose meaningful variable names, have a beginning block comment as well as smaller comments throughout, design a clear algorithm, make it all indented and readable. Remember that you may discuss the work with classmates, but you are to write your own program to hand in.