Skip to content
Advertisement

Object array with variable from another class

Worksheet Question:

The question said to declare an array of 5 objects of the class Node in the main Class – I managed to this as shown below

But then the question continues populate the array by objects with seqNo values assigned to 1,2,3,4,5 respectively. Then traverse the array and print the list of its object using method show()

I have an error when I am trying to show the array to the user. I am trying to display the array by this line of code:

nodObj[].show();

Below I have all the code except the Person class. Does someone have any idea if I should do a loop. When i tried if loop I got an error too. I have the display part code wrong I can’t figure out what to change

My AnyClass

import java.util.Scanner;
public class AnyClass
{
public int seqNo;

/* -----------------Constructor------------*/
public  AnyClass(int num) 
{
     seqNo = num; //initializing 
}
//empty constructor
public  AnyClass() 
{
     
}
//intialized    
public void initializeseqNo(int seqNum)
{
    seqNum = seqNo;
}
/*-----Mehtods*/
public String getData()
{return "Sequence number " +seqNo+".";
}

public String getKey()
{
    return String.valueOf(seqNo); //for search by seqNo
}

public void editData() //empty method to be overriden by future subcclasses
{
    
}

public void edit(){
    Scanner sc = new Scanner(System.in);
    seqNo = sc.nextInt();//next line for String
}

} //end of AnyClass

My Node class

public class Node
{
public AnyClass obj;

public Node(AnyClass newObj)
{ 
    obj = newObj;
}

public void show()
{
    System.out.println(obj.getData());
}
}

MainProg

class MainProg{
    public static void main (String[] args) {

        //-----------Construction of objects---------
        Person head = new Person ("Gatt", 21445667);
        Person clerk = new Person();
        clerk.name = "Delia";

        System.out.println ("Data of a new Head: " +head.getData());

        AnyClass ac1 = new AnyClass(51);
        AnyClass ac2 = new AnyClass(52);
        AnyClass ac3 = new AnyClass(53);

        ac1.getData();
        ac2.getData();
        ac3.getData();
        
        
        //edit value of ac1
        ac1.edit();
        
        //print all values again
        ac1.getData();
        ac2.getData();
        ac3.getData();

        Node n = new Node(new AnyClass(3));

        //print values
        n.show();

        Node nodObj[] = new Node[5]; //allocating memory to array
        
        //populate array
        nodObj[0] = new Node(new AnyClass(1));
        nodObj[1] = new Node(new AnyClass(2));
        nodObj[2] = new Node(new AnyClass(3));
        nodObj[3] = new Node(new AnyClass(4));
        nodObj[4] = new Node(new AnyClass(5));

        //printing array
        
        nodObj[].show(); //ERROR THIS IS WRONG!

        
    }//end of Main()
}//end of program class 



           

Advertisement

Answer

Below I have all the code except the Person class. Does someone have any idea if I should do a loop. When i tried if loop I got an error too. I have the display part code wrong I can’t figure out what to change

Yes, you need to loop over the array. At this level of instruction you should use a for or foreach loop.

for (int index = 0; index < nodObj.length; index++) {
  nodObj[index].show();
}

Or

for (Node node : nodObj) {
  node.show();
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement