Hi, I wanna ask how to create 3D volume object contains x, y, z without using 3D array in Java?

What exactly do you want to accomplish?
When you say 3D volume object, you mean Graphics, or just a java class that represents a 3D Rectangular?

Not a graphic, only an array that represent 3d rectangular...
for ex for 2d array we declare : int grid[][]
then usually for 3d array we declare: int grid[][][]

but in my assignment, we are not supposed to do something like that...
so is there another way to do it??

You will not use an array. You need arrays to store data, not create objects.

You will create a class with attributes the "sizes" of the Recatngular: x ,y, z
Then you will have a constructor that as arguments 3 values and sets the above attributes: x,y,x
You will also have methods that set and get those values. Here is a link that I took from another post by stephen84s:
http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html
http://www.maths.abdn.ac.uk/~igc/tch/mx3015/notes/node50.html


You will 1, or 2 constructors, 3 get Methods, 3 set Methods and a method that returns the volume

how do i am going to use that class in the
main driver?
just create something like this:
Point3D position = new Point3D(x, y, z);
or
Point3D [][]position = new position[x][y];

This: Point3D [][]position = new position[x][y]; is only for arrays and it is wrong the way you wrote it.

You will use this to create 1 Point3D instance: Point3D position = new Point3D(x, y, z); If you want for example an array that will have Point3D objects:

Point3D [] points = new Point3D[5];
points[0] = new Point3D(1,2,3);
points[1] = new Point3D(...);
points[2] = new Point3D(...);
points[3] = new Point3D(...);
points[4] = new Point3D(...);

Also post your code just to see what you did

the Shark and OceanLife class contains Point3D.
User will be asked to input the value for x, y, and z.
Basically, i am trying to store the sharks that I have created in one variable, so that I can loop and print it out based on their position.

import java.awt.*;
import java.util.*;

public class Ocean
{
    private LinkedList<Shark> sharks;
    protected String gender[] = {"female", "male"};
    private Random rand = new Random();
    private int theX, theY, theZ;
    private OceanLife [][] position;
   
    public Ocean(int x, int y, int z, int sharkNum)
    {
        theX = x;
        theY = y;
        theZ = z;
        
        sharks = new LinkedList<Shark>();
        position = new OceanLife[theX][theY];
        
        for(int i = 0; i < sharkNum; i++)
        {   
            String theGender = getGender();
            Shark sharkTemp = new Shark(rand.nextInt(x), rand.nextInt(y), rand.nextInt(z), 20, 10, 10, theGender); 
            addShark(sharkTemp);
            position[sharkTemp.position.getX()][sharkTemp.position.getY()] = sharkTemp;
        }
        
        print();
    }
    
    public void print()
    {
        
        for(int i = 0; i < sharks.size(); i++)
        {
            System.out.println("shark num: " + i + " with x : " + sharks.get(i).position.getX() + " , y : " + sharks.get(i).position.getY() + " , z : "+ sharks.get(i).position.getZ() + ", gender: " + sharks.get(i).getGender());
        }
        
        for(int i = 0; i < theX; i++)
        {
            for(int j = 0; j < theY; j++)
            {
                if(position[i][j] instanceof Shark)
                    System.out.print("Shark ");
                else
                    System.out.print("Ocean ");
            }
            System.out.println();
        }
    }
    
    public void addShark(Shark sharkTemp)
    {
        sharks.add(sharkTemp);
    }
    
    public String getGender() 
    {
        int rnd = rand.nextInt(gender.length);
        return gender[rnd];
    }

I guess, you didn't get it. So, a 3D array is not an array of 3D coordinates, but an array that uses 3D coordinates as indexes.

If you create a 3D array, you made a structure like a book of charts: You can look up values with the page, row and column number.
If you write int grid[][][] , then each field of this book will contain a single integer.

If you make an array of 3D coordinates, you've made a structure like a long scroll with only one column, and a lot of rows. If you write point3D grid[] , then each field of this paper will contain a single integer.

Alternatively you can make a 2D array with only 3 columns for the 3 coordinates, but I personally prefer object arrays.

This article has been dead for over six months. Start a new discussion instead.