Problem reading objects

Discussion in 'Java' started by Maniacmike3128, Apr 10, 2011.

  1. Maniacmike3128

    Maniacmike3128 New Member

    Joined:
    Apr 8, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     

    Attached Files:

  2. Maniacmike3128

    Maniacmike3128 New Member

    Joined:
    Apr 8, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Here is my code:
    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 
    else 
    	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 
    }}
     
    Last edited by a moderator: Apr 11, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice