Tag: code

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);   } } <<<<<<<<<<<<<<<<<<<<<

Basic Hello World application

******************* HelloWorld.java Basic Hello World application. *******************  */ public class HelloWorld {  public static void main(String[] args) {     System.out.println(“Hello, world.”);   } } /*

StringTest.java Demonstrates various methods of the String 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 StringTest {   public static void main (String[] args) {     String str = “”;     if (args.length > 0) …

Continue reading

NegativeLengthException.java Illustrates defining and throwing your own exceptions.

import java.io.*; /** 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 NegativeLengthException extends Exception {   /** Test NegativeLengthException */   public static void main(String[] args) {     try { …

Continue reading

TreeTest.java Builds a binary tree and prints the contents of the nodes. Uses the following classes:

Treetest.java /** A NodeOperator that prints each node.  *  *  Taken from Core Web Programming from  *  Prentice Hall and Sun Microsystems Press,  *  .  *  © 2001 Marty Hall and Larry Brown;  *  may be freely used or adapted.  */ class PrintOperator implements NodeOperator {   public void operateOn(Node node) {     System.out.println(node.getNodeValue());   …

Continue reading

Illustrates the use of arrays

/** Report on a round of golf at St. Andy?s.  *  *  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 Golf {   public static void main(String[] args) {     …

Continue reading