Counter that will remember it's value

Posted by owca on Stack Overflow See other posts from Stack Overflow or by owca
Published on 2010-03-21T14:30:12Z Indexed on 2010/03/21 14:31 UTC
Read the original article Hit count: 399

Filed under:
|

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 it's 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
//
}

© Stack Overflow or respective owner

Related posts about java

Related posts about constants