Transcript Class6B

Happy October
Exam Review
10/01/2014
The Exam

Will be in Canvas

Two parts

Part A is recall – closed book, closed notes
◦ Quizzes, in class activity
◦ Know the terminology
◦ Be able to evaluate expressions

Part B is coding – open book, closed notes, closed “examples”
◦
◦
◦
◦
You will have a shell program that you will need to fill in
Prompts are given in the program itself
You may use whatever editor you prefer
You may NOT use code from labs since they were done with a partner.
Lab
EnergyDriver
public static…main()
{
Energy ed;
ed = new Energy()
energy = ed.kineticEnergy(100, 30);
}
Energy
public kineticEnergy(double mass, double velocity)
What is tester?
Energy tester;
tester = new Energy()
tester
kineticEnergy
tester contains a reference to an Energy object. The object has [data] and methods.
Primitive vs Reference types
PRIMITIVE

The space at declaration time holds the value.

There are only 8.
◦
◦
◦
◦
◦
◦
◦
◦
byte
short
int
long
float
double
char
boolean
REFERENCE

The space at declaration time holds the
reference to the value (object).

There are as many as you want to make.

Examples include:
◦
◦
◦
◦
DecimalFormat
Scanner
String
NumberFormat
Primitives, cont


We think of primitive containers as holding values.
Java can widen primitives to match the larger types but can only
narrow by using the cast operation.

int count;

double sum;

count = sum is illegal, but

count = (int) sum is legal since we are saying take the
integer part only.
Object-Oriented Programming
Object
Programming
Interface
Attributes (data)
typically private to this object
Other
objects
Methods
(behaviors / procedures)
1-7
Calling methods
“static”


Examples: Math class
No need for a specific version of Math. You
are just using its functions.
not static


Examples: String class
Which String do we mean? object or specific
set of data.

total = Math.pow(25.0, 3);

String message;

value = Math.abs(-250 * 4);

message = “CS 139 Exam”;

value = Math.abs(Math.pow(-250.0 * 4);

size = message.length();

part = message.substring(5);
void Methods and Value-Returning Methods

A void method is one that simply performs a task and then terminates.
System.out.println("Hi!");

A value-returning method not only performs a task, but also sends a value back to the code that called
it.
static method, class name
int number = Integer.parseInt("700");

It is possible to have a value returning method not save the result.
keyboard.nextLine(); // consume the new line
non static method, must have an object. This was created
by declaring keyboard to be a Scanner then instantiating
the Scanner.
5-9
Parts of a Method Header
Method
Modifiers
Return
Type
Method
Name
Parameter
List
public static void displayMessage ()
{
System.out.println("Hello");
}
5-10
Calling a Method


A method executes when it is called.
The main method is automatically called when a program starts, but
other methods are executed by method call statements.
displayMessage();


Notice that the method modifiers and the void return type are not
written in the method call statement. Those are only written in the
method header.
Examples: SimpleMethod.java, LoopCall.java, CreditCard.java,
DeepAndDeeper.java
5-11
Passing a Reference as an Argument
Both variables reference the same object
showLength(name);
“Warren”
address
The address of the object is copied into the
str parameter.
address
public static void showLength(String str)
{
System.out.println(str + " is " + str.length()
characters long.");
str = "Joe" // see next slide
}
5-12
+ "
Strings are Immutable Objects

Strings are immutable objects, which means that they cannot be
changed. When the line
str = "Joe";
is executed, it cannot change an immutable object, so creates a new
object.
The name variable holds the address of
a String object
The str variable holds the address of a
different String object

See example: PassString.java
5-13
address
“Warren”
address
“Joe”
Defining a Value-Returning Method
public static int sum(int num1, int num2)
{
Return type
int result;
result = num1 + num2;
return result;
}
This expression must be of the same data
type as the return type
5-14
The return statement causes the
method to end execution and it
returns a value back to the
statement that called the method.
Other questions