Category: Java/J2EE/J2ME

URLTest.java Demonstrates try/catch blocks.

/** Taken from Core Web Programming from  *  Prentice Hall and Sun Microsystems Press,  *  .  *  © 2001 Marty Hall and Larry Brown;  *  may be freely used or adapted.    */    // Further simplified getURL method.    public URL getURL() {     if (url != null) {       return(url);     }     …

Continue reading

ExecTest.java illustrates use of the Exec class.

/** A test of the Exec class.  *  *  Taken from Core Web Programming from  *  Prentice Hall and Sun Microsystems Press,  *  .  *  © 2001 Marty Hall and Larry Brown;  *  may be freely used or adapted.  */ public class ExecTest {   public static void main(String[] args) {     // Note: no …

Continue reading

DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground.

/** Simulating dropping a ball from the top of the Washington  *  Monument. The program outputs the height of the ball each  *  second until the ball hits the ground.  *  *  Taken from Core Web Programming from  *  Prentice Hall and Sun Microsystems Press,  *  .  *  © 2001 Marty Hall and Larry Brown; …

Continue reading

Loading Images

JavaMan1.java Applet that loads an image from a relative URL. ************************************************************* import java.applet.Applet; import java.awt.*; /** An applet that loads an image from a relative URL.  * >>>>>>>>>>>>>>>>>>> public class JavaMan1 extends Applet {   private Image javaMan;   public void init() {     javaMan = getImage(getCodeBase(),”images/Java-Man.gif”);   }   public void paint(Graphics g) { …

Continue reading

Basic template for a Java applet

AppletTemplate.java >>>>>>>>>>>>>>>>>>>> import java.applet.Applet; import java.awt.*; ******************** public class AppletTemplate extends Applet {   // Variable declarations.   public void init() {     // Variable initializations, image loading, etc.   }   public void paint(Graphics g) {     // Drawing operations.   } } >>>>>>>>>>>>>>>>>>>>>

ForwardSnippet.java Partial servlet illustrating how to use a RequestDispatcher to forward requests

ForwardSnippet.java Partial servlet illustrating how to use a RequestDispatcher to forward requests public void doGet(HttpServletRequest request,                   HttpServletResponse response)     throws ServletException, IOException {   String operation = request.getParameter(“operation”);   if (operation == null) {     operation = “unknown”;   }   if (operation.equals(“operation1”)) {     gotoPage(“/operations/presentation1.jsp”,              request, response);   } else if (operation.equals(“operation2”)) …

Continue reading

Example illustrating inheritance and abstract classes

*********************************** # Example illustrating inheritance and abstract classes.     * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes.     * Curve.java An (abstract) curved Shape (open or closed).     * StraightEdgedShape.java A Shape with straight edges (open or closed).     * Measurable.java Interface defining classes with measurable areas.     * …

Continue reading

Speedboat.java Illustrates inheritance from Ship class

***************************** Speedboat.java Illustrates inheritance from Ship class. See SpeedboatTest.java for a test. ***************************** /** A fast Ship. Red and going 20 knots by default.  *  *********************** public class Speedboat extends Ship {   private String color = “red”;   /** Builds a red Speedboat going N at 20 knots. */     public Speedboat(String name) …

Continue reading

Demonstrates overloading methods in class Ship4

********************* class Ship4 {   public double x=0.0, y=0.0, speed=1.0, direction=0.0;   public String name;   // This constructor takes the parameters explicitly.     public Ship4(double x, double y, double speed,                double direction, String name) {     this.x = x;     this.y = y;     this.speed = speed;     this.direction = direction;     …

Continue reading

HelloWWW.java Basic Hello World (Wide Web) Applet

********************* import java.applet.Applet; import java.awt.*; *********************   public class HelloWWW extends Applet {   private int fontSize = 40;     public void init() {     setBackground(Color.black);     setForeground(Color.white);     setFont(new Font(“SansSerif”, Font.BOLD, fontSize));   }     public void paint(Graphics g) {     g.drawString(“Hello, World Wide Web.”, 5, fontSize+5);   } } <<<<<<<<<<<<<<<<<<<<<