CS 302 Week 10 Jim Williams.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
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 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 7 Objects and Classes
Topic: Classes and Objects
Chapter 15 Abstract Classes and Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 11 Inheritance and Polymorphism
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
IST311 Advanced Issues in OOP: 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.
Advanced Programming in Java
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS Week 8 Jim Williams, PhD.
Chapter 9 Inheritance and Polymorphism
CS Week 7 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CSC 113 Tutorial QUIZ I.
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.
CS 302 Week 9 Jim Williams.
CS 200 Loops Jim Williams, PhD.
Today’s topics UML Diagramming review of terms
CS 200 Additional Topics and Review
CS 200 Additional Topics and Review
CS 200 Objects and ArrayList
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.
CS 200 Objects and ArrayList
Chapter 11 Inheritance and Encapsulation and Polymorphism
CS302 Week 6 Jim Williams.
CS 200 Additional Topics and Review
Chapter 7 Objects and Classes
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

CS 302 Week 10 Jim Williams

This Week P2 Final Milestone Due Thursday Lab: UFOs Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions Lecture: Review & More Object Concepts, OO Design

Class vs Instance http://buggyandbuddy.com/wp-content/uploads/2016/08/owl-horiz..png

How are these terms related? field, attribute, member variable class, related data and methods, template object, instance instance variable, non-static variable class field, static field, static variable

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.

Does this print 0, 1, other or error? public class Person { static int count = 0; private boolean something = false; Person(boolean something) { this.something = something; count++; } System.out.println( Person.count); //in other class in package 1 other error try it and see

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

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

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;

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.

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.

Will toString return results of Graphic toString()? yes no NullPointerException other class Bird { private Graphic graphic; Bird(String name) { } public String toString() { return this.graphic.toString(); 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);

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

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.