This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Java apprenticeship

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks straightforward enough; please show us how far you have got.
 
lilly Potter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
 
lilly Potter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have only done this so far which doesnt look right!

 
Carey Brown
Saloon Keeper
Posts: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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 Potter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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 Potter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should move 2 frogs sat on top of each other together.
 
Carey Brown
Saloon Keeper
Posts: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 66975
255
  • Mark post as helpful
  • send pies
  • 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 Potter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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: 2431
107
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 66975
255
  • Mark post as helpful
  • send pies
  • 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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!