|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 2
The unnamed package is the package to which a source file's classes/interfaces belong when their source file lacks a package directive. From an implementation perspective, the unnamed package corresponds to whatever directory is current when you
invoke the java command.
classpath?
classpath is an environment variable that helps the JVM's classloader locate class and jar files.
shapes package with classes Point, Circle, Rectangle, and Square. Of those classes, ensure that Point is the only class not accessible outside its package. Use implementation inheritance to derive Circle from Point and Rectangle from Square. Provide an Area interface with a double getArea() method that returns the area of a Circle, a Square, or a Rectangle. Once you finish creating the package, create a TestShapes program that imports class and interface names from shapes, creates objects from shape classes, and computes the area of the shape each object represents. After compiling and running
TestShapes (successfully), move shapes to another location on your hard drive and change classpath so that a second attempt to run TestShapes results in the same output as the previous run.
Complete the following steps:
Read more about Core Java in JavaWorld's Core Java section.
classpath environment variable exists.shapes directory.Area.java file that appears in shapes:// Area.java
package shapes;
public interface Area
{
double getArea ();
}
Circle.java file that appears in shapes:// Circle.java
package shapes;
public class Circle extends Point implements Area
{
private int radius;
public Circle (int x, int y, int radius)
{
super (x, y);
this.radius = radius;
}
// Why do I need to redeclare getX () and getY ()? Hint: Comment
// out both methods and try to call them from TestShapes.
public int getX () { return super.getX (); }
public int getY () { return super.getY (); }
public int getRadius () { return radius; }
public double getArea () { return 3.14159 * radius * radius; }
}
Point.java file that appears in shapes:// Point.java
package shapes;
class Point
{
private int x, y;
Point (int x, int y)
{
this.x = x;
this.y = y;
}
int getX () { return x; }
int getY () { return y; }
}
Rectangle.java file that appears in shapes:// Rectangle.java
package shapes;
public class Rectangle extends Square
{
private int length;
public Rectangle (int width, int length)
{
super (width);
this.length = length;
}
public int getLength () { return length; }
public double getArea () { return getWidth () * length; }
}
Square.java file that appears in shapes:// Square.java
package shapes;
public class Square implements Area
{
private int width;
public Square (int width)
{
this.width = width;
}
public int getWidth () { return width; }
public double getArea () { return width * width; }
}
TestShapes.java file that appears in shapes's parent directory:// TestShapes.java
import shapes.*;
class TestShapes
{
public static void main (String [] args)
{
Area [] a = { new Circle (10, 10, 20),
new Square (5),
new Rectangle (10, 15) };
for (int i = 0; i < a.length; i++)
System.out.println (a [i].getArea ());
}
}
TestShapes.java is the current directory, execute javac TestShapes.java to compile TestShapes.java and all files in the shapes directory. Then execute java TestShapes to run this application.shapes to another directory and set classpath to refer to that directory and the current directory. For example, under Windows, move shapes \temp moves shapes into the temp directory. set classpath=\temp;. points classpath to the temp directory (just below the root directory) and current directory so java TestShapes still runs.