JustEtc Technologies
Short Notes & Video Tutorials on Business & Computer Member Login
Training Videos | Short Notes | Step by Step Courses | Books | Ezine | Post Articles

JustEtc Magazine on Business & Computer

Next Issue   Previous Issue


Message.java Applet that reads customization parameters from an HTML file [ More in this category ] [Add your comments]


*******************
Message.java Applet that reads customization parameters from an HTML file
*******************
import java.applet.Applet;
import java.awt.*;

****************
 
public class Message extends Applet {
  private int fontSize;
  private String message;
  
  public void init() {
    setBackground(Color.black);
    setForeground(Color.white);
    
    // Base font size on window height.
    fontSize = getSize().height - 10;
    
    setFont(new Font("SansSerif", Font.BOLD, fontSize));

    // Read heading message from PARAM entry in HTML.
    message = getParameter("MESSAGE");
  }

  public void paint(Graphics g) {
    if (message != null) {
      g.drawString(message, 5, fontSize+5);
    }
  }
}
*******************


HelloWWW.java Basic Hello World (Wide Web) Applet [ More in this category ] [Add your comments]


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


Application that reports all command-line arguments [ More in this category ] [Add your comments]


******************
ShowArgs.java Application that reports all command-line arguments.
******************
 */

public class ShowArgs {
  public static void main(String[] args) {
    for(int i=0; i

					


Basic Hello World application [ More in this category ] [Add your comments]


*******************
HelloWorld.java Basic Hello World application. 
*******************
 */

public class HelloWorld {
 public static void main(String[] args) {
    System.out.println("Hello, world.");
  }
}
/*


Statics.java Demonstrates static and non-static methods. [ More in this category ] [Add your comments]


 
*/

public class Statics {
 public static void main(String[] args) {
    staticMethod();
    Statics s1 = new Statics();
    s1.regularMethod();
  }

  public static void staticMethod() {
    System.out.println("This is a static method.");
  }

  public void regularMethod() {
    System.out.println("This is a regular method.");
  }
}


Example demonstrating the use of packages [ More in this category ] [Add your comments]


&&&&&&&&&&&&&&&&&&&
Example demonstrating the use of packages.

    * Class1.java defined in package1.
    * Class2.java defined in package2.
    * Class3.java defined in package2.package3.
    * Class1.java defined in package4.
    * PackageExample.java Driver for package example
&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~
Class1.java defined in package1.
~~~~~~~~~~~~~~~~~~~~~
package package1;

*****************
 
public class Class1 {
  public static void printInfo() {
    System.out.println("This is Class1 in package1.");
  }
}
&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~
Class2.java defined in package2. 
~~~~~~~~~~~~~~~~~~~~~
package package2;
$$$$$$$$$$$$$$$$

public class Class2 {
  public static void printInfo() {
    System.out.println("This is Class2 in package2.");
  }
}
&&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~~
Class3.java defined in package2.package3
~~~~~~~~~~~~~~~~~~~~~~
package package2.package3;

@@@@@@@@@@@@@@@@@@@@@@@@@

public class Class3 {
  public static void printInfo() {
    System.out.println("This is Class3 in " +
                       "package2.package3.");
  }
}
&&&&&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~~~~~
Class1.java defined in package4.
~~~~~~~~~~~~~~~~~~~~~~~~~
package package4;

@@@@@@@@@@@@@@@@

public class Class1 {
  public static void printInfo() {
    System.out.println("This is Class1 in package4.");
  }
}
&&&&&&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~~~~~~
PackageExample.java Driver for package example.
~~~~~~~~~~~~~~~~~~~~~~~~~~
import package1.*;
import package2.Class2;
import package2.package3.*;

***************************

public class PackageExample {
  public static void main(String[] args) {
    Class1.printInfo();
    Class2.printInfo();
    Class3.printInfo();
    package4.Class1.printInfo();
  }
}
****************************


Example illustrating inheritance and abstract classes [ More in this category ] [Add your comments]


***********************************
# 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.
    * Circle.java A circle that extends Shape and implements Measurable.
    * MeasureUtil.java Operates on Measurable instances.
    * Polygon.java A closed Shape with straight edges; extends StraightEdgedShape and implements Measurable.
    * Rectangle.java A rectangle that satisfies the Measurable interface; extends Polygon.
    * MeasureTest.java Driver for example.
**************************************
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Shape.java 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** The parent class for all closed, open, curved, and 
 *  straight-edged shapes.
 *
 ############################
public abstract class Shape {
  protected int x, y;

  public int getX() {
    return(x);
  }

  public void setX(int x) {
    this.x = x;
  }

  public int getY() {
    return(y);
  }

  public void setY(int y) {
    this.y = y;
  }
}
#############################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Curve.java An (abstract) curved Shape (open or closed)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** A curved shape (open or closed). Subclasses will include
 *  arcs and circles.
 *
***********************

public abstract class Curve extends Shape {}
##############################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StraightEdgedShape.java A Shape with straight edges (open or closed). 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** A Shape with straight edges (open or closed). Subclasses
 *  will include Line, LineSegment, LinkedLineSegments,
 *  and Polygon.
 *
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public abstract class StraightEdgedShape extends Shape {}
################################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Measurable.java Interface defining classes with measurable areas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** Used in classes with measurable areas. 
 *
 **************

public interface Measurable {
  double getArea();
}
#################################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Circle.java A circle that extends Shape and implements Measurable.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** A circle. Since you can calculate the area of
 *  circles, class implements the Measurable interface.
 *
***********************************
public class Circle extends Curve implements Measurable {
  private double radius;

  public Circle(int x, int y, double radius) {
    setX(x);
    setY(y);
    setRadius(radius);
  }

  public double getRadius() {
    return(radius);
  }

  public void setRadius(double radius) {
    this.radius = radius;
  }

  /** Required for Measurable interface. */

  public double getArea() {
    return(Math.PI * radius * radius);
  }
}
############################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MeasureUtil.java Operates on Measurable instances
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** Some operations on Measurable instances. 
 *

************************
public class MeasureUtil {
  public static double maxArea(Measurable m1,
                               Measurable m2) {
    return(Math.max(m1.getArea(), m2.getArea()));
  }

  public static double totalArea(Measurable[] mArray) {
    double total = 0;
    for(int i=0; i
~~~~~~~~~~~~~~~~~~~~~~


					


Code examples for interfaces [ More in this category ] [Add your comments]


****************************
Code examples for interfaces:

    * Class1.java implements Interface1.java
    * Abstract Class2.java implements Interface1.java and Interface2.java
    * Class3.java extends abstract class Class2.java
    * Interface3.java extends Interface1.java and Interface2.java
***************************
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Class1.java 
~~~~~~~~~~~~~~~~~~~~~~~~~~~

// This class is not abstract, so it must provide 
// implementations of method1 and method2.

public class Class1 extends SomeClass
                    implements Interface1 {
  public ReturnType1 method1(ArgType1 arg) {
    someCodeHere();
    ...
  }
                      
  public ReturnType2 method2(ArgType2 arg) {
        someCodeHere();
    ...
  }

  ...
}
>>>>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~
Interface1.java
~~~~~~~~~~~~~~~~~~~~~~~~~~
public interface Interface1 {
   ReturnType1 method1(ArgType1 arg);
  ReturnType2 method2(ArgType2 arg);
}
>>>>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~
Abstract Class2.java implements Interface1.java and Interface2.java
~~~~~~~~~~~~~~~~~~~~~~~~~~
Class2.java 
~~~~~~~~~~~~~~~~~~~~~~~~~~

// This class is abstract, so does not have to provide
// implementations of the methods of Interface 1 and 2.

public abstract class Class2 extends SomeOtherClass
                             implements Interface1,
                                        Interface2 {
  ...
}
>>>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~~~~
Interface2.java
~~~~~~~~~~~~~~~~~~~~~~~~~

public interface Interface2 {
  ReturnType3 method3(ArgType3 arg);
}
~~~~~~~~~~~~~~~~~~~~~~~~~
# Class3.java extends abstract class Class2.java 
~~~~~~~~~~~~~~~~~~~~~~~~~
Class3.java
>>>>>>>>>>>>>>>>>>>>>>>>>

// This class is not abstract, so it must provide
// implementations of method1, method2, and method3.

public class Class3 extends Class2 {
  public ReturnType1 method1(ArgType1 arg) {
    someCodeHere();
    ...
  }
                      
  public ReturnType2 method2(ArgType2 arg) {
       someCodeHere();
    ...
  }

  public ReturnType3 method3(ArgType3 arg) {
     someCodeHere();
    ...
  }

  ...
}
>>>>>>>>>>>>>>>>>>>>>>>
# Interface3.java extends Interface1.java and Interface2.java
>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~
Interface3.java 
~~~~~~~~~~~~~~~~~~~~~~

// This interface has three methods (by inheritance) and 
// two constants.

public interface Interface3 extends Interface1,
                                    Interface2 {
  int MIN_VALUE = 0;
  int MAX_VALUE = 1000;
}
<<<<<<<<<<<<<<<<<<<<<


Speedboat.java Illustrates inheritance from Ship class [ More in this category ] [Add your comments]


*****************************
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) {
    super(name);
    setSpeed(20);
  }

  /** Builds a speedboat with specified parameters. */
  
  public Speedboat(double x, double y, double speed,
                   double direction, String name,
                   String color) {
    super(x, y, speed, direction, name);
    setColor(color);
  }

  /** Report location. Override version from Ship. */
  
  public void printLocation() {
    System.out.print(getColor().toUpperCase() + " ");
    super.printLocation();
  }
  
  /** Gets the Speedboat's color. */
  
  public String getColor() {
    return(color);
  }

  /** Sets the Speedboat's color. */
  
  public void setColor(String colorName) {
    color = colorName;
  }
}
**********************
SpeedboatTest.java 
**********************
/** Try a couple of Speedboats and a regular Ship. 
 *
*****************************

public class SpeedboatTest {
  public static void main(String[] args) {
    Speedboat s1 = new Speedboat("Speedboat1");
    Speedboat s2 = new Speedboat(0.0, 0.0, 2.0, 135.0,
                                 "Speedboat2", "blue");
    Ship s3 = new Ship(0.0, 0.0, 2.0, 135.0, "Ship1");
    s1.move();
    s2.move();
    s3.move();
    s1.printLocation();
    s2.printLocation();
    s3.printLocation();
  }
}
*****************************


A Ship class illustrating object-oriented programming concepts [ More in this category ] [Add your comments]


************************
Ship.java A Ship class illustrating object-oriented programming concepts. Incorporates Javadoc comments. See ShipTest.java for a test. 
************************
/** Ship example to demonstrate OOP in Java.
 *
 *  @author 
 *          Larry Brown
 *  @version 2.0
 */

public class Ship {
  // Instance variables

  private double x=0.0, y=0.0, speed=1.0, direction=0.0;
  private String name;

  // Constructors

  /** Build a ship with specified parameters. */

  public Ship(double x, double y, double speed,
              double direction, String name) {
    setX(x);
    setY(y);
    setSpeed(speed);
    setDirection(direction);
    setName(name);
  }

  /** Build a ship with default values
   *  (x=0, y=0, speed=1.0, direction=0.0).
   */

  public Ship(String name) {
    setName(name);
  }

  /** Move ship one step at current speed/direction. */

  public void move() {
    moveInternal(1);
  }

  /** Move N steps. */

  public void move(int steps) {
    moveInternal(steps);
  }

  private void moveInternal(int steps) {
    double angle = degreesToRadians(direction);
    x = x + (double)steps * speed * Math.cos(angle);
    y = y + (double)steps * speed * Math.sin(angle);
  }

  private double degreesToRadians(double degrees) {
    return(degrees * Math.PI / 180.0);
  }

  /** Report location to standard output. */

  public void printLocation() {
    System.out.println(getName() + " is at (" + getX() +
                       "," + getY() + ").");
  }

  /** Get current X location. */

  public double getX() {
    return(x);
  }

  /** Set current X location. */

  public void setX(double x) {
    this.x = x;
  }

  /** Get current Y location. */

  public double getY() {
    return(y);
  }

  /** Set current Y location. */

  public void setY(double y) {
    this.y = y;
  }

  /** Get current speed. */

  public double getSpeed() {
    return(speed);
  }

  /** Set current speed. */

  public void setSpeed(double speed) {
    this.speed = speed;
  }

  /** Get current heading (0=East, 90=North, 180=West,
   *  270=South).  I.e., uses standard math angles, not
   *  nautical system where 0=North, 90=East, etc.
   */

  public double getDirection() {
    return(direction);
  }

  /** Set current direction (0=East, 90=North, 180=West,
   *  270=South). I.e., uses standard math angles,not
   *  nautical system where 0=North,90=East, etc.
   */

  public void setDirection(double direction) {
    this.direction = direction;
  }

  /** Get Ship's name. Can't be modified by user. */

  public String getName() {
    return(name);
  }

  private void setName(String name) {
    this.name = name;
  }
}
*********************
ShipTest.java 
*********************
public class ShipTest {
 public static void main(String[] args) {
    Ship s1 = new Ship("Ship1"); 
    Ship s2 = new Ship(0.0, 0.0, 2.0, 135.0, "Ship2");
    s1.move();
    s2.move(3);
    s1.printLocation();
    s2.printLocation();
  }
}

Next Issue   Previous Issue


   Top 5 Writers




Contact us for advertisement rates

Please note that our sole purpose is to provide useful contents/information for Computer Science, Engineering, and IT professionals. However, we do not carry any responsibility for the information as provided throughout our web-sites. Please use them with your own responsibility. Our web-sites use terms like Microsoft, Oracle, J2EE, EJB, Hibernate and similar. These are trademarks of the corresponding companies where JustEtc is our brand. We tried to comply with copyrights to the best of our knowledge. Still, if you have any concern, please let us know. We will address your concerns at our earliest convenience. All the contents and videos authored by us are our copyrights. You can use them for your personal learning and research, however, you are not allowed to distribute JustEtc authored contents/videos to others. Also, you are not allowed to sale them. You are not allowed to publish our materials in any other web-sites or prininting materials.

Privacy Policy Terms of Service