Lecture 2-1: Writing Simple Java Programs
After reading through Chapter 2, you should be able to
- understand the meaning and placement of the parts of a simple Java program
- create named constants using the
finalkey word - understand the uses of indentations, spaces, and blank lines in a program to reflect a programming style
- understand the use of variables
- know the primitive data types
- know the arithmetic operators and the order of operations
- know how to use parentheses to group parts of a mathematical expression
- write statements that display console output
- understand the
Scannerclass to read keyboard input. - create
Stringobjects - understand the difference between primitive data type variables and a reference variables
- understand the use of literals and the different types of literals
- write statements using combined assignment operators
- understand the concept of scope
- write single-line and multi-line comments
- use the
JOptionPaneclass to display input and message dialogs
Today's Learning Objectives
- Know how to write simple procedural Java programs.
- Know the edit, compile, execute, debug process.
- Know how to use comments, indentation, and proper brace placement to improve the readability of your programs.
- Know how to declare and use Java variables and constants.
Simple Java Programs
Here is a simple Java program that displays "Programming is great fun" on the computer screen.
Code Example 2-1-1
// Displays the message "Programming is great fun" public class Simple { public static void main (String[] args) { System.out.println("Programming is great fun"); } }
Building blocks of a Java program
- Classes. A class is a collection of related
variables and/or methods (usually both). A Java program consists of one
or more classes. In our simple example there is one class named
Simple. - Methods. A method is a series of statements. Each
class may contain any number of methods. In our simple example there is
one method named
main. - Statements. A statement is a single command. Each method may contain any number of statements. In our simple example
the one statement is "
System.out.println("Programming is great fun");" - Comments: Comments are there for people to read (not used by the computer). They inform the reader of what the code does.
In our simple example, there is one comment // Displays the message "Java rules!".
The two slashes (
//) at the beginning of the line indicates that this is a comment.
Notes
- Java is a case-sensitive language.
- All Java programs must be stored in a file with a
.javafile extension. - Comments are ignored by the compiler.
- A
.javafile may contain many classes but may only have one public class. - If a
.javafile has a public class, the class must have the same name as the filename.
System.out.println()andSystem.out.print()statements are used to produce console output
- All Java applications must have a main method.
- For every left brace, or opening brace, there must be a corresponding right brace, or closing brace.
- Statements are terminated with semicolons.
- Comments, class headers, method headers, and braces are not considered Java statements.
Writing simple Java Programs
Steps involved in writing a Java program:
- Enter (or Edit) the program (Java source code *.java)
- Compile the program (Java bytecode *.class)
- Execute the program (Java Virtual Machine)
- Debug the program (return to step 1)
Programming process illustration
While it is possible to perform all these steps at the command prompt, most programmers prefer to use an integrated development environment (IDE) that provides a Graphical User Interface (GUI also known as WIMP) to the four steps involved in writing Java programs.
The computers in the Hebeler labs have jGRASP IDE installed and ready to use with CS 110 and CS 111 assignments. jGRASP is available online with tutorials to help first time users.
All but the simplest Java programs consist of multiple source code files (*.java) and class files (*.class). jGRASP's projects manage the multiple source code and class files that make up a Java program.
Programming Style
Programming style refers to the coding standards, conventions, and guidelines for writing correct, high-quality, and maintainable Java code.
The CS 110 Java Programming Style Guide outlines the conventions we will follow in CS 110 and 111. It contains separate sections for identifier naming conventions, commenting, and formatting.
If you are brand new to programming, some of the content of the Java Programming Style Guide at his point may be confusing. One student learning objective of CS 110 is to have you leave knowing how to correctly apply the style guidelines that produce readable and maintainable Java programs.
Variables
Variables are names for values in our programs that can be changed.
In Java, every variable must be declared once before it can be used.
Declaring a variable means informing the compiler of the variable's name and its type.
The two most common primitive types in Java are
double: Variables of type double store numbers (with decimal points).int: Variables of type int also store numbers (but with no decimal points allowed).
In Java, the pattern to declare (designate) a name with a variable is
<the type> <the name>;
For example:
Code Example 2-1-2
// Declare two variablesintmilesWalked;doubleoutsideTemperature;
The Java Style convention for capitalizing variable names is to begin with a lower-case letter and then begin each new word in the name begins with an upper-case letter. Do not use spaces or underscore characters in your variable names.
If you know the variable's initial value, you can also set it to that value at the same time as the variable declaration.
The pattern to assign a name to variable along with an initial value is
<the type> <the name> = <the initial value>;
For example:
Code Example 2-1-3
// Declare two variables with initial valuesdoublecombinedWeight = 250.7;intage = 48;
Named Constants
Named Constants are names for variables whose values cannot be changed
Java uses the keyword final to indicate constants.
In Java, the pattern to assign a name to constant value that cannot be changed is
final<the type> <the name> = <the constant value>;
Examples:
Code Example 2-1-4
// Declare two constantsfinal intMINIMUM_AGE = 21;final doublePI = 3.14159;
The Java Style convention for capitalizing constant names is to use all capital letters. To improve readability, subsequent words in the name are combined with the underscore character ('_')