• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java apprenticeship

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I rece ntly started an apprenticehsip and I am currently learning Java. I have been asked to write a public instance method moveBy() whose first argument is an int representing the distance to move and whose second argument is a char representing the direction in which to move. It should return no value.

Table 1: Directions and corresponding x and y increments

        direction xInc yInc
right  'R'        1 0
left 'L'       -1 0
up 'U'        0 -1
down'D'        0 1

   
Begin by declaring local int variables xInc and yInc and use the table above to set these for the appropriate increments depending on the supplied argument for direction.

       The method should then move the Shape (class) the required distance and direction 1 step at a time, using a loop.
 
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks straightforward enough; please show us how far you have got.
 
lilly Rankine
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I havent yet done anything as I am new to do this. But if someone can show me how to start then I will try and do it and post it back here for feedback. Thanks
 
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know how to write a method? A loop?
 
lilly Rankine
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have only done this so far which doesnt look right!

 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming that xInc would be a local int variable and would need to be declared. It is not a class variable is it?
 
lilly Rankine
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm assuming that xInc would be a local int variable and would need to be declared. It is not a class variable is it?



yes Its a local int variable and needs to be declared. How would i go about starting this quesiton. I think my method is somewhat correct but not the argument
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Describe in English what you expect this code to do.

Have you tried to compile it? After every little change you should re-compile to see if something is broken.
 
lilly Rankine
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it should move 2 frogs sat on top of each other together.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lilly Potter wrote:it should move 2 frogs sat on top of each other together.


Nothing in that line has "frog" in it.
 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frogs don't sit on top of each other round here until the end of February at the earliest.
I presume you have a book to go with your teaching. Doesn't that say how to write a method? If not, try the Java™ Tutorials.
But before you write any more code, write down with pencil and paper what will happen to your xPos and yPos variables if I write something likeShow us what you wrote on your paper. Make sure to get a decent size eraser; you will need it.
 
lilly Rankine
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Frogs don't sit on top of each other round here until the end of February at the earliest.
I presume you have a book to go with your teaching. Doesn't that say how to write a method? If not, try the Java™ Tutorials.
But before you write any more code, write down with pencil and paper what will happen to your xPos and yPos variables if I write something likeShow us what you wrote on your paper. Make sure to get a decent size eraser; you will need it.



done bewlo so far. How do I use the table above to set these for the appropriate increments depending on the supplied argument for direction.

 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lilly Potter wrote:...
done bewlo so far. How do I use the table above to set these for the appropriate increments depending on the supplied argument for direction.


Let me explain what your code does:
1. You defined a method called "moveBy". It accepts two inputs: distance and direction
2. You are defining two local variables "xInc" and "yInc" in your methods and are setting it's value to 0.

I dont think this remotely represents your original problem.


 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lilly Potter wrote:

Campbell Ritchie wrote:. . . write down with pencil and paper what will happen . . . .

done bewlo so far. . . .

That isn't pencil and paper. That is code. When I said to use pencil and paper I meant to use pencil and paper (or a text editor or WP on your computer), not code. It is not usually possible to solve such problems in code; you must have a design beforehandd.
 
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

lilly Potter wrote:

Campbell Ritchie wrote:. . . write down with pencil and paper what will happen . . . .

done bewlo so far. . . .

That isn't pencil and paper. That is code. When I said to use pencil and paper I meant to use pencil and paper (or a text editor or WP on your computer), not code. It is not usually possible to solve such problems in code; you must have a design beforehandd.



Dont listen to Campbell Ritchie he clearly doesnt know how to code, you can write up code if you wish! if i were you id find another website, with actual programmers that will actually help you! the code below is incorrect but is going in the right direction to what you need just substitute the subtle changes as mine is for a stickFigure and your question is for frogs.

  public void moveBy(int distance, char direction)
  {
     int xInc;
     int yInc;
     
     if(direction == 'R'){
        this.delay(20);
        xInc = 1;
        yInc = 0;
        xPos = xInc + distance++;
     }
     else
     if(direction == 'L'){
        xInc = 1;
        yInc = 0;
        xPos = xInc + distance++;
        this.delay(20);
     }
     else if(direction == 'U'){
        xInc = 0;
        yInc = -1;
        yPos = yInc - distance--;
        this.delay(20);
     }
     else if(direction == 'D'){
        xInc = 0;
        yInc = 1;
        yPos = yInc + distance++;
        this.delay(20);
     }
 
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John -

I'm fairly new to Java but was wondering if it make more sense to use a Switch Statement?

Thanks for any insights...
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Royale Summers wrote:John -

I'm fairly new to Java but was wondering if it make more sense to use a Switch Statement?

Thanks for any insights...



Well, yes it would. I'm also unclear on why 'R' and 'L' use the same x and y increments. Looks to me like 'R' and 'L' do the same thing. And why this.delay(20) is called before the incrementing for 'R' but after it for the others -- maybe it doesn't matter but it makes programmers ask that question, which should be unnecessary. And why the code does arithmetic on distance++ when that version of ++ is the postfix version. And for that matter why the xInc and yInc variables are even used, why not just "xPos += distance"?
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john besty wrote:Dont listen to Campbell Ritchie he clearly doesnt know how to code, ...


One of the major tenets of these forums is "Be nice."  Please don't write personally attacks.  It's fine to say, "I think it's okay to just start writing code," but it's not to throw invectives at someone.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Knute.

That's why I left Stack Overflow.  But people in general have definitely turned up the rudeness.  

It's okay to disagree but let's be respectable; just my two cents.

Also, when I first learned coding, I would jump right into 'coding' the solution while my classmate would pull out the pencil and paper and 'design' the solution.  It was definitely a hare versus the tortoise moment for me.  Each person has to decide what works for them, but I was off to the races and got stuck while the person using pencil and paper had me scratching my head as I observed his approach by beginning with psuedocode.  Long story short -- he would have his homework done after the slow start and I was up all night after the fast take-off trying to finish the assignment.

 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may be slightly above beginner level but this is how you might start to solve the problem in an object oriented fashion.

Whenever I work on something involving a grid I make an XY class to hold x & y coordinates. If movement of a coordinate is involved, I make a Direction enum with increment values.

Here's a basic XY class.
Here's a pretty basic enum for Direction with UP, DOWN, LEFT, and RIGHT enum constants.
And a little demo program
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since we're talking about objects, Here's what came to my mind..

 
lilly Rankine
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to everyone who have helped and have been nice. Much apprecited!
 
lilly Rankine
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys, please have a look at another question I have posted. I am struggling with it. Not sure if it is correct but I have attempted it! Many thanks
 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john besty wrote:. . . . Dont listen to Campbell Ritchie . . .

Nobody else listens to me

I did say, “usually”. There are some people who can keep a design in their heads but most people need to write it down, as well as using a pseudocode approach or similar. RS' experience is much more typical of how people learn.

the code below is incorrect but is going in the right direction . . . .

Please expain why you are using ++ and -- operators; I don't think that is “the right direction” at all.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic