2

I have an issue with the following exercise (I’m using BlueJ): They asked me to make the following method in the class House without making any change to the class Circle

  • name : getPositionsTwoSuns
  • return value : the String described below
    description of the string : returns the string of the form "x=12, y=7" from the coordinates of aXPosition and aYPosition of each suns of the picture ;
    To separate the positions of the two suns, we can use "|" for example.

Here is a copy of the class House :

public class House
{
private Square    aWall;
private Square    aWindow;
private Triangle aRoof ;
private Circle   aSun;
private Circle   aSun2;

/**
 * Constructor for objects of class House
 */
public House()
{
    this.aWall= new Square();
    this.aWall.changeSize( 100 );
    this.aWindow= new Square();
    this.aWindow.changeColour( "black" );
    this.aRoof= new Triangle();  
    this.aRoof.changeSize( 140, 50 );
    this.aSun= new Circle();
    this.aSun.changeColour( "yellow" );
    this.aSun.changeSize( 60 );
    this.aSun2= new Circle();
    this.aSun2.changeColour( "green" );
    this.aSun2.changeSize( 60 );
    this.aWall.MakeVisible();
    this.aWindow.MakeVisible();
    this.aRoof.MakeVisible();
    this.aSun.MakeVisible();
    this.aSun2.MakeVisible();
    
} // Picture()

/**
 * New function
 */
public String getPositionsTwoSuns()
{
    return "x=aSun.aXPosition y=aSun.aYPosition | x=aSun2.aXPosition y=aSun2.aYPosition";
}

/**
 * Draw this picture.
 */
public void draw()
{
    this.aWall.MakeVisible();
    this.aWindow.MakeVisible();
    this.aRoof.MakeVisible();
    this.aSun.MakeVisible();
    this.aSun2.MakeVisible();
} // draw()    
} // House

Here is the Circle class :

import java.awt.geom.Ellipse2D;
public class Circle
{
private int     aDiameter;    
private int     aXPosition;
private int     aYPosition;
private String  aColour;
private boolean aIsVisible;

/**
 * Makes a new Circle
 */
public Circle()
{
    this.aDiameter=  30;
    this.aXPosition= 20;
    this.aYPosition= 60;
    this.aColour=   "blue";
    this.aIsVisible= false;
} // Circle()

/**
 * Makes a new circle with the default values
 */
public Circle( final int pDiameter, final int pXPosition, final int pYPosition, final String pColour )
{
    this.aDiameter=  pDiameter;
    this.aXPosition= pXPosition;
    this.aYPosition= pYPosition;
    this.aColour=   pColour;
    this.aIsVisible= false;
} // Circle()

/**
 * Make the Circle visible.
 */
public void MakeVisible()
{
    this.aIsVisible= true;
    this.draw();
} // MakeVisible()
/**
 * New function
 */
public int getPosition()
{
    return 1000*aXPosition+aYPosition;
}
}

I unsuccessfully tried to make the method but once it’s compiled, and after creating a new object called house1, the method returns me this :

"x=aSun.aXPosition y=aSun.aYPosition | x=aSun2.aXPosition y=aSun2.aYPosition";

Any help would be appreciated

Abra
  • 19,142
  • 7
  • 29
  • 41
user3368757
  • 51
  • 1
  • 2
  • Well, given that your method does `return "x=aSun.aXPosition y=aSun.aYPosition | x=aSun2.aXPosition y=aSun2.aYPosition"`, it's not really suprising. You need to concatenate literal strings (like `"x="`) with variable values. The Java concatenation operator is `+`. – JB Nizet Mar 01 '14 at 17:48
  • Thanks for your answer, but i just did not get how to concatenate, can you explain it just by giving an example ? – user3368757 Mar 01 '14 at 18:32
  • Google is your friend. Here is what I get when googling for "concatenate in Java": http://stackoverflow.com/questions/3753869/how-do-i-concatenate-two-strings-in-java. Is it really so hard to use google? – JB Nizet Mar 01 '14 at 18:34
  • Thank you. The only problem now is that i'm getting this : aXPosition has private access in Circle ; I understood that I have to use the getPosition() method created in Circle but I didnt get how ? – user3368757 Mar 01 '14 at 18:55
  • `aSun.getPosition()` allows calling the `getPosition()` method on the object referenced by the variable `aSun`. Don't you have a text book? – JB Nizet Mar 01 '14 at 18:59

1 Answers1

0

You can use private for all the methods as well as variables and then call the method by creating an object. An object in Java can be created like Circle run = new Circle(); and then call all the private methods like: run.Circle(); run.MakeVisible(); The object must have the class name and the syntax will be like:

ClassName ObjectName = new ClassName(); and for calling the method the syntax is: Objectname.MethodName();

Make sure the object name are correct for method calling also. Same object can be used for calling multiple methods. You have to call methods for all methods with the object and it must be placed in:
public static void main() 
{ 
//object creation and all your method calls here
}

Or more specifically the code in main will be like

public static void main()
{
    Circle run = new Circle();
    run.Circle();
    run.MakeVisible();
}

To prevent confusion it is better to change the method name from Circle to another name as the class also has the same name.
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40