CS 200 More Classes Jim Williams, PhD
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
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.
Accessor/Getter Methods Methods to retrieve information Provide read access to information within the instance/object. String getName() { } String name() { } boolean isHappy() { }
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) { }
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.
this implicit parameter class Car { String owner; void setOwner( String owner) { this.owner = owner; } in main: Car mine = new Car(); mine.setOwner("me");
Is an instance of Car mutable? public class Car { private String model; public Car(String model) { this.model = model; } public String toString() { return model;
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
Demo Creating a class with instance variables Instantiating a class Writing and calling a constructor Writing and calling a getter/accessor
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());
Visibility Modifiers For members of a class: public private protected <package> Package Demo
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.
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
More Classes The Object class Derived classes Overriding methods Polymorphism ArrayLists of Objects is-a vs has-a
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 {
Derived Classes Since all classes extend from Object they inherit: public String toString() public boolean equals(Object obj)
Overriding Replacing inherited methods, may call super class method class Picture { @Override //compiler directive public String toString() { return "Picture: " +super.toString(); }
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 ???; }
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?
ArrayLists of Objects Demo polymorphism using ArrayList
is-a vs has-a Relationships is-a: inheritance class Dog extends Animal { } has-a: composition class Car { Engine engine; Wheel [] wheels; }
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
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
Analysis and Design Analysis: Understanding the Problem Design: Preparing a Solution Diagramming can be useful for both.
UML Diagrams Goal in CS 200 is to recognize: Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow
UML Diagrams: References Examples of Diagrams: https://www.uml-diagrams.org/ Simple Drawing Tool: http://www.umlet.com/umletino/umletino.html
Use Case Behavior diagram How external users (actors) interact with the system. https://www.uml-diagrams.org/use-case-diagrams.html
Class Diagram Shows structure of a system with features, constraints and relationships. Independent of time. https://www.uml-diagrams.org/class-diagrams-overview.html
Object Diagram A snapshot of a system at a point in time. Instances of classes with specific attribute values. https://www.uml-diagrams.org/class-diagrams-overview.html
Activity Diagram (Control Flow) Behavior diagram Shows flow of control emphasizing sequence and conditions https://www.uml-diagrams.org/activity-diagrams.html
What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow
What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow
What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow
What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow