• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with a Method moveToLocation (Vehicle Class)

 
Ranch Hand
Posts: 41
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

First post, so please be kind!

I could use some guidance in how to create this method for moveToLocation in line 63 of this Class. The updateMileage I think I can figure out, but the moveToLocation seems positively diabolical...

Any assistance would be greatly appreciated!

Also, if anything here makes you want to gouge your eyes out, please let me know so I don't show my mentor before I fix it! lol


 
Aaron McCarthy
Ranch Hand
Posts: 41
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please let me know if I have failed to provide any pertinent info as to the point of this exercise, what I'm actually trying to accomplish, etc... Thanks!
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your moveForward() I recommend adding two member variables (fields): longitudeIncrement, and lattitudeIncrement. You can initialize them to default values. I assume with lat/long being doubles and that you have some concept of speed that you may want to vary the increment during the course of running the program. You can also change the sign of each independently so that you can move up/down and left/right. You can also adjust increments to move at an arbitrary angle using trigonometry.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is no name conflict (e.g. "name" is different than "n") so you don't need "this". However, I'd suggest leaving "this" and follow a typical convention and use "name" instead of "n".
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aaron McCarthy wrote:. . .

Welcome to the Ranch

You have made a common mistake in those assignments. Don't combine ++ or -- and assignment. Use ++ or -- on their own. If you don't know why, try running this sort of code:-Does increasing the latitude actually correspond to turning right or moving right? I don't think the names of those four methods above correspond to their intent.
Have you come across the idiom this(x, y, z); yet? You can only use it in the first statement in a constructor? It calls a different constructor. So you can shorten this sort of code:-to this:-Why are you using @SuppressWarnings("unused")? “Ecipse told me to,” only starts to answer the question. Only use @SuppressWarnings if you are sure the code is correct despite what Eclipse tells you. What are you doing with the inner class? Are you going to write a getLocation() method? In which case it will no longer be unused. But I am not convinced that simply returning a Location object will give the correct current location. Test this out, but I think you will end up returning the original location regardless. Also, do you really want an inner class?
Why have you got so many constructors? What will happen if you call the constructor on line 17 and then try to maintain the speed of the car so as not to exceed its maximum speed?
 
Aaron McCarthy
Ranch Hand
Posts: 41
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all so much for the help! It's immensely appreciated!
 
Aaron McCarthy
Ranch Hand
Posts: 41
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the actual assignment as well for additional context.

I'm open to suggestions regarding anything else I'm obviously butchering here... lol


Create a Vehicle class, it should have the following attributes (you should define what the types the attributes should be - except for the Location attribute see below):

- Name
- Color
- Max Speed
- Mileage
- Type (values can be: auto, truck, motorcycle)
- Location (this represents a class called Location which has 2 points: longitude & latitude)

- Make sure you define 3 constructors:
1) A constructor that takes the name, color, type
2) A constructor that takes name, color, type, max speed
3) A constructor that takes name, color, max speed, mileage, type

You should also create the following methods

1) moveForward - doesn't take any parameters but updates the Location of the Vehicle to be the x+1 and y+1)

2) moveBackward - doesn't take any parameters but updates the Location of the Vehicle to be the x-1 and y-1)

3) moveToLocation - takes a location and set the vehicles Location to be the Location that was passed

4) updateMileage - take mileage as a parameter and updates the Vehicles mileage to what was passed

Once you have the above and everything compiles, create another class called GritSeedVehicles which has a main method (so it can be run)

In the GritSeedVehicles, create 4 to 8 different types of vehicles, call all the different constructors and methods and print the values of the car Location, mileage as you call the methods etc.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aaron McCarthy wrote:. . .  Location (this represents a class called Location which has 2 points: longitude & latitude)

It doesn't say it has to be an inner class.

- Make sure you define 3 constructors:
. . .
You should also create the following methods
. . .

I don't like assignments so prescriptive; I prefer to allow the students to work out things for themselves. If you follow the lat and long values through the execution of the program, youi will find all sorts of weird and wonderful things happening, which wouldn't if your constructor set default values.

GritSeedVehicles . . .

Anybody who can think us that sort of name for a class can be forgiven everything.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing to stop you setting the default values for lat and long to your current location in the other constructors, at least I think there isn't. Have a word with your instructor.
 
Aaron McCarthy
Ranch Hand
Posts: 41
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if I move Location to a separate class altogether, won't I need to make the latitude and longitude public, rather than private so members of the vehicle class can get/set these values?

I feel like I'm supposed to avoid making them public from the standpoint of best practice...

Thank you all again so much! I'm so glad to be here!


 
Aaron McCarthy
Ranch Hand
Posts: 41
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There is nothing to stop you setting the default values for lat and long to your current location in the other constructors, at least I think there isn't. Have a word with your instructor.



There is not, no. Would this avoid problems down the road if I were to set the default values to 0,0 for lat & long so that all newVehicles inherit those defaults?
 
Aaron McCarthy
Ranch Hand
Posts: 41
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this?


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

Aaron McCarthy wrote:So, if I move Location to a separate class altogether, won't I need to make the latitude and longitude public, rather than private . . . I'm supposed to avoid making them public from the standpoint of best practice...
. . .

You are right. Make sure the lat and long fields in the Location class are private. There are two ways I can think of to do it:-
  • 1: Use getXXX methods to extract lat and long from a Location parameter. Use those figures to set the lat and long fields in your vehicle object.
  • 2: Have a Location field in your Vehicle objects. There are all sorts of errors that can occur if you follow that approach, but you can prevent most of them by making Location immutable. Read about immutability in the Java™ Tutorials.
  • There is bound to be a third way which will occur to me in five minutes.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As mentioned, a Location class helps keep your Vehicle class cleaner and is a more object oriented design. Similarly a Direction enum with NORTH, SOUTH, EAST, and WEST would greatly simplify moving a Location and turning left/right.
     
    Aaron McCarthy
    Ranch Hand
    Posts: 41
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:As mentioned, a Location class helps keep your Vehicle class cleaner and is a more object oriented design. Similarly a Direction enum with NORTH, SOUTH, EAST, and WEST would greatly simplify moving a Location and turning left/right.



    I have moved Location to a separate Class. I think the term "simplify" here is very subjective, as I've only just glossed over Enum so far (been learning/writing Java for only 3 days at this point). What you have shown me here is a little beyond my comprehension level at this point.

    I will try to wrap my head around it though, as it's clearly very relevant to the task at hand.

    Sorry for the shouting and whatnot, BUT YOU FOLKS ARE LITERALLY THE BEST!!!
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Enums are a bit confusing at first. They can be very simple, like:
    This defines 4 constants with automatically assigned (ordinal) numbers 0 through 3. They can be used as cases in switch statements. You can print them. You can compare them. You can assign them. They can have methods including getters (they don't have setters though). They are user defined types similar to classes.

    Once you need more info to be associated with the constants beyond their ordinal numbers you start to find that enums have many of the capabilities of a class. They can have constructors, member variables, and methods. So in the Direction5 enum I need to have an east/west increment and a north/south increment therefore I needed two member variables. To initialize them I needed a private constructor that can take two arguments.

    Line 3 has the same constants (NORTH, SOUTH, EAST, and West) as before but now we utilize a constructor to initialize the two member variables, Hence: NORTH(1,0).

    Note that the constant declaration must appear first inside the enum.

    I provided a convenience methd turnRight that will take a direction and return a new direction. Because enums are constants, a method can't modify the member variables but they can return anything they want. In this case, a new direction.
     
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Aaron McCarthy wrote:Would this avoid problems down the road if I were to set the default values to 0,0 for lat & long so that all newVehicles inherit those defaults?



    From the computer programmer's point of view that would seem to be a normal thing to do. But from the real-life point of view, it seems a bit strange to have a vehicle out in the middle of the Atlantic Ocean. So personally, if all vehicles are supposed to have a location I wouldn't use that location as the default. And I would probably push back against a design that allowed a default location unless it was a sensible location (e.g. the factory where the vehicles are built).
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Aaron McCarthy wrote:I think the term "simplify" here is very subjective, as I've only just glossed over Enum so far (been learning/writing Java for only 3 days at this point). What you have shown me here is a little beyond my comprehension level at this point.

    Sorry, I seem to have jumped the gun a bit. I get excited about problem solving and elegant solutions.
     
    Aaron McCarthy
    Ranch Hand
    Posts: 41
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Aaron McCarthy wrote:I think the term "simplify" here is very subjective, as I've only just glossed over Enum so far (been learning/writing Java for only 3 days at this point). What you have shown me here is a little beyond my comprehension level at this point.

    Sorry, I seem to have jumped the gun a bit. I get excited about problem solving and elegant solutions.



    I get excited about extremely talented folks helping me solve my problems elegantly! I've got to crawl before I can run though!
     
    Rancher
    Posts: 4801
    50
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just want to point out, with all the discussion regarding "directions" and how to move this thing, that the requirements mention no such thing:


    1) moveForward - doesn't take any parameters but updates the Location of the Vehicle to be the x+1 and y+1)
    2) moveBackward - doesn't take any parameters but updates the Location of the Vehicle to be the x-1 and y-1)
    3) moveToLocation - takes a location and set the vehicles Location to be the Location that was passed
    4) updateMileage - take mileage as a parameter and updates the Vehicles mileage to what was passed



    There is (from the above) no need to have a direction, and the methods turnLeft and turnRight aren't actually part of the requirements at all.
    So I would argue that writing that sort of thing is jumping the gun a bit.
     
    Ranch Hand
    Posts: 146
    2
    Mac Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Aaron McCarthy wrote:Here is the actual assignment as well for additional context.
    - Location (this represents a class called Location which has 2 points: longitude & latitude)



    I'd suggest that you ought not to be passing the latitude and longitude as arguments, but should instead be passing a Location object.



    In your move methods, you can create a new Location object, e.g:



    Etc.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please don't supply more code than that, otherwise there is a risk that people will accuse you of doing too much of OP's assignment. That looks like what I suggested yesterday morning. I didn't notice then that the requirements say there should be a Location field in the Vehicle class rather than lat and long, so that would match the assignment better. As I said then, it would be better to make the Location class immutable; since it hasn't got any mutable reference types as fields, that isn't too difficult. See link I posted earlier. The really clever way to make an immutable Location object would be to give it a method like this. That is more object oriented than using + 1 in the Vehicle method because the calculations are done inside the Location object:-Another challenge: Use the remainder operator so you never have a |lat| > 90° or a |long| > 180°.

    By the way: tiny little style thing: if you are doing double arithmetic, make your number literals double type with a decimal point: 1.0 rather than 1.
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One more thing on the requirements as posted above.
    The Location definition talks about lat and long, however the method descriptions talk about x and y.

    I suspect latitude and longitude is a bit of a red herring, and x and y makes a bit more sense, though I am thinking in terms of graphics.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What does the requirements statement say? Cartesian coordinates would be much easier to calculate than lat and long.
     
    Bartender
    Posts: 5465
    212
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    For this exercise it is irrelevant what the fields of Location represent.

    The simple question was how to implement the moveToLocation method. Since then we've been flooding OP with code, enums and immutable classes. Isn't it time to let OP finish this exercise, as good or as bad as he can get it, and hand it over to his teacher? Then he can say it was all of his own work.
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Aaron McCarthy wrote:Here is the actual assignment as well for additional context.

    I'm open to suggestions regarding anything else I'm obviously butchering here... lol


    Create a Vehicle class, it should have the following attributes (you should define what the types the attributes should be - except for the Location attribute see below):

    - Name
    - Color
    - Max Speed
    - Mileage
    - Type (values can be: auto, truck, motorcycle)
    - Location (this represents a class called Location which has 2 points: longitude & latitude)

    - Make sure you define 3 constructors:
    1) A constructor that takes the name, color, type
    2) A constructor that takes name, color, type, max speed
    3) A constructor that takes name, color, max speed, mileage, type

    You should also create the following methods

    1) moveForward - doesn't take any parameters but updates the Location of the Vehicle to be the x+1 and y+1)

    2) moveBackward - doesn't take any parameters but updates the Location of the Vehicle to be the x-1 and y-1)

    3) moveToLocation - takes a location and set the vehicles Location to be the Location that was passed

    4) updateMileage - take mileage as a parameter and updates the Vehicles mileage to what was passed

    Once you have the above and everything compiles, create another class called GritSeedVehicles which has a main method (so it can be run)

    In the GritSeedVehicles, create 4 to 8 different types of vehicles, call all the different constructors and methods and print the values of the car Location, mileage as you call the methods etc.



    That's the requirements, as posted above.
    Nothing about turning left or right.
    Nothing about latitude and longitude actually representing anything other than x/y coordinates.
    It looks to me like the only thing needed was a Location class that was actually used.
     
    Aaron McCarthy
    Ranch Hand
    Posts: 41
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:For this exercise it is irrelevant what the fields of Location represent.

    The simple question was how to implement the moveToLocation method. Since then we've been flooding OP with code, enums and immutable classes. Isn't it time to let OP finish this exercise, as good or as bad as he can get it, and hand it over to his teacher? Then he can say it was all of his own work.



    Thanks everyone!

    I have come to the realization that I was doing the opposite of good coding by over-complicating the whole thing... Here is what I am working with now.

    I have included the first few relevant lines from each class/enum, etc... I think the more simple I make it, the easier it is to wrap my head around (keep in mind I started learning Java Monday).

    Thank you all again so much for the help on this!











     
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Aaron,

    Location object can and probably supposed to be immutable. It shouldn't have default nor no-argument constructor. Its fields supposed to be private and final as well as the class itself supposed to be final.
    It could be even avoided any getters in location class, but that might would be too complicated for you as of now. So probably proceed with 2 getters, 1 for x coordinate and 1 for y to get.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Aaron McCarthy wrote:. . . I was doing the opposite of good coding by over-complicating the whole thing... . . .

    Your code looks a lot better now

    Please work out what the maximum speed will be after that constructor has completed. Ditto mileage. It is probably a good idea for every constructor to do one of the following to every field:-
  • explicitly to initialise that field, even if it is to 0, or
  • to pass that field to a different constructor with this(....);
  • So every field will be initialised.
    If you are using the this.xxx convention, don't have one‑letter parameters for thhe constructor. Use the parameters to expose the proper names, as used by the fields.

    Arrange the class so all the constructors are together, all the methods together, etc. I presume you have omitteed the getLatitude() method for brevity's sake only.
    Why have you got a no‑arguments constructor? Work out what values that will give to lat and long.
    Make all fields of all classes private, except those used as global constants, or by special arrangement with your team at work.
    If you don't have any setXXX methods or other methods to change the state of the Location object, you can make it immutable by changing public class Location to public final class Location.

    . . .

    Avoid CapitalLetters in package names; call it vehicle not Vehicle. Why does grit seed vehicles extend Vehicle? Please have a look at the earlier code, where it seems to be a collection of Vehicles rather than a kind of vehicle.
    If all your classes are in the same package, why are they public?
     
    Aaron McCarthy
    Ranch Hand
    Posts: 41
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you all again so much for the assistance here! I've just checked out all of your LinkedIn profiles, Junilu, Paul, Liutauras, Carey, you guys are all extremely experienced programmers/developers!

    This is literally like having a bunch of Hall Of Fame Quarterbacks come coach your children's pick up football game.

    Thanks a million again! I think if I stick around, I might actually learn this crazy thing called Java. I feel like I'm doing ok for my first week, but I wouldn't be near where I am without your gracious and talented assistance!
     
    Aaron McCarthy
    Ranch Hand
    Posts: 41
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks folks!

     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Success:)
    Did you try anything using the constructor with the fewest parameters? What did that display?
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic