CSC111 – Lab #1 Review Spring 2009 1 Lab #1 – Review of Java Fundamentals This lab is a refresher (introduction?) to the fundamentals of Java programming. The first few exercises will introduce using the Java environment, and some fundamentals, such as system I/O loops, and conditionals. Exercise #1 – Hello World! This is a simple exercise to ensure that you can edit and compile a Java program. You do not need to use Eclipse to write Java Code! Using a text editor, create the following file named Hello.java: Example 1 - Hello World Example Hint: You will get some error messages when you try to compile the program if the class “Hello” is not in the “Hello.java” source code file. After you have the file, you can compile and run the program using the Java language. To do this, you need to run a “command” window. To compile your program, you need to use the Java Compiler. From the command prompt: Example 2 - Example compilation and execution public class Hello extends java.lang.Object { public static void main (String args[]) { System.out.println("Hello World"); } } bash-2.03$ javac Hello.java bash-2.03$ java Hello Hello World CSC111 – Lab #1 Review Spring 2009 2 import java.io.*; public class Hello extends java.lang.Object { public static void main (String args[]) { BufferedReader in; String yourName; /* Solicit input and flush the buffers */ System.out.print("Enter your name: "); System.out.flush(); /* Initialize an input stream from the Operating Systems' * standard input */ in = new BufferedReader(new InputStreamReader(System.in)); try { yourName = in.readLine ( ); System.out.println("Hello " + yourName); } catch (IOException e) { e.printStackTrace(); } } } Discussion: One of the most oft asked questions is why do I get the error indicated in example 3. It is because Java places a restriction that a class is defined in a file named the same as the class. In other words, if the class is named “Hello”, then it needs to be defined in “Hello.java” Example 3 - Error Message Exercise #2 – System I/O The Java language contains the definition for the “System” object that allows you to interact with the operating system in very basic ways. This next program will expose you to reading input and printing some results. It will also exercise your use of the Java “String” class. Example 4 - Example using System.out Hello.java:1: Public class HiThere must be defined in a file called "HiThere.java". public class HiThere extends java.lang.Object { ^ 1 error CSC111 – Lab #1 Review Spring 2009 3 if ( a < b) { c = 0; } else { c = 1; } Discussion: • This uses the system output routines System.out.print and System.out.println, can you tell the difference between them? If not, try using a print instead of println. Now can you tell the difference? • This also introduces the System.in and BufferedReader. These two work hand in hand. BufferedReader provides for buffered input, and System.in is a standard location to read from. For now, assume that whenever you need to read a value from the keyboard, you use the line that constructs in, and the in.readLine() syntax. • Another new feature of this exercise is the try { } and catch { }. This is standard Java code to handle errors ( called Exception Handling). Exercise #3 – Putting it Together with If Statements One of the foundations of any programming language is the conditional test, usually of the form “if this then do that.” Java uses this statement. Consider example 5, it is an example of an “if statement” in Java. Example 5 - Sample if ... else ... statement In this exercise, start with the program you wrote for Exercise #2. Change it to ask for a person’s name. Use an “if” statement to compare the name to yours. Print a message to tell the user whether their name is the same as yours. Hints: • you will need to store your name in a String object. You can do this using the String constructor method and a literal string. You can then use the string object with your name in to compare using the compareTo operator. • make sure you enter a name that is the same as your name and one that is not the same as your name! Discussion: Did you have any troubles getting it to work? This is a small almost trivial program, but it has already grown. CSC111 – Lab #1 Review Spring 2009 4 Exercise #4 – Putting it Together with Loops One of the other foundations of almost all programming languages is the concept of a loop. Loops allow programs to do the same thing either for a set number of times, or while something is true. In this example, you will start with the program that you wrote in Exercise #3, with some changes. Ask the user to guess your name by reading it from the keyboard. If they guess it, print “good job!” and then stop asking them (exit). If they don’t guess it, print a message asking them to enter another guess, or enter some phrase that indicates they want to quit. The program must loop while they have not guess the correct answer and they do not want to quit. Hints: • you will need to use a loop to keep asking them, but which loop? • make sure that your loop has some exit condition! • your loop will need to check two different conditions to determine whether it should exit. Think about how you will code that. • if your program runs forever, you will need to press CTRL-C to kill it Exercise #5 –Loops & Arrays Ask the user to enter 10 numbers. After they have entered the 10 numbers, print the numbers entered, and a running sum of the numbers, and a grand total at the bottom. Additional Java Resources “Appendix A” in our textbook. “The Java™ Tutorial,” by Sun Microsystems at : http://java.sun.com/docs/books/tutorial/?frontpage-spotlight Sun Microsystems maintains a robust Java website at http://java.sun.com