Sep 15-16 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Sep 15-16

Description:

g.setColor (oldColor); Box.java // Describe the box via its position and shape and color. public String toString () return color ', ' height 'x' width ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 13
Provided by: claire67
Category:
Tags: sep | strings

less

Transcript and Presenter's Notes

Title: Sep 15-16


1
Sep 15-16
  • Strings, toString
  • review of classes
  • scoping

2
Class String
String s Hi Mom s s and Dad String t
Hi Mom and Dad if (s t) ... if
(s.equals(t)) ...
3
New program boxes
4
Box.java
import java.awt. // An instance of class Box
is a rectangle. public class Box public int
xUpperLeft // The box has an upper left corner
at public int yUpperLeft // position
(xUpperLeft, yUpperLeft). public int width //
width of box public int height // height of
box public Color color // color of box
5
Box.java
// Draw the box in designated color using
Graphics g public void drawBox (Graphics
g) // save original color in variable
oldColor Color oldColor g.getColor () //
set new color and draw the rectangle g.setColor
(color) g.fillRect (xUpperLeft, yUpperLeft,
width, height) // restore original
color g.setColor (oldColor)
6
Box.java
// Describe the box via its position and shape
and color. public String toString ()
return color , height x width
box at xUpperLeft
, yUpperLeft Convention Every
class implements method toString! In B
(catenation), if B is not a String, method
B.toString() is used to convert it to a String.
7
CUCSDrawing Class
// Class CUCSDrawing a simple graphics
window. public class CUCSDrawing extends
Frame public void paint(Graphics
g) g.setColor(Color.black) g.drawOval(15,3
0,30,30) g.drawRect(77,30,40,30) g.setColor(
Color.red) g.drawString("circle",15,80) g.dr
awString("rectangle",75,80)
8
main program
// Main program for simple Graphics Applications.
The program creates the // drawing window and
sets its size and title. The actual drawing is
done // by the paint method in class Drawing.
The drawing window is moved down // the screen
slightly so it won't overlap the System.in window
if used. public class CUCSGraphicsApplication
public static void main(String args) CUCSDr
awing d new CUCSDrawing() d.resize(200,150)
d.move(0,75) d.setTitle("Drawing") d.show(
) d.toFront()
9
new paint method
// Draw 2 Boxes and print their
locations public void paint(Graphics
g) Box b1 // first box Box b2 //
second box // Create the first box and
initialize its fields. b1 new
Box() b1.xUpperLeft 50 b1.yUpperLeft
70 b1.width 60 b1.height 20
10
// Create the second box and initialize its
fields. b2 new Box() b2.xUpperLeft
150 b2.yUpperLeft 150 b2.width
20 b2.height 30 // Draw and describe the
boxes b1.drawBox(g) System.out.println
(b1) b2.drawBox(g) System.out.println
(b2)
11
Scoping
  • scope of a name is the area of program within
    which it can be referenced
  • a variable must be declared before being used
  • can only be declared once within a method
  • scope of a local variable in a method is the
    sequence of statements that follow it in the
    method and that are in the same block (.)
  • scope of a formal parameter is the sequence of
    statements in the method body

12
Scoping
  • scope of a public field is the bodies of all
    methods in the class (and the declarations of
    fields that follow) and anywhere the class can be
    referenced (e.g., usually the main method)
  • scope of a private field is the bodies of all
    methods in the class (and the declarations of
    fields that follow)
  • cannot redeclare a formal parameter within the
    method
  • if you declare a local variable of the same name
    as a field, then you cant reference that field
    in that method
Write a Comment
User Comments (0)
About PowerShow.com