Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

Study guide: Java's character and assorted string classes support text-processing

Brush up on Java terms, learn tips and cautions, review homework assignments, and read Jeff's answers to student questions

  • Print
  • Feedback

Page 2 of 2

  • What is the unnamed package?
  • 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.

  • What is the purpose of classpath?
  • classpath is an environment variable that helps the JVM's classloader locate class and jar files.

  • Create a 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.

    1. Ensure no classpath environment variable exists.
    2. Create a shapes directory.
    3. Copy the following source code into an Area.java file that appears in shapes:
      // Area.java
      package shapes;
      public interface Area
      {
         double getArea ();
      }
      
      
    4. Copy the following source code into a 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; }
      }
      
      
    5. Copy the following source code into a 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; }
      }
      
      
    6. Copy the following source code into a 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; }
      }
      
      
    7. Copy the following source code into a 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; }
      }
      
      
    8. Copy the following source code into a 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 ());
         }
      }
      
      
    9. Assuming the directory that contains 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.
    10. Move 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.


  • Print
  • Feedback