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 Classes, Javadoc BP2: M3 due Thursday
Submit to zyBooks M2 tests Follow the specification Do any extensions or improvements, after you turn in. Lecture: More Instantiable Class Basics

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

4 Accessor/Getter Methods
Methods to retrieve information Provide read access to information within the instance/object. String getName() { } String name() { } boolean isHappy() { }

5 Mutator/Setter Methods
Allow user to change data/state of an object. Provide write access to data. void setName( String newName) { } void name( String name) { } void happy( boolean happy) { }

6 this implicit parameter
Available within instance methods. Refers to the instance used to call the method. String str = "hello". System.out.println( str.charAt(0)); Within the charAt instance method, this keyword refers to the reference in str.

7 this implicit parameter
class Car { String owner; void setOwner( String owner) { this.owner = owner; } in main: Car mine = new Car(); mine.setOwner("me");

8 Is an instance of Car mutable?
public class Car { private String model; public Car(String model) { this.model = model; } public String toString() { return model;

9 Encapsulation Enclosing something
Internal/Developer vs External/Users access Use visibility and getters and setters to allow appropriate user access Data should be Private Provide getters/accessors for read access Provide setters/mutators for write access Provide constructors to initialize on creation

10 Demo Creating a class with instance variables Instantiating a class
Writing and calling a constructor Writing and calling a getter/accessor

11 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 Shapes program that: Creates 2 rectangles (4 by 10) and (3.5 by 25.4) Display width, height, area and perimeter public class Rectangle { //implicitly extends Object //instance variables aka fields and attributes private double height; private double width; private static int count = 0; //class variable aka class fields and attributes public Rectangle() { //no-arg constructor this( 1.0, 1.0); //call the 2-arg constructor } public Rectangle( double size) { //constructor this( size, size); //call the 2-arg constructor public Rectangle(double height, double width) { //constructor this.height = height; this.width = width; count++; //class (static) variables can be accessed from instance methods and constructors public double getArea() { //derived field return height * width; public double getPerimeter() { //another derived field return height * 2 + width * 2; public double getHeight() { return this.height; //setter/mutator for height that validates any changes to height public void setHeight(double newHeight) { if ( newHeight > 0.0) { this.height = newHeight; //accessor for class field public static int getCount() { //instance fields cannot be accessed from class (static) methods //as which instance isn't defined - no implicit this parameter return count; //overriding the same toString() method inherited from Object public String toString() { return "Rectangle height=" + height + " width=" + width; import java.util.ArrayList; public class Shapes { public static void main(String[] args) { //user of the Rectangle class //since this Shapes class is in the same package (default package) //as the Rectangle class then all non-private members of Rectangle //would be accessible. ArrayList<Rectangle> list = new ArrayList<>(); list.add( new Rectangle()); list.add( new Rectangle( 4.0, 10.0)); System.out.println( "count: " + Rectangle.getCount()); for ( Rectangle shape : list) { System.out.println( shape.getArea());

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

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

14 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

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

16 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 {

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

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

19 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 ???; }

20 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?

21 ArrayLists of Objects Demo polymorphism using ArrayList

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

23 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

24 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

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

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

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

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

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

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

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

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

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

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

35 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