Download presentation
Presentation is loading. Please wait.
1
CS 302 Week 9 Jim Williams
2
Note: For this correlation, we can't claim TopHat causes higher midterm score simply because it occurred before midterm. There could be something else (e.g., attendance) that influences both.
3
Mid term Evaluations: Other Resources 30 min delay
4
This Week Program 3 Pants on Fire
Focus Partners & Milestone 1 due by Thursday Objects (Instances) and Classes Conceptual Level and Code Level
5
Classes and Objects public, private, protected, <package>
accessors (getters),mutators (setters) state of an object constructor overloading this constructor, no-arg, default
6
Visibility Modifiers For members of a class: public private protected
<package> Demo
7
Can methodA call methodB?
//classes in different files in same package class A { public void methodA() { methodB(); } class B { void methodB() { yes no depends error
8
Can a method outside the package call methodA()?
//classes in different files in same package class A { public void methodA() { methodB(); } class B { void methodB() { yes no depends error
9
Accessors (getters) class Pizza { private String toppings;
public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; }
10
Mutators (setters) class Pizza { private String toppings;
public void setToppings( String tops) { if ( tops != null && tops.length() > 0) toppings = tops; }
11
Can a setter also get? yes class Pizza { no private String toppings;
public String setToppings( String tops) { String previous = toppings; toppings = tops; return previous; } yes no depends error
12
Object/Instance State
enum Color {GREEN,YELLOW, RED}; class TrafficLight{ private Color color = Color.RED; public void change() { if ( color == Color.GREEN) color = Color.YELLOW; else if ( color == Color.YELLOW) color = Color.RED; else color = Color.GREEN; } public Color getColor() { return color; } public String toString() { return color.name(); }
13
What variables are used for the state of "instance who's reference is in p"?
class Person { private static count = 0; private boolean amHungry = false; public Person( String name) { } } public static void main(String []args) { Person p = new Person("Fred"); count name amHungry error/other
14
this class Picture { private boolean hasFrame;
public Picture( boolean hasFrame) { this.hasFrame = hasFrame; }
15
Does this print true or false?
class Person { static count = 0; private boolean something = false; boolean getThing(boolean something) { return this.something; } Person p = new Person(); System.out.println( p.getThing( true)); true false error/other
16
Does this print 0, 1, other or error?
class Person { static count = 0; private boolean something = false; Person(boolean something) { this.something = something; count++; } System.out.println( Person.count); 1 other error
17
Default Constructor class Person { private String name; }
Can we create an instance? Person p = new Person();
18
Constructor Overloading
class Person { private String name; Person() { this("no name"); } Person(String name) { this.name = name;
19
Constructors yes - the default, no argument constructor yes - a really good one. no error If there is no constructor in a class is a constructor automatically provided by the compiler?
20
Will the no-argument constructor be provided in this case?
class Cup { private String contents; Cup(String contents) { this.contents = contents; } yes - there is not a no-arg constructor no yes - why not? error
21
More Classes and Objects
class, instance, object field, attribute, instance variable toString() instance method, overriding this, super printing object
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.