Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class heiarchy/Inheritance issue. Please help

Status
Not open for further replies.

mighty2000

IS-IT--Management
Dec 17, 2002
4
US
Hey all!!

I am new to Java. I am supposed to create 3 classes under one project/package, and I am to access thoes classes using the specific methods and data members that my wacked out instructor gave me.

Here is the question: (I am only going to show the part that I am having trouble with)

Create the following classes:

Class GeoObj
data members:
Coordinate2D coord;
Type2D type;

public methods:
public void setCoord
public Coordinate2D getCoord

I know how to go between the main method and interface the methods in a class using any object that I create. But how do you write a Method like the two above that actually go into another class and get and set what the Coordinate2D class is getting and setting? I am only allowed to use one object called myObj to access all of the other classes including the Coordinate2D class.

By the way the Coordinate2D class:
data members:
int xpos, int ypos;
public methods:
public void setXpos;
public int getXpos;
public void setYpos;
public int getXpos;

I have no trouble with this class because I tested it with new objects in the main method(basically bypassing the GeoObj class methods).

anyway, here is my code sample from the GeoObj class.
It is incomplete in the main method, because that is where I am stuck. I dont know if I need to change the main method pointers or to change the get and set methods in the GeoObj class.

Please Help if you can...

Thanks.

Code:
package geoproject;

public class GeoObj
{
  Coordinate2D coord;
  Type2D type;
public void setCoord()
  {
    this.coord=coord;
  }
public Coordinate2D getCoord()
  {
    return coord;
  }
public void setType()
  {
    this.type=type;
  }
public Type2D getType()
  {
    return this.getType();
  }
public static void main(String[] args)
  {
    GeoObj myObj = new GeoObj();
    myObj.setCoord();
    System.out.println("The X position is: " + myObj.getCoord());
    myObj.getCoord();
    myObj.getType().setType(1);
  }
}
 
I think you should go back to basics. Your "set" methods for a start, must receive a parameter, e.g.

public void setType(Type2D t) {
this.type = t;
// NB. "this." is not necessary here as the passed parameter is a different name than the instance variable.
}


and consequently the "get" methods simply return what has been stored.

public Type2D getType() {
return this.type // "this." is not necessary here
}
 
Thanks for your reply kobbler, I appreciate it.

Like I said, I am new to Java :)

I'll give it a shot.

Thanks again bro.

 
well, It turns out that I have already tried that. I was confused as what to pass into the main method for the
Code:
myObj.setCoord();
statement.

dont I need another object to reference if I pass it into the method like that?

thanks again
 
You'll have to create an instance of Coordinate2D object to pass to the setCoord() method og the geoObj. Before sending this object you can use it's set methods to set X, Y etc..

These values could be arguments to the "main" method, but for simple testing could be hardwired in the main method.

e.g.

int x = 100;
int y = 50;


Coordinate2D coord = new Coordinate2D();
coord.setXpos(x);
coord.setYpos(y);

GeoObj geoObj = new GeoObj();
geoObj.setCoord(coord);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top