Printing Output Now that you know how to use BlueJ it's time to start learning some Java. This lesson will demonstrate the use of print and println. print print is used to have the computer print a line of text in the Terminal Window with the cursor remaining on the same line as the text. The following line of code demonstrates the use of print.
System.out.print("I am a computer programmer."); Explanation: You do not need to understand the 'System.out.print' at this point in the class. An explanation would involve a discussion of Object Oriented Programming which will be presented in Programming 2. What you do need to understand that it tells the computer to print what is between the parentheses inside the quotation marks. The semicolon on the end of the code line tells the computer that the line of code is finished.
println println (pronounced as the two words 'print line') is used to have the computer print a line of text in the Terminal Window and then drop the cursor down one line. The ln in println is referred to as a line feed. The following line of code demonstrates the use of println.
System.out.println("Whoa! I feel the cursor dropping down one line."); System.out.println("Here I am on the next line!");
Special note: Make sure you understand the difference between print and println. Try writing a few lines of code using both commands and then predict the outcome.
For each of the Programming 1 lessons you are strongly encouraged to view and play around with the sample code provided in the BlueJ Java Student Samples folder. Open BlueJ and choose Open Project from the Project menu. Navigate to your Programming 1 folder on the I: drive and open the BlueJ Java Student Samples folder. Open the project named first. Lesson example: Project first, Class Hello, method sayHi()
- Open the Hello Class and view the code
- Right-click on the Hello Class and choose new Hello()
- Right-click on the red rectangle and choose the method named sayHi()
Code of sayHi() method: public class Hello { void sayHi() { System.out.println("Hi there!"); } }
|