• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Help with a Problem

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm having difficulty with one part of my program.
There is a myPoint Class as follows:


and a myCircle Class (unfinished)


There is also another class containing main but that is ok.

In the setCenterXY I am having difficulty incorporating the parameters x and y to give a center of type (x,y). This is a simple program not using an array.
Any help would be greatly appreciated thanks. I have spent ages trying out different things and I'm sure it is something silly but I cannot find it.

I am learning Java on my own.

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the setCenterXY I am having difficulty incorporating the parameters x and y to give a center of type (x,y).


There are two ways you can do it. You can create a new MyPoint object or you can call the setXXX() methods on an existing MyPoint object to set the X and Y values.

BTW I suggest you stick to using the Java naming conventions, it makes it so much easier for other people to read your code. For example class names start with an upper case letter so myPoint should be MyPoint
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tony,

Thanks for your help and advice. I can't use a set as I haven't got that far yet.

Creating a new myPoint object is ok but I am still clueless as to how to implement the setCircleXY using the 2 paraments of type int x and y?

circle is of type myPoint and I can create myPoint circle2 = new myPoint() but cannot see a way to complete this. But neither can be assigned to x and y as integers. That is where I am having the difficulty.

I know it's a stupid question but I am really at a loss to understand this and I am desparate.

Thanks for your time anyway. I realise I have a long way to go.

Brian
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your help and advice. I can't use a set as I haven't got that far yet.


Edit: Sorry, my reply came across as less friendly than I intended so I've edited it.

I think you may have misunderstood, I didn't say to use a Set I said to use two of the methods that are already in your MyPoint class that begin with the word 'set' and end in some value that denotes what they are setting.
For example: You have an X and a Y value that you want to use to configure the MyPoint object so what methods does MyPoint have (that begin with 'set') that might be useful for setting the X and Y values of the object?
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Tony, it's fine.

It's like learning how to walk. At the start you will go down blind alleys and make loads of mistakes. I am not that quick on the uptake either.
It is trial and error when it comes to coding to see what the result is.

I did try center.setXY(x,y) which is syntactically correct but now I need to see exactly what it does. It is more than likely wrong again :-)

There is something I am missing and reading helps but it is only when you start to code that everything falls into place (hopefully)
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is trial and error when it comes to coding to see what the result is.


Don't be tempted into the "I'll just try this and see what happens" school of coding. Always analyse the problem and come to a rational reason (within your limited understanding) as to why you are going to try X before trying it. Then try it and look carefully at what happens and see if your original thought process was correct. Randomly trying code may eventually get something working if you are really lucky but you won't know why it is working and so won't learn anything.

I did try center.setXY(x,y) which is syntactically correct but now I need to see exactly what it does.


First of all look at your MyPoint class and see what the code for that method does then add a couple of System.out.println(center) statements to your code, one before calling the setXY method and one after calling it. Run your code and see if your analysis is correct.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Tony.

Writing the MyPoint class is ok and easy to understand, but writing the MyCircle only in relation to the Set centerXY method is not because in the MyCircle method center is of type MyPoint rather than a regurlar type and although it is easy to use "center.", to access the methods in the MyPoint class I don't fully understand why the Set centerXY is coded as such:

public void setCenterXY(int x, int y){
center.setXY(x,y);
}.

Is there anything or article I could read up on to help explain this?

Thanks In Advance


 
Marshal
Posts: 80763
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is called setXY because that is what you called it in the Point class. There are all sorts of ways to write that method. You could just as easily write this…or you could implement that method in Point like this:-By the way, don't use x3 x4 etc as names for parameters to methods. There is no need to distinguish the parameters between methods. I prefer the convention whereby you use this. or you could write newX or similar.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. I will try this and play around with it to help me understand.

It is important to understand this concept as it is used for triangles, squares etc..

Brian
 
Campbell Ritchie
Marshal
Posts: 80763
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
It is an important concept because it is used for objects of all sorts.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I know that and mastering it would be a major step forward which will happen eventually.

One further question if I may?

Suppose we had a triangle rather than a circle.
Surely you could use the MyPoint class and create in the triangle class 3 variables of type MyPoint rather than passing 6 variables through MyPoint?

Thanks again in advance
 
Campbell Ritchie
Marshal
Posts: 80763
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You can create a Triangle class which has three Point objects representing the three corners.
You cannot pass six arguments to anything in your point class, however, because you don't have a constructor or method which takes six parameters. And you don't want a method with six parameters in the point class. In the Triangle class, you might have such a method, however.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Campbell, I wrote the code and sorted it and it worked so another step in the right direction.

Thanks to everyone who helped.

 
Campbell Ritchie
Marshal
Posts: 80763
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us what you have got, and well done getting it working, and “you're welcome”
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic