Monday: - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Monday:

Description:

Car constructor sets x and y locations. Includes draw(Graphics2D g2) method ... i.e., all necessary info is local to the method ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 21
Provided by: michaelc7
Category:
Tags: monday

less

Transcript and Presenter's Notes

Title: Monday:


1
  • Monday
  • First Exam

2
break and continue
  • Ways to get around the strict structures
  • i. e., not really structures anymore
  • break completely exits the structure
  • continue skips the rest of current iteration
    (while, for, or do/while structures only)
  • Also labeled versions for nested structures
  • Usual advice is to find a better way
  • i.e., should look for a structured alternative

3
Boolean operators , , !
  • For combining simple boolean expressions into
    more complex expressions
  • Operands are boolean expressions
  • e.g., grade A weight gt 10
  • Note relational operators have higher precedence
  • Truth tables whole result given partial results
  • op1 op2 - true if both operands are true
  • op1 op2 - true if either operand is true
  • !op - true if operand is false
  • See LogicalOperators.java (Fig. 5.18, pp.
    184-185)
  • Note has greater precedence than

4
More boolean expressions
  • Note a difference from math descriptions
  • In math (0 lt amount lt 1000)
  • In Java (0 lt amount amount lt 1000)
  • De Morgans Law has 2 forms, both useful to
    simplify boolean expressions
  • Let A and B represent boolean values
  • 1. !(A B) is the same as !A !B
  • 2. !(A B) is the same as !A !B
  • Q How say not( 0 lt amount lt 1000)?

5
Review 7 control structures
6
Structure rule 1 start with the simplest
flowchart
  • One rectangle
  • A good (and widely applicable) example
  • get some data, calculate and show some results
  • Really just a way to start clarifies the big
    picture

7
Rule 2 replace any rectangle by two rectangles
in sequence
  • This stacking rule can apply repeatedly
    one?two, two?three, For example
  • Get data
  • Process
  • Show results

8
Rule 3 replace any rectangle by any control
structure
  • This nesting rule also applies repeatedly, as
    each control structure has rectangles
  • e.g., nest a while loop in an if structure
  • if (n gt 0)
  • while (i lt n)
  • System.out.println(i)

9
Rule 4 apply rules 2 and 3 repeatedly, and in
any order
  • Stack, nest, stack, nest, nest, stack, gets
    more and more detailed as one proceeds
  • Think of control structures as building blocks
    that can be combined in two ways only.
  • Captures the essence of stepwise refinement keep
    adding details as they arise
  • Basically means keep adding control structures as
    long as they are needed
  • Top-down design start with forest, do trees later

10
Programming graphics
  • Need a window javax.swing.JFrame
  • Several essential steps to use (necessary
    plumbing)
  • Set the size width and height in pixels
  • Set a title (optional), and a close operation
  • Make it visible
  • e.g., see lines 20-25 of ShapesTest (Fig. 5.27,
    p. 193)
  • Add javax.swing.JComponents to window
  • Note JPanel is a subclass of JComponent
  • Draw shapes, colors, on these components
  • Thats all there is to it!
  • Except for the painstaking labor, of course

11
Drawing rectangles for example
  • Define class that extends JComponent (or one of
    its subclasses)
  • Makes the new class a JComponent subclass too
  • Implement paintComponent method
  • Use Graphics object passed to this method
  • Actually better a Graphics2D object since Java
    1.2
  • Let that object draw rectangles (or Rectangle
    objects)
  • e.g., Shapes.java (Fig. 5.26, p. 192)
  • Or RectangleComponent.java (see links on Slides
    page)
  • Add the component to a frame for viewing
  • e.g., ShapesTest or RectangleViewer.java

12
C o l o r
  • Current color applies to text, lines, and fills
  • g.setColor(Color.RED)
  • g.drawLine() // (or g2.draw()) draws in red
  • g.setColor(Color.BLUE)
  • g.fillRect() // (or g2.fill()) fills with
    blue
  • Custom colors available
  • Can set by float values in range 0.0F to 1.0F
  • Color gb new Color(0.0F, 0.7F, 1.0F)
  • g.setColor(gb)
  • Or by int values in range 0 to 255
  • g.setColor( new Color(0, 255, 175) )
    // also
    shows technique if dont need a reference
    variable
  • ColoredSquareComponent.java and ColorViewer.java

13
Drawing more complex shapes
  • A simple car, for example Car.java (see links)
  • Acts like a Car that can draw itself
  • Car constructor sets x and y locations
  • Includes draw(Graphics2D g2) method
  • Lets Graphics2D object draw lines, ellipses,
    rectangles
  • A class like CarComponent.java just uses it
  • Car myCar new Car(x, y)
  • myCar.draw(g2) // passes reference to graphics
    object
  • Still need a window to view it, like
    CarViewer.java
  • Upcoming demo (after chapter 6) animate this
    drawing

14
Modularity
  • Also a structured programming topic
  • Can replace a rectangle with a module
  • Modules contain stacked/nested structures
  • Java modules
  • methods (the most basic modular units)
  • classes (collections of related methods)
  • packages (collections of related classes)

15
Using methods invoking
  • Direct translation of algorithm e.g.,
  • getData()
  • process()
  • showResults()
  • In turn, the method process() might do
  • result calculate(x, y)
  • where calculate is another method, one that
    returns a value based on x and y.
  • And so on

16
static methods and variables
  • A.k.a. class methods and class variables
  • Technically, same for all instances of a class
  • No particular instance (object) is involved
  • So instance variables have no meaning in a static
    context
  • Access by class name, not object reference
  • Good for self-contained methods
  • i.e., all necessary info is local to the method
  • May not use non-static methods or variables of
    class
  • Good for shared data and instance counts
  • e.g., if (Martian.count gt 10) retreat()

17
java.lang.Math static methods
  • Maths public methods are all static
  • So no need to make an object first
  • Invoke by class name and the dot . operator
  • Math.max(x, y) and Math.min(x, y)
  • max and min are overloaded return type same as
    x, y
  • Usually double parameters and return type
  • double r Math.toRadians(57.)
  • System.out.println(Sine of 57 degrees is
  • Math.sin(r))
  • Also two constant values Math.PI and Math.E
  • Math is in java.lang so no need to import

18
  • Maybe more
  • Maybe less

19
About constants like PI and E
  • final variables are constants
  • May only assign value once usually when declared
  • More efficient code (and often programming)
  • Should always avoid magic numbers
  • e.g., decipher this line of code
  • cost price 1.0775 4.5
  • More typing, but worth it
  • final double TAX_RATE 0.0775
  • final double SHIPPING 4.5
  • cost price (1. TAX_RATE) SHIPPING
  • Class constants final static variables
  • e.g., Math.PI is declared in java.lang.Math as
    follows
  • public static final double PI
    3.14159265358979323846

20
Some String methods
  • Accessing sub-strings (Note positions start
    at 0, not 1)
  • substring(int) returns end of string
  • substring(int, int) returns string from first
    position to just before last position
  • charAt(int) returns single char
  • length() the number of characters
  • toUpperCase(), toLowerCase(), trim(),
  • valueOf() converts any type to a String
  • But converting from a String more difficult
    must use specialized methods to parse
Write a Comment
User Comments (0)
About PowerShow.com