Top Banner
CS 302 Week 10 Jim Williams
21

CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Sep 22, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

CS 302 Week 10Jim Williams

Page 2: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

● P2 Final Milestone Due Thursday● Midterm 2 next Thursday

○ Exam conflict arrangements emailed this week.○ See Piazza for Review Questions

● Review & More Object Concepts, OO Design

This Week

Page 3: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Class vs Instance

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

Page 4: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

How are these terms related?

field, attribute, member variableclass, related data and methods, templateobject, instanceinstance variable, non-static variableclass field, static field, static variable

Page 5: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

this

class Picture {private boolean hasFrame;public Picture( boolean hasFrame) {

this.hasFrame = hasFrame; }}

Page 6: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

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

Page 7: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

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

0

1

other

error

Page 8: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Visibility Modifiers

For members of a class:● public● private● protected● <package>Demo

Page 9: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Can methodA call methodB?//classes in different files in same packageclass A { public void methodA() {

B b = new B();b.methodB();

}}class B { void methodB() { }}

yes

no

depends

error

Page 10: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Can a method outside the package call methodA()?//classes in different files in same packageclass A { public void methodA() {

B b = new B();b.methodB();

}}class B { void methodB() { }}

yes

no

depends

error

Page 11: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

What will print out?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);

}}}

0

1

can't access a private field outside the class

error

Page 12: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Default Constructor class Person {

private String name;}

Can we create an instance?Person p = new Person();

Page 13: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Constructor Overloadingclass Person {

private String name; Person() {

this("no name");}Person(String name) {

this.name = name; }}

Page 14: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Constructors

If there is no constructor in a class is a constructor automatically provided by the compiler?

yes - the default, no argument constructor

yes - a really good one.

no

error

Page 15: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

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

Page 16: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Will toString return results of Graphic toString()?

class Bird { private Graphic graphic; Bird(String name) { } public String toString() { return this.graphic.toString(); }}

yes

no

NullPointerException

other

Page 17: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Which are true?

class Light {}// in some methodLight aLight = new Light();System.out.println( aLight);

error - no constructor

Object classes' toString() will be called

an instance of Light has nothing in it

error

Page 18: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

How many Bug instances in list?

ArrayList<Bug> list;list = new ArrayList<Bug>();list.add( new Bug());Bug aBug = new Bug();list.add( aBug);list.add( 0, aBug);

2

2 copies of reference to 1 bug

none, error, no list

3

Page 19: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Circle Class

Design a Circle Class● Field: radius● Constructor: radius is the argument● Methods: getArea(), getCircumference(), toString()● Recall: Area = π * radius * radius; Circumference = π ×

diameterDraw a UML Class diagramCreate TestCircle Class● Create circles with radius 3.5 and 34.1● Print out area, circumference, and radius

Page 20: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

Rectangle

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

2. Draw a UML diagram for the class then implement the class.3. Write a TestRectangle program that:

○ Creates 2 rectangles (4 by 10) and (3.5 by 25.4)○ Display width, height, area and perimeter

Page 21: CS 302 Week 10P2 Final Milestone Due Thursday Midterm 2 next Thursday Exam conflict arrangements emailed this week. See Piazza for Review Questions

BikeDesign a bike class.

Instance Fields: numWheels, Color, unique idClass 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.