Category Archive: Java/J2EE/J2ME

Print three-dimensional valarray line-by-line

/* The following code example is taken from the book  * “The C++ Standard Library – A Tutorial and Reference”  * by Nicolai M. Josuttis, Addison-Wesley, 1999  *  * (C) Copyright Nicolai M. Josuttis 1999.  * Permission to copy, use, modify, sell and distribute this software  * is granted provided this copyright notice appears in …

Continue reading »

Permanent link to this article: http://bangla.salearningschool.com/print-three-dimensional-valarray-line-by-line/

Example demonstrating the use of packages

&&&&&&&&&&&&&&&&&&& 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 …

Continue reading »

Permanent link to this article: http://bangla.salearningschool.com/example-demonstrating-the-use-of-packages/

Code examples for interfaces

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

Continue reading »

Permanent link to this article: http://bangla.salearningschool.com/code-examples-for-interfaces/

A Ship class illustrating object-oriented programming concepts

************************ 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; …

Continue reading »

Permanent link to this article: http://bangla.salearningschool.com/a-ship-class-illustrating-object-oriented-programming-concepts/

Accesses instance variables in a Ship object.

Test1.java Accesses instance variables in a Ship object. ******************************************************** // Create a class with five instance variables (fields): // x, y, speed, direction, and name. Note that Ship1 is // not declared “public”, so it can be in the same file as // Test1. A Java file can only contain one “public” class // definition. …

Continue reading »

Permanent link to this article: http://bangla.salearningschool.com/accesses-instance-variables-in-a-ship-object/

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

Permanent link to this article: http://bangla.salearningschool.com/application-that-reports-all-command-line-arguments/

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 »

Permanent link to this article: http://bangla.salearningschool.com/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-2/

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 »

Permanent link to this article: http://bangla.salearningschool.com/numformat-java-formats-real-numbers-with-decimalformat/

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 »

Permanent link to this article: http://bangla.salearningschool.com/modificationtest-java-demonstrates-changing-fields-of-an-object-inherits-from-referencetest-java/

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 »

Permanent link to this article: http://bangla.salearningschool.com/tests-the-class-type-of-an-object-using-the-isinstance-method-preferred-over-instanceof-operator/

Older posts «