Download presentation
Presentation is loading. Please wait.
1
Happy October Exam Review 10/01/2014
2
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.
3
Lab EnergyDriver Energy public static…main()
{ Energy ed; ed = new Energy() energy = ed.kineticEnergy(100, 30); } public kineticEnergy(double mass, double velocity)
4
What is tester? Energy tester; tester = new Energy() tester kineticEnergy tester contains a reference to an Energy object. The object has [data] and methods.
5
Primitive vs Reference types
The space at declaration time holds the value. There are only 8. byte short int long float double char boolean 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
6
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.
7
Object-Oriented Programming
Attributes (data) typically private to this object Methods (behaviors / procedures) Other objects Programming Interface
8
Calling methods “static” not static Examples: Math class
No need for a specific version of Math. You are just using its functions. total = Math.pow(25.0, 3); value = Math.abs(-250 * 4); value = Math.abs(Math.pow( * 4); Examples: String class Which String do we mean? object or specific set of data. String message; message = “CS 139 Exam”; size = message.length(); part = message.substring(5);
9
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. int number = Integer.parseInt("700"); It is possible to have a value returning method not save the result. keyboard.nextLine(); // consume the new line static method, class name non static method, must have an object. This was created by declaring keyboard to be a Scanner then instantiating the Scanner.
10
Parts of a Method Header
Method Modifiers Return Type Method Name Parameter List public static void displayMessage () { System.out.println("Hello"); }
11
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
12
Passing a Reference as an Argument
Both variables reference the same object “Warren” showLength(name); public static void showLength(String str) { System.out.println(str + " is " + str.length() + " characters long."); str = "Joe" // see next slide } address The address of the object is copied into the str parameter. address
13
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. See example: PassString.java The name variable holds the address of a String object address “Warren” The str variable holds the address of a different String object address “Joe”
14
Defining a Value-Returning Method
public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } Return type The return statement causes the method to end execution and it returns a value back to the statement that called the method. This expression must be of the same data type as the return type
15
Other questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.