free album Dark Horse cheap music Sweet Dreams (Remixes) USA hit Valkyrie
| ICANN | • My Account |
Your First Java Programs |
| Written by Scott Collier |
|
Computers are getting smarter all the time. Scientists tell us that soon they will be able to talk to us. (And by ‘they’, I mean ‘computers’. I doubt scientists will ever be able to talk to us.) - Dave Barry This lesson will help you to create a Program Skeleton that you can use as a starting point for you other programs. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. /**
* @(#)HelloWorld.java
*
* A description of your application
*
* @author Your Name Here
* @version 1.00 Date created goes here (ex. 2009/2/17)
*/
public class HelloWorld {
public static void main(String args[]){
//TODO: Your code will go here
}
}
You do not need to worry about what the above code means at this point. Just know that you need to do this every time in order for your application to run. Making the code do something usefulNow, add some code inside the main method so that your program actually does something. You will also add a comment to your code. 9. 10. 11. 12. public class HelloWorld {
public static void main(String args[]) {
System.out.println(“Hello world!”); //This will print out your string
}
}
Single Line CommentsNotice the comment in the previous code starts with a // (Line 11). This is called an end-of-line or single-line comment. You can put comments anywhere in the program without affecting how the program runs. Comments should be used throughout the code to explain parts of the code that may be confusing or to help document programs. Comments can help the readability of the code and help other people read and understand your programs. Java ignores comments, so they do not affect how your program runs. Multiple-line CommentsLines 1 through 8 in the above program are called traditional comments or multiple-line comments. These comments begin with the delimiter /* and ends with */. All of the text between the two delimiters is ignored by Java. Printing a Line of TextLine 11 in the above code is the line of code that prints our text to the computer screen. Please notice the semicolon (;) at the end of the line. The semicolon is used to mark the end of a statement. Every statement must end with a semicolon. Run the previous code and you should see the following: Hello World! Now, let’s try changing the text inside the parenthesis and see what happens. Be sure to recompile the program after each change before running the program.
Next, let’s modify the main method of the program so it looks like this: public static void main(String args[]) {
System.out.println(“Hello world!”);
System.out.println(“I am programming now.”);
}
Hello World! I am programming now. public static void main(String args[]){
System.out.print(“Hello world!”);
System.out.println(“I am programming now.”);
}
You should get the following output: Hello World! I am programming now.
Let’s take a closer look at the following line of code: System.out.println(“I am programming now.”); System.out is known as the standard output object. It allows the Java application to display characters in the command window. println is called a method. Methods perform some sort task for an object. In the code above, their task is to print the string of characters in between the double quotes. We call these characters between the double quotes, strings. The string between the parentheses is called the argument to the method. Escape CharactersPlease create a new class called HelloWorld2 with the following code: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. /**
* @(#)HelloWorld2.java
*
* This application prints out several lines of text using only one
* statement.
*
* @author Programmer Name
* @version 1.00 2009/2/17
*/
public class HelloWorld2{
public static void main(String args[]) {
System.out.println(“Hello\nWorld!\nI\nam\nprogramming\nnow.”);
}
}
Hello World! I am programming now. You can display multiple lines of text by using only a single statement. You can do this by using newline characters which tells System.out’s println and print methods that they should position the output cursor at the beginning of the next line. Normally, the characters between the double quotes are displayed exactly as they appear. But, notice in Line 11 the two characters \ and n. The backslash (\) is called an escape character. It lets System.out’s println and print methods that a special character is to output. In Java, when a backslash appears with another character, it is called an escape sequence. Here is a list of some common escape sequences and their descriptions: Escape Sequence Description \n Newline. Positions the cursor at the beginning of the next line. \t Horizontal tab. Moves the cursor to the next tab stop \\ Backslash. Used to print the backslash character \” Double quote. Used to print a double-quote character. Example: System.out.println(“\”I am quoting\””); “I am quoting” Lesson 3 Exercises Project 3.1 – Get in ShapeCreate a new project called GetInShape that has a GetInShape class. This class should have the following code. Be sure to update the comments at the top of the program to reflect your name and the date that you are writing the program: /**
* @(#)GetInShape.java
*
* Prints out different shapes
*
* @author Ima Programmer
* @version 1.00 2009/2/17
*/
public class GetInShape
{
public static void main(String args[])
{
// Replace this line with your code
}
}
* ******** * ***** ** * * * * ***** *** * * * * ***** **** * * * * ***** ***** * * * * ***** ****** ******** * ***** Project 3.2 – MonkeyCreate a new project called Monkey that has a Monkey class. This class should have the following code. Be sure to update the comments at the top of the program to reflect your name and the date that you are writing the program: /**
* @(#)Monkey.java
*
* Prints out a monkey
*
* @author Ima Programmer
* @version 1.00 2009/2/17
*/
public class Monkey
{
public static void main(String args[])
{
// Replace this line with your code
}
}
Remembering escape sequences, produce the following output:
__------__
/ \
| //^\\//^\|
/~~\ || o| |o|:~\
| |6 ||___|_|_||:|
\__ / o \/
| ( O )
/~~~~\ `\ \ /
| |~~\ | ) ~------~`\
/ | | | / ____ /~~~)\
(_/ | | | / | ( |
| | | \ / __)/ \
\ \ \ \/ /' \ `\
\ \|\ / | |\___|
\ | \____/ | |
\________/
|