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).
I am supposed to: Write statements to printout the x-position of an GeoObj named myObj. Write statements to set the y-position my myObj to 15 Write statements to set the type description of myObj to "rectangle"
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.
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); } }
/** data members: */ Coordinate2D coord; Type2D type;
public static void main(String[] args){ GeoObj myObj = new GeoObj(); myObj.coord = new Coordinate2D(); /** Write statements to set the y-position my myObj to 15*/ myObj.setCoord(33, 15); myObj.type = new Type2D();
/** Write statements to printout the x-position of an GeoObj named myObj. */ System.out.println("x-position of an GeoObj named myObj: " + String.valueOf(myObj.getCoord().getXpos()));
/** Write statements to set the type description of myObj to "rectangle" */ myObj.type.setDescription("rectangle");
}
/** public method. */ public void setCoord(int x, int y){ if (coord == null) coord = new Coordinate2D(); coord.setXpos(x); coord.setYpos(y); }
/** public method. */ public Coordinate2D getCoord(){ return coord; } }
class Coordinate2D{ /** data members: */ int xpos =0; int ypos = 0;
/** first public method.*/ public void setXpos(int xpos){ this.xpos = xpos; }
/** second public method.*/ public int getXpos(){ return xpos; }
/** third public method.*/ public void setYpos(int ypos){ this.ypos = ypos; }
/** fourth public method.*/ public int getYpos(){ return ypos; } }
class Type2D{
String description = ""; /* No info given */
public String getDescription(){ return description; }
public void setDescription(String description){ this.description = description; } }