Even even more on being classy - PowerPoint PPT Presentation

About This Presentation
Title:

Even even more on being classy

Description:

Turn on. Turn off. Get temperature and fan setting. Set temperature and fan setting mutation ... Turn on. Turn off. Get temperature and fan setting accessing ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 43
Provided by: csVir
Category:
Tags: being | classy | even | more | turn

less

Transcript and Presenter's Notes

Title: Even even more on being classy


1
Even even more on being classy
  • Aaron Bloomfield
  • CS 101-E
  • Chapter 4

2
Consider this sequence of events
3
What happened?
  • Java didnt repaint the rectangles when
    necessary
  • Java only painted the rectangle once
  • You can tell Java to repaint it whenever
    necessary
  • This is beyond the scope of this class, though!

4
Seeing double
  • import java.awt.
  • public class SeeingDouble
  • public static void main(String args)
  • ColoredRectangle r new ColoredRectangle()
  • System.out.println("Enter when ready")
  • Scanner stdin new Scanner (System.in)
  • stdin.nextLine()
  • r.paint()
  • r.setY(50)
  • r.setColor(Color.RED)
  • r.paint()

5
Seeing double
  • When paint() was called, the previous rectangle
    was not erased
  • This is a simpler way of implementing this
  • Perhaps clear and repaint everything when a
    rectangle paint() is called

6
Code from last class
  • // Purpose Create two windows containing colored
    rectangles.
  • import java.util.
  • public class BoxFun
  • //main() application entry point
  • public static void main (String args)
  • ColoredRectangle r1 new ColoredRectangle()
  • ColoredRectangle r2 new ColoredRectangle()
  • System.out.println("Enter when ready")
  • Scanner stdin new Scanner (System.in)
  • stdin.nextLine()
  • r1.paint() // draw the window associated with
    r1
  • r2.paint() // draw the window associated with
    r2

7
public class ColoredRectangle // instance
variables for holding object attributes private
int width private int x private int
height private int y private JFrame
window private Color color //
ColoredRectangle() default constructor public
ColoredRectangle() color Color.BLUE width
40 x 80 height 20 y
90 window new JFrame("Box Fun") window.set
Size(200, 200) window.setVisible(true) //
paint() display the rectangle in its
window public void paint() Graphics g
window.getGraphics() g.setColor(color) g.fil
lRect(x, y, width, height)
8
ColoredRectangle r new ColoredRectangle()
r
public class ColoredRectangle private int
width private int x, y private int
height private int y private JFrame
window private Color color
public void paint() Graphics g
window.getGraphics() g.setColor(color)
g.fillRect (x, y, width, height)
public ColoredRectangle() color
Color.BLUE width 40 height 20 y
90 x 80 window new JFrame ("Box
Fun") window.setSize (200, 200)
window.setVisible (true)
g
9
The Vector class
  • In java.util
  • Must put import java.util. in the java file
  • Probably the most useful class in the library (in
    my opinion)
  • A Vector is a collection of things (objects)
  • It has nothing to do with the geometric vector

10
Vector methods
  • Constructor Vector()
  • Adding objects add (Object o)
  • Removing objects remove (int which)
  • Number of elements size()
  • Element access elementAt()
  • Removing all elements clear()

11
Vector code example
  • Vector v new Vector()
  • System.out.println (v.size() " " v)
  • v.add ("first")
  • System.out.println (v.size() " " v)
  • v.add ("second")
  • v.add ("third")
  • System.out.println (v.size() " " v)
  • String s (String) v.elementAt (2)
  • System.out.println (s)
  • String t (String) v.elementAt (3)
  • System.out.println (t)
  • v.remove (1)
  • System.out.println (v.size() " " v)

0 1 first 3 first, second,
third third (Exception) 2 first,
third 0
12
What we wish computers could do
13
The usefulness of Vectors
  • You can any object to a Vector
  • Strings, ColoredRectanges, JFrames, etc.
  • They are not the most efficient for some tasks
  • Searching, in particular

14
About that exception
  • The exact exception was
  • Exception in thread "main" java.lang.ArrayIndexOut
    OfBoundsException 3 gt 3
  • at java.util.Vector.elementAt(Vector.java
    431)
  • at VectorTest.main(VectorTest.java15)

Where the problem occured
15
A Vector of ints
  • Consider the following code
  • Vector v new Vector()
  • v.add (1)
  • Causes a compile-time error
  • Most of the time - see disclaimer later
  • C\Documents and Settings\Aaron\My
    Documents\JCreator\VectorTest\VectorTest.java7
    cannot resolve symbol
  • symbol method add (int)
  • location class java.util.Vector
  • v.add (1)

16
What happened?
  • The Vector add() method
  • boolean add(Object o)
  • Primitive types are not objects!
  • Solution use wrapper classes!

17
More on wrapper classes
  • A wrapper class allows a primitive type to act as
    an object
  • Each primitive type has a wrapper class
  • Boolean
  • Character
  • Byte
  • Short
  • Note that char and int dont have the exact same
    name as their wrapper classes
  • Integer
  • Long
  • Float
  • Double

18
Vector code example
  • Vector v new Vector()
  • System.out.println (v.size() " " v)
  • v.add (new Integer(1))
  • System.out.println (v.size() " " v)
  • v.add (new Integer(2))
  • v.add (new Integer(3))
  • System.out.println (v.size() " " v)
  • Integer s (Integer) v.elementAt (2)
  • System.out.println (s)
  • Integer t (Integer) v.elementAt (3)
  • System.out.println (t)
  • v.remove (1)
  • System.out.println (v.size() " " v)

0 1 1 3 1, 2, 3 3 (Exception)
2 1, 3 0
19
Even more on wrapper classes
  • They have useful class (i.e. static) variables
  • Integer.MAX_VALUE
  • Double.MIN_VALUE
  • They have useful methods
  • String s 3.14159
  • double d Double.parseDouble (s)

20
A disclaimer
  • Java 1.5 (which we are not using) has a new
    feature called autoboxing/unboxing
  • This will automatically convert primitive types
    to their wrapper classes (and back)

21
An optical illusion
22
Design an air conditioner representation
  • Context
  • Repair person
  • Sales person
  • Consumer
  • Behaviors
  • Attributes

- Consumer
23
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on
  • Turn off
  • Get temperature and fan setting
  • Set temperature and fan setting
  • Build construction
  • Debug
  • Attributes

24
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on
  • Turn off
  • Get temperature and fan setting
  • Set temperature and fan setting
  • Build construction
  • Debug to stringing
  • Attributes

25
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on
  • Turn off
  • Get temperature and fan setting
  • Set temperature and fan setting mutation
  • Build construction
  • Debug to stringing
  • Attributes

26
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on
  • Turn off
  • Get temperature and fan setting accessing
  • Set temperature and fan setting mutation
  • Build construction
  • Debug to stringing
  • Attributes

27
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on mutation
  • Turn off mutation
  • Get temperature and fan setting accessing
  • Set temperature and fan setting mutation
  • Build construction
  • Debug to stringing
  • Attributes

28
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on mutation
  • Turn off mutation
  • Get temperature and fan setting accessing
  • Set temperature and fan setting mutation
  • Build construction
  • Debug to stringing
  • Attributes

29
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on
  • Turn off
  • Get temperature and fan setting
  • Set temperature and fan setting
  • Build -- construction
  • Debug
  • Attributes
  • Power setting
  • Fan setting
  • Temperature setting

30
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on
  • Turn off
  • Get temperature and fan setting
  • Set temperature and fan setting
  • Build -- construction
  • Debug
  • Attributes
  • Power setting
  • Fan setting
  • Temperature setting integer

31
Design an air conditioner representation
  • Context - Consumer
  • Behaviors
  • Turn on
  • Turn off
  • Get temperature and fan setting
  • Set temperature and fan setting
  • Build -- construction
  • Debug
  • Attributes
  • Power setting binary
  • Fan setting binary
  • Temperature setting integer

32
Ugh
33
Design an air conditioner representation
  • // Represent an air conditioner from consumer
    view point
  • public class AirConditioner
  • // instance variables
  • // constructors
  • // methods
  • Source AirConditioner.java

34
Static variables and constants
  • // shared resource for all AirConditioner objects
  • static public final int OFF 0
  • Static public final int ON 1
  • static public final int LOW 0
  • Static public final int HIGH 1
  • static public final int DEFAULT_TEMP 72
  • Every object in the class has access to the same
    static variables and constants
  • A change to a static variable is visible to all
    of the objects in the class
  • Examples StaticDemo.java and DemoStatic.java

35
Instance variables
  • // individual object attributes
  • int powerSetting
  • int fanSetting
  • int temperatureSetting
  • Instance variables are always initialized as soon
    the object comes into existence
  • If no value is specified
  • 0 used for numeric variables
  • false used for logical variables
  • null used for object variables
  • Examples InitializeDemo.java

36
Constructors
  • // AirConditioner() default constructor
  • public AirConditioner()
  • this.powerSetting AirConditioner.OFF
  • this.fanSetting AirConditioner.LOW
  • this.temperatureSetting AirConditioner.DEFAULT_
    TEMP
  • // AirConditioner() specific constructor
  • public AirConditioner(int myPower, int myFan, int
    myTemp)
  • this.powerSetting myPower
  • this.fanSetting myFan
  • this.temperatureSetting myTemp
  • Example AirConditionerConstruction.java

37
Simple mutators
  • // turnOn() set the power setting to on
  • public void turnOn()
  • this.powerSetting AirConditioner.ON
  • // turnOff() set the power setting to off
  • public void turnOff()
  • this.powerSetting AirConditioner.OFF
  • Example TurnDemo.java

38
Simple accessors
  • // getPowerStatus() report the power setting
  • public int getPowerStatus()
  • return this.powerSetting
  • // getFanStatus() report the fan setting
  • public int getFanStatus()
  • return this.fanSetting
  • // getTemperatureStatus() report the temperature
    setting
  • public int getTemperatureStatus ()
  • return this.temperatureSetting
  • Example AirConditionerAccessors.java

39
Parametric mutators
  • // setPower() set the power setting as indicated
  • public void setPower(int desiredSetting)
  • this.powerSetting desiredSetting
  • // setFan() set the fan setting as indicated
  • public void setFan(int desiredSetting)
  • this.fanSetting desiredSetting
  • // setTemperature() set the temperature setting
    as indicated
  • public void setTemperature(int desiredSetting)
  • this.temperatureSetting desiredSetting
  • Example AirConditionerSetMutation.java

40
Facilitator toString()
  • // toString() produce a String representation of
    the object
  • public String toString()
  • String result " power " this.powerSetting
  • ", fan " this.fanSetting
  • ", temperature " this.temperatureSettin
    g
  • " "
  • return result

41
Sneak peek facilitator toString()
  • public String toString()
  • String result " power "
  • if ( this.powerSetting AirConditioner.OFF )
  • result result "OFF"
  • else
  • result result "ON "
  • result result ", fan "
  • if ( this.fanSetting AirConditioner.LOW )
  • result result "LOW "
  • else
  • result result "HIGH"
  • result result ", temperature "
    this.temperatureSetting " "
  • return result

42
What computers were made for
  • NASAs WorldWind
  • See http//learn.arc.nasa.gov/worldwind/
Write a Comment
User Comments (0)
About PowerShow.com