Lec04-10 - UMass CS !EdLab
Download
Report
Transcript Lec04-10 - UMass CS !EdLab
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
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)
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
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
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);
}
}
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);
}
}
Terminology:
Book is the base class, Dictionary is the derived class
Book is the super class, Dictionary is the sub class
Dictionary specializes Book
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();
}
}
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);
}
}
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);
}
}
public class Words2
{
public static void main (String[] args)
{
Dictionary2 webster = new Dictionary2 (1500, 52500);
webster.pageMessage();
webster.definitionMessage();
}
}
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;
}
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);
}
}
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);
}
}
œ´œ ----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]
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);
}
}
}
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()); }
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()); }
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]