cs 121 – intro to programming:java - lecture 10 announcements two owl assignments up, due 17th,...
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/15.jpg)
œ´œ ----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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/17.jpg)
![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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022072011/56649e375503460f94b271e9/html5/thumbnails/20.jpg)
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]