tags:

views:

107

answers:

5

I have a task to operate on complex number. Each number consists of double r = real part, double i = imaginary part and String name. Name must be set within constructor, so I've created int counter, then I'm sending its value to setNextName function and get name letter back. Unfortunately incrementing this 'counter' value works only within costructor and then it is once again set to 0. How to deal with that?Some constant value? And second problem is that I also need to provide setNextNames(char c) function that will change the counter current value.

The code :

public class Imaginary {

private double re;
private double im;
private String real;
private String imaginary;
private String name;
private int counter=0;

public Imaginary(double r, double u){
    re = r;
    im = u;
    name = this.setNextName(counter);
    counter++;
}

public static String setNextName(int c){

    String nameTab[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N",
                        "O","P","Q","R","S","T","U","W","V","X","Y","Z"};

    String setName = nameTab[c];
    System.out.println("c: "+c);
    return setName;
}

public static String setNextName(char c){

//
//don't know how to deal with this part
//
}
+4  A: 

It's hard to tell what you're doing, but I suspect this will solve your immediate problem:

private static int counter = 0;
Pointy
I wish there was only python... :) thanks
owca
@owca: Why would you want to live in such a limited universe?! No matter how good any given language is, I wouldn't want just *one* language. Or flavor of ice cream. Or style of music. Or...or...or... ;-)
T.J. Crowder
@TJC: well said :)
codaddict
+2  A: 

You should make counter static.

You should also make nameTab a private static field, then in setNextName(), you can iterate through it to find the name corresponding to the given character, and get its index. (in the plain ASCII world, of course one could simply calculate the index by subtracting the numeric value of 'A' from the given character, but I am not quite sure how it would work out with Java, in Unicode, with crazy inputs - iteration is on the safe side.)

Péter Török
A: 

Because the field counter is not static, every object has its own counter.

meriton
+1  A: 

In OO languages there are typically two types of variables that go into a class:

  • instance variables that are unique to each instance
  • class variables that are shared by all instances of the class

Given a class like:

public class Person
{
    // class variable
    private static int numberOfEyes;

    // instance variable
    private String name;

    // other code goes here
}

If you were to do something like:

Person a = new Person("Jane Doe");
Person b = new Person("John Doe");

and then do something like:

a.setName("Jane Foe");

the name for Person "a" would change, but the one for Person "b" would stay the same.

If you woke up one morning and decided you wanted 3 eyes:

Person.setNumberOfEyes(3);

then Person "a" and Person "b" and every other Person instance out there would suddenly have 3 eyes as well.

You want to put "static" in your counter declaration.

TofuBeer
+1  A: 

is your code being used by multiple threads than i would suggest that making counter static won't solve ur problem.

you need to take extra care by implementing thread synchronization use lock keyword as shown below.

private static readonly obj = new Object();
private static int counter =0;

public Imaginary(double r, double u)
{ 
    re = r; 
    im = u; 
    lock(obj)
    {
        name = this.setNextName(counter); 
        counter++; 
    }
}

this will ensure thread safety also while incrementing your counter (there are another ways also to provide thread security but this one is having least code).

Java does not have readonly and lock... I think you are using C# in your example.
TofuBeer