New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Computer Science I

by: Reuben Rohan

Computer Science I CMPU 101

Reuben Rohan

GPA 3.88

Marc Smith

Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Marc Smith
Class Notes
25 ?




Popular in Course

Popular in ComputerScienence

This 7 page Class Notes was uploaded by Reuben Rohan on Wednesday October 28, 2015. The Class Notes belongs to CMPU 101 at Vassar College taught by Marc Smith in Fall. Since its upload, it has received 12 views. For similar materials see /class/230535/cmpu-101-vassar-college in ComputerScienence at Vassar College.

Similar to CMPU 101 at

Popular in ComputerScienence


Reviews for Computer Science I


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/28/15
Plan Chapter 15 Comparable interface Interfaces amp Polymorphism Talker interface Polymorphism CMPU101 ProblemSolving and Abstraction Passmg meth ds Sing p 39ym rphism Marc mith Produce tables amp bar graphs Vassar College Spring 2007 The Two Meanings of quotInterfacequot Musical Instruments 1 The set of public methods offered by a class For CanPlayNotes interface example the classes listed in the Java API list their Xylophone strike the key with a mallet public methods Violin finger and then bow or pluck Should allow one to change the implementation Flute finger and then blow air without changing the interface Piano strike the key with a finger 2 A named set of public methods which may be offered All instruments can play notes but they do it in different by any number of classes A class may implement an interface if it offers the methods promised by that inierface39 Their similarities are expressed in an interface and their differences in the way they play notes ProngOl The Comparable Interface Compare TWO Students Requires only one method to be declared in classes that while true implement this interface int compareToObject Systemoutprintln Student s1 Student readSystemout kb Student s2 Student readSystemout kb 1 T 2 lt O 1 001 o paarreeToo oz 0 1 i 2 1 int compare s1compareTos2 39 p 1 7 2 Systemoutprintlnquotns1compareTos2 gt quot i olcompareTo 02 gt 0 I 01gt02 i compare Implementing an interface is a contract class Student implements Comparable at if class Student fails to contain int compareToObject 2 rors found Flle UsersaennyDesktopcs101lect15chaptterl5programsFrog1501Java llne 22 otflnd vmbol symbol method compareTolSmdent E a o E E a a a a Flle UsersaennyDesktopcs101lect15chaptterl5programsFrog1501Java llne 29 Error student Is not abstract and does not ovemde abstract method compareToUwaJanngject Injavalangcomparable Prog1502 int compare s1 compareTos2 Systemoutprintquotnquot s1 if compare gt 0 Systemoutprintquot is greater than quot else if compare lt 0 emoutprintquot is less than quot else System outprintquot equals quot Systemoutprintlns2 What Good Are Interfaces For one thing they allow us to use handyAPl methods Arrayssorta0used Sorts the input array a 39om position 0 to position used in the manner speci ed by the compareTo method What kind of method is sort Sort is a static method that works on an array of objects that implement the comparable Interface The sort method is overloaded in 18 different ways to sort any array of prim tive type too Student Class compareTo method public int compareTo Object 0 Student that Student 0 return thisgrade thatgrade mm 1 mm 2 result bill 93 sue 3 bill 98 sally 100 bill 98 annie 93 public boolean isLessThan Student that return thiscompareTothat lt 0 public boolean isEqualTo Student that return thiscompareTothat public boolean isGreaterThan Student that return thiscompareTothat gt 0 Prog1503 Sorting an Array In class Section public void sort Arrays sorta0 used Driver Systemoutprintlnquotnln forward order students arequot sectionprintSystemout SystemoutprintlnquotnNow sorting section s Systemoutprintlnquotnln forward order students arequot sectionprintSystemout Sorting Arrays of Primitive Datatypes inta314159 Arrayssorta0a length Classes Can Implement Talker class Dog implements Talker l public void talk PrintStream ps psprintlnquotWoofquot Every class that implements the Talker interface must define a talk method with exactly the same prototype as is defined in the interface except for local parameter name Sorting In Chapter 19 we39ll write our own sort routines You may have to wait until CMPU 102 to write your own sort methods Interface Definition Talker interface Talker public void talk PrintStream ps Interfaces can contain 1 method prototypes each of which is followed by a semicolon and 2 static final constants class Cat implements Talker l public void talk PrintStream ps psprintlnquotMeowquot class Human implements Talker private String name public Human String name thisname name public void tak PrintStream ps psprintlnquotMy name is quot name quotquot Variables Can Be Declared as Type Talker Talker t t new Dog t new Cat t new MimequotMarcelquot t new HumanquotAshtonquot Arrays Can Be Declared as Type Talker Talker a new Talker5 tO new Dog t new Cat new HumanquotBiquot t3 new MimequotMarcelquot t4 new HumanquotSuequot 2 NA class Mime implements Talker private String name public Mime String name thisname name public void talk PrintStream ps psprintln Code Output t new Dog Woof ttalk t new Cat ttalk t new MimequotMarcelquot ttalk t new HumanquotAshtonquot ttalk My name is Ashton Meow Array of CanPlayNotes CanPlayNotes quartet new CanPlayNotes4 quartetO new Violin quartet1 new Violin quartet2 new Viola quartet3 new Cello Polymorphism Talker t t new Dog t new Cat t new MimequotMarceIquot t new HumanquotAshtonquot Polymorphism forinti0ilt5i aitalkSystemout Woof Meow My name is Bill My name is Sue A Class Can Implement Multiple Interfaces class Copier implements Printer Scanner Prog1505 The class that implements multiple interfaces must rovi e implementations ofall prototype methods from e ch interface it implements Polymorphism Talker a new Talker5 a0 new Dog a1 new Cat a2 new HumanquotBiIIquot a3 new MimequotMarceIquot a4 new HumanquotSuequot Before Polymorphism forinti0ilt5i if ai instanceof Dog else if ai instanceofCat else if ai instanceofHuman else if ai instanceofMime Instead replace if else statements with a single aitak Passing One Method to Another Method method limits makeGraph Prog1506 interface Function1D u n public int valueAt inti end interface Function1 D Classes that implement Function1 D provide a method valueAtint so that we can make a table of values 39om their instances Prog1506 class CubeFunction implements Function1D public int valueAt int i return i i i u n l end class CubeFunction Prog1506 Dnver SquareFunction sq new SquareFunction makeTablesqquotSquarequot CubeFunction cu new CubeFunction makeTablecuquotCubequot Prog1506 class SquareFunction implements Function1D u n public int valueAt int i return i i u n l end class SquareFunction Prog1506 private static void makeTable Function1 D f String label SystemoutprintlnquotnTable for function quot label forinti0iltMAXi Systemoutprintfquot3d 8dnquot i fvalueAti Output Table for function Square 0 l 4 9 16 Ple O Table for function Cube 0 ibme N q Interfaces we saw when programming GUls 1 Interfaces and GUls Bar Graphs ActionListener requires public void actionPerformedActionEvent e MouseListener requires public void mousePressedMouseEvent m public void mouseClickedMouseEvent m public void mouseReleasedMouseEvent m public void mouseEnteredMouseEvent m public void mouseExitedMouseEvent m Prog1507 Bar Graph for inti0 ilt MAXROWS i Systemoutprintf quot51f 51f39 doublei fvalueAti int width intfvalueAti if Width gt MAXWIDTH Width MAXWIDTH if Width lt 0 Width 0 for intj 0 jltwidth j n lt 9 m 3 o C F 12 S R Systemout println


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Janice Dongeun University of Washington

"I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.