Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Problem reading objects (http://www.go4expert.com/forums/problem-reading-objects-t25493/)

Maniacmike3128 11Apr2011 01:19

Problem reading objects
1 Attachment(s)
My professor has given us a project and three classes that he has written. We are to impliment a 4th class, "Sprite" in order to work with the other classes. He has given us UML for the class and I have implimented this as well. (The UML picture is attached). For some reason I keep getting errors around myPicture and Picture later in the code. especially
myPicture.getX() or getY() methods.

Can someone please tell me what I am doing wrong? We don't need to change the other classes he has given us at all, he said we shouldnt even have to look at them if we dont want to. So then i dont understand why im getting those two errors in multiple places. "myPicture" and "Picture" are having issues. Can anyone point out my problem? thanks

Maniacmike3128 11Apr2011 01:19

Re: Problem reading objects
Here is my code:

import java.util.Random; //initialize random object


public class Sprite { //claim the class
private int xPos; //declare instance variable to be used throughout the class
private int yPos;
private Random positioner; //declare a random variable
private Picture myPicture; //declare a picture variable


public Sprite(Picture pict){

Random chooseValue = new Random(); //Create random object "ChooseValue"
myPicture = pict; //Copy constructor
positioner = chooseValue; //Object is randomized
myPicture.getX(); //randomly obtain an x
myPicture.getY(); //randomly obtain a y



public Sprite(Sprite other){
this.xPos = other.xPos; //Take value of xpos from other sprite
this.yPos = other.yPos; //take value of ypos from other sprite
this.positioner = new Random(); //positioner can't be copied
this.Picture = new Picture(other.Picture); //copy constructor



public String toString(){
return "Sprite - location: " + xPos + "," + yPos + " picture: " + Picture;



public boolean equals(Sprite other){
if (this==other) //if this object is the same object
return true;//its the same object
        return false;      //otherwise it's not


public int getX(){ //Obtain xPos
int chooseValue = positioner.nextInt(76); //randomly choose a number from 0-75
xPos = chooseValue; //assign xPos to that value.
return xPos; //return xPos as that value


public int getY(){ //obtain yPos
int chooseValue = positioner.nextInt(76); //randomly choose a number from 0-75
yPos = chooseValue; //assign yPos to that value
return yPos; //return yPos as that value



public Picture getPicture(){ //get picture
return Picture; //return the picture



public void setNewPos(int newX, int newY){ //set new positions for values
newX = xPos; //new xPos set
newY = yPos; //new yPos set

All times are GMT +5.5. The time now is 00:46.