CS 180 - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

CS 180

Description:

Sign the Academic Integrity Policy!! Project 3 due Thursday, September 28th ... an application that simulates a screensaver by drawing various geometric shapes ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 45
Provided by: webIcs
Category:

less

Transcript and Presenter's Notes

Title: CS 180


1
  • CS 180
  • Recitation 9/22/06

2
Announcements
  • Exam 1 is Wednesday, September 27th
  • Room EE170
  • Time 700 800 pm
  • If you have a conflict with the exam, contact Dr.
    Van Zandt
  • Sign the Academic Integrity Policy!!
  • Project 3 due Thursday, September 28th
  • There have been updates to the description

3
Boolean Expressions
  • boolean is a primitive data type
  • Two values true or false
  • Compares two values using a relational expression
  • lt, gt, , lt, gt, !
  • Examples
  • boolean isOK true
  • boolean isLarge num gt 100

4
Boolean Operators
  • Boolean expressions can be combined using boolean
    operators
  • And
  • Or
  • Not !

5
Boolean Operators
  • bool1 bool2
  • true if bool1 AND bool2 are true
  • false if either bool1 or bool2 is false
  • bool1 bool 2
  • true if either bool1 OR bool2 is true
  • false if both bool1 AND bool2 are false
  • !bool1
  • true if bool1 is false
  • false if bool1 is true

6
Boolean Operator Examples
int x 20 boolean isValid x gt 0 x lt
100 int x 5 boolean isValid x gt 0 x lt
-100 int x 5 int y 10 boolean isDouble
(y (x 2))
  • What are the results of the above expressions?

7
Operator Precedence
  • Parenthesis should be used to indicate the order
    of operations
  • When there are no parenthesis, operator
    precedence is followed
  • Higher precedence preformed before lower
    precedence
  • Equal precedence performed left-to-right, except
    for unary operations which are performed
    right-to-left

8
Operator Precedence Rules
9
Operator Precedence Examples
int x 20 5 10 int y (20 5)
10 boolean w true true false
false boolean z true (true false)
false
  • What are the values of x, y, w, and z?

10
Selection Statements
  • Selection statements change the flow of control
    in a program
  • Use when the action to be preformed is dependent
    on the answer to a question
  • E.g. If the value of x is less than zero, print
    an error message. Otherwise add 1

11
If-else Statement
  • A branching statement used to choose between two
    actions

if ( ltBoolean expressiongt ) ltthen blockgt else
ltelse blockgt
  • The else branch is optional

12
If-else Statement
  • To enclose multiple statements in a branch,
    enclose the branch in braces

if ( ltBoolean expressiongt ) line 1 line
2 line 3 else line 4 line 5
13
If-else Statement
  • Single line blocks are not required to be
    enclosed in braces, but it is a good idea

if ( ltBoolean expressiongt ) ltthen blockgt else
ltelse blockgt
Is equivalent to
if ( ltBoolean expressiongt ) ltthen
blockgt else ltelse blockgt
14
Multibranch If-else Statement
if ( ltBoolean Expression 1gt ) statement 1 else
if (Boolean Expression 2) statement 2 else if
(Boolean Expression 3) statement 3 else if
(Boolean Expression 4) statement 4 else Default
Statement
15
If-else Statement Examples
int grade 11 if (grade lt 5 grade gt 10)
grade else grade--
int grade 70 if (count gt 90 count lt 100)
System.out.println(A) else if(count gt
80) System.out.println(B) else if(count gt
70) System.out.println(C) else if(count gt
60) System.out.println(D) else System.out.pri
ntln(F)
  • What is the value of grade in each statement?

16
If-else Statement Examples
int grade 5 if(grade lt 5) grade grade
10
int grade 5 if(grade lt 5) grade grade
10
  • What is the value of grade in each statement?

17
If-else Statement Examples
int grade 5 if(grade lt 5) grade 10
int grade 5 if(grade gt 0) grade if(grade
gt 5) grade else grade 0
  • What is the value of grade in each statement?

18
Switch Statements
  • Switch statements are a multiway branch which
    makes its decision based on an integer expression
  • char, byte, short, or int
  • A list of cases is searched until a match is
    found
  • If no match is found, the default case is
    executed
  • Optional

19
Switch Statement Syntax
switch(Controlling_Expression) case
Label1 statement(s) ltbreakgt case
Label2 statement(s) ltbreakgt ltdefaultgt s
tatement(s)
  • The breaks and the default case label above are
    optional
  • What happens if we take the breaks out?

20
Switch Statement Examples
int section 7 int room 0 switch(section)
case 1 room 101 break case 7 room
102 break case 5 room
103 break default System.out.println(inval
id)
int section 7 int room 0 switch(section)
case 1 room 101 break case 7 room
102 case 5 room 103 default System.out.p
rintln(invalid)
  • What are the results of the above statements?

21
Switch Statement Examples
char gender f switch(section) case
f case F System.out.println(female)
break case m case M System.out.println(
male) break
  • What is the result of the above statement?
  • Notice the empty case bodies
  • What if gender x ?

22
Drawing Graphics
  • Chapter 5 introduces four standard classes
    related to drawing geometric shapes. They are
  • java.awt.Graphics
  • java.awt.Color
  • java.awt.Point
  • java.awt.Dimension
  • These classes are used in the Sample Development
    section
  • Please refer to Java API for details

23
Sample Drawing
import javax.swing. //for JFrame import
java.awt. //for Graphics and Container class
Ch5SampleGraphics public static void main(
String args ) JFrame win
Container contentPane Graphics g
win new JFrame("My First Rectangle")
win.setSize(300, 200)
win.setLocation(100,100)
win.setVisible(true) contentPane
win.getContentPane() g
contentPane.getGraphics()
g.drawRect(50,50,100,30)
24
The Effect of drawRect
graphic.drawRect(ltxgt, ltygt, ltwidthgt, ltheightgt)
graphic.drawRect(50,50,100,30)
25
Problem Statement
  • Write an application that simulates a
    screensaver by drawing various geometric shapes
    in different colors. The user has an option of
    choosing a type (ellipse or rectangle), color,
    and movement (stationary, smooth, or random).

26
Overall Plan
  • Tasks
  • Get the shape the user wants to draw.
  • Get the color of the chosen shape.
  • Get the type of movement the user wants to use.
  • Start the drawing.

27
Required Classes
standard class
28
Development Steps
  • We will develop this program in six steps
  • Start with a program skeleton. Explore the
    DrawingBoard class.
  • Define an experimental DrawableShape class that
    draws a dummy shape.
  • Add code to allow the user to select a shape.
    Extend the DrawableShape and other classes as
    necessary.
  • Add code to allow the user to specify the color.
    Extend the DrawableShape and other classes as
    necessary.
  • Add code to allow the user to specify the motion
    type. Extend the DrawableShape and other classes
    as necessary.
  • Finalize the code by tying up loose ends.

29
Step 1 Design
  • The methods of the DrawingBoard class
  • public void addShape(DrawableShape shape)
  • Adds a shape to the DrawingBoard. No limit to
    the number shapes you can add
  • public void setBackground(java.awt.Color color)
  • Sets the background color of a window to the
    designated color
  • public void setDelayTime(double delay)
  • Sets the delay time between drawings to delay
    seconds
  • public void setMovement(int type)
  • Sets the movement type to STATIONARY, RANDOM, or
    SMOOTH
  • public void setVisible(boolean state)
  • Sets the background color of a window to the
    designated color
  • public void start( )
  • Starts the drawing of added shapes using the
    designated movement type and delay time.

30
Step 1 Code
Directory Chapter5/Step1 Source Files
Ch5DrawShape.java
Program source file is too big to list here. From
now on, we ask you to view the source files using
your Java IDE.
31
Step 1 Test
  • In the testing phase, we run the program and
    verify that a DrawingBoard window with black
    background appears on the screen and fills the
    whole screen.

32
Step 2 Design
  • Define a preliminary DrawableShape class
  • The required methods of this class are
  • public void draw(java.awt.Graphics g)
  • Draws a shape on Graphics object g.
  • public java.awt.Point getCenterPoint( )
  • Returns the center point of this shape
  • public java.awt.Dimension getDimension( )
  • Returns the bounding rectangle of this shape
  • public void setCenterPoint(java.awt.Point pt)
  • Sets the center point of this shape to pt.

33
Step 2 Code
Directory Chapter5/Step2 Source Files
Ch5DrawShape.java DrawableShape.java
34
Step 2 Test
  • We compile and run the program numerous times
  • We confirm the movement types STATIONARY, RANDOM,
    and SMOOTH.
  • We experiment with different delay times
  • We try out different background colors

35
Step 3 Design
  • We extend the main class to allow the user to
    select a shape information.
  • We will give three choices of shapes to the user
    Ellipse, Rectangle, and Rounded Rectangle
  • We also need input routines for the user to enter
    the dimension and center point. The center point
    determines where the shape will appear on the
    DrawingBoard.
  • Three input methods are
  • private int inputShapeType( )
  • private Dimension inputDimension( )
  • private Point inputCenterPoint( )

36
Step 3 Code
Directory Chapter5/Step3 Source Files
Ch5DrawShape.java DrawableShape.java
37
Step 3 Test
  • We run the program numerous times with different
    input values and check the results.
  • Try both valid and invalid input values and
    confirm the response is appropriate

38
Step 4 Design
  • We extend the main class to allow the user to
    select a color.
  • We follow the input pattern of Step 3.
  • We will allow the user to select one of the five
    colors.
  • The color input method is
  • private Color inputColor( )

39
Step 4 Code
Directory Chapter5/Step4 Source Files
Ch5DrawShape.java DrawableShape.java
40
Step 4 Test
  • We run the program numerous times with different
    color input.
  • Try both valid and invalid input values and
    confirm the response is appropriate

41
Step 5 Design
  • We extend the main class to allow the user to
    select a movement type.
  • We follow the input pattern of Step 3.
  • We will allow the user to select one of the three
    movement types.
  • The movement input method is
  • private int inputMotionType( )

42
Step 5 Code
Directory Chapter5/Step5 Source Files
Ch5DrawShape.java DrawableShape.java
43
Step 5 Test
  • We run the program numerous times with different
    movement input.
  • Try both valid and invalid input values and
    confirm the response is appropriate

44
Step 6 Finalize
  • Possible Extensions
  • Morphing the object shape
  • Changing the object color
  • Drawing multiple objects
  • Drawing scrolling text
Write a Comment
User Comments (0)
About PowerShow.com