Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ICS3U1 – Unit 1 Name: _______________ Output in Java Syntax: // To print text and move to the next line, use the following. System.out.println(…); // To print text and stay on the same line for further printing, use the following. System.out.print(…); Structure of a Java Program Consider the following simple Java program. The green text in the above program are comments. Their purpose is to inform the reader of the author of the code, when it was written, and any necessary explanations. Comments are denoted by two slashes (//) or by sandwiching text between the symbols /* and */. Consider some of the lines of this program in detail: Line 1: class HelloWorld All Java programs are contained in a class. The name of the class (HelloWorld) must match the name of the Java source code file (HelloWorld.java). Class names in Java are traditionally named using PascalCase, which means that each distinct word in the name starts with a capital letter and there are no spaces. The start of a class is indicated by the reserved word class. There are about fifty (50) reserved words in Java. They are commands understood by the Java compiler. The body of a class is always contained in parantheses { and }. All contained lines of code are indented. ICS3U1 – Unit 1 Name: _______________ Line 2: public static void main (String [] args) Java programs consist of one or more methods. A method is a body of code that can be executed by a program. Every Java program requires a method called main! The main method is the starting point of any Java program. The other reserved words in the method (public, static, void) will be explained at a later date. Note that the body of the main method (the code contained inside of it) is indented and contained in parentheses. Line 3: System.out.println("Hello, world"); This statement calls a system method called println to send a string of characters to the output device (typically an output window on the monitor). Two Commands for Printing: print vs println To print a single blank line, use the line: System.out.println(); Each println statement will advance the printing position to the next line. If we wish to stay on the same line, at the end of the last printed character, use the print statement instead: System.out.print(“This text is on the same line”); System.out.print(“…as this text”); ICS3U1 – Unit 1 Name: _______________ Printing Special Characters Certain characters are difficult to represent in text in Java either because they are not viewable (like an Enter key) or because they are part of the formatting of strings in Java (like " or ' or \). In order to display these characters, you must represent them using something called an escape sequence. Shown below is a list of some common "escape sequences". Escape Sequence Resulting Output \n Moves the cursor to a new line \t Inserts a Tab (space) \\ Inserts a backward slash “\” \’ Inserts a single quotation \” Inserts a double quotation Example: The following examples illustrate the use of some common escape sequences. a) Command: Output: b) Command: Output: c) Command: Output: System.out.println (“Hello\nMr. Smith”); Hello Mr. Smith System.out.println (“Hello\tBob”); Hello Bob System.out.println (“Then he said \”Goodbye\” .”); Then he said “Goodbye”. ICS3U1 – Unit 1 Name: _______________ Output in Java – Problem Set 1. Create a program that outputs the school address as it would appear on a postage envelope. Save your program as Address.java. R.S. McLaughlin CVI 570 Stevenson Road North Oshawa, ON L1J 5P1 Canada 2. Create a program that outputs a brief dialogue between two people. Your dialogue should include all proper punctuation and spacing including quotation marks, new lines, and tabulations. Challenge: Complete the above using one output statement. Save your program as Dialogue.java. 3. Draw a picture of an animal on the screen using various symbols and texts. Be creative! An example is shown below. Save your program as Animal.java.