Tag: code

Application that reports all command-line arguments

****************** ShowArgs.java Application that reports all command-line arguments. ******************  */ public class ShowArgs {   public static void main(String[] args) {     for(int i=0; i

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

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 …

Continue reading

NumFormat.java Formats real numbers with DecimalFormat.

import java.text.*; /** Formatting real numbers with DecimalFormat.  *  *  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 NumFormat {   public static void main (String[] args) {     DecimalFormat …

Continue reading

ModificationTest.java Demonstrates changing fields of an object. Inherits from ReferenceTest.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.  */ import java.awt.Point; public class ModificationTest extends ReferenceTest {   public static void main(String[] args) {     Point p1 = new Point(1, 2); // …

Continue reading

Tests the class type of an object using the isInstance method (preferred over instanceof operator).

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

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.   } } >>>>>>>>>>>>>>>>>>>>>