← All AP Java lessons

AP Java / J01

Your First Java Program

Java program structure, printing, comments, headings, escape characters, and first assignments.

Java - Intro 1. Your First Program

1. Your First Program

1.1 Write your first program

1.2 Add more lines to your program

1.3 Comments

1.4 Program Headings

1.5 Escape Characters

Chapter 1: Assignments

A1.1 - Favorites

A1.2 - Knowledge is Power

A1.3 - Tricky Printing

A1.4 - Initials

1. Your First Program

1.1 Write your first program

public class Prog11

{
public static void main(String[ ] args)

{
System.out.println("This is my first program!!");

}
}

There is no input for this program. The computer simply reads this line, using Java as the language to interpret it, and sends the output to the monitor. This should be your output:

This is my first program!!!

Whatever you put in between the quotes gets printed to the screen. Change the text between the quotes to whatever you want and have it printed to the screen.

1.2 The Anatomy of a Program

public class Prog11

{
public static void main(String[ ] args)

{
System.out.println("This is my first program!!");

System.out.println("It has more than one line.");

System.out.println();

System.out.println("Now the program is finished!");

}
}

Output:

This is my first program!!!

It has more than one line.

Now the program is finished!

1.3 Comments and Documentation

// - Two forward slashes in a row indicates that everything to the right of it should not be interpreted by the computer as code.

/* */ - A forward slash followed by an asterisk is used to begin the comment. Everything is ignored by the computer until an asterisk followed by a forward slash is found. This style is used to create larger areas of non-Java text.

public class Prog11

{
public static void main(String[ ] args)

{
System.out.println("This is my first program!!");

// System.out.println("It has more than one line.");

// System.out.println();

System.out.println("Now the program is finished!");

}
}

This would produce the following output. The two middle print statements are ignored

because of the double slashes:

This is my first program!!!

Now the program is finished!

1.4 Program Headings (A1.1 - A1.2)

// Program 1.2 First Program

// 10/04/20

// Josh Finkel

// This program demonstrates a print statement

It could also look like this:

/////////////////////////////////////////////////////////////////////////////////////////////////

////// Program 1.2 First Program //////

////// 10/04/20 //////

////// Josh Finkel //////

////// This program demonstrates a print statement //////

/////////////////////////////////////////////////////////////////////////////////////////////////

It could also look like this:

/*

Program 1.2 First Program

10/04/20

Josh Finkel

This program demonstrates a print statement

*/

Each of these headings are ignored by the computer when it is running the program. These three examples all get the job done but use different styles. The style you choose is up to you.

1.5 Escape Characters (A1.3 - A1.4)

He said “wow!”

If you use the following, you will get an error:

System.out.println(“He said “wow!”);

The problem is that the computer will see “He said “ as the quotes around the text to print and the rest of the line will confuse it. By using the escape character “ \ ” (a backslash), Python will understand that the next character should not have its usual action. To generate the desired output, you could write:

System.out.println(“He said \“wow!\””);

Notice that the first backslash allows the quote at the beginning of wow to be printed as a quote and not end the print statement text. The second quote is printed using the same technique.

\’ - single quote - Allows you to print a single quote

\” - double quote - Allows you to print a double quote

\\ - backslash - Allows you to print a backslash

\n - newline - Moves to a new line

\t - tab - Inserts a tab

Examples:

Output Code

  1. This is a backslash: \ System.out.println(“This is a backslash: \\”);
  2. “hi” System.out.println(“\”hi\””);
  3. Backslash in quotes: “\” System.out.println(“Backslash in quotes: \”\\\”);

Chapter 1: Assignments

A1.1 - Favorites

Write a program that prints, on separate lines, your name, your birthday, your hobbies, your favorite book, and your favorite movie. Label each piece of information as shown in the example. Match the sample as closely as possible, but with your own information.

Make sure you are putting a commented heading for your program.

PROGRAM STRUCTURE:

/*

Program title and author here

*/

public class // Assignment name

{
public static void main(String[ ] args)

{
// print the line with the name

// print the line with the birthday

// continue printing the lines with the hobbies, book and movie

}
}

SAMPLE RUN:

Name: Josh Finkel

Birthday: March 30, 1972

Hobbies: Spending time with annoying students

Favorite book: East of Eden

Favorite movie: Princess Bride

A1.2 - Knowledge is Power

Write a program that prints the phrase “Knowledge is Power inside a box made up of the characters “ = “ and “ | “. The last character is known as a pipe and can usually be found above the backslash on your keyboard:

Make sure you are putting a commented heading for your program.

PROGRAM STRUCTURE:

/*

Displays a “Knowledge is Power” message on the screen

*/

public class Prog14a

{
public static void main(String[ ] args)

{
// Add the code here to create the desired output.

// This may require multiple lines of code.

}
}

DESIRED OUTPUT:

=============================

| |

| Knowledge is Power |

| |

=============================

A1.3 - Tricky Printing

Write a program with all three parts that prints the following. Don’t forget your heading with all relevant information:

Part A) His last words were “Don’t do it!”

Part B) //

“Hi”

Part C) Use only one print statement to print the following two lines:

This is a backslash: \

And this is a backslash in quotes: “\”

A1.4 - Initials

This may take a little time and as always with programming trial and error is your friend here.

Write an application (program) that displays your initials in large block letters. Make each large letter out of the corresponding regular character. You can use a print statement for each line and make the final product as big as you want with a minimum of 6 lines high per letter.

SAMPLE OUTPUT: If your name is John Long, this is what your initials should look like.

JJJJJJJJJJJJJJJ LLLL

JJJJJJJJJJJJJJJ LLLL

JJJJ LLLL

JJJJ LLLL

JJJJ LLLL

JJ JJJJ LLLL

JJ JJJJ LLLL

JJJJJJJJJJJ LLLLLLLLLLLLLL

JJJJJJJJJ LLLLLLLLLLLLLL