Piece Questions with Inheritance and Polymorphism

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1114 Views - Last Post: 16 May 2010 - 12:10 PM Rate Topic: -----

#1 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 06:11 AM

i am trying to extract superclass, so made createNewPiece() method to be able to instantiate it in subclass and move main() method in superclass.
but it doesn't work and throw an exception "java.lang.NoSuchMethodError: main Exception in thread "main"
What i am doing wrong here?



public class SpieceTest
{

  /**
   * The main method.
   * 
   * @param args the arguments
   * @param piece
   */
  public static void main(final String args[], final Piece piece)
  {
    String command;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    createNewPiece();
    System.out.println(piece);
    try
    {
      do
      {
        System.out.print("(C) Clockwise or (X) Counter-clockwise? ");
        command = reader.readLine();
        if (command.length() > 0)
        {
          if (command.toUpperCase().charAt(0) == 'C')
          {
            piece.rotateClockwise();
          }
          else if (command.toUpperCase().charAt(0) == 'X')
          {
            piece.rotateCounterClockwise();
          }
          System.out.println(piece);
        }
      }
      while (command.length() > 0);
    }
    catch (final IOException ioe)
    {
    }
  }

  private static void createNewPiece()
  {
    final Point[] points =
        {new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)};
    new Piece(3, points, Color.blue);

  }

}


This post has been edited by talkinrussian: 15 May 2010 - 06:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Piece Questions with Inheritance and Polymorphism

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 07:20 AM

For running the method
there should be a main method with the signature
public static void main(String [] args){
// main method code here
}

or
public static void main(String... args){
// main method code here
}

the main method you have written in the class is not the one you are expecting to run
hope this helps
Was This Post Helpful? 1
  • +
  • -

#3 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 09:59 AM

I tried to change. Now t trow another exception:
Exception in thread "main" java.lang.NullPointerException
	at java.io.Writer.write(Unknown Source)
	at java.io.PrintStream.write(Unknown Source)
	at java.io.PrintStream.print(Unknown Source)
	at java.io.PrintStream.println(Unknown Source)
	at piece.TpieceTest.main(TpieceTest.java:26)



That's what i run as a test class:


package piece;

import java.awt.Color;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TpieceTest
{

  private static char[] piece;

  /**
   * The main method.
   * 
   * @param args the arguments
   * @param piece
   */
  public static void main(final String args[])
  {
    String command;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    createNewPiece();
    System.out.println(piece);
    try
    {
      do
      {
        System.out.print("(C) Clockwise or (X) Counter-clockwise? ");
        command = reader.readLine();
        if (command.length() > 0)
        {
          if (command.toUpperCase().charAt(0) == 'C')
          {
            Piece.rotateClockwise();
          }
          else if (command.toUpperCase().charAt(0) == 'X')
          {
            Piece.rotateCounterClockwise();
          }
          System.out.println(piece);
        }
      }
      while (command.length() > 0);
    }
    catch (final IOException ioe)
    {
    }
  }

  private static void createNewPiece()
  {
    final Point[] points =
        {new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)};
    new Piece(3, points, Color.blue);

  }

}






And that is the class i am testing;
/**
 * The Class Piece.
 */
public class Piece
{

  /** The Constant MIN_SIZE. */
  public static final int MIN_SIZE = 1;

  /** The my_color. */
  protected Color my_color;

  /** The my_matrix. */
  protected static boolean[][] my_matrix;

  /** The my_size. */
  protected static int my_size;

  /**
   * Instantiates a new piece.
   * 
   * @param the_size the the_size
   * @param the_squares the the_squares
   * @param the_color the the_color
   */
  public Piece(final int the_size, final Point[] the_squares, final Color the_color)
  {
    if (the_size >= MIN_SIZE)
    {
      my_size = the_size;
    }
    else
    {
      my_size = MIN_SIZE;
    }
    my_color = the_color;
    my_matrix = new boolean[my_size][my_size];
    for (int variable_i = 0; variable_i < the_squares.length; variable_i++)
    {
      if (the_squares[variable_i].x < my_size && the_squares[variable_i].y < my_size)
      {
        my_matrix[the_squares[variable_i].x][the_squares[variable_i].y] = true;
      }
    }
  }

  /**
   * Gets the color.
   * 
   * @return the color
   */
  public Color getColor()
  {
    return my_color;
  }

  /**
   * Gets the size.
   * 
   * @return the size
   */
  public int getSize()
  {
    return my_size;
  }

  /**
   * Rotate clockwise.
   */
  public static void rotateClockwise()
  {
    final int last = my_size - 1;
    final boolean[][] newRotated = new boolean[my_size][my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        newRotated[c][r] = my_matrix[r][last - c];
      }
    }
    my_matrix = newRotated;
  }

  /**
   * Rotate counter clockwise.
   */
  public static void rotateCounterClockwise()
  {

    final int last = my_size - 1;
    final boolean[][] newRotated = new boolean[my_size][my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        newRotated[c][r] = my_matrix[last - r][c];
      }
    }
    my_matrix = newRotated;
  }

  /**
   * Square at.
   * 
   * @param the_c the the_c
   * @param the_r the the_r
   * @return true, if successful
   */
  public boolean squareAt(final int the_c, final int the_r)
  {
    return squareAt(new Point(the_c, the_r));
  }

  /**
   * Square at.
   * 
   * @param the_point the the_point
   * @return true, if successful
   */
  public boolean squareAt(final Point the_point)
  {
    if (the_point.x < my_size && the_point.y < my_size)
    {
      return my_matrix[the_point.x][the_point.y];
    }
    else
    {
      return false;
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    String str = "Color: " + my_color.toString();
    str += "; Size: " + my_size;
    str += "; State ('*' = true, '-' = false):";
    final String[] lines = new String[my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        if (c == 0)
        {
          lines[r] = "\n";
        }
        lines[r] += my_matrix[c][r] ? "*" : " ";
        if (c == my_size - 1)
        {
          lines[r] += " ";
        }
      }
    }
    for (int l = 0; l < lines.length; l++)
    {
      str += lines[l];
    }
    return str;
  }
}



Where is the mistake? Why it throw all of these exceptions?
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 10:10 AM

Because you trying to 'print' an undefined array:

...
private static char[] piece; <<<<<<<<<<<<<<<<<<<<<<<<<

public static void main(String[] args) {
    String command;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    createNewPiece();
    System.out.println(piece);  <<<<<<<<<<<<<<<<<<<<<<<<<
    try {
...


Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 10:13 AM

You really should be working through objects, not class methods like you do here. What you need to do is make a new Piece object and USE it. Make your creatNewPiece() method return that new Piece that you make:
import java.awt.Color;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TpieceTest
{

  /**
   * The main method.
   * 
   * @param args the arguments
   * @param piece
   */
  public static void main(String args[])
  {
    String command;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    Piece piece = createNewPiece();
    System.out.println(piece);
    try
    {
      do
      {
        System.out.print("(C) Clockwise or (X) Counter-clockwise? ");
        command = reader.readLine();
        if (command.length() > 0)
        {
          if (command.toUpperCase().charAt(0) == 'C')
          {
            piece.rotateClockwise();
          }
          else if (command.toUpperCase().charAt(0) == 'X')
          {
            piece.rotateCounterClockwise();
          }
          System.out.println(piece);
        }
      }
      while (command.length() > 0);
    }
    catch (final IOException ioe)
    {
    }
  }

  private static Piece createNewPiece()
  {
    final Point[] points =
        {new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)};
    return new Piece(3, points, Color.blue);

  }

}



But that also means making a couple of changes in the Piece class by making your methods non-static.
import java.awt.Color;
import java.awt.Point;

/**
 * The Class Piece.
 */
public class Piece
{

  /** The Constant MIN_SIZE. */
  public static final int MIN_SIZE = 1;

  /** The my_color. */
  protected Color my_color;

  /** The my_matrix. */
  protected boolean[][] my_matrix;

  /** The my_size. */
  protected int my_size;

  /**
   * Instantiates a new piece.
   * 
   * @param the_size the the_size
   * @param the_squares the the_squares
   * @param the_color the the_color
   */
  public Piece(final int the_size, final Point[] the_squares, final Color the_color)
  {
    if (the_size >= MIN_SIZE)
    {
      my_size = the_size;
    }
    else
    {
      my_size = MIN_SIZE;
    }
    my_color = the_color;
    my_matrix = new boolean[my_size][my_size];
    for (int variable_i = 0; variable_i < the_squares.length; variable_i++)
    {
      if (the_squares[variable_i].x < my_size && the_squares[variable_i].y < my_size)
      {
        my_matrix[the_squares[variable_i].x][the_squares[variable_i].y] = true;
      }
    }
  }

  /**
   * Gets the color.
   * 
   * @return the color
   */
  public Color getColor()
  {
    return my_color;
  }

  /**
   * Gets the size.
   * 
   * @return the size
   */
  public int getSize()
  {
    return my_size;
  }

  /**
   * Rotate clockwise.
   */
  public void rotateClockwise()
  {
    final int last = my_size - 1;
    final boolean[][] newRotated = new boolean[my_size][my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        newRotated[c][r] = my_matrix[r][last - c];
      }
    }
    my_matrix = newRotated;
  }

  /**
   * Rotate counter clockwise.
   */
  public void rotateCounterClockwise()
  {

    final int last = my_size - 1;
    final boolean[][] newRotated = new boolean[my_size][my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        newRotated[c][r] = my_matrix[last - r][c];
      }
    }
    my_matrix = newRotated;
  }

  /**
   * Square at.
   * 
   * @param the_c the the_c
   * @param the_r the the_r
   * @return true, if successful
   */
  public boolean squareAt(final int the_c, final int the_r)
  {
    return squareAt(new Point(the_c, the_r));
  }

  /**
   * Square at.
   * 
   * @param the_point the the_point
   * @return true, if successful
   */
  public boolean squareAt(final Point the_point)
  {
    if (the_point.x < my_size && the_point.y < my_size)
    {
      return my_matrix[the_point.x][the_point.y];
    }
    else
    {
      return false;
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    String str = "Color: " + my_color.toString();
    str += "; Size: " + my_size;
    str += "; State ('*' = true, '-' = false):";
    final String[] lines = new String[my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        if (c == 0)
        {
          lines[r] = "\n";
        }
        lines[r] += my_matrix[c][r] ? "*" : " ";
        if (c == my_size - 1)
        {
          lines[r] += " ";
        }
      }
    }
    for (int l = 0; l < lines.length; l++)
    {
      str += lines[l];
    }
    return str;
  }
}



See? Static methods are class-level whereas normal methods are object-level
Was This Post Helpful? 1
  • +
  • -

#6 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 07:06 PM

View PostDogstopper, on 15 May 2010 - 04:13 PM, said:

You really should be working through objects, not class methods like you do here. What you need to do is make a new Piece object and USE it. Make your creatNewPiece() method return that new Piece that you make:
import java.awt.Color;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TpieceTest
{

  /**
   * The main method.
   * 
   * @param args the arguments
   * @param piece
   */
  public static void main(String args[])
  {
    String command;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    Piece piece = createNewPiece();
    System.out.println(piece);
    try
    {
      do
      {
        System.out.print("(C) Clockwise or (X) Counter-clockwise? ");
        command = reader.readLine();
        if (command.length() > 0)
        {
          if (command.toUpperCase().charAt(0) == 'C')
          {
            piece.rotateClockwise();
          }
          else if (command.toUpperCase().charAt(0) == 'X')
          {
            piece.rotateCounterClockwise();
          }
          System.out.println(piece);
        }
      }
      while (command.length() > 0);
    }
    catch (final IOException ioe)
    {
    }
  }

  private static Piece createNewPiece()
  {
    final Point[] points =
        {new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)};
    return new Piece(3, points, Color.blue);

  }

}



But that also means making a couple of changes in the Piece class by making your methods non-static.
import java.awt.Color;
import java.awt.Point;

/**
 * The Class Piece.
 */
public class Piece
{

  /** The Constant MIN_SIZE. */
  public static final int MIN_SIZE = 1;

  /** The my_color. */
  protected Color my_color;

  /** The my_matrix. */
  protected boolean[][] my_matrix;

  /** The my_size. */
  protected int my_size;

  /**
   * Instantiates a new piece.
   * 
   * @param the_size the the_size
   * @param the_squares the the_squares
   * @param the_color the the_color
   */
  public Piece(final int the_size, final Point[] the_squares, final Color the_color)
  {
    if (the_size >= MIN_SIZE)
    {
      my_size = the_size;
    }
    else
    {
      my_size = MIN_SIZE;
    }
    my_color = the_color;
    my_matrix = new boolean[my_size][my_size];
    for (int variable_i = 0; variable_i < the_squares.length; variable_i++)
    {
      if (the_squares[variable_i].x < my_size && the_squares[variable_i].y < my_size)
      {
        my_matrix[the_squares[variable_i].x][the_squares[variable_i].y] = true;
      }
    }
  }

  /**
   * Gets the color.
   * 
   * @return the color
   */
  public Color getColor()
  {
    return my_color;
  }

  /**
   * Gets the size.
   * 
   * @return the size
   */
  public int getSize()
  {
    return my_size;
  }

  /**
   * Rotate clockwise.
   */
  public void rotateClockwise()
  {
    final int last = my_size - 1;
    final boolean[][] newRotated = new boolean[my_size][my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        newRotated[c][r] = my_matrix[r][last - c];
      }
    }
    my_matrix = newRotated;
  }

  /**
   * Rotate counter clockwise.
   */
  public void rotateCounterClockwise()
  {

    final int last = my_size - 1;
    final boolean[][] newRotated = new boolean[my_size][my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        newRotated[c][r] = my_matrix[last - r][c];
      }
    }
    my_matrix = newRotated;
  }

  /**
   * Square at.
   * 
   * @param the_c the the_c
   * @param the_r the the_r
   * @return true, if successful
   */
  public boolean squareAt(final int the_c, final int the_r)
  {
    return squareAt(new Point(the_c, the_r));
  }

  /**
   * Square at.
   * 
   * @param the_point the the_point
   * @return true, if successful
   */
  public boolean squareAt(final Point the_point)
  {
    if (the_point.x < my_size && the_point.y < my_size)
    {
      return my_matrix[the_point.x][the_point.y];
    }
    else
    {
      return false;
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    String str = "Color: " + my_color.toString();
    str += "; Size: " + my_size;
    str += "; State ('*' = true, '-' = false):";
    final String[] lines = new String[my_size];
    for (int c = 0; c < my_size; c++)
    {
      for (int r = 0; r < my_size; r++)
      {
        if (c == 0)
        {
          lines[r] = "\n";
        }
        lines[r] += my_matrix[c][r] ? "*" : " ";
        if (c == my_size - 1)
        {
          lines[r] += " ";
        }
      }
    }
    for (int l = 0; l < lines.length; l++)
    {
      str += lines[l];
    }
    return str;
  }
}



See? Static methods are class-level whereas normal methods are object-level

Was This Post Helpful? 0
  • +
  • -

#7 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 07:13 PM

Well, the usage of object rather than method fixed the test file itself, so I can run it. But now here is another problem: via creating the object createNewPiece() when I am making a superclass from several subclasses the compiler is giving an error that the method is undefined.
Here is a superclass(to avoid redundancy in several subclasses I extracted main() method):


package piece;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AbstractTest
{

  public static void main(final String args[])
  {
    String command;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    final Piece piece = createNewPiece();// Eclipse error: The method
                                         // createNewPiece() is undefined for
                                         // the type AbstractTest
    System.out.println(piece);
    try
    {
      do
      {
        System.out.print("(C) Clockwise or (X) Counter-clockwise? ");
        command = reader.readLine();
        if (command.length() > 0)
        {
          if (command.toUpperCase().charAt(0) == 'C')
          {
            piece.rotateClockwise();
          }
          else if (command.toUpperCase().charAt(0) == 'X')
          {
            piece.rotateCounterClockwise();
          }
          System.out.println(piece);
        }
      }
      while (command.length() > 0);
    }
    catch (final IOException ioe)
    {
    }
  }

  public AbstractTest()
  {
    super();
  }

}




And here is a subclass that differs only by this createNewPiece() object:

public class TpieceTest extends AbstractTest
{

  private static Piece createNewPiece()
  {
    final Point[] points =
        {new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(1, 1)};
    return new Piece(3, points, Color.blue);

  }

}



It still doesn't recognizes the object "piece" :-(
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 07:56 PM

The method is declared private so you can't access it without it being invoked from an instance of TpieceTest.

Change

private static Piece createNewPiece()



to

public static Piece createNewPiece()



Change

final Piece piece = createNewPiece();



to

// static method requires class reference
final Piece piece = TpieceTest.createNewPiece();


This post has been edited by n8wxs: 15 May 2010 - 07:58 PM

Was This Post Helpful? 1
  • +
  • -

#9 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 08:50 PM

View Postn8wxs, on 16 May 2010 - 01:56 AM, said:

The method is declared private so you can't access it without it being invoked from an instance of TpieceTest.

Change

private static Piece createNewPiece()



to

public static Piece createNewPiece()



Change

final Piece piece = createNewPiece();



to

// static method requires class reference
final Piece piece = TpieceTest.createNewPiece();


Was This Post Helpful? 0
  • +
  • -

#10 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 08:57 PM

I changed. Now no errors. But no inheritance either. Subclasses are differ only by points that mentioned in createNewPiece() for each subclass. Rest of methods are inherited from superclass, including main(). When I am running subclass as a test class, it shows only for TpieceTest class points, the same picture it show when I am running SpieceTest, IpieceTest, etc.
What is annuling this createNewPiece() for all subclasses but one, TpieceTest, and how make it work for each subclass? Is this the problem with arrays of points I created? Where to find a problem?

Here is the code for
subclass TpieceTest:
public class TpieceTest extends AbstractTest
{

  public static Piece createNewPiece()

  {
    final Point[] points =
        {new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(1, 1)};
    return new Piece(3, points, Color.blue);

  }

}



And another subclass:, SpieceTest:

package piece;

import java.awt.Color;
import java.awt.Point;

public class SpieceTest extends AbstractTest
{

  public static Piece createNewPiece()

  {
    final Point[] points =
        {new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(2, 0)};
    return new Piece(3, points, Color.yellow);

  }

}




Here is the superclass:
public static void main(final String args[])
  {
    String command;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    final Piece piece = TpieceTest.createNewPiece();

    System.out.println(piece);
    try
    {
      do
      {
        System.out.print("(C) Clockwise or (X) Counter-clockwise? ");
        command = reader.readLine();
        if (command.length() > 0)
        {
          if (command.toUpperCase().charAt(0) == 'C')
          {
            piece.rotateClockwise();
          }
          else if (command.toUpperCase().charAt(0) == 'X')
          {
            piece.rotateCounterClockwise();
          }
          System.out.println(piece);
        }
      }
      while (command.length() > 0);
    }
    catch (final IOException ioe)
    {
    }
  }




And I am planning to add 5 more subclasses.
How to make them work according their points?

Thank you in advance.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,140
  • Joined: 27-December 08

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 09:21 PM

I see that SpieceTest is packaged package piece;, but your other classes (at least TpieceTest) aren't. Given the code you provided, if you use SpeiceTest instead of TpieceTest, I don't see any reason why it shouldn't work.

Also, please stop quoting the post above you if you aren't going to add anything. :)
Was This Post Helpful? 1
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 11:24 PM

What is the point of your subclasses? From your posted code they all return a new Piece object differing only in the parameters used to create it.

createNewPiece() should be a method of Piece, with arguments to create the specific object needed: TpieceTest, SpeiceTest, etc. These objects would be declared in main() and instantiated there.

Dogstopper already pointed out these should be objects.
Was This Post Helpful? 1
  • +
  • -

#13 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 15 May 2010 - 11:42 PM

I think I lost. I am trying to create 7 objects and instantiate it in main, but there is something wrong here.
Please, give me a wink how to instantiate it in main.
public abstract class AbstractTest
{

  private static Object actual;
  private static Object[] actuals;
  private static boolean condition;
  private static boolean condition_2;
  private static Object[] expecteds;
  private static Matcher<Object> matcher;
  private static String message;
  private static String message2;
  private static Object object;
  private static Object object_2;

  public static void main(final String[] an_args) throws IOException
  {

    String command = null;
    final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    final Piece piece = AbstractTest.createNewPiece();

    System.out.println(piece);
    System.out.print("(6) Clockwise or (4) Counter-clockwise? ");
    command = reader.readLine();
    if (command.length() > 0)
    {
      if (command.toUpperCase().charAt(0) == '6')
      {
        piece.rotateClockwise();
      }
      else if (command.toUpperCase().charAt(0) == '4')
      {
        piece.rotateCounterClockwise();
      }
      System.out.println(piece);
    }

    fail();

    assertTrue(condition);

    assertFalse(message, condition_2);

    assertNull(message2, object);

    assertNotNull(message2, object_2);

    assertArrayEquals(expecteds, actuals);

    assertThat(actual, matcher);

    fail(command);

  }

  private static Piece createNewPiece()
  {
    final Piece piece = TpieceTest.createNewPiece();
    // final Piece piece = SpieceTest.createNewPiece();
  }

  public AbstractTest()
  {
    super();
  }

}


Was This Post Helpful? 0
  • +
  • -

#14 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 16 May 2010 - 12:22 AM

should I put all 7 of these objects in an array while creating?
Was This Post Helpful? 0
  • +
  • -

#15 talkinrussian  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 23-January 10

Re: Piece Questions with Inheritance and Polymorphism

Posted 16 May 2010 - 11:58 AM

.

This post has been edited by talkinrussian: 16 May 2010 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2