free album Dark Horse cheap music Sweet Dreams (Remixes) USA hit Valkyrie

Domain Names, Web Hosting and SSL Certificates - Onlinespaces

Domain Names, Web Hosting and SSL Certificates - Onlinespaces

Express Yourself. The domain that's all about YOU! Available NOW!

Domain Names, Web Hosting and SSL Certificates - Onlinespaces

Affordable, reliable web hosting solutions

ICANN • My Account• What's New• FAQ• Support: (480) 624-2500Logout
You are here  : Home Articles Java Lessons Your First Java Programs

Your First Java Programs

( 0 Votes )
User Rating: / 0
PoorBest 
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 useful

Now, 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 Comments

Notice 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 Comments

Lines 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 Text

Line 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.

  1. “Computer Science is my favorite class!”
  2. “When do we start programming games?”
  3. 36/7 4. 2 * 7.25 5. 3 + 4 – 6

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.”);
}
When you run this, you get the following:
Hello World!
I am programming now.
Now, change your code to this:
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.
  • System.out.println() prints a line of text in the command window and then positions the cursor at the beginning of the next line.
  • System.out.print() prints the text on the current line and stops. It does not position the cursor at the beginning of the next line. Any other output is displayed immediately after the last character that the System.out.print() method displays.

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 Characters

Please 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.”);
 	}
}
You should get the following output:
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\””);
Output:
“I am quoting”

Lesson 3 Exercises Project 3.1 – Get in Shape

Create 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
     }
}
Your code should produce the following output:
*               ********             *               *****
**              *      *           *   *            *****
***             *      *         *       *         *****
****            *      *         *       *        *****
*****           *      *           *   *           *****
******          ********             *               *****

Project 3.2 – Monkey

Create 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   )        
         /~~~~\    `\  \         /
        | |~~\ |     )  ~------~`\
       / |   | |   /     ____ /~~~)\
      (_/    | | |     /     |    ( |
             | | |     \    /   __)/ \
             \  \ \      \/    /' \   `\
               \  \|\        /   | |\___|
                 \ |  \____/     | |
                       \________/ 


Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
 

Add comment


Security code
Refresh

Unlimited Online Backup Only $4.95

Find Us on Facebook

Facebook Image
Happy Mother's Day to all the mothers who make the house a home.
Avengers Assemble!!!
Bought and DLed #MrUniverse a couple days ago. Watched it last night. @JimGaffigan is hilarious as always. Get it at http://t.co/rAvhYUIr.
I liked a @YouTube video http://t.co/7UQ24LDf 2 year old dancing the jive
Another great day in Austin. Time for some @Torchys tacos!
#runLBrun Congrats!!!
#runLBrun Awesome job!!!
#runLBrun
waffles are weally fun. http://t.co/t1rKH2vy #BigBangonTBS
@FoxBusiness I used to be homeless and now I am in top 10%. Hope to be top 1% someday. No shame in working hard and being successful.
Next-gen Xbox promises six times the power http://t.co/7d7zfN3r
#goldrush addicted to this show.
Merry Christmas!!!
I liked a @YouTube video http://t.co/d6ON5ppD THE DIGITAL STORY OF THE NATIVITY
I liked a @YouTube video http://t.co/rPixWFEt The Hobbit An Unexpected Journey | [HD] OFFICIAL trailer #1 US
I uploaded a @YouTube video http://t.co/sEqW3Tzq http://t.co/5gRkKqha
I liked a @YouTube video http://t.co/neXm6lqE Afterburner with Bill Whittle: Three and a Half Days

ThinkGeek Stuff for Smart Masses