Presentation is loading. Please wait.

Presentation is loading. Please wait.

2009 Test Key.

Similar presentations


Presentation on theme: "2009 Test Key."— Presentation transcript:

1 2009 Test Key

2 Reminder: Unit 3 Quiz – Wednesday
Unit 3 Test: Thurs May 2 AP Exam: MAY 17!!! (Please….PLEASE order review books today. If you already have one, start putting in an hour every night!)

3 Answer: A

4 Answer: D

5 Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables. Strings, on the other hand, are not primitive: there is a class called String that has methods, such as .length( ), .charAt( ), etc. So, when you create a String variable, you are actually creating an object of the String class. Using primitive types is more efficient, in terms of code and computer memory. But what if you want to use certain methods on an int, a double, or a char?

6 Wrapper Classes A wrapper class allows you to “wrap” or “box” a primitive type into an object, so that you can use methods associated with that object. Example: Integer age = new Integer(34); The value of age is 34, but you can do more with this than you could with a normal, primitive int. Note: Integers are immutable – their values cannot be changed. So, this is illegal: age = 35;

7 What is the point of a Wrapper Class?
The 3 most common uses of a wrapper class are: To use in an ArrayList (we’ll learn about ArrayLists soon – for now, you should know that they hold Objects, not primitive types. So, you need wrapper classes [which are subclasses of Object] when using an ArrayList. ) When you want to use a null reference (to deliberately set a variable value to null. When would you do this? One example: When you can’t be 100% sure that a method will return a valid integer.) When you want to use an Integer, Double, or Char polymorphically example: Object num = new Integer(23);

8 Interfaces An abstract method is a method without an implementation. In other words, the method has a first line (also called a signature or a header), but it has no body. An interface is a collection of abstract methods and (sometimes) constants An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish, as a “formal contract”, a set of methods that a class will implement. In simple terms: an interface is a blueprint/template/protocol that shows programmers how to write a certain type of class.

9 interface is a reserved word
public interface Sample { public final double PI = 3.14; public void times2(int x); public double getPI(); } An interface can contain constants Notice: none of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

10 in Sample must be implemented (given a definition)
public class Example implements Sample { public void times2(int x) System.out.println(x*2); } public double getPI() return PI; Each method listed in Sample must be implemented (given a definition) Example has access to the constant PI (from Sample) Open Sample, Example

11 In a class diagram (sometimes called a UML diagram), a dotted or dashed arrow points from any class that implements an interface to the interface. Example:

12 Implementing an interface is technically a form of inheritance, since the constants and methods in an interface are inherited.

13 A class formally implements an interface by
An interface cannot be instantiated (meaning you can’t create an object of it from a client program). Methods in an interface cannot be private. It is nonsensical to have a private method in an interface – where would this method be called from? Remember, all methods in an interface are abstract (empty). A class formally implements an interface by stating so in the class header (using the word implements) providing implementations (writing code) for each abstract method in the interface If a class implements an interface, then it MUST define/implement (provide code for) all methods that are in the interface

14 A class that implements an interface can contain other methods as well
In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

15 The interfaces are listed in the implements clause
A class can implement multiple interfaces. (Note that this is different from inheritance: a class can only extend (inherit from) ONE parent.) The interfaces are listed in the implements clause The class MUST implement (write code for) all methods in all interfaces listed in the header Also, many different classes can implement the same interface. public class Mult implements Interface1, Interface2 { /* all methods of both interfaces MUST be defined here */ }

16 Note: extends must occur before implements
It is possible for a class to both extend a parent class and implement interfaces: public class Car extends Vehicle implements Interface1 { // code here } Note: extends must occur before implements

17 The Java standard class library contains many helpful interfaces
The Comparable interface contains only one abstract method. This method is compareTo(), which is used to compare two objects lexicographically (comparing their ASCII values). The String class implements Comparable, giving us the ability to put strings in lexicographic order: compares the letters using their ASCII (aka Unicode) values Demo: Compare

18 When a programmer writes a class that implements the Comparable interface, it’s up to him/her to determine what makes one object greater or less than another Not all objects are compared numerically or lexicographically

19 Confusing terminology:
A class that “connects” to an interface is said to implement the interface, by using the keyword implements Sometimes, you will see exams and text books mention that a certain method has not been implemented – what they mean is that the method has not been given any code in its body. In other words, the method has a signature, but is otherwise empty.

20 Answer: C

21 Answer: B Answer: C

22 Answer: E

23 Answer: B

24 Assignments 1) Open pages 291-293 from the Unit 3 folder.
Answer the following questions. Save answers in a Word doc called p291-3Qs. p multiple choice #4, 6, 7, 9, 10 P. 293 true/false #9 – 10 2) Open 2014 Practice Exam Qs. Complete Section I (Multiple Choice). Skip these: 5, 14, 18, 23, 39, 40. Save in a Word Doc called 2014_Practice_Answers. to me when done. 3) Complete Battleship.


Download ppt "2009 Test Key."

Similar presentations


Ads by Google