CS 200 - Week 13 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
UML Basics & Access Modifier
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Peyman Dodangeh Sharif University of Technology Fall 2014.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Topics Instance variables, set and get methods Encapsulation
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
1 More About Derived Classes and Inheritance Chapter 9.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Introduction to Object-oriented Programming
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Classes and Objects Introduced
Inheritance ITI1121 Nour El Kadri.
Chapter 11 Inheritance and Polymorphism
Advanced Programming in Java
CS Week 14 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
CS 200 Arrays, Loops and Methods
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CS 200 More Classes Jim Williams, PhD.
CS 302 Week 8 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Implementing Non-Static Features
CS 302 Week 9 Jim Williams.
Today’s topics UML Diagramming review of terms
Inheritance Inheritance is a fundamental Object Oriented concept
CS 200 Additional Topics and Review
Basics of OOP A class is the blueprint of an object.
CS 200 Additional Topics and Review
CS 200 Objects and ArrayList
Object-Oriented Programming
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism
CS 200 Creating Classes Jim Williams, PhD.
Chapter 11 Inheritance and Polymorphism Part 2
CS 200 More Classes Jim Williams, PhD.
OO Programming Concepts
CS 200 Creating Classes Jim Williams, PhD.
Barb Ericson Georgia Institute of Technology Oct 2005
CS 200 Objects and ArrayList
Chapter 11 Inheritance and Encapsulation and Polymorphism
Lecture 8 Object Oriented Programming (OOP)
CS 200 Additional Topics and Review
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

CS 200 - Week 13 Jim Williams, PhD

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

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.

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

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

Constructor Called when instance created Used to initialize instance fields

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;

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

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.

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.

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

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.

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

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

UML Diagrams Examples of Diagrams: http://agilemodeling.com https://www.uml-diagrams.org/ Simple Drawing Tool: http://www.umlet.com/umletino/umletino.html

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

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

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() { 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

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

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.

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

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);

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

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

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.

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

Animals in Zoo

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

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.