Java - Intro 1. Your First Program
1.2 Add more lines to your program
1. Your First Program
1.1 Write your first program
- Type the following text into your text editor:
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
- The Layout of a Program:
- Spacing:
- Spacing doesn’t matter to Java. Spacing matters to people reading the program. White space is made by using the spacebar, tab button or return key. The proper use of white space allows for easy understanding of the program for programmers.
- Blocks and braces:
- Java uses braces (or curly brackets { } ) to create blocks of code. It is very important to keep organized and use the proper spacing to keep track of blocks. If this is not done, programs will be very difficult to read and debug.
- Braces are nested, as shown in the first program.
- Braces are used to hold one or more statements.
- Statements:
- A line of code written in Java is known as a statement. It ends in a semicolon. This is similar to a period in English. This is how Java knows that the statement is over.
- The “main” method:
- When a Java program starts, it finds the main method and begins to execute the first statement. Every program needs a main method.
- Edit the first program to look like this and run it:
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
- When writing a program, there are two types of “languages” that we are writing in. The first is the programming language that the computer understands. In this case, it is Java. The second is English, which is used to inform other programmers of what is going on in the program.
- There is typically a lot of writing in English that goes along with a program. This is referred to as documentation. When the documentation is interspersed (combined throughout) the programming language (code), we don’t want the computer to read the English and confuse it for the code. This is why the English pieces are inserted as what are known as comments, which the computer ignores.
- There are two ways to create comments:
// - 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.
- “Commenting out” - This is a term used to take a line out of the execution of a program without deleting it. The use of this technique will be clearer and comes in very handy as we write more complex programs. Look at the following example:
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)
- Every program that you write should have a heading which should include a:
- name
- date
- title
- brief description
- The heading for the program that you wrote in section 1.2 should look something 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 //////
/////////////////////////////////////////////////////////////////////////////////////////////////
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)
- Escape characters, also known as escape sequences, are used to help you format (layout) your printing. In a print statement, Java will print to the screen anything that is between the quotes, but what if you want to print:
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.
- Types of escape characters
\’ - 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
- This is a backslash: \ System.out.println(“This is a backslash: \\”);
- “hi” System.out.println(“\”hi\””);
- 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