The following instructions lead you through writing and modifying several Java programs. The goals are for you to learn more about emacs, Linux, and Java programs that do nothing but print.
// ******************************************** // 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 -p- MyHello.java | lpr -h
*** Netscape HINT: When you need to supply the pathname for a file when saving something from the Web or attaching a file it is usually easiest to type the pathname. However occasionally you may want to browse to find the file. In the Save As window you should see two side-by-side windows -- one labeled Directories and the other labeled Files. To browse click on the name of the directory you want to look in then click the Filter button. The Directories and Files windows will now show you the contents of the directory you just selected. Continue this (select a directory then press Filter) until you find what you want. Notice that as you do this the pathname in the Selection box is changed to indicate where you currently are.
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" + 55 + "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 and string concatenation. Print out a copy of your completed (and working!) program.