interfaces: a quick review - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

interfaces: a quick review

Description:

creates a new SkiJumper with no //training or jumps. private String myName; ... sOne = new String('one'); String sTwo = new String('two'); sTwo = sOne; sOne ' ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 39
Provided by: paulc1
Category:

less

Transcript and Presenter's Notes

Title: interfaces: a quick review


1
interfaces a quick review
  • public interface Athlete
  • void train(double dHours)
  • //increases the number of hours
  • //trained by dHours
  • public interface Flier
  • void fly() //increments the number
  • //of jumps
  • class SkiJumper
  • public SkiJumper(String sName)
  • //creates a new SkiJumper with no
  • //training or jumps
  • private String myName

2
interfaces a quick review
  • public interface Athlete
  • void train(double dHours)
  • public interface Flier
  • void fly()
  • class SkiJumper implements Athlete, Flier
  • public SkiJumper(String sName)
  • //creates a new SkiJumper with no
  • //training or jumps
  • public void train(double dHours)
  • public void fly()
  • private String myName
  • private double myHoursTrained

3
interfaces a quick review
  • class SkiJumper implements Athlete, Flier
  • public SkiJumper(String sName)
  • //creates a new SkiJumper with no
  • //training or jumps
  • public void train(double dHours)
  • myHoursTrained dHours
  • public void fly()
  • myJumps
  • private String myName
  • private double myHoursTrained
  • private int myJumps

4
interfaces a quick review
  • class SkiJumper implements Athlete, Flier
  • public SkiJumper(String sName)
  • //creates a new SkiJumper with no
  • //training or jumps
  • myName sName
  • myHoursTrained 0
  • myJumps 0
  • public void train(double dHours)
  • myHoursTrained dHours
  • public void fly()
  • myJumps
  • private String myName

5
interfaces a quick review
  • class SkiJumper implements Athlete, Flier
  • public SkiJumper(String sName)
  • myName sName
  • myHoursTrained 0
  • myJumps 0
  • public void train(double dHours)
  • myHoursTrained dHours
  • public void fly()myJumps
  • ?? getHoursTrained() ??
  • ?? getJumps() ??
  • private String myName
  • private double myHoursTrained
  • private int myJumps

6
interfaces a quick review
  • class SkiJumper implements Athlete, Flier
  • public SkiJumper(String sName)
  • myName sName
  • myHoursTrained 0
  • myJumps 0
  • public void train(double dHours)
  • myHoursTrained dHours
  • public void fly()myJumps
  • public double getHoursTrained()
  • return myHoursTrained
  • public int getJumps()
  • return myJumps

7
Applications vs. Applets
  • An applet is a program that runs within a browser
  • An application is a program that is installed on
    a single computer
  • We can make applications in Java pretty much like
    with did in C

8
Applications vs. Applets
  • public class AppDemo
  • public static void main(String args)
  • System.out.println("Hello World")

9
Applications vs. Applets
  • Q What is the String args for?
  • A You don't really care, but here's an example
  • public class AppDemo
  • public static void main(String args)
  • for(int nI 0 nI lt args.length nI)
  • System.out.println("Argument " nI
  • " " argsnI)

10
Applications vs. Applets
  • public class AppDemo
  • public static void main(String args)
  • for(int nI 0 nI lt args.length nI)
  • System.out.println("Argument " nI
  • " " argsnI)

11
Applications vs. Applets
  • To run our Java application, we need to Build it
    first
  • Then we go to the command prompt and select the
    correct folder
  • Typing java lets windows know what's coming up is
    a java application
  • Then comes the name of the program AppDemo
  • And then the arguments
  • Testing out this silly program
  • C\folder namegtjava AppDemo Testing out
  • this silly program

12
Applications vs. Applets
  • You need to know that you can make both kinds of
    programs in Java
  • But the AP exam doesn't test Applications or
    Applets
  • On the AP you will only be asked to write classes
    and methods
  • These classes and methods could be part of an
    application or an applet

13
Handles
  • You know you don't get a new object in Java until
    you ask for one
  • Thingy someThing
  • someThing new Thingy()
  • After the declaration Thingy someThing you have
    a handle to a Thingy called someThing
  • Handles are also called "References" or "Pointers"

14
Only Objects have Handles
  • In Java, there are two types of data
  • Objects
  • Primitives
  • Objects are made with classes
  • Primitives are simple pieces of data like int,
    double, char and boolean
  • int nNum 3
  • //creates an int

15
Handles
  • You can think of a handle as the name of an
    object
  • String sString
  • Does NOT make a new String, it only makes a
    handle to a String
  • sString can also be called a reference variable
  • sString new String("hello")
  • Now the handle is "hooked up" to a new "mailbox"

16
Handles
  • String sString

sString
17
Handles
  • String sString
  • sString new String("hello")

"hello"
sString
18
vs. .equals
  • String sString1 new String("hello")
  • String sString2 new String("hello")
  • System.out.println(sString1sString2)
  • System.out.println(sString1.equals(sString2))
  • /Sample Output
  • false
  • true /

19
vs. .equals
  • String sString1 new String("hello")
  • String sString2 new String("hello")
  • System.out.println(sString1sString2)
  • System.out.println(sString1.equals(sString2))

"hello"
sString1
"hello"
sString2
20
vs. .equals
  • String sString1 new String("hello")
  • String sString2 sString1
  • System.out.println(sString1sString2)
  • System.out.println(sString1.equals(sString2))
  • /Sample Output
  • true
  • true /

21
vs. .equals
  • String sString1 new String("hello")
  • String sString2 sString1
  • System.out.println(sString1sString2)
  • System.out.println(sString1.equals(sString2))

"hello"
sString1
sString2
22
vs. .equals
  • Rule always use .equals with objects unless you
    are positive that you need

23
garbage
  • If no variables hold a reference to an object,
    there is no way to find it. It becomes "garbage".
  • This can also be called an "orphan"
  • String sOne new String("one")
  • String sTwo new String("two")
  • sTwo sOne

"one"
sOne
"two"
sTwo
24
alias
  • Two references to the same object are called an
    "alias"
  • Thingy A new Thingy(5)
  • Thingy B A
  • B.set(3)
  • System.out.println(A.get())
  • class Thingy
  • private int myNum
  • public void set(int nNum)
  • myNum nNum
  • public int get()return myNum
  • public Thingy(int nNum)
  • myNum nNum

25
alias
  • Thingy A new Thingy(5)
  • Thingy B A

5
A
B
26
alias
  • Thingy A new Thingy(5)
  • Thingy B A
  • B.set(3)
  • System.out.println(A.get())

3
A
B
27
null
  • Sometimes you don't want a handle to be "hooked
    up" to anything
  • In that case it should be set to null
  • Careful! Using a null reference will give an
    exception
  • String sString null
  • System.out.println(sString.length())
  • //NullPointerException

28
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first second)
  • //displays false
  • class DoWicky
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

29
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first.equals(second))
  • //we want this to display true
  • class DoWicky
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

30
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first.equals(second))
  • class DoWicky
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

DoWicky
DoWicky
31
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first.equals(second))
  • class DoWicky
  • public boolean equals(DoWicky aD)
  • ??
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

32
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first.equals(second))
  • class DoWicky
  • public boolean equals(DoWicky aD)
  • myNum ?? aD.myNum
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

33
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first.equals(second))
  • class DoWicky
  • public boolean equals(DoWicky aD)
  • myNum ?? aD.myNum
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

34
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first.equals(second))
  • class DoWicky
  • public boolean equals(DoWicky aD)
  • return myNum aD.myNum
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

35
Writing an equals method
  • DoWicky first new DoWicky(7)
  • DoWicky second new DoWicky(7)
  • System.out.println
  • (first.equals(second))
  • class DoWicky
  • public boolean equals(DoWicky aD)
  • return getNum() aD.getNum()
  • public DoWicky(double dNum)myNum dNum
  • public double getNum()return myNum
  • public void setNum(double dNum)myNum dNum
  • private double myNum

36
Practice Quiz Question Complete the Thingy class
so that1. it has an equals method 2. it
implements Comparable
  • interface Comparable
  • int compareTo (Object other)
  • /returns -1 is this object is less than
  • other, 0 if this object is equal to other and
  • 1 if this object is greater than other /
  • class Thingy
  • private int myNum
  • public void set(int nNum)
  • myNum nNum
  • public int get()return myNum
  • public Thingy(int nNum)
  • myNum nNum
  • public boolean equals(Thingy other)
  • //your code here

37
Practice Quiz Question Complete the Thingy class
so that1. it has an equals method 2. it
implements Comparable
  • Thingy first new Thingy(3)
  • Thingy second new Thingy(4)
  • Thingy third new Thingy(4)
  • System.out.println(first.equals(second))
  • System.out.println(second.equals(third))
  • System.out.println(first.compareTo(second))
  • System.out.println(second.compareTo(first))
  • System.out.println(second.compareTo(third))
  • / Should display
  • false
  • true
  • -1
  • 1
  • 0 /

38
Practice Quiz Question Complete the Thingy class
so that1. it has an equals method 2. it
implements Comparable
  • class Thingy implements Comparable
  • private int myNum
  • public void set(int nNum)
  • myNum nNum
  • public int get()return myNum
  • public Thingy(int nNum)
  • myNum nNum
  • public boolean equals(Thingy other)
  • return myNum other.myNum
  • public int compareTo (Object other)
  • int nOther ((Thingy)other).myNum
  • if(myNum nOther)
  • return 0
  • else if(myNum lt nOther)
  • return -1
Write a Comment
User Comments (0)
About PowerShow.com