CS 46A - Lecture 3
Pre-class reading
- Sections 1.8, 2.1 - 2.3
- Did you take the quiz?
Algorithms
- Algorithm: A sequence of steps that is:
- unambiguous
- executable
- terminating
- Algorithm for deciding which car to buy, based on total cost:
For each car, compute the total cost as follows:
annual fuel consumed = annual miles driven / fuel efficiency
annual fuel cost = price per gallon x annual fuel consumed
operating cost = 10 x annual fuel cost
total cost = purchase price + operating cost
If total cost1 < total cost2
Choose car1
Else
Choose car2
Pseudocode
- Pseudocode: An informal description of an algorithm
- Describe how a value is set or changed:
total cost = purchase price + operating cost
- Describe decisions and repetitions:
For each car
operating cost = 10 x annual fuel cost
total cost = purchase price + operating cost
Use indentation to indicate which statements should be selected or
repeated
- Indicate results:
Choose car1
Program Development Process
Types
- A type defines a set of values and the operations that
can be carried out on the values
- Examples:
- 13 has type int
- "Hello, World" has type String
- System.out has type PrintStream
- Java has separate types for integers and
floating-point numbers
- The double type denotes floating-point numbers
- A value such as 13 or 1.3 that occurs in a Java program
is called a number literal
Number Literals
Number Types
- Number types are primitive types
- Numbers can be combined by arithmetic operators such as +,
-, and *
Lecture 3 Clicker Question 1
What is the type of the values 0, 0.0, and "0"?
int, double, string
int, float, string
primitive, primitive, object
- None of the above
Variables
- Use a variable to store a value that you want to use at
a later time
- A variable has a type, a name, and a value:
String greeting = "Hello, World!";
PrintStream printer = System.out;
int width = 13;
- Variables can be used in place of the values that they store:
printer.println(greeting); // Same as System.out.println("Hello, World!")
printer.println(width); // Same as System.out.println(20)
- It is an error to store a value whose type does not match the type of the
variable:
String greeting = 20; // ERROR: Types don’t match
Variable Declarations
Identifiers
- Identifier: name of a variable, method, or class
- Rules for identifiers in Java:
- Can be made up of letters, digits, and the underscore (_)
and dollar sign ($) characters
- Cannot start with a digit
- Cannot use other symbols such as ? or %
- Spaces are not permitted inside identifiers
- You cannot use reserved words such as
public
- They are case sensitive
- By convention, variable names start with a lowercase letter
Camel case
: Capitalize the first letter of a word in a
compound word such as farewellMessage
- By convention, class names start with an uppercase letter
- Do not use the $ symbol in names — it is intended for names
that are automatically generated by tools
Syntax 2.1 Variable
Declaration
Variable Names
Lecture 3 Clicker Question 2
How many of the following are legal identifiers?
Greeting1
g
void
101dalmatians
Hello, World
<greeting>
- one of them
- two of them
- three of them
- four of them
The Assignment Operator
- Assignment operator =
- Used to change the value of a variable
int width = 10;
The Assignment Operator
int width = 10;
width = 20;
Uninitialized Variables
- It is an error to use a variable that has never had a value assigned to
it:
int height;
width = height; // ERROR—uninitialized variable height
- Remedy: assign a value to the variable before you use it:
int height = 30;
width = height; // OK
- Even better, initialize the variable when you declare it:
int height = 30;
int width = height; // OK
Syntax 2.2 Assignment
Assignment
- The right-hand side of the = symbol can be a mathematical
expression:
width = height + 10;
- Means
compute the value of width + 10
and store that value in the variable
width 
Lecture 3 Clicker Question 3
What is the value of x
and y
after the following
statements?
double x = 10;
double y = 2 * x;
x = 2 * y;
y = 2 * y;
- x = 10, y = 20
- x = 20, y = 40
- x = 40, y = 40
- x = 40, y = 80
Lecture 3 Clicker Question 4
Suppose you have a variable
String greeting = "Hello, World!";
How do you change the value of the greeting variable to "Hello,
Nina!"?
greeting = "Hello, Nina!";
String greeting = "Hello, World!";
greeting.changeTo("Hello, Nina!");
greeting.substring(6, 11) = "Nina";