Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 More Classes Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 200 More Classes Jim Williams, PhD."— Presentation transcript:

1 CS 200 More Classes Jim Williams, PhD

2 Week 13 Team Lab: Instantiable Class BP2 Milestone 3 Due Thursday
P7 Due next Thursday CS 300 Programming II in the future? Lecture: More Classes, UML Diagrams

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

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

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

6 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.

7 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.

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

9 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.

10 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

11 More Classes The Object class Derived classes Overriding methods
Polymorphism ArrayLists of Objects is-a vs has-a

12 Object class Base class for all other classes
Every class in Java directly or indirectly extends (inherits) from Object. class Picture { //implicitly extends Object } class Picture extends Object {

13 Derived Classes Since all classes extend from Object they inherit:
public String toString() public boolean equals(Object obj)

14 Overriding Replacing inherited methods, may call super class method
class Picture { @Override //compiler directive public String toString() { return "Picture: " +super.toString(); }

15 Equals - What does equals mean?
Depends on the implementation in the class. class Picture { public boolean equals(Object obj) { //what do equal Picture objects mean? return ???; }

16 Polymorphism (runtime)
At runtime determines the correct method to call based on the actual object. Object o = new Picture(); System.out.print( o.toString()); Is Object's or Picture's toString method called?

17 ArrayLists of Objects Demo polymorphism using ArrayList

18 is-a vs has-a Relationships
is-a: inheritance class Dog extends Animal { } has-a: composition class Car { Engine engine; Wheel [] wheels; }

19 Shapes Add Circle Class to Shapes program: Field: radius
Constructor: radius is the argument Methods: getArea(), getCircumference(), toString() Recall: Area = π * radius * radius; Circumference = π × diameter Add Circle instances/objects to Shapes Draw UML Diagrams

20 We will fill the blackboard today with diagrams.
Move closer to the front if you can't clearly see.

21 Lots of Jobs if you understand Programming
Software Developer/Programmer/Software Engineer Testing/Quality Assurance Technical Support Technical Writing Business/Systems Analyst Software Architect Product Manager Project Manager Development Manager Technical Sales Marketing

22 Analysis and Design Analysis: Understanding the Problem
Design: Preparing a Solution Diagramming can be useful for both.

23 UML Diagrams Goal in CS 200 is to recognize: Use Case Diagram
Class Diagram Object Diagram Activity Diagram/Control Flow

24 UML Diagrams: References
Examples of Diagrams: Simple Drawing Tool:

25 Use Case Behavior diagram
How external users (actors) interact with the system.

26 Class Diagram Shows structure of a system with features, constraints and relationships. Independent of time.

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

28 Activity Diagram (Control Flow)
Behavior diagram Shows flow of control emphasizing sequence and conditions

29 What Kind of Diagram is this?
Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

30 What Kind of Diagram is this?
Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

31 What Kind of Diagram is this?
Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

32 What Kind of Diagram is this?
Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow


Download ppt "CS 200 More Classes Jim Williams, PhD."

Similar presentations


Ads by Google