cs 121 – intro to programming:java - lecture 10 announcements two owl assignments up, due 17th,...

20
CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment up soon Next Monday is a Thursday - so discussion sections will meet then. Preregistration advice: for CS majors, premajors: you can get help/RAC code at mtg in CS BLDG 151 today, Wednesday, both at 4 pm

Upload: meagan-moore

Post on 27-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

CS 121 – Intro to Programming:Java - Lecture 10Announcements

Two Owl assignments up, due 17th, 22ndAnother up today, due 11/30Next programming assignment up soon

Next Monday is a Thursday - so discussion sections will meet then.

Preregistration advice: for CS majors, premajors: you can get help/RAC code at mtg in CS BLDG 151 today, Wednesday, both at 4 pm

Page 2: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

Recall: statement level / class level dichotomy

Today: Inheritance - we’re at the class level

If a class is a variant of another, existing class - call it A - we’d like to reuse A as much as we can for the implementation of the new class.

When the new class IS-A (an example of) the original class, we call this process Inheritance.

Sometime this extension is an obvious one:

Person -> Student (add a gpa, year at school, student id, etc)

Rectangle -> colored Rectangle (add a color)

Vehicle -> motorized vehicle (yup: add a motor)

Page 3: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

Let’s focus on a concrete example

Suppose you have a Person class, with these attributes:

String firstName

String lastName

String SSN

int age

And so forth (gender, address, city, zip, state, phone#..)

-----

Now I want to create an Employee class, that’s just like a person, only an Employee has: an EmployeeNumber and a boolean tbShot

Page 4: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

How can I do this?

Note that: I don’t want to touch the Person part of the class - it’s been debugged, etc.

What about the attributes of Person - can I see them?

What about the methods of Person - how can I use them, and indeed can I use them at all

What about the Employee constructor?

----

The big, big picture: programming is theft. A large OO program, say in Java, might use hundreds or even thousands of classes, a great many of which have been previously created, or are derived from classes that have been previously created

Page 5: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

class Book{ protected int pages = 1500;

//---------------------------------------------------------------- // Prints a message about the pages of this book. //---------------------------------------------------------------- public void pageMessage () { System.out.println ("Number of pages: " + pages); }}

Page 6: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

class Dictionary extends Book{ private int definitions = 52500;

//----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public void definitionMessage () { System.out.println ("Number of definitions: " + definitions);

System.out.println ("Definitions per page: " + definitions/pages); }}

Page 7: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

Terminology:

Book is the base class, Dictionary is the derived class

Book is the super class, Dictionary is the sub class

Dictionary specializes Book

Page 8: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

public class Words{ //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // local methods. //----------------------------------------------------------------- public static void main (String[] args) { Dictionary webster = new Dictionary ();

webster.pageMessage(); // an inherited method webster.definitionMessage(); }}

Page 9: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

class Book2{ protected int pages;

public Book2 (int numPages) { pages = numPages; }

//---------------------------------------------------------------- // Prints a message about the pages of this book. //---------------------------------------------------------------- public void pageMessage () { System.out.println ("Number of pages: " + pages); }}

Page 10: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

class Dictionary2 extends Book2{ private int definitions;

public Dictionary2 (int numPages, int numDefinitions) { super (numPages);

definitions = numDefinitions; }public void definitionMessage () { System.out.println ("Number of definitions: " + definitions);

System.out.println ("Definitions per page: " + definitions/pages); }}

Page 11: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

public class Words2{public static void main (String[] args) { Dictionary2 webster = new Dictionary2 (1500, 52500);

webster.pageMessage(); webster.definitionMessage(); }}

Page 12: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

import element.*; import java.awt.Color;

public class ColoredRect extends Rect{ private Color c; public ColoredRect(int x, int y, int width, int height, Color c){ super(x,y,width,height); this.c = c; } public Color getMyColor(){ return c; } public void setMyColor(Color clr){ c = clr; }

Page 13: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

public void fillOut(DrawingWindow d){ d.setForeground(c); fillOn(d); d.setForeground(Color.white); } public String toString(){ return(super.toString() + '\n' + " my color is " + c); } }

Page 14: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

import element.*; import java.awt.Color;

public class Draw3{ public static void main(String[] args) { DrawingWindow d = new DrawingWindow(); ColoredRect r = new ColoredRect(30,40,50,60,Color.red); System.out.println(r); r.fillOut(d); } }

Page 15: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

œ´œ ----jGRASP exec: java Draw3œœßœ<Rectangle: left=30 top=40 width=50 height=60>œœßœ my color is java.awt.Color[r=255,g=0,b=0]

Page 16: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

import element.*; import java.awt.Color;

public class DrawRects{ public static void main(String[] args) { DrawingWindow d = new DrawingWindow(); ColoredRect r; for(int j=0; j < 20; j++){ if (j%2 ==0) r = new ColoredRect(10*j,40,10,50,Color.red); else r = new ColoredRect(10*j,40,10,50,Color.blue); r.fillOut(d); } } }

Page 17: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment
Page 18: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

import java.util.Random;

public class Dice{ Random r; // aggregation - p 250

int[] scoreboard = new int[13]; public Dice(){ r = new Random(); initializeScoreboard(); } public void initializeScoreboard(){ for(int j = 0; j < 13; j++) scoreboard[j] = 0; } public int tossDie(){ return (1+r.nextInt(6)); } public int throwDice(){ return(tossDie() + tossDie()); }

Page 19: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

import java.util.Random;

public class Dice2 extends Random{

int[] scoreboard = new int[13]; public Dice2(){ super(); // note call to super

initializeScoreboard(); } public void initializeScoreboard(){ for(int j = 0; j < 13; j++) scoreboard[j] = 0;} public int tossDie(){ return (1+nextInt(6)); } // notice nextInt call..

public int throwDice(){ return(tossDie() + tossDie()); }

Page 20: CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment

Let’s try these:

ComicBook extends Book2 [a book with panels..]

MultiCoinFlipper extends Random [ has a number of coins]

Employee extends Person [add empnumber, tbshot]