Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2007, 9:36 PM   #1
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
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...
physicist is offline   Reply With Quote
Old Apr 20th, 2007, 11:02 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.

Last edited by titaniumdecoy; Apr 20th, 2007 at 11:44 PM.
titaniumdecoy is offline   Reply With Quote
Old Apr 21st, 2007, 11:27 AM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Quote:
Originally Posted by titaniumdecoy View Post
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Apr 21st, 2007, 1:46 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 841
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by Dameon View Post
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.
titaniumdecoy is offline   Reply With Quote
Old Apr 21st, 2007, 3:24 PM   #5
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
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...
physicist is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
EXECryptor software protection Jean5 C++ 35 Oct 10th, 2006 7:10 PM
Little help whoawhoayoyo Assembly 8 Apr 18th, 2006 7:10 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 11:53 AM
How to post a question nnxion C++ 0 Jun 3rd, 2005 8:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 8:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:41 AM.

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