Title: Sep 15-16
1Sep 15-16
- Strings, toString
- review of classes
- scoping
2Class String
String s Hi Mom s s and Dad String t
Hi Mom and Dad if (s t) ... if
(s.equals(t)) ...
3New program boxes
4Box.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
5Box.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)
6Box.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.
7CUCSDrawing 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)
8main 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()
9new 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)
11Scoping
- 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
12Scoping
- 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