Handling Errors in Java Programming
Dec 21, 2015
Handling Errors in Java Programming
// The “name-of-application” class
import java.awt.*;
import hsa.Console;
public class name-of-application
{
static Console c;
static public void main (String [] args)
{
c = new Console ();
your program goes here
} // main method
any user-defined methods go here
} // name-of-application class
// The “GreenOval” class
import java.awt.*;
import hsa.Console;
public class GreenOval
{
static Console c;
static public void main (String [] args)
{
c = new Console ();
c.setColor (Color.green);
c.fillOval (0, 0, 100, 200);
} // main method
} // GreenOval class
// The “GreenOval” class
import java.awt.*;
import hsa.Console;
public class GreenOval
{
static Console c;
static public void main (String [] args)
{
c = new Console ();
c.setColor (Color.green);
c.fillOval (0, 0, 100, 200);
} // main method
} // GreenOval class
delete the semicolon
// The “GreenOval” class
import java.awt.*;
import hsa.Console;
public class GreenOval
{
static Console c;
static public void main (String [] args)
{
c = new Console ();
c.setColor (Color.green);
c.fillOval (0, 0, 100, 200);
} // main method
} // GreenOval class
delete c.
// The “GreenOval” class
import java.awt.*;
import hsa.Console;
public class GreenOval
{
static Console c;
static public void main (String [] args)
{
c = new Console ();
c.setColor (Color.green);
c.fillOval (0, 0, 100, 200);
} // main method
} // GreenOval class
delete the close curly brace
// The “GreenOval” class
import java.awt.*;
import hsa.Console;
public class GreenOval
{
static Console c;
static public void main (String [] args)
{
c = new Console ();
c.setColor (Color.green);
c.fillOval (0, 0, 100, 200);
} // main method
} // GreenOval class
uppercase the c
Multiple errors
There are actually 4 errors
lowercase c
missing semicolonc. is missing
main mispelled
Debugging Activity
• Before your program is compiling and running correctly
• Put in 2 simple errors
• Compile
• Swap seats with a classmate
• Debug each other’s “buggy” programs
FaceWrite a program that produces a picture of a face. It must consist of at least 4 different colors and the following: head, eyes, nose, mouth. Name this class Face.
graphics in java.awt.*…
void drawLinedrawLine (int x1, int y1, int x2, int y2)
void drawOvaldrawOval (int x, int y, int width, int height)
…
graphics in hsa.console.*void clearRectclearRect (int x, int y, int width, int
height)
…
void drawArcdrawArc (int x, int y, int width, int height, int startAngle, int arcAngle)
drawLine (int x1, int y1, int x2, int y2);
fillOval (int x, int y, int width, int height);
(x,y) -------------------width------------------------
Height
•Start a new hsa console app and call it Face}//main methodstatic public void circle(int xc, int yc, int radius, Color clr) { int width = radius * 2; int height = radius * 2; int x = xc - radius; int y = yc - radius; c.setColor(clr); c.drawOval(x,y,width,height); }//circle method
public static void main (String[] args) { c = new Console (); int xc = c.getWidth() /2; int yc = c.getHeight() /2; int radius = c.getHeight() /2; circle(xc, yc, radius, Color.red);
c.setColor(Color.black); c.drawLine(250,300,400,300);
int xe = c.getWidth() /3; int ye = c.getHeight() /4; int e_rad = c.getHeight() /50; circle(xe, ye, e_rad, Color.blue);
int ae = xc + (xe / 2); int be = c.getHeight() /4; int a_rad = c.getHeight() / 50; circle(ae, be, a_rad, Color.orange); int ayn = c.maxy () / 2; int axn = c.maxx () / 2; int rad = 16; circle(axn, ayn, rad, Color.green); } // main method