0

I have to questions according to instances and input using System.in.

First the instances:

I have created an instance variable, named woodenSword, with:

Sword woodenSword=new Sword("Wooden Sword", 2);

public Sword(String nameSword, int damageSword){
        this.nameSword=nameSword;
        this.damageSword=damageSword;
        numberOfSwords++;
}

Now, I want to access the damageSword, but how do I do this? I tried woodenSword.damageSword, but apparently that doesn't work... I thought that that was because I made the variables private, but I don't want to change that, because I read somewhere that it's better to keep variables private. (And a side question: why is it better to keep variables private?)

And another question: how can I get input with System.in? Does it have to be done with System.in.toString()?

Should I use a function for this? To get the private variables from the class, and put that function in the class? I thought about this function:

public static int getSwordStats(String nameSword){
    damageSword=nameSword.damageSword;

}

But, I'm getting an error on nameSword.damageSword, I think it doesn't understand it's a variable... How can I fix this?

Hope you can help me!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
ikhebgeenaccount
  • 353
  • 6
  • 20
  • For reading from System console refer this http://stackoverflow.com/questions/4644415/how-to-get-input-from-console-class-in-java – Bimalesh Jha Nov 17 '13 at 16:26

2 Answers2

3

It looks like your sword class is responsible for keeping track of 3 things:

  1. The name of each sword
  2. The damage of each sword
  3. The total number of swords

The first two need to be member variables, while the last needs to be a static variable.

public class Sword {
    // private (so no one can access it but Sword)
    // static (so it belongs to the class Sword and not any specific Sword)
    private static int numberOfSwords = 0; // initialize to 0
    // public accessor method
    public static int getNumberOfSwords() {
        return numberOfSwords;
    }
    // notice there's no "setNumberOfSwords" - no one can come along and change
    // our data - it's 'encapsulated' in the class

    private String name; // private
    private int damage; // private

    public Sword(String name, int damage) {
        this.name = name;
        this.damage = damage;
        numberOfSwords++; // the only place we change number of swords
    }

    // this is how people outside Sword access the name
    // note that we could add a "setName(String name)" if we want
    public String getName() {
        return name;
    }

    // same with name - protect and provide an accessor
    public int getDamage() {
        return damage;
    }
}

In a later class, you can now do this:

Sword wood = new Sword("Wooden Sword", 2);
System.out.println("wood's name is " + wood.getName());
System.out.println("wood's damage is " + wood.getDamage());
System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());

Sword mithril = new Sword ("Mithril Sword", 10);
System.out.println("mithril 's name is " + mithril .getName());
System.out.println("mithril 's damage is " + mithril .getDamage());
System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());

Which will print

Wooden Sword
2
1
Mithril Sword
10
2

For your second question, there are some great resources that I'm sure Google can help you find. As a quick example, here's what I do:

// assumes you "import java.util.Scanner"
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
    String line = sc.nextLine();
    System.out.println("You typed: " + line);
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204
1

If you need to access the damage of your sword from anywhere, then you should have a public method returning this information:

public int getDamage() {
    return this.damageSword;
}

(note that I named the method getDamage(), and not getDamageSword(). The method is in the class Sword. Putting sword everywhere is useless, only adds noise, and makes the code less readable).

Regarding your second question. yes, System.in is the standard input stream. toString() won't return what the user enters. Read the javadoc of the class to udnerstand how it works. Also read the Java IO tutorial, which has a section about the command line.

Regarding the last part, your code tries to get the damage of a String. Strings don't have a damage. Swords have a damage.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255