Tag: Java/J2EE/J2MEObject Oriented Programming

Jan 02

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

Jan 02

Demonstrates overloading methods in class Ship4

********************* class Ship4 { public double x=0.0, y=0.0, speed=1.0, direction=0.0; public String name; // This constructor takes the parameters explicitly. public Ship4(double x, double y, double speed, double direction, String name) { this.x = x; this.y = y; this.speed = speed; this.direction = direction; this.name = name; } // This constructor requires a name but …

Continue reading

Jan 02

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

Continue reading

Jan 02

Speedboat.java Illustrates inheritance from Ship class

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

Continue reading

Jan 02

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 extends SomeClass implements Interface1 …

Continue reading

Jan 02

Example illustrating inheritance and abstract classes

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

Continue reading

Jan 02

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 static void printInfo() { System.out.println("This is Class1 …

Continue reading

Jan 02

Statics.java Demonstrates static and non-static methods.

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

Skip to toolbar