• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Moth.java program  RSS feed

 
Jonathan Squires
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implement a class Moth that models a moth flying across a straight line. The moth has a position, the distance from a fixed origin. When the moth moves toward a point of light, its new position is halfway between its old position and the position of the light source. Supply a constructor
public Moth(double initialPosition)
and methods
public void moveToLight(double lightPosition)
public double getPosition()
Your MothTester should construct a moth, move it toward a couple of light sources, and check that the moth's position is as expected.

You need to supply the following class in your solution:

Moth

Use the following class as your tester class:


This is the problem I have and here is what I have done so far:


 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it is
(moth - position) * 0.5?
Shouldn’t there be a + in it somewhere?

You have lots of code you don’t need. You only need one position field. The move method changes that field, and the getPosition field returns it. You can reduce your move method to one line. You see in that method you are setting three things to the same value and discarding one of them. Why do you want two copies of the same value, unless you need to record a previous position? It’s like a lot of beginners’ code: I can delete half of it and it doesn’t make any difference
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!