Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: creating a package

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation creating a package

    So, Continuing from my last homework, I now have to make a package called games.

    A package named games. In the package are these types (a type is a class or interface):
    A class named Game. This is an abstract class that can be extended to create classes for specific games that are played on a square board, like checkers, chess, and tic-tac-toe. The Game class has one instance variable that is an object in the Board class. Its methods should be public, but its instance of Board should be default. If it has any other instance variables, they should be private.
    An interface named Moves. It specifies the methods that are to be implemented by the Board class.
    A class named Board that implements Moves. You should be able to use the Board class from the previous assignment with some changes. The Board class and its methods are public, but its variables should be private. In particular, it has an nXn array that represents the game board, and it must be private. The main modifications that you have to make are to allow game pieces of different kinds. For example, in checkers, pieces can be red or black, and they can be regular checkers pieces or kings, giving a total of four kinds of pieces. (You don't need to know what these mean to do this assignment.) Each kind of piece is identified by a unique code. Squares on the game board can be unoccupied or occupied by a single piece of some kind. Use the following codes to indicate the occupancy of a square:
    0: unoccupied
    1: red checker
    2: black checker
    3: red king
    4: black king
    A class named Checkers, which must be a direct subclass of Game. The Checkers class and its methods should be public, but its variables should be private.
    A class named TestCheckers containing a main program that thoroughly tests the Checkers and Board classes. TestCheckers should not be in the package games.
    The classes Game, Board and Checkers must satisfy the interfaces given below; that is, you are not free to change the name, type, or behavior of the given methods.

    I'm pretty sure I can do the interfaces and whatnot myself. Just let me clear this up, Interfaces are just methods in an Interface right? They don't actually have a definition do they? It's just an empty method..


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: creating a package

    An interface is declared with the interface keyword instead of the class keyword. The basically define that for every class/object which implements that interface must have those methods defined. They cannot be defined in the interface.

    A simple example:

    public interface MyInterface
    {
        public void doit();
    }

    public class MyClass implements MyInterface
    {
        public void doit()
        {
            System.out.println("Hello world!");
        }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    x3rubiachica3x (September 26th, 2010)

  4. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: creating a package

    Whats the point of interfaces, couldn't I just create a class that has those methods?

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: creating a package

    Quote Originally Posted by Brt93yoda View Post
    Whats the point of interfaces, couldn't I just create a class that has those methods?
    Yes you could, but interfaces allow you to do so much more, and in my view are one of the more powerful but difficult to understand aspects in java. With interfaces you can loosely couple your code through abstraction (as opposed to tightly coupling it - making everything depend upon everything else and reducing the re-usability of the code). Loosely coupling your code has advantages in many ways, from helping code be re-usable, to facilitate making changes in a complex program easier down the line, to making an algorithm easier to write and test. One great example of abstracting an algorithm is the Collections.sort method - if the parameter was hardcoded to an object every object in a List must be of a particular class or extend from that class for it to be sorted. An interface gets around this allowing any object to be sorted as long as it implements the Comparable interface.

  6. The Following User Says Thank You to copeg For This Useful Post:

    Brt93yoda (September 22nd, 2010)

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: creating a package

    In Java you are only allowed to inherit from one class (abstract or not). However, you are allowed to inherit as many interfaces as you please.

    For example:

    Say I have a class called animal. I can create two interfaces, PlantEater and MeatEater. Now an omnivore can eat both plants and meat, so they would implement both interfaces.

    public abstract class Animal
    {
        public abstract void eat();
    }
    public interface PlantEater
    {
        public abstract void graze();
    }
    public interface MeatEater
    {
        public abstract void hunt();
    }

    Now, say I had an animal Bear. It can extend animal as well as implement MeatEater and PlantEater

    public class Bear extends Animal implements MeatEater, PlantEater
    {
        public void eat()
        {
            // ... eat code
        }
        public void graze()
        {
            // ... graze code
        }
        public void hunt()
        {
            // ... hunt code
        }
    }

    Then when I want to manage a list of animals, I can manage this bear as both a herbivore and as a carnivore.

    ArrayList<PlantEater> herbivores = new ArrayList<PlantEater>();
    Bear myBear = new Bear();
    ArrayList<MeatEater> carnivores = new ArrayList<MeatEater>();
    herbivores.add(myBear);
    carnivores.add(myBear);
    if(plantsAvailable)
    {
        // all herbivores graze
        for(PlantEater herbivore:herbivores)
        {
            herbivore.graze();
        }
    }
    if(meatAvailable)
    {
        for(MeatEater carnivore:carnivores)
        {
            carnivore.hunt();
        }
    }

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    Brt93yoda (September 22nd, 2010)

  9. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: creating a package

    Thanks for the help. I still only get it about %50. It seems like it will be very powerful once it finally clicks.

  10. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: creating a package

    Thanks for clearing up interfaces, I kinda get them. When I create a package, do I need to have all my classes, and interfaces and stuff in separate files?

Similar Threads

  1. Accessing Package from a Different Directory
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2010, 11:39 PM
  2. How to instal JDBC driver package in eclipse
    By sathish in forum JDBC & Databases
    Replies: 2
    Last Post: September 2nd, 2010, 10:09 AM
  3. [SOLVED] Finding acm package
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2010, 11:50 AM
  4. "showMessageDialog" method in swing package
    By Delmi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 02:52 PM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM