Java notes
Java notes
Which number type would you use for storing the area of a circle?
Answer: double.
• Assignment operator: =
• Used to change the value of a variable:
int width= 10;
width = 20;
• Means:
1.compute the value of width + 10
2.store that value in the variable width
Note that
String greeting = "Hello, Nina!";
• Return value: a result that the method has computed for use by
the code that called it:
int n = greeting.length(); // return value stored in n
Answer: 12.
Program Run:
x: 20
Expected: 20
y: 35
Expected: 35
To show a frame:
1. Construct an object of the JFrame class:
JFrame frame = new JFrame();
How do you display a square frame with a title bar that reads
"Hello, World!”?
Answer: Modify the EmptyFrameViewer program as
follows:
frame.setSize(300, 300);
frame.setTitle("Hello, World!");
Continued
1. Construct a frame.
2. Construct an object of your component class:
RectangleComponent component = new RectangleComponent();
• To run an applet, you need an HTML file with the applet tag
• An HTML file can have multiple applets; add a separate
applet tag for each applet
• You view applets with the applet viewer or a Java enabled
browser:
appletviewer RectangleApplet.html
Continued
• To draw a line:
Line2D.Double segment =
new Line2D.Double(x1, y1, x2, y2);
g2.draw(segment);
or,
Color.BLUE 0, 0, 255
Color.GREEN 0, 255, 0
Color.RED 255, 0, 0
Continued