Question with java
Results 1 to 3 of 3

Thread: Question with java

  1. #1
    Member
    Join Date
    Dec 2003
    Posts
    48

    Question with java

    My latest assignment in my java class is to write 2 class files the Set.class and the TestSet.class. In the TestSet, we are to make 2 instances of Set one with a default constructor and the other with a constructor that accepts the max value of the set. The problem i am having is naming each instance of Set. The problem is that the first set gets named corectly (Set1) but then the second gets named Set3. It looks to me that each time i create a instance of Set it exucutes both constructors not just the one it needs. Or atleast this is the only explination i can think of.

    On kind of a seperate note, one way around this i thought of is that each of two sets are named Set1 and Set2 in TestSet. So if there was a way to reference the name of the instance in the class that would be perfect. ( i was thinking something like this.Set but the compiler did not like that

    The reason i need the name is for in the AddElement and DeleteElement methods, i print that the element was added/deleted from wich ever set. Also note that according to the specifacation the only argument i can pass is the max value, so i cant just pass it the name when i create it.

    Any ideas or thoughts would help me a lot, i am relitivly new to java, and to programing in general.

    public class Set{

    int maxSize;
    int actSize;
    int temp;
    int [] array;
    String name;
    boolean hascopies = false;
    boolean ispresent = false;
    static int numberOfSets = 1;
    static int minSize = 0;

    public static void IncreaseNumber(){
    numberOfSets++;
    }

    public Set(){
    this.maxSize = 10;
    this.array = new int [ this.maxSize ];
    this.actSize = 0;
    this.name = "Set" + numberOfSets;
    Set.IncreaseNumber();
    }

    public Set(int max){
    Set.IncreaseNumber();
    this.maxSize = max;
    this.array = new int [max];
    this.actSize = 0;
    this.name = "Set" + numberOfSets;
    }


    public String MakeString(int [] s){
    int [] set = s;
    String result = "" ;

    if (this.actSize == 0) {
    result = "Null Set";
    }
    else{
    for (int i = 0; i < set.length; i++){
    if (set[i] > 0){
    result +=( this.array[i] + " ") ;
    } }

    }
    return result;

    }


    public void PrintSet(){

    System.out.println(MakeString(this.array));

    }

    public int GetActSize(){
    return actSize;
    }

    public void AddElement(int e){

    for(int i = 0; i < this.array.length; i++){
    if(e == this.array[i]){
    hascopies = true;
    }

    }
    if(hascopies){
    System.out.println("Can not add element " + e + " because it is already in " + this.name);
    }

    else if(actSize < maxSize){
    this.array[actSize] = e;
    actSize++;
    System.out.println("Element " + e + " was added successfully to " + this.name );
    if(actSize == maxSize){
    System.out.println(this.name + " is now full, can not addd more elements");
    }

    }
    else{
    System.out.println("The Set is full, can not add " + e);
    }

    }

    public void DeleteElement(int d){
    for(int i = 0; i < this.array.length; i++){
    if (d == this.array[i]){
    ispresent = true;
    }
    }

    if(ispresent == false) {
    System.out.println("Can not delete element " + d + " because it does not exists in " + this.name);
    }
    else{
    for(int i = 0; i < this.array.length; i++){
    if (d == this.array[i]){
    this.array[i] = 0;
    actSize--;
    temp = i;
    System.out.println("Element " + d + " was successfully deleted from " + this.name);
    }
    }

    }

    for(int i = temp; i < this.array.length -1 ; i++){
    this.array[i] = this.array[i+1];

    }


    }


    }

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    public Set(){
    this.maxSize = 10;
    this.array = new int [ this.maxSize ];
    this.actSize = 0;
    this.name = "Set" + numberOfSets;
    Set.IncreaseNumber();
    }

    public Set(int max){
    Set.IncreaseNumber();
    this.maxSize = max;
    this.array = new int [max];
    this.actSize = 0;
    this.name = "Set" + numberOfSets;
    }
    the problem is, let's say you instantiate one object with the no parameter constructor. You get the name set to Set1, and the counter incremented, so the counter is not 2. then you call the constructor with a parameter. That starts by incrementing the counter, which goes to 3, then you name the set and you get Set3. So all you need to do is move the "Set.IncreaseNumber();" line in the second constructor to the very end to be called after you've already named the set. Not sure if you had any other problems, as I just skimmed your post and code. If so just ask.

  3. #3
    Member
    Join Date
    Dec 2003
    Posts
    48
    No that was my only problem, and thanks for the help, i feel like an idiot now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides