Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   alternative to this code? (http://www.programmingforums.org/showthread.php?t=13030)

physicist Apr 20th, 2007 10:36 PM

alternative to this code?
 
is there a way that i can get rid of having to make a class static that i declare an object of in the main method?
like i made this program to practice utilizing abstract classes
by the way, it works PERFECTLY without errors WITH the bolded code.
:

import java.util.Random;
public abstract class Ship {
     
    private String name;
    public Ship(String name2)
    {name2=name;}
    public String GetName()
    {return name;}
    public abstract int hit();
    public abstract int shoot();
   
    public static class Battleship extends Ship {
        private int square; private int firepower;
        public Battleship(int squares, int firepowers, String name)
        {super(name); square=squares; firepower=firepowers;}
        public int hit()
        {
            int i=square;
            while (i>0)
            {
                shoot();
                i--;
            }
            System.out.println("Hit "+a+" time(s) for "+g+" damage!!!");
            return 0;
        }
        public int a; public int g;
        public int shoot()
        {
            System.out.println("Salvo!");
            Random r = new Random();
            int v=r.nextInt(5)+1;
            if (v<2)
            {
                System.out.println("HIT! For "+ firepower +" damage!");
                a+=1;
                g+=firepower;
            }
            return 0;
        }
    }


 public static void main(String[] args) {
      Battleship k = new Battleship(5,5,"q");
      k.hit();
    }
}

without the bolded static, i got a classic
:

non static variable cannot be referenced from a static context
error....i know there are ways to sidestep having to make the class static in this case; i dont quite remember what they were....i know if i didnt have the abstract ship class and just battleship, and i named the file battleship.java then it would work

i heard something about making the class non public; would this do anything? i tried it and i get other errors...

titaniumdecoy Apr 21st, 2007 12:02 AM

Ship is an abstract class, and as such should not contain any method definitions, including main.

Inner classes should (for the most part) only be used when the inner class will be used only by the outer class. Moreover, an inner class should be thought of as part of its outer class; a Battleship is a Ship, not part of one. Battleship objects should be be created outside the Ship object.

You need to divide your code into three classes: Ship.java; Battleship.java; and Main.java (the naming of this last class is arbitrary), which might read:

:

public class Main {
    public static void main(String[] args) {
        Ship k = new Battleship(5, 5, "q");
        k.hit();
    }
}

EDIT: One last thing I forgot to mention: since Ship is abstract it cannot be instantiated, and should not have a constructor.

Dameon Apr 21st, 2007 12:27 PM

Quote:

Originally Posted by titaniumdecoy (Post 126971)
Ship is an abstract class, and as such should not contain any method definitions, including main.

EDIT: One last thing I forgot to mention: since Ship is abstract it cannot be instantiated, and should not have a constructor.

If Java abstract classes are anything like those in C#, having method definitions and constructors is both valid and useful. Even if it's abstract, a base class will probably still have common functionality that can be implemented. Even though you can't instantiate it, an abstract class may have constructors that can be called by the sublcass. Correct me if this is one of those few differences.

titaniumdecoy Apr 21st, 2007 2:46 PM

Quote:

Originally Posted by Dameon (Post 126975)
If Java abstract classes are anything like those in C#, having method definitions and constructors is both valid and useful. Even if it's abstract, a base class will probably still have common functionality that can be implemented. Even though you can't instantiate it, an abstract class may have constructors that can be called by the sublcass. Correct me if this is one of those few differences.

Ah, you're right. I can't believe I forgot about that. My apologies to the OP for any confusion I might have caused. :(

physicist Apr 21st, 2007 4:24 PM

yeah i understand what you said, no confusion :)
what daemon said is right and since i DID use teh constructor super(name) which uses the abstract classes constructor, it has a use.

thank you so much though titanium i realize how easy this was lol im stupid...


All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC