Exam Review 10/01/2014 Happy October
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 Energy public kineticEnergy(double mass, double velocity) public static…main() { Energy ed; ed = new Energy() energy = ed.kineticEnergy(100, 30); }
What is tester? Energy tester; tester tester = new Energy() 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.
1-7 Object-Oriented Programming Object Attributes (data) typically private to this object Methods (behaviors / procedures) Other objects Programming Interface
Calling methods “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); not static 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);
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.
5-10 Parts of a Method Header public static void displayMessage () { System.out.println("Hello"); } Method Modifiers Return Type Method Name Parameter List
5-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.javaSimpleMethod.javaLoopCall.javaCreditCard.java DeepAndDeeper.java
5-12 Passing a Reference as an Argument showLength(name); public static void showLength(String str) { System.out.println(str + " is " + str.length() + " characters long."); str = "Joe" // see next slide } address “Warren” Both variables reference the same object The address of the object is copied into the str parameter.
5-13 String s are Immutable Objects String s 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.javaPassString.java address “Warren” “Joe” The name variable holds the address of a String object The str variable holds the address of a different String object
5-14 Defining a Value-Returning Method public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } Return type This expression must be of the same data type as the return type The return statement causes the method to end execution and it returns a value back to the statement that called the method.
Other questions