Lab 7: If-then-else and While


Handed out: Mar  14

Due:  Mar. 20


The Unix "cp" command -- copying files

Unix has a command named "cp" that you can use to copy files. It takes two arguments, the first being the existing file that you want to copy, and the second being the name that you want to name the new copy. Since you will be writing Java programs from now on, and they all need a class definition and a main() method, to save typing you might just want to copy an existing Java program file over to a new one, and then edit the new one and change what you need to. So, if you have your Hello.java file from the last lab, you can do the command
cp Hello.java Lab7a.java
and then edit the file Lab7a.java to write the program you need for the first problem on this lab. From then on, you can take the Java program that seems closest to what the new program needs to do, copy it, and start from there. It will save you typing and time!

PS: Don't forget the "rm" command to remove unwanted files. But beware, "rm" does not warn you or save backups. Once you remove a file, it is gone for good.


So far we have been taking our input from the command line. Command-line arguments are not the only way for users to provide data to a Java program. The program can ask for data to be input while it is running. The Addition program on page 88 of the Java textbook is an example of a program that reads in data while the program is running. It does this with a statement of the form "string1 = stdin.readLine();", which reads in what you type on the keyboard (until you hit Enter), and places it in the String variable string1.

Compile and run this program to see what it does. You don't need to type it in, just copy it from the file ~gupta/public_html/Classes/CS171/java/Addition.java (see the top of the lab for the copy command). Or, if you are using a Web browser, just click here and then save it locally.

One important point about the data input that Addition uses -- just like with command-line arguments, the input data is in the form of strings (the String type), and you must convert it to numbers if that's what you need.
 

Problems

1. Write a java program called lab7a.java (class name lab7a) that inputs three integers (getting the three inputs from the command line), and prints out the maximum one. You need to write a function static int max3(int a, int b, int c) which takes three integer arguments, and returns the maximimum one. Your program should print out "The largest is: " and then the largest one, as computed by the max3 function.


2. Write a java program called  lab7b.java (class name lab7b) that  inputs three integers (using the input method of  the Addition program , i.e., using the stdin.readLine() function), and prints out the maximum one. You need to write a function static int anothermax3(int a, int b, int c) which takes three integer arguments, and returns the maximimum one. Your program should print out "The largest is: " and then the largest one, as computed by the max3 function. Programs 7a and 7b are the same except the way in which they accept input.


3. Write a Java program (named lab7c.java) that will repeatedly accept integers from the keyboard until the value -99 is detected.  Then the program should calculate and print the average of the integers (excluding the -99) along with an appropriate message.  Use a while loop.

Hint: the average of a group of integers is the total sum of all the integers divided by the number of integers there are. The total sum can be calculated while you are reading them in by keeping a running total. The same can be done for keeping the count of the numbers. The average that you print should be a real number. Dividing two integers normally produces an integer result. You can get a real result by first converting one of the integers to a real number, like  "((double) 5) / 2"  -- this expression would result in the real number 2.5 (in Java, the type name for a real is double).


4. Write a Java program (named lab7d.java) that will accept 10 integer numbers (one per line) from the keyboard.  The program should calculate and print the minimum and maximum values of the 10 numbers, along with appropriate messages.  Use a while loop.

Hint: Declare two variables min and max for holding the minimum and maximum of the three given integers respectively. Read the first number. Set both min and max to the value of the first number. Next read the second number. Compare min with the second number. If the second number is smaller, then update the value of min to that of the second number. Compare max with the second number. If the second number is larger, then update the value of max to that of the second number. Repeat the same steps for other numbers. After all the numbers have been read and processed, min and max has the minimum and maximum values of the 10 numbers.