-2

This is my first Java project.

So I'm working on my own simulation project, and some of my core stuff has gone awry. I have two classes I'm focusing on right now - settlement and townRey, which extends settlement.

The error is thrown when I try

System.out.println(townRey.returnStrength());

Here are my two relevant classes:

settlement:

public class settlement
{
    //
    //
    // VARIABLES
    //
    //

    /**
     * The town's unique name.
     */
    public String name;

    /**
     * The settlement's location in latitude (N-S)
     */
    public int latitude;

    /**
     * The settlement's location in longitude (E-W)
     */
    public int longitude;

    /**
     * What faction a town or village is aligned to. This determines production and consumption, mostly.
     */
    public String faction;

    /**
     * What a specific village or town produces.
     */
    public String[] production;

    /**
     * What a specific town consumes (villages don't consume)
     */
    public String[] consumption;

    /**
     * How dangerous a specific town is with bandits
     * A 1-10 scale, with 10 being the most dangerous.
     * Any town with a danger over 8 can be raided and destroyed temporarily by bandits.
     * Being raided successfully depends on the Strength of a town.
     */
    public int danger;

    /**
     * How much a town takes in taxes.
     */
    public float tax;

    /**
     * How easily a town is raided by bandits.
     * If a bandit raid has a lower strength than the town, then the town wins.
     */
    public int strength;

    //
    //
    // METHODS
    //
    //

    public int returnLatitude() 
    {
        return latitude;
    }

    public int returnLongitude()
    {
        return longitude;
    }

    public String returnFaction()
    {
        return faction;
    }

    public String[] returnProduction()
    {
        return production;
    }

    public String[] returnConsumption()
    {
        return consumption;
    }

    public int returnDanger()
    {
        return danger;
    }

    public float returnTax()
    {
        return tax;
    }

    public int returnStrength()
    {
        return strength;
    }
}

and townRey:

public class townRey extends settlement
{{
    name = "Rey";
    latitude = 5;
    longitude = 5;
    String faction = "Nord";
    String[] production;
    String[] consumption;
    danger = 1;
    tax = 0.05F;
    strength = 6;
}}

EDIT:: Thanks for all the help! I fixed all issues now. Below is 'Settlement' and 'Start'.

public class Settlement
{
    //
    //
    // VARIABLES
    //
    //

    /**
     * The town's unique name.
     */
    public String name;

    /**
     * The settlement's location in latitude (N-S)
     */
    public int latitude;

    /**
     * The settlement's location in longitude (E-W)
     */
    public int longitude;

    /**
     * What faction a town or village is aligned to. This determines production and consumption, mostly.
     */
    public String faction;

    /**
     * What a specific village or town produces.
     */
    public String[] production;

    /**
     * What a specific town consumes (villages don't consume)
     */
    public String[] consumption;

    /**
     * How dangerous a specific town is with bandits
     * A 1-10 scale, with 10 being the most dangerous.
     * Any town with a danger over 8 can be raided and destroyed temporarily by bandits.
     * Being raided successfully depends on the Strength of a town.
     */
    public int danger;

    /**
     * How much a town takes in taxes.
     */
    public float tax;

    /**
     * How easily a town is raided by bandits.
     * If a bandit raid has a lower strength than the town, then the town wins.
     */
    public int strength;

    //
    //
    // METHODS
    //
    //

    public int returnLatitude() 
    {
        return latitude;
    }

    public int returnLongitude()
    {
        return longitude;
    }

    public String returnFaction()
    {
        return faction;
    }

    public String[] returnProduction()
    {
        return production;
    }

    public String[] returnConsumption()
    {
        return consumption;
    }

    public int returnDanger()
    {
        return danger;
    }

    public float returnTax()
    {
        return tax;
    }

    public int returnStrength()
    {
        return strength;
    }
}

and Start, where I create 'townRey' then access a bit of data in two different ways.

public class Start 
{
    public static void main(String[] args) 
    {
        //Creates 'Rey'
        Settlement townRey = new Settlement();
        townRey.name = "Rey";
        townRey.latitude = 5;
        townRey.longitude = 5;
        townRey.faction = "Nord";
        townRey.danger = 1;
        townRey.tax = 0.05F;
        townRey.strength = 6;

        //This calls the returnLongitude method from Settlement, and is the 'proper' way to do it.
        System.out.println(townRey.returnLongitude());

        //This also works.
        System.out.println(townRey.longitude);

        //Thanks for the help!
    }
}
Trent
  • 81
  • 1
  • 1
  • 7
  • 6
    *"This is my first Java project."* - Please DO NOT start Java class names with a lower case letter. Not ever! Learn this lesson now. – Stephen C Aug 21 '13 at 02:38
  • While I've heard this often and go with it, because it is the prevailing style, I'm curious to know why it is that this topic inspires so much fury among people. What would be wrong with the standard of having classes start with lowercase and variables with uppercase, for example? – qaphla Aug 21 '13 at 02:48
  • 1
    @qaphla There would be nothing wrong with it if that were the standard. The standard is arbitrary but pervasive, and using the wrong capitalization is like misspelling: A reader can usually figure out what you meant, but it takes a lot more effort that would be better spent solving the actual problem. – chrylis -cautiouslyoptimistic- Aug 21 '13 at 02:51
  • I'm not sure whether this is a bad copy-and-paste or a bug the OP hasn't mentioned, but the current code for `townRey` (with the double braces `{{`) has everything in an initializer block, which almost certainly isn't what's intended, given the array declarations. – chrylis -cautiouslyoptimistic- Aug 21 '13 at 02:53
  • I would *highly* suggest starting with a beginner's book on Java or the Oracle tutorials. Randomly typing things into your computer and expecting them to work generally isn't going to produce the results you seek. – Brian Roach Aug 21 '13 at 03:05
  • Thanks for the help. I have read the Oracle tutorials, but it's been a while and I haven't yet nailed down certain stuff. >.> Beginner's problems. I edited the post with the solutions I had. @chrylis, it was an error (my source wasn't that.) – Trent Aug 21 '13 at 04:32

5 Answers5

0

townRey shouldn't be extending settlement. You should be declaring it as an instance of settlement in some method, as follows:

townRey = new settlement();
townRey.name = "Rey";
...
townRey.strength = 6;

Or, better still, making a new constructor for settlement that takes the different fields as inputs.

Also, a style note: Generally, in Java, classes should begin with a capital letter, so Settlement rather than settlement might make a better name.

qaphla
  • 4,707
  • 3
  • 20
  • 31
0

You should define a townRey object then use this object to call returnStrength

townRey mytownRey = new townRey();
System.out.println(townRey.returnStrength());
ningyuwhut
  • 609
  • 12
  • 27
0

I expect you want townRey to be an instance of settlement, not a subclass. Unless you want to have multiple copies of townRey. Replace the line public class townRey extends settlement with settlement townRey = new settlement(), and add a semicolon after }}. Leave everything else the same.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0
public class mainclss()
{
public static void main(String arg[])
{
townRey= new settlement();
//you can do sth you like
}  
}

create a new class to check.DO NOT start Java with Class!It is a little difficult.

PengWu
  • 65
  • 5
0

Create a separate class with main() method. Inside this method, you should create an object of townRey, in order to access the method returnStrength(). You can't access it using the class name 'townRay' if you are doing so. So Add this class with the code below:

public class MainClass {

    public static void main(String[] args) {

        townRey  tr = new townRey();
        System.out.println( tr.returnStrength () );


    }

}

This worked fine with me. So you can safely use it.

NOTE: You should learn by practice to start each word in your class name with a capital letter such as Settlement and TownRey. Good Luck!

Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83