Category: FromSitesTree.com

NegativeLengthException.java Illustrates defining and throwing your own exceptions. #Programming Code Examples #Java/J2EE/J2ME #Basic Java

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 { int lineLength = …

Continue reading

Illustrates the use of arrays #Programming Code Examples #Java/J2EE/J2ME #Basic Java

/** 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) { int[] pars …

Continue reading

Tests the class type of an object using the isInstance method (preferred over instanceof operator). #Programming Code Examples #Java/J2EE/J2ME #Basic Java

/** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ interface Barking {} class Mammal {} class Canine extends Mammal {} class Dog extends Canine implements Barking {} class Retriever extends Dog {} …

Continue reading

Exec.java Provides static methods for running external processes from applications. #Programming Code Examples #Java/J2EE/J2ME #Basic Java

import java.io.*; /** A class that eases the pain of running external processes * from applications. Lets you run a program three ways: * * exec: Execute the command, returning * immediately even if the command is still running. * This would be appropriate for printing a file. * execWait: Execute the command, but don?t …

Continue reading

ExecTest.java illustrates use of the Exec class. #Programming Code Examples #Java/J2EE/J2ME #Basic Java

/** 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 trailing "&" …

Continue reading

Factorial.java Computes an exact factorial, n!, using a BigInteger #Programming Code Examples #Java/J2EE/J2ME #Basic Java

import java.math.BigInteger; /** Computes an exact factorial, using a BigInteger. * * 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 Factorial { public static void main(String[] args) { for(int i=1; …

Continue reading

URLTest.java Demonstrates try/catch blocks. #Programming Code Examples #Java/J2EE/J2ME #Basic Java

/** 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); } System.out.print("Enter URL: "); System.out.flush(); BufferedReader in = …

Continue reading

Creates three radio buttons and illustrates handling #Programming Code Examples #Java/J2EE/J2ME #Basic Swing

JRadioButtonTest.java Creates three radio buttons and illustrates handling ItemEvents in response to selecting a radio button. import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** */ public class JRadioButtonTest extends JPanel implements ItemListener { public JRadioButtonTest() { String[] labels = {"Java Swing","Java Servlets", "JavaServer Pages"}; JRadioButton[] buttons = new JRadioButton[3]; ButtonGroup group = …

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. #Programming Code Examples #Java/J2EE/J2ME #Basic Java

/** 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

mall example showing the basic use of a JToolBar #Programming Code Examples #Java/J2EE/J2ME #Basic Swing

mall example showing the basic use of a JToolBar import java.awt.*; import javax.swing.*; import java.awt.event.*; /** Small example showing basic use of JToolBar. * * */ public class JToolBarExample extends JFrame implements ItemListener { private BrowserToolBar toolbar; private JCheckBox labelBox; public static void main(String[] args) { new JToolBarExample(); } public JToolBarExample() { super("JToolBar Example"); WindowUtilities.setNativeLookAndFeel(); …

Continue reading