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”)) …
Tag: code
Aug 29
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. * …
Aug 29
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) …
Aug 29
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; …
Aug 29
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); } } <<<<<<<<<<<<<<<<<<<<<
Aug 29
Basic Hello World application
******************* HelloWorld.java Basic Hello World application. ******************* */ public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello, world.”); } } /*
Aug 29
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) …
Aug 29
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 { …
Aug 29
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()); …
Aug 29
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) { …