AddressVerifier.java Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes: #Programming Code Examples #Java/J2EE/J2ME #Network Programming


AddressVerifier.java  Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes: 

import java.net.*;
import java.io.*;

/** Given an e-mail address of the form user@host,
 *  connect to port 25 of the host and issue an
 *  'expn' request for the user. Print the results.
 *
 *  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 AddressVerifier extends NetworkClient {
  private String username;

  public static void main(String[] args) {
    if (args.length != 1) {
      usage();
   }
    MailAddress address = new MailAddress(args[0]);
    AddressVerifier verifier
      = new AddressVerifier(address.getUsername(),
                            address.getHostname(), 25);
    verifier.connect();
  }

  public AddressVerifier(String username, String hostname,
                         int port) {
    super(hostname, port);
    this.username = username;
  }

  /** NetworkClient, the parent class, automatically establishes
   *  the connection and then passes the Socket to
   *  handleConnection. This method does all the real work
   *  of talking to the mail server.
   */

  // You can't use readLine, because it blocks. Blocking I/O
  // by readLine is only appropriate when you know how many
  // lines to read. Note that mail servers send a varying
  // number of lines when you first connect or send no line
  // closing the connection (as HTTP servers do), yielding
  // null for readLine. Also, we'll assume that 1000 bytes
  // is more than enough to handle any server welcome
  // message and the actual EXPN response.

  protected void handleConnection(Socket client) {
    try {
      PrintWriter out = SocketUtil.getWriter(client);
      InputStream in = client.getInputStream();
      byte[] response = new byte[1000];
      // Clear out mail server's welcome message.
      in.read(response);
      out.println("EXPN " + username);
      // Read the response to the EXPN command.
      int numBytes = in.read(response);
      // The 0 means to use normal ASCII encoding.
      System.out.write(response, 0, numBytes);
      out.println("QUIT");
      client.close();
    } catch(IOException ioe) {
      System.out.println("Couldn't make connection: " + ioe);
    }
  }

  /** If the wrong arguments, thn warn user. */

  public static void usage() {
    System.out.println ("You must supply an email address " +
       "of the form 'username@hostname'.");
    System.exit(-1);
  }
}


MailAddress.java  Separates the user and host components of an email address. 

import java.util.*;

/** Takes a string of the form "user@host" and
 *  separates it into the "user" and "host" parts.
 *
 *  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 MailAddress {
  private String username, hostname;

  public MailAddress(String emailAddress) {
    StringTokenizer tokenizer
      = new StringTokenizer(emailAddress, "@");
    this.username = getArg(tokenizer);
    this.hostname = getArg(tokenizer);
  }

  private static String getArg(StringTokenizer tok) {
    try { return(tok.nextToken()); }
    catch (NoSuchElementException nsee) {
      System.out.println("Illegal email address");
      System.exit(-1);
      return(null);
    }
  }

  public String getUsername() {
    return(username);
  }

  public String getHostname() {
    return(hostname);
  }
}


NetworkClient.java  Starting point for a network client to communicate with a remote computer.

import java.net.*;
import java.io.*;

/** A starting point for network clients. You'll need to
 *  override handleConnection, but in many cases connect can
 *  remain unchanged. It uses SocketUtil to simplify the
 *  creation of the PrintWriter and BufferedReader.
 *
 *  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 NetworkClient {
  protected String host;
  protected int port;

  /** Register host and port. The connection won't
   *  actually be established until you call
   *  connect.
   */

  public NetworkClient(String host, int port) {
    this.host = host;
    this.port = port;
  }

  /** Establishes the connection, then passes the socket
   *  to handleConnection.
   */

  public void connect() {
    try {
      Socket client = new Socket(host, port);
      handleConnection(client);
    } catch(UnknownHostException uhe) {
      System.out.println("Unknown host: " + host);
      uhe.printStackTrace();
    } catch(IOException ioe) {
      System.out.println("IOException: " + ioe);
      ioe.printStackTrace();
    }
  }

  /** This is the method you will override when
   *  making a network client for your task.
   *  The default version sends a single line
   *  ("Generic Network Client") to the server,
   *  reads one line of response, prints it, then exits.
   */

  protected void handleConnection(Socket client)
    throws IOException {
    PrintWriter out = SocketUtil.getWriter(client);
    BufferedReader in = SocketUtil.getReader(client);
    out.println("Generic Network Client");
    System.out.println
      ("Generic Network Client:n" +
       "Made connection to " + host +
       " and got '" + in.readLine() + "' in response");
    client.close();
  }

  /** The hostname of the server we're contacting. */

  public String getHost() {
    return(host);
  }

  /** The port connection will be made on. */

  public int getPort() {
    return(port);
  }
}


SocketUtil.java  Provides utilities for wrapping a BufferedReader  and PrintWriter around the Socket's input and output streams, respectively. 

import java.net.*;
import java.io.*;

/** A shorthand way to create BufferedReaders and
 *  PrintWriters associated with a Socket.
 *
 *  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 SocketUtil {
  /** Make a BufferedReader to get incoming data. */

  public static BufferedReader getReader(Socket s)
      throws IOException {
    return(new BufferedReader(
       new InputStreamReader(s.getInputStream())));
  }

  /** Make a PrintWriter to send outgoing data.
   *  This PrintWriter will automatically flush stream
   *  when println is called.
   */

  public static PrintWriter getWriter(Socket s)
      throws IOException {
    // Second argument of true means autoflush.
    return(new PrintWriter(s.getOutputStream(), true));
  }
}



Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10229
Categories:Programming Code Examples, Java/J2EE/J2ME, Network Programming
Tags:Java/J2EE/J2MENetwork Programming
Post Data:2017-01-02 16:04:28

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

SplitTest.java Illustrates parsing a string with a String.split #Programming Code Examples #Java/J2EE/J2ME #Network Programming


SplitTest.java  Illustrates parsing a string with a String.split


/** Prints the tokens resulting from treating the first
 *  command-line argument as the string to be tokenized
 *  and the second as the delimiter set. Uses
 *  String.split instead of StringTokenizer.
 */

public class SplitTest {
  public static void main(String[] args) {
    if (args.length == 2) {
      String input = args[0], delimiters = args[1];
      String[] tokens = args[0].split(delimiters);
      for(int i=0; i<tokens .length; i++) {
        String token = tokens[i];
        if (token.length() != 0) {
          System.out.println(token);
        }
      }
    } else {
      System.out.println
        ("Usage: java SplitTest string delimeters");
    }
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10228
Categories:Programming Code Examples, Java/J2EE/J2ME, Network Programming
Tags:Java/J2EE/J2MENetwork Programming
Post Data:2017-01-02 16:04:28

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

TokTest.java Illustrates parsing a string with a StringTokenizer. #Programming Code Examples #Java/J2EE/J2ME #Network Programming


TokTest.java  Illustrates parsing a string with a StringTokenizer. 

import java.util.StringTokenizer;

/** Prints the tokens resulting from treating the first
 *  command-line argument as the string to be tokenized
 *  and the second as the delimiter set.
 *
 *  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 TokTest {
  public static void main(String[] args) {
    if (args.length == 2) {
      String input = args[0], delimiters = args[1];
      StringTokenizer tok =
        new StringTokenizer(input, delimiters);
      while (tok.hasMoreTokens()) {
        System.out.println(tok.nextToken());
      }
    } else {
      System.out.println
        ("Usage: java TokTest string delimeters");
    }
  }
}




Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10227
Categories:Programming Code Examples, Java/J2EE/J2ME, Network Programming
Tags:Java/J2EE/J2MENetwork Programming
Post Data:2017-01-02 16:04:28

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

NetworkClientTest.java Makes a simple connection to the host and port specified on the command line. Uses the following classes: #Programming Code Examples #Java/J2EE/J2ME #Network Programming

NetworkClientTest.java  Makes a simple connection to the host and port specified on the command line. Uses the following classes:

/** Make simple connection to host and port specified.
 *
 *  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 NetworkClientTest {
  public static void main(String[] args) {
    String host = "localhost";
    int port = 8088;
    if (args.length > 0) {
      host = args[0];
    }
    if (args.length > 1) {
      port = Integer.parseInt(args[1]);
    }
    NetworkClient nwClient = new NetworkClient(host, port);
    nwClient.connect();
  }
}



NetworkClient.java  Starting point for a network client to communicate with a remote computer.

import java.net.*;
import java.io.*;

/** A starting point for network clients. You'll need to
 *  override handleConnection, but in many cases connect can
 *  remain unchanged. It uses SocketUtil to simplify the
 *  creation of the PrintWriter and BufferedReader.
 *
 *  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 NetworkClient {
  protected String host;
  protected int port;

  /** Register host and port. The connection won't
   *  actually be established until you call
   *  connect.
   */

  public NetworkClient(String host, int port) {
    this.host = host;
    this.port = port;
  }

  /** Establishes the connection, then passes the socket
   *  to handleConnection.
   */

  public void connect() {
    try {
      Socket client = new Socket(host, port);
      handleConnection(client);
    } catch(UnknownHostException uhe) {
      System.out.println("Unknown host: " + host);
      uhe.printStackTrace();
    } catch(IOException ioe) {
      System.out.println("IOException: " + ioe);
      ioe.printStackTrace();
    }
  }

  /** This is the method you will override when
   *  making a network client for your task.
   *  The default version sends a single line
   *  ("Generic Network Client") to the server,
   *  reads one line of response, prints it, then exits.
   */

  protected void handleConnection(Socket client)
    throws IOException {
    PrintWriter out = SocketUtil.getWriter(client);
    BufferedReader in = SocketUtil.getReader(client);
    out.println("Generic Network Client");
    System.out.println
      ("Generic Network Client:n" +
       "Made connection to " + host +
       " and got '" + in.readLine() + "' in response");
    client.close();
  }

  /** The hostname of the server we're contacting. */

  public String getHost() {
    return(host);
  }

  /** The port connection will be made on. */

  public int getPort() {
    return(port);
  }
}


SocketUtil.java  Provides utilities for wrapping a BufferedReader around the Socket's input stream and a PrintWriter  around the Socket's output stream

import java.net.*;
import java.io.*;

/** A shorthand way to create BufferedReaders and
 *  PrintWriters associated with a Socket.
 *
 *  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 SocketUtil {
  /** Make a BufferedReader to get incoming data. */

  public static BufferedReader getReader(Socket s)
      throws IOException {
    return(new BufferedReader(
       new InputStreamReader(s.getInputStream())));
  }

  /** Make a PrintWriter to send outgoing data.
   *  This PrintWriter will automatically flush stream
   *  when println is called.
   */

  public static PrintWriter getWriter(Socket s)
      throws IOException {
    // Second argument of true means autoflush.
    return(new PrintWriter(s.getOutputStream(), true));
  }
}

 

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10226
Categories:Programming Code Examples, Java/J2EE/J2ME, Network Programming
Tags:Java/J2EE/J2MENetwork Programming
Post Data:2017-01-02 16:04:28

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Statics.java Demonstrates static and non-static methods. #Programming Code Examples #Java/J2EE/J2ME #Object Oriented Programming

 
*/

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.");
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10396
Categories:Programming Code Examples, Java/J2EE/J2ME, Object Oriented Programming
Tags:Java/J2EE/J2MEObject Oriented Programming
Post Data:2017-01-02 16:04:39

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Example demonstrating the use of packages #Programming Code Examples #Java/J2EE/J2ME #Object Oriented Programming

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

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10395
Categories:Programming Code Examples, Java/J2EE/J2ME, Object Oriented Programming
Tags:Java/J2EE/J2MEObject Oriented Programming
Post Data:2017-01-02 16:04:39

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Example illustrating inheritance and abstract classes #Programming Code Examples #Java/J2EE/J2ME #Object Oriented Programming

***********************************
# 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<marray .length; i++) {
      total = total + mArray[i].getArea();
    }
    return(total);
  }
}
#########################
~~~~~~~~~~~~~~~~~~~~~~~~~
Polygon.java A closed Shape with straight edges; extends StraightEdgedShape and implements Measurable.
~~~~~~~~~~~~~~~~~~~~~~~~~
/** A closed Shape with straight edges. 
 *
***************************************

public abstract class Polygon extends StraightEdgedShape
                              implements Measurable {
  private int numSides;

  public int getNumSides() {
    return(numSides);
  }

  protected void setNumSides(int numSides) {
    this.numSides = numSides;
  }
}
###########################
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Rectangle.java A rectangle that satisfies the Measurable interface; extends Polygon.
~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** A rectangle implements the getArea method. This satisfies 
 *  the Measurable interface, so rectangles can be instantiated.
 *
*****************************

public class Rectangle extends Polygon {
  private double width, height;
  
  public Rectangle(int x, int y, 
                   double width, double height) {
    setNumSides(2);
    setX(x);
    setY(y);
    setWidth(width);
    setHeight(height);
  }

  public double getWidth() {
    return(width);
  }

  public void setWidth(double width) {
    this.width = width;
  }

  public double getHeight() {
    return(height);
  }

  public void setHeight(double height) {
    this.height = height;
  }

  /** Required to implement Measurable interface. */

  public double getArea() {
    return(width * height);
  }
}
##########################
~~~~~~~~~~~~~~~~~~~~~~~~~~
MeasureTest.java Driver for example.
~~~~~~~~~~~~~~~~~~~~~~~~~~
/** Test of MeasureUtil. Note that we could change the 
 *  actual classes of elements in the measurables array (as
 *  long as they implemented Measurable) without changing
 *  the rest of the code.
 ************************
public class MeasureTest {
  public static void main(String[] args) {
    Measurable[] measurables =
      { new Rectangle(0, 0, 5.0, 10.0),
        new Rectangle(0, 0, 4.0, 9.0),
        new Circle(0, 0, 4.0),
        new Circle(0, 0, 5.0) };
    System.out.print("Areas:");
    for(int i=0; i<measurables.length; i++)
      System.out.print(" " + measurables[i].getArea());
    System.out.println();
    System.out.println("Larger of 1st, 3rd: " +
       MeasureUtil.maxArea(measurables[0],
                     measurables[2]) +
                     "nTotal area: " +
                     MeasureUtil.totalArea(measurables));
  }
}
@@@@@@@@@@@@@@@@@@@@@@@@
~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10394
Categories:Programming Code Examples, Java/J2EE/J2ME, Object Oriented Programming
Tags:Java/J2EE/J2MEObject Oriented Programming
Post Data:2017-01-02 16:04:39

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Code examples for interfaces #Programming Code Examples #Java/J2EE/J2ME #Object Oriented Programming

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

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10393
Categories:Programming Code Examples, Java/J2EE/J2ME, Object Oriented Programming
Tags:Java/J2EE/J2MEObject Oriented Programming
Post Data:2017-01-02 16:04:39

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Speedboat.java Illustrates inheritance from Ship class #Programming Code Examples #Java/J2EE/J2ME #Object Oriented Programming

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

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10392
Categories:Programming Code Examples, Java/J2EE/J2ME, Object Oriented Programming
Tags:Java/J2EE/J2MEObject Oriented Programming
Post Data:2017-01-02 16:04:39

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

A Ship class illustrating object-oriented programming concepts #Programming Code Examples #Java/J2EE/J2ME #Object Oriented Programming

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

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10391
Categories:Programming Code Examples, Java/J2EE/J2ME, Object Oriented Programming
Tags:Java/J2EE/J2MEObject Oriented Programming
Post Data:2017-01-02 16:04:39

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada