Lab Objectives
|
The following instructions lead you through writing and modifying several Java programs.
Traditionally the first program a computer scientist writes in a new language is a simple one that prints out a "Hello, World" message so that's what we will do first. Do the following:
// ******************************************** // FILE: Hello.java // Author: Put your name here! // // Purpose: Print a Hello, World message. // ******************************************** public class Hello { // ----------------------------------- // main method -- prints the greeting // ----------------------------------- public static void main (String[] args) { System.out.println ("Hello, World!"); } }
You should see the hello message followed by the command line prompt on the next line.
Hello.java:15: unclosed string literal System.out.println ("Hello, World!); ^ Hello.java:15: ';' expected System.out.println ("Hello, World!); ^ Hello.java:17: reached end of file while parsing } ^ 3 errorsNote the error message tells you where the error is (line 15) and what it is (unclosed string literal -- string literals are enclosed in quotation marks so this tells you the closing one is missing). Also note that the compiler doesn't tell you where the mark should go and even though there is only one mistake the compiler says there are three. This is because the compiler is assuming every character it is reading after the first quotation mark (including the ending parenthesis and the semicolon) is part of your string but then it gets to the end of the line without finding the end quotation mark (so that is one error). Now it looks for the semicolon and can't find it (it already read past it) so that is the second error. Finally, it keeps reading and comes to the end of the file (past the last brace) but it has not found a complete syntactically correct program so that is the third error.
*** emacs HINT -- Line Numbers: In emacs there are two ways to find a specific line when you know its number. Observe that in the status bar (the one near the bottom of the window that contains the buffer name) there is among other things an L followed by a number. That number is the current line number (where the cursor currently is) -- so L23 means the cursor is currently on line 23. In short programs you can just move the cursor up or down until you find the correct line. In longer programs you can use a keystroke command: Hold the Alt key (this is called the META key in emacs), and hit the x-key. In the minibuffer at the bottom of the emacs window you will see M-x with your cursor sitting beside it waiting for you to type a command. The command you need to type is goto-line (type the words) then press ENTER then type in the line number you want and press ENTER.
*** Try the META-x method getting to a specific line.
Hello.java:15: cannot find symbol symbol : method primtln(java.lang.String) location: class java.io.PrintStream System.out.primtln ("Hello, World!");This tells you the compiler can't find a symbol then it says the symbol is a method named primtln that takes a String as an argument. It is looking in the PrintStream class since System.out is a PrintStream object. When the message is "cannot find symbol" most likely the problem is misspelling (or mis-typing) the symbol it says it can't find.
******************* Hello, World! *******************Save, compile, and run your new version (make sure it prints the correct pattern with the text perfectly centered between the stars).
System.out.println();Change each print back to println then insert statements to print 4 blank lines: one before the first row of stars, one between the first row of stars and the message, one between the message and the second row of stars, and finally one after the last row of stars. Save, compile and run your program. Does the last blank line make any difference in the way the output looks?
cp Hello.java MyHello.java
***************************** Bozo says Hello, World! *****************************
System.out.println ("It is NOT okay to go to the next line in a LONG string!!!");The solution is to break the long string up into two shorter strings that are joined using the concatenation operator (which is the + symbol). This is discussed in Section 2.1 (pages 34 - 37) in the book. So the following would be legal
System.out.println ("It is OKAY to break a long string into " + "parts and join them with a + symbol.");So, when working with strings the + symbol means to concatenate the strings (join them). BUT, when working with numbers the + means what it has always meant -- add! To see the behavior of + in different settings do the following:
// ******************************************************************* // FILE: PlusTest.java // // Purpose: Demonstrate the different behaviors of the + operator // ******************************************************************* public class PlusTest { // ------------------------------------------------- // main prints some expressions using the + operator // ------------------------------------------------- public static void main (String[] args) { System.out.println ("This is a long string that is the " + "concatenation of two shorter strings."); System.out.println (7 + 8); System.out.println ("7" + "8"); System.out.println ("7" + 8); System.out.println ("7" + 8 + 9); System.out.println("8 plus 5 is " + 8 + 5); System.out.println("8 plus 5 is " + (8 + 5)); System.out.println(8 + 5 + " equals 8 plus 5."); System.out.println("The first computer was invented about" + 60 + "years ago."); } }
So, the output for the second print statement was just the number 15 - the two operands for the + operator are numbers so they were added and the result printed. In the third line the two operands for the + are strings (be sure you understand why) so the two strings are concatenated and the result 78 is printed. Finally in the fourth line "7" is a string and 8 is a number so the 8 is converted to a string and the two strings are concatentated resulting in the string "78".
So for the first of the four statements there are no parentheses so the leftmost + is evaluated first. The first operand is a string so + denotes string concatenation and the result is the string "78". The second + then concatenates the string "78" with the 9 to give "789".
Ten apples plus 13 bananas is 23 pieces of fruit.
Your program must use only one statement that invokes the println method. It must use the + operator both to do arithmetic (add 10 and 13) and string concatenation. Make sure that your program includes comments that document the heading information (Filename, Author, and Purpose) and the key steps of the program.
zip YourName *Be sure to replace YourName with Your last name. This command compresses all of the files in your current directory (the * is a wildcard that denotes all of the files) and places them in a file named YourName.zip (with your name in place of "YourName").
unzip YourName.zip