My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
Welcome to the RanchAaron McCarthy wrote:. . .
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
It doesn't say it has to be an inner class.Aaron McCarthy wrote:. . . Location (this represents a class called Location which has 2 points: longitude & latitude)
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.- Make sure you define 3 constructors:
. . .
You should also create the following methods
. . .
Anybody who can think us that sort of name for a class can be forgiven everything.GritSeedVehicles . . .
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
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.
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
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:-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...
. . .
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.
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
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?
Sorry, I seem to have jumped the gun a bit. I get excited about problem solving and elegant solutions.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.
Carey Brown wrote:
Sorry, I seem to have jumped the gun a bit. I get excited about problem solving and elegant solutions.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.
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
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
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)
"Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain
There are three kinds of actuaries: those who can count, and those who can't.
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.
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.
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
Your code looks a lot better nowAaron McCarthy wrote:. . . I was doing the opposite of good coding by over-complicating the whole thing... . . .
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:-
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.
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.. . .
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
My presence here is concomitant to my first and current mid-life crisis. Never mind sales, it's time to learn Java!
Don't get me started about those stupid light bulbs. |