How to write a program using inheritance in java?

Posted on 2007-10-05
Medium Priority
Last Modified: 2008-01-09
Ok, well I must start by saying the following is a project that was assigned to me in an intermediate java programming class.  I'm just completely lost as to where I should even start on this program.  This is the first time that I will have knowingly used inheritance as well as interfaces or abstract classes.  Any sort of guidance on how to go about doing this program would be greatly appreciated.  It may have not been the best idea to cut & paste the whole assignment here, but I really wasn't sure how else to sum up what is trying to be accomplished with this problem.  Here is the assignment:

Problem Specification
Hopefully, you have at some point in your life, either played - or at least seen the game of Tetris. Where colored blocks of different shape falls slowly, then faster, and faster, while you can rotate them, with the goal of forming complete lines at the bottom of the game board. These completed lines, i.e., blocks filling every space on the lines, are then removed and gives you points. There are tons of different versions of this game out there, but there are not so many three dimensional versions, though I found one java version online, you can try it out at: http://www.3dtetris.com.  I remember playing a similar game on my Spectrum 48k1 back in the days...  Although, in this assignment, we will not develop an entire game  You are going to develop some of the components that could be used in order to make it work. In particular, you are going to create the different types of compound blocks that could be in the game, and you are also going to get a chance to implement rotation, and view point switching. Ill explain more about this later.

The Inheritance Structure
Weve talked in class about inheritance structures, and we have also found out, that there could be differences in structures, depending on who designs them. In this assignment - you have been given three java interfaces, called TwoDObject.java, ThreeDObject.java, and Viewable. The source is embedded along side the compiled versions in the assignment jar file. For each of the stages of the assignment, a new interface is added to the functionality.  Each interface contains methods that your classes must implement, and its crucial that these inclusions happen at the right place in the inheritance hierarchy, otherwise you might be writing a lot more code than what you really need. We are going to use the concept of polymorphism in this project to control (rotate) the objects through the a driver class
that you must also write.An example could look something like this:

TwoDObject block = new LShape3D ( );
block.rotate ( );

As long as the LShape block structure at some point implements the ThreeDObject interface, which in turn extends the TwoDObject, this construct will work, and hopefully rotate the three dimensional block 90 degrees clock wise.  Im not going to tell you exactly what the inheritance structure is going to look like, that is part of your assignment, but lets list some hints:
" Using an abstract class fairly high up in the structure is probably a good idea.
" Group objects that seem to fit together in the same hierarchy - can you find a good common
  denominator for the different block structures and reuse stuff that you have already written, using  

Minimum Required Work
The minimum required work for this assignment is the following:
" Two dimensional representations of the five standard Tetris block structures:

Tshape, Lshape, Ishape, Oshape, Zshape, LMshape, ZMshape

Note that each of these structures have the default view as the are shown above. According to the TwoDObject interface, these block structures can only be rotated clockwise through the rotate method. And, they can also be converted into a String representation.

" A test driver called TestDriver.java that will test to make sure that all your classes work the way they
  are supposed to. This is most easily done by creating instances of the different block structures, then  
  rotating them four times, to make sure that they return back to their original configuration.
" Over all, this should give you at least 8 java files to turn in when the time for that comes.

As the first part of the assignment is done, its now time to expand your block creation to three dimensions. This will also include rotating the blocks in three dimensions, and flattening the three dimensional shapes into two dimensions so that they can be printed out to the console window through the toString methods. This is what you must to to complete this phase of the assignment.

" Write classes for the 5 standard 2D Tetris blocks, but now in three dimensions, with the third dimension
  being only one block deep. Each of these classes must implement all the methods specified in the
  ThreeDObject interface, and can therefore be rotated freely in three dimensions.
" Create at least two more "thicker" block structures that adhere to the same interface as the regular 3D
  blocks do, but that are a bit more complicated than just one layer. Here you can let your imagination run
  free more or less, however the blocks must be connected somehow, otherwise it would be hard to
  "glue" them together. Hopefully, you will find a good use of inheritance while doing this.
" All classes must implement, either directly, or through inheritance, all methods defined in the interface.
" You must extend your TestDriver class to incorporate testing of the 3D versions of the blocks. Make  
  your tests good enough so that you can convince the TAs that your program actually work.
" In this part of the assignment you should have generated at least 7 more .java files that youll have to
  turn in once you have finished the assignment.

The cool part of the assignment has to do with being able to change viewing perspective of your blocks. In order to do this, each of your classes for the three dimensional blocks must implement the Viewable interface, and the methods that are defined therein. Read the documentation for each method in the code for the interface. You are likely to have to do some modifications to your prior files  make sure you have backup copies in case you want to start over, or go back. So through this part of the assignment, you probably wont create any more files to turn in, but you must make sure that your TestDriver also tests for changing viewpoints as we rotate our blocks.

Well, that's it, any kind of guidance as to how to go about doing this program will be greatly appreciated.
Question by:gnardizz
LVL 24

Expert Comment

ID: 20026980
I would advice you to start writing a simple program in where you extend an
existing standard class, by adding some variables and methods.
Example: write a simple program with a GUI in which you use a JTextField.
Normally you .setText() in such a field.
Now you write:

public class AmountField extends JTextField

in this there is a setText that refuses, so it blocks the inherited one.
You add a .setAmount(long amount);
that formats the amount and does a super.setText();

Work this out to get an understanding of the mechanism.


Accepted Solution

TheMajestic earned 2000 total points
ID: 20032437

i think this will help u a lot.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question