Fundamentals of Programming
Lecture 3
Programming Basics (in Java)
Java Programming
• Every time you use a computer, you execute various
applications that perform tasks for you.
• For example, your e-mail application helps you send and
receive e-mail, and your web browser lets you view web
pages from websites around the world.
• Computer programmers create such applications by writing
computer programs.
• A Java application is a computer program that executes
when you use the java command to launch the Java Virtual
Machine (JVM).
01/21/2026 Programming by T. Lawrence 2
Printing a Line of Text
01/21/2026 Programming by T. Lawrence 3
Java Comments
• Programmers insert comments to document programs
and improve their readability.
• This helps other people read and understand your
programs.
• The Java compiler ignores comments
01/21/2026 Programming by T. Lawrence 4
public class Welcome1
• Begins a class declaration for class Welcome1. Every program
in Java consists of at least one class declaration that is defined
by you(the programmer).
• By convention, all class names in Java begin with a capital letter
and capitalize the first letter of each word they include (e.g.,
SampleClassName).
• Java is case sensitive i.e, uppercase and lowercase letters are
distinct, so a1 and A1 are different (but both valid) identifiers.
01/21/2026 Programming by T. Lawrence 5
[Link]( "Welcome to Java Programming!" );
instructs the computer to perform an action—namely, to
print the string of characters contained between the double
quotation marks (but not the quotation marks themselves).
• [Link]
is known as the standard output object. [Link] allows
Java applications to display sets of characters in the
command window from which the Java application executes.
01/21/2026 Programming by T. Lawrence 6
• Method [Link] displays (or prints) a
line of text in the command window. The string in
the parentheses in line 9 is the argument to the
method.
• Method [Link] performs its task by
displaying (also called outputting) its argument in
the command window.
01/21/2026 Programming by T. Lawrence 7
Modifying Our First Java Program
• Displaying a Single Line of Text with Multiple Statements
• The first statement uses [Link]’s method print to
display a string. Unlike println, after displaying its
argument, print does not position the output cursor at the
beginning of the next line
01/21/2026 Programming by T. Lawrence 8
Modifying Our First Java Program
• Displaying Multiple Lines of Text with a Single
Statement
Note, however, that the two characters \ and n
(repeated three times in the statement) do not appear
on the screen.
The backslash (\) is called an escape character.
01/21/2026 Programming by T. Lawrence 9
Some common escape sequences.
01/21/2026 Programming by T. Lawrence 10
Try out
Programmi
g is fu
ny bu it’s
until you “try it out”
01/21/2026 Programming by T. Lawrence 11
Assignment
01/21/2026 Programming by T. Lawrence 12
Displaying Text with printf
• the [Link] method for displaying
formatted data the f in the name printf stands for
“formatted.”
call method [Link] to display the
program’s output. The method call specifies three
arguments. When a method requires multiple
arguments, the arguments are separated with
commas (,).
01/21/2026 Programming by T. Lawrence 13
Statements and expressions
• A statement is the simplest thing you can do in Java; a
statement forms a single Java operation. All the following are
simple Java statements:
• int i = 1;
• import [Link];
• [Link](“This motorcycle is a “ + color + “ ”+ make);
• Statements sometimes return values for example, when you
add two numbers together or test to see whether one value is
equal to another. These kind of statements are called
expressions.
• The most important thing to remember about Java statements
is that each one ends with
01/21/2026
a semicolon.
Programming by T. Lawrence 14
Expressions
• expression: A value or operation that computes a
value.
• Examples: 1 + 4 * 5
(7 + 2) * 6 / 3
42
– The simplest expression is a literal value.
– A complex expression can use operators and
parentheses.
01/21/2026 Programming by T. Lawrence 15
Arithmetic operators
• operator: Combines multiple values or expressions.
–+ addition
–- subtraction (or negation)
–* multiplication
–/ division
–% modulus (a.k.a. remainder)
• As a program runs, its expressions are evaluated.
– 1 + 1 evaluates to 2
– [Link](3 * 4); prints 12
• How would we print the text 3 * 4 ?
01/21/2026 Programming by T. Lawrence 16
Integer division with /
• When we divide integers, the quotient is also an integer.
– 14 / 4 is 3, not 3.5
3 4 52
4 ) 14 10 ) 45 27 ) 1425
12 40 135
2 5 75
54
21
• More examples:
– 32 / 5 is 6
– 84 / 10 is 8
– 156 / 100 is 1
– Dividing by 0 causes an error when your program runs.
01/21/2026 Programming by T. Lawrence 17
Integer remainder with %
• The % operator computes the remainder from integer division.
– 14 % 4 is 2
What is the result?
– 218 % 5 is 3 45 % 6
3 43
2 % 2
4 ) 14 5 ) 218
12 20 8 % 20
2 18 11 % 0
15
3
• Applications of % operator:
– Obtain last digit of a number: 230857 % 10 is 7
– Obtain last 4 digits: 658236489 % 10000 is 6489
– See whether a number is odd: 7 % 2 is 1, 42 % 2 is 0
01/21/2026 Programming by T. Lawrence 18
Precedence
• precedence: Order in which operators are evaluated.
– Generally operators evaluate left-to-right.
1 - 2 - 3 is (1 - 2) - 3 which is -4
– But * / % have a higher level of precedence than + -
1 + 3 * 4 is 13
6 + 8 / 2 * 3
6 + 4 * 3
6 + 12 is 18
– Parentheses can force a certain order of evaluation:
(1 + 3) * 4 is 16
– Spacing does not affect order of evaluation
1+3 * 4-2 is 11
01/21/2026 Programming by T. Lawrence 19
Precedence examples
• 1 * 2 + 3 * 5 % 4 1 + 8 % 3 * 2 - 9
• \_/ \_/
| |
2 + 3 * 5 % 4 1 + 2 * 2 - 9
• \_/ \___/
| |
2 + 15 % 4 1 + 4 - 9
• \___/
\______/
| |
2 + 3 5 - 9
• \________/
\_________/
| |
-4
5
01/21/2026 Programming by T. Lawrence 20
Precedence questions
• What values result from the following expressions?
–9 / 5
– 695 % 20
–7 + 6 * 5
–7 * 6 + 5
– 248 % 100 / 5
–6 * 3 - 9 / 4
– (5 - 7) * 4
– 6 + (18 % (17 - 12))
01/21/2026 Programming by T. Lawrence 21
Real numbers (type double)
• Examples: 6.022 , -42.0 , 2.143e17
– Placing .0 or . after an integer makes it a double.
• The operators + - * / % () all still work with
double.
– / produces an exact answer: 15.0 / 2.0 is 7.5
– Precedence is the same: () before * / % before + -
01/21/2026 Programming by T. Lawrence 22
Real number example
• 2.0 * 2.4 + 2.25 * 4.0 / 2.0
• \___/
|
4.8 + 2.25 * 4.0 / 2.0
• \___/
|
4.8 + 9.0 / 2.0
• \_____/
|
4.8 + 4.5
• \____________/
|
9.3
01/21/2026 Programming by T. Lawrence 23
Mixing types
• When int and double are mixed, the result is a double.
– 4.2 * 3 is 12.6
• The conversion is per-operator, affecting• only
2.0 +its
10operands.
/ 3 * 2.5 - 6 / 4
– 7 / 3 * 1.2 + 3 / 2 • \___/
– \_/ |
| 2.0 + 3 * 2.5 - 6 / 4
2 * 1.2 + 3 / 2 • \_____/
– \___/ |
| 2.0 + 7.5 - 6 / 4
2.4 + 3 / 2 •
– \_/ \_/
| |
2.4 + 1 2.0 + 7.5 - 1
– \________/ • \_________/
| |
3.4 9.5 - 1
• \______________/
– 3 / 2 is 1 above, not 1.5. |
8.5
01/21/2026 Programming by T. Lawrence 24
String concatenation
• string concatenation: Using + between a string and another value
to make a longer string.
"hello" + 42 is "hello42"
1 + "abc" + 2 is "1abc2"
"abc" + 1 + 2 is "abc12"
1 + 2 + "abc" is "3abc"
"abc" + 9 * 3 is "abc27"
"1" + 1 is "11"
4 - 1 + "abc" is "3abc"
• Use + to print a string and an expression's value together.
– [Link]("Grade: " + (95.1 + 71.9) / 2);
• Output: Grade: 83.5
01/21/2026 Programming by T. Lawrence 25
Data types
• type: A category or set of data values.
– Constrains the operations that can be performed on data
– Many languages ask the programmer to specify types
– Examples: integer, real number, string
• Internally, computers store everything as 1s and 0s
104 01101000
"hi" 01101000110101
01/21/2026 Programming by T. Lawrence 26
Primitive Types vs. Reference Types
Data types in Java are divided into two categories: primitive
types and reference types
The primitive types are
• boolean, byte,
• char, short,
• int, long,
• float double.
All nonprimitive types are reference types, so classes, which
specify the types of objects, are reference types.
01/21/2026 Programming by T. Lawrence 27
Integer Types
01/21/2026 Programming by T. Lawrence 28
Java's primitive types
• primitive types: 8 simple types for numbers, text, etc.
– Java also has object types, which we'll talk about later
Name Description Examples
– int integers (up to 231 - 1) 42, -3, 0, 926394
– double real numbers (up to 10308) 3.1, -0.25, 9.4e3
– char single text characters 'a', 'X', '?', '\n'
– boolean logical values true, false
• Why does Java distinguish integers vs. real numbers?
01/21/2026 Programming by T. Lawrence 29
Identifiers
• Identifiers are the names that identify the elements such as classes,
methods, and variables in a program.
• All identifiers must obey the following rules:
1. An identifier is a sequence of characters that consists of letters, digits,
underscores (_), and dollar signs ($).
2. An identifier must start with a letter, an underscore (_), or a dollar sign
($). It cannot start with a digit.
3. An identifier cannot be a reserved word.
4. An identifier cannot be true, false, or null.
5. An identifier can be of any length.
01/21/2026 Programming by T. Lawrence 30
Tip
• Identifiers are for naming variables, methods, classes, and other items in a
program.
• Since Java is case sensitive, area, Area, and AREA are all different
identifiers.
• Descriptive identifiers make programs easy to read. Avoid using
abbreviations for identifiers. Using complete words is more descriptive. For
example, numberOfStudents is better than numStuds, numOfStuds, or
numOfStudents.
• For example, $2, ComputeArea, area, radius, and print are legal identifiers,
whereas 2A and d+4 are not because they do not follow the rules.
01/21/2026 Programming by T. Lawrence 31
Variables
• variable: A piece of the computer's memory that is given
a name and type, and can store a value.
• Variables are used to represent values that may be
changed in the program.
– Like preset stations on a car stereo, or cell phone
speed dial:
– Steps for using a variable:
• Declare it - state its name and type
• Initialize it - store a value into it
• Use it - print it or use it as part of an expression
01/21/2026 Programming by T. Lawrence 32
Variable Naming
To use variables in your programs, you must know how to create
variable names. Variable names must adhere to the following rules:
1. The name can contain letters, digits, and the underscore
character (_).
2. The first character of the name must be a letter. The
underscore is also a legal first character, but its use is not
recommended.
3. Case matters: Thus, the names “count” and “COUNT” refer to
two different variables.
4. Keywords can't be used as variable names. A keyword is a
word that is part of the language. Keywords are the words
whose meaning has already been explained to the compiler
01/21/2026 Programming by T. Lawrence 33
Variable Declarations
Before you can use a variable in a java program, it must
be declared. A variable declaration tells the compiler the
name and type of a variable. If your program attempts to
use a variable that hasn't been declared, the compiler
generates an error message.
A variable declaration has the following form:
• typename varname; typename specifies the variable
type and must be one of the keywords
• int count, number, start; /* three integer variables */
• float percent, total; /* two float variables */
01/21/2026 Programming by T. Lawrence 34
Variable Declaration
• variable declaration: Sets aside memory for storing a value.
– Variables must be declared before they can be used.
• Syntax:
type name;
• The name is an identifier.
x
– int x;
myGPA
– double myGPA;
01/21/2026 Programming by T. Lawrence 35
Initializing Numeric Variables
• When you declare a variable, you instruct the compiler
to set aside storage space for the variable. However,
the value stored in that space--the value of the
variable--isn't defined. It might be zero, or it might be
some random "garbage" value. Before using a variable,
you should always initialize it to a known value.
• You can do this independently of the variable
declaration by using an assignment statement, as in
this example:
• int count; /* Set aside storage space for count */
01/21/2026 • count = 0; Programming
/* Storeby T.0Lawrence
in count */ 36
Assignment
• assignment: Stores a value into a variable.
– The value can be an expression; the variable stores its result.
• Syntax:
name = expression;
x 3
– int x;
x = 3;
– double myGPA; myGPA 3.25
myGPA = 1.0 + 2.25;
01/21/2026 Programming by T. Lawrence 37
Using variables
• Once given a value, a variable can be used in expressions:
int x;
x = 3;
[Link]("x is " + x); // x is 3
[Link](5 * x - 1); // 5 * 3 - 1
• You can assign a value more than once:
x 3
11
int x;
x = 3;
[Link](x + " here"); // 3 here
x = 4 + 7;
[Link]("now x is " + x); // now x is 11
01/21/2026 Programming by T. Lawrence 38
Declaration/initialization
• A variable can be declared/initialized in one statement.
• Syntax:
type name = value;
myGPA 3.95
– double myGPA = 3.95;
x 14
– int x = (11 % 3) + 12;
01/21/2026 Programming by T. Lawrence 39
Assignment and algebra
• Assignment uses = , but it is not an algebraic equation.
= means, "store the value at right in variable at left"
• The right side expression is evaluated first,
and then its result is stored in the variable at left.
• What happens here? x 3
5
int x = 3;
x = x + 2; // ???
01/21/2026 Programming by T. Lawrence 40
Assignment and types
• A variable can only store a value of its own type.
– int x = 2.5; // ERROR: incompatible types
• An int value can be stored in a double variable.
– The value is converted into the equivalent real number.
myGPA 4.0
– double myGPA = 4;
avg 5.0
– double avg = 11 / 2;
• Why does avg store 5.0
and not 5.5 ?
01/21/2026 Programming by T. Lawrence 41
Compiler errors
• A variable can't be used until it is assigned a value.
– int x;
[Link](x); // ERROR: x has no value
• You may not declare the same variable twice.
– int x;
int x; // ERROR: x already exists
– int x = 3;
int x = 5; // ERROR: x already exists
• How can this code be fixed?
01/21/2026 Programming by T. Lawrence 42
Printing a variable's value
• Use + to print a string and a variable's value on one line.
– double grade = (95.1 + 71.9 + 82.6) / 3.0;
[Link]("Your grade was " + grade);
int students = 11 + 17 + 4 + 19 + 14;
[Link]("There are " + students +
" students in the course.");
• Output:
Your grade was 83.2
There are 65 students in the course.
01/21/2026 Programming by T. Lawrence 43
Constants
Like a variable, a constant is a data storage location used by your
program. Unlike a variable, the value stored in a constant can't be
changed during program execution.
Literal Constants
• A literal constant is a value that is typed directly into the source
code wherever it is needed. Here are two examples:
int count = 20;
float taxRate = 0.28;
• The 20 and the 0.28 are literal constants. The preceding statements
store these values in the variables count and taxRate.
• Note that one of these constants contains a decimal point, whereas
the other does not. The presence or absence of the decimal point
distinguishes floating-point constants from integer constants.
01/21/2026 Programming by T. Lawrence 44
Symbolic Constants
• A symbolic constant is a constant that is represented by a name
(symbol) in your program. Like a literal constant, a symbolic constant
can't change. Whenever you need the constant's value in your program,
you use its name as you would use a variable name. The actual value of
the symbolic constant needs to be entered only once, when it is first
defined.
• Symbolic constants have two significant advantages over literal
constants, as the following example shows. Suppose that you're writing
a program that performs a variety of geometrical calculations. The
program frequently needs the value ,, (3.14159) for its calculations. For
example, to calculate the circumference and area of a circle with a
known radius, you could write
circumference = 3.14159 * (2 * radius);
area = 3.14159 * (radius)*(radius);
01/21/2026 Programming by T. Lawrence 45
If, however, you define a symbolic constant with the name PI and the
value 3.14, you could write
circumference = PI * (2 * radius);
area = PI * (radius)*(radius);
The resulting code is clearer. Rather than puzzling over what the
value 3.14 is for, you can see immediately that the constant PI is
being used.
2. The second advantage of symbolic constants becomes apparent
when you need to change a constant. Continuing with the preceding
example, you might decide that for greater accuracy your program
needs to use a value of PI with more decimal places: 3.14159 rather
than 3.14. If you had used literal constants for PI, you would have to
go through your source code and change each occurrence of the
value from 3.14 to 3.14159. With a symbolic constant, you need to
01/21/2026
make a change only in the place where the constant is defined.
Programming by T. Lawrence 46
Reading Input from the Console
• Reading input from the console enables the program to accept input from
the user.
• Java uses [Link] to refer to the standard output device and
[Link] to the standard input device.
• By default, the output device is the display monitor and the input device is
the keyboard.
• To perform console output, you simply use the println method to display a
primitive value or a string to the console. Console input is not directly
supported in Java, but you can use the Scanner class to create an object
to read input from [Link]
01/21/2026 Programming by T. Lawrence 47
Reading Input from the Console
• Scanner input = new Scanner([Link]);
• The syntax new Scanner([Link]) creates an object of the
Scanner type. The syntax Scanner input declares that input is a
variable whose type is Scanner.
• The whole line Scanner input = new Scanner([Link])
creates a Scanner object and assigns its reference to the variable
input.
01/21/2026 Programming by T. Lawrence 48
Reading Input from the Console
• To invoke a method on an object is to ask the object to perform a
task. You can invoke the nextDouble() method to read a double
value as follows:
double radius = [Link]();
• This statement reads a number from the keyboard and assigns
the number to radius.
01/21/2026 Programming by T. Lawrence 49
[Link]
1 import [Link];
2 public class ComputeAreaWithConsoleInput {
3 public static void main(String[] args) {
4 // Create a Scanner object
5 Scanner input = new Scanner([Link]);
6 // Prompt the user to enter a radius
7 [Link]("Enter a number for radius: ");
8 double radius = [Link]();
9 // Compute area
10 double area = radius * radius * 3.14159;
11 [Link](“Area for the circle of radius " + radius + " is " +
area);
12 }
13 }
01/21/2026 Programming by T. Lawrence 50
• The Scanner class is in the [Link] package. It is imported in line 1.
There are two types of import statements: specific import and wildcard
import.
import [Link];
• The wildcard import imports all the classes in a package by using the
asterisk as the wildcard. For example, the following statement imports
all the classes from the package [Link].
import [Link].*;
• Line 7 displays a string "Enter a number for radius: " to the console.
This is known as a prompt, because it directs the user to enter an input.
• Your program should always tell the user what to enter when expecting
input from the keyboard.
• The print method in line 7
[Link]("Enter a number for radius: ");
01/21/2026 Programming by T. Lawrence 51
• Line 5 creates a Scanner object.
• The statement in line 8 reads input from the keyboard.
double radius = [Link]();
• After the user enters a number and presses the Enter key, the
program reads the number and assigns it to radius.
01/21/2026 Programming by T. Lawrence 52
Try out
1. Write a program that can read multiple input from the keyboard, computes
and display their average. At least three numbers.
2. How do you write a statement to let the user enter a double value from the
keyboard?
3. What happens if you entered 5a when executing the following code?
double radius = [Link]();
4. Are there any performance differences between the following two import
statements?
1. import [Link];
2. import [Link].*;
01/21/2026 Programming by T. Lawrence 53
END
01/21/2026 Programming by T. Lawrence 54