CS 302 Week 9 Jim Williams
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. https://learnuw.wisc.edu/toolbox/tophat.html
Mid term Evaluations: Other Resources 30 min delay
This Week Program 3 Pants on Fire Focus Partners & Milestone 1 due by Thursday Objects (Instances) and Classes Conceptual Level and Code Level
Classes and Objects public, private, protected, <package> accessors (getters),mutators (setters) state of an object constructor overloading this constructor, no-arg, default
Visibility Modifiers For members of a class: public private protected <package> Demo
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
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
Accessors (getters) class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; }
Mutators (setters) class Pizza { private String toppings; public void setToppings( String tops) { if ( tops != null && tops.length() > 0) toppings = tops; }
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
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(); }
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
this class Picture { private boolean hasFrame; public Picture( boolean hasFrame) { this.hasFrame = hasFrame; }
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
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
Default Constructor class Person { private String name; } Can we create an instance? Person p = new Person();
Constructor Overloading class Person { private String name; Person() { this("no name"); } Person(String name) { this.name = name;
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?
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
More Classes and Objects class, instance, object field, attribute, instance variable toString() instance method, overriding this, super printing object