Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 - Week 13 Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 200 - Week 13 Jim Williams, PhD."— Presentation transcript:

1 CS Week 13 Jim Williams, PhD

2 This Week Team Lab: Instantiable Class BP2 Strategy
Lecture: Classes as templates

3 BP2 Strategy M1: 2 of 3 milestone tests didn't require reading a file.
M2: matchCase and translate (2 of 3 tests) don't require file I/O Opportunities to reuse code make supporting method rather than copy code. No change in Milestone deadlines However, 2 more days (Saturday 12/9) before final program grading.

4 Class as a template A template for objects/instances.
attributes/fields accessors/getters mutators/setters constructors this (implicit parameter) public, private, protected, <package>

5 'this' implicit parameter
The way to access the current instance from within the instance methods.

6 Constructor Called when instance created
Used to initialize instance fields

7 Default Constructor class Person { private String name; }
Can we create an instance? Person p = new Person();

8 Constructor Overloading
class Person { private String name; Person() { this("no name"); } Person(String name) { this.name = name;

9 toppings is a(n) instance (non-static) variable class Pizza {
private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } instance (non-static) variable class (static) variable parameter local variable A is true, B, C and D are false

10 getToppings() is a(n) method class Pizza { getter
accessor provides read-only access to toppings field class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } All are true.

11 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? best answer: A, yes - the default, no argument constructor.

12 this class Picture { private boolean hasFrame;
public Picture( boolean hasFrame) { this.hasFrame = hasFrame; }

13 Does this print true or false?
class Person { private boolean something = false; boolean getThing(boolean something) { return this.something; } public static void main(String []args) { Person p = new Person(); System.out.println( p.getThing( true)); true false error/other try it and see.

14 Circle Class Design a Circle Class Field: radius
Constructor: radius is the argument Methods: getArea(), getCircumference(), toString() Recall: Area = π * radius * radius; Circumference = π × diameter Draw a UML Class diagram Create TestCircle Class Create circles with radius 3.5 and 34.1 Print out area, circumference, and radius

15 Rectangle Design a Rectangle class
Fields: width & height as double with default of 1.0 and private Constructors: no-arg constructor & a constructor with specified width and height, public Methods: getArea() and getPerimeter(), public Draw a UML diagram for the class then implement the class. Write a TestRectangle program that: Creates 2 rectangles (4 by 10) and (3.5 by 25.4) Display width, height, area and perimeter

16 UML Diagrams Examples of Diagrams: http://agilemodeling.com
Simple Drawing Tool:

17 Use Case How external users (actors) interact with the system.

18 More Classes derived classes, overriding member methods, the Object class, polymorphism, ArrayLists of Objects, is-a vs has-a relationships.

19 Visibility Modifiers For members of a class: public private protected
<package> Demo

20 Can methodA call methodB?
//classes in different files in same package class A { public void methodA() { B b = new B(); b.methodB(); } class B { void methodB() { yes no depends error try it and see.

21 Can a method outside the package call methodA()?
//classes in different files in same package class A { public void methodA() { B b = new B(); b.methodB(); } class B { void methodB() { yes no depends error try it and see

22 The visibility modifier private is
appropriate should be public doesn't allow a user of this class to change the field (attribute) directly allows only methods within this class to change the field. class Pizza { private String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } A, C & D are true B is false

23 Will the default constructor be provided in this case?
class Cup { private String contents; Cup(String contents) { this.contents = contents; } yes no compile time error runtime error try it and see.

24 Which are true? class Light { } // in some method
error - no constructor Object classes' toString() will be called an instance of Light has nothing in it error class Light { } // in some method Light aLight = new Light(); System.out.println( aLight); best answer: B: Object classes' toString() will be called

25 How many Bug instances in list?
2 2 copies of reference to 1 bug none, error, no list 3 ArrayList<Bug> list; list = new ArrayList<Bug>(); list.add( new Bug()); Bug aBug = new Bug(); list.add( aBug); list.add( 0, aBug); import java.util.ArrayList; class Bug { private static int count = 0; private final int id; Bug() { id = ++count; } public String toString() { return "Bug:" + id; public class ClassNameHere { public static void main(String[] args) { ArrayList<Bug> list = new ArrayList<>(); list.add( new Bug()); Bug aBug = new Bug(); list.add( aBug); list.add( 0, aBug); System.out.println( list);

26 class (static) vs instance (non-static)
fields methods

27 What will print out? 1 can't access a private field outside the class
1 can't access a private field outside the class error class Employee { private static int employeeCount = 0; private final int id; Employee() { this.id = ++employeeCount; } public static void main(String []args) { Employee anEmployee = new Employee(); System.out.println( anEmployee.id); try it and see

28 What is the value of num? num: 10 Stuff.num: 6 Stuff.num: 10
class Stuff { final static int MAX_VALUE = 10; //allowed BP2 static int num = 6; //NOT allowed in BP2 static void change( int n) { num = n + 1; } public static void main( String [] args) { int num = MAX_VALUE; change( num); System.out.println("num:" + num); System.out.println("Stuff.num:" + Stuff.num); num: 10 Stuff.num: 6 Stuff.num: 10 Stuff.num: 11 error try it.

29 Class Diagram Shows structure of a designed system at level of classes. Independent of time.

30 Animals in Zoo

31 Object Diagram A snapshot of a system at a point in time.
Instances of classes with specific attribute values.

32 Bike Design a bike class. Instance Fields: numWheels, Color, unique id
Class Field: numBikesCreated, used to assign unique id’s to each bike. Constructor: numWheels and Color, automatically sets the unique identifier. Instance Methods: Number of Wheels and id can be accessed but not changed. Color can be changed. Add a toString() method to return all instance field values in String form. Class Method: returns the number of bikes created. Draw the UML diagram and then write the code. Create a BikeShop class that creates 10 bikes and stores in an array. Print out each bike’s number of wheels, color and id using the toString method.


Download ppt "CS 200 - Week 13 Jim Williams, PhD."

Similar presentations


Ads by Google