Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 302 Week 11 Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 302 Week 11 Jim Williams, PhD."— Presentation transcript:

1 CS 302 Week 11 Jim Williams, PhD

2 Notes Midterm 2 Thursday Topics: Locations
Inheritance, overriding, Object class, polymorphism Misconceptions

3 Review Members of a Class
new Dog( “Fido”); new Dog( “Sadie”); Dog() ? Save to instance var Assign final in ctor? Static vs instance Public vs private Accessor vs mutator toString, equals public class Dog { public String name; private final int ID; static int numIds = 0; public Dog( String name) { this.name = name; ID = ++numIds; } public String toString() { return name + ID; No there isn't a no-argument constructor. The default isn't available since a constructor is provided. is the constructor parameter saved to the instance var? no. need this.name = name; Yes, a value can be assigned to a final instance variable in the constructor A static (class) variable there is one. An instance variable there is one per instance. public accessible outside the class and package, private accessible inside the class An accessor retrieves (reads, gets) information, a mutator changes (writes, sets) information. toString and equals methods are inherited from Object and may be overridden.

4 Demo Equals method Object class inheritance overriding polymorphism
is-a vs has-a @Override public boolean equals(Object obj) { if ( obj == this) { return true; } else if ( obj instanceof Apple){ Apple objApple = (Apple)obj; System.out.println("objApple: " + objApple); if ( this.name.equals(objApple.name)) { } else { return false; }

5 Does equals() _______ Object's method?
override overload neither error class Apple { private String brand; public Apple(String brand) { this.brand = brand; } public boolean equals(Apple obj) { return this.brand.equals( obj.brand); overload, since Object's equals method is defined: public boolean equals(Object obj)

6 Does equals() _______ Object's method?
override overload neither error class Apple { private String brand; public Apple(String brand) { this.brand = brand; } public boolean equals(Object obj) { Apple anApple = (Apple)obj; return this.brand.equals( anApple.brand); } override is true, but is not safe. Before doing the downcast of obj to Apple, the safe was is to use instanceof to check whether obj is an instance of Apple.

7 Exam Today

8 Draw what is happening in memory.
public class Cup{ private int count = 0; private double percentFull= 0; public Cup( double percentFull) { this.percentFull = percentFull; count++; } public static void main(String []args) { Cup[] cups = new Cup[5]; for ( int i = 0; i < cups.length; i++) { cups[i] = new Cup((double)i / cups.length);

9 Drawing Program Create a class Shape with methods getArea and draw.
Extend Shape with classes Circle and Rectangle and implement those methods. Recall for Circle: Area = π * radius * radius; Circumference = π × diameter Create a Board class that provides methods for adding shapes and showing shapes. Create a main method for adding and showing shapes.

10 Misconceptions

11 True or False, Why? (counter example?)
The computer knows the intention of a piece of code and acts accordingly. Assignment moves a value from one variable to another. Assignment works in both directions. Assignment copies a value from the right side to the variable on the left side. 1 false 2 false 3 false 4 true

12 True or False, Why? (counter example?)
Expressions (not their values) are passed as parameters. A variable can hold multiple values at a time. A variable remembers old values. In Java, objects and instances are the same thing (synonymous) and are different from classes. 1 false 2 false 3 false 4 true

13 True or False, Why? Two objects with the same value of a “name” attribute are the same object. true false depends error False, two objects may have the same value of any attribute

14 Two different variables must refer to two different objects.
True or False, Why? Two different variables must refer to two different objects. true false depends error False, two different variables may have the reference to the same object

15 True or False, Why? Two objects with the same class and the same state are the same object. true false depends error False, two objects are different objects regardless of their state. The state of an object is the values of its attributes. Two instances of a Stoplight class, for example, could both have state 'red' but would not be the same stoplight.

16 A set (such as “team” or “the species of birds”) cannot be a class.
True or False, Why? A set (such as “team” or “the species of birds”) cannot be a class. true false depends error False, anything can be the name of a class.

17 True or False, Why? Constructors can include only assignment statements to initialize attributes. true false depends error False. Frequently constructors contain assignment statements, but they can include other statements as well.

18 True or False, Why? Instantiation involves only the execution of the constructor method body, not the allocation of memory. true false depends error False, instantiation includes the allocation of memory and calling the constructor.

19 Declaring a variable also creates an object.
True or False, Why? Declaring a variable also creates an object. true false depends error False. Duck d; declares a variable but does not create an object(instance) of Duck.

20 True or False, Why? There is no need to invoke the constructor because its definition is sufficient for object creation. true false depends error False

21 An object cannot be the value of an attribute.
True or False, Why? An object cannot be the value of an attribute. true false depends error False. The instance of the string is an object that is the value of an attribute. class Person { String name = "Gilmore"; }

22 Methods do not affect object state.
True or False, Why? Methods do not affect object state. true false depends error False, mutators specifically affect an object's state


Download ppt "CS 302 Week 11 Jim Williams, PhD."

Similar presentations


Ads by Google