The following instructions lead you through writing and modifying several Java programs. The goals are for you to learn more about emacs, Linux, and simple Java programs.
// ******************************************** // 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!"); } }
Hello.java:15: unclosed string literal System.out.println ("Hello, World!); ^ Hello.java:15: ')' expected System.out.println ("Hello, World!); ^ 2 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 two. 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 parenthesis to enclose the argument to the println method and can't find it (it already read past it) so that is the second 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: Hit the Esc key (this is called the META key in emacs), release it, then 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, World! *******************Save, compile, and run your new version (make sure it prints the correct pattern).
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! *****************************
nenscript -2rG MyHello.javaNote: The above nenscript command prints to the default printer for the computer you are using. When you are in a Trexler lab (either first or second floor), the default printer should be the printer in that lab. You can specify which printer using the -P option on the nenscript command. For example,
nenscript -2rG -P Lab2 MyHello.javadirects the file to be printed to Lab2, the second floor lab printer. If you replace Lab2 by Lab1 in the command the file will be printed on the printer on first floor.
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.2 (pages 67 - 71) 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 ("The first computer was invented about" + 60 + "years ago."); 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."); } }
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. Print out a copy of your completed (and working!) program.