• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems bringing in a class into a program  RSS feed

 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello friends.  I am taking my first java class, did pretty good, now I'm down to the final project and crashing and burning.  I have created a class that collects five variables to inventory cars for a dealership.  So that's the first code, that compiled fine.  Then I go to bring it into my program so I can do an array list it fails.  I'm not even sure I'm calling it in correctly.  Can you guys give me some pointers?  I'm really struggling to get my head around this.  The second program pasted in here is the one with the failure.  If you look at the code, I'm trying to create sequential classes that I can then add to an array, but getting nowhere.  Any help you could give would be greatly appreciated, I'm getting nowhere.

Thanks!

My class:



My failing program:



It also failed using this format, though the variables didn't fail, just the class:



 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"1000" is not a valid variable name. Variable names should begin with a lower case letter.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your AutoInventory class would more appropriately be named "Vehicle".

Your AutoInventory (Vehicle) class sets all the attributes in the constructor, which is a good way to do it. But once "model" has been set in the constructor it really shouldn't be changing for the life of the object. Ditto for the other attributes. So in essence you have (almost) an immutable class, which is a good thing. To make it officially immutable you need to remove all setter methods but leave the getters.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
"1000" is not a valid variable name. Variable names should begin with a lower case letter.


Man do I feel stupid, that did the trick! 

Thanks!
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your AutoInventory class would more appropriately be named "Vehicle".

Your AutoInventory (Vehicle) class sets all the attributes in the constructor, which is a good way to do it. But once "model" has been set in the constructor it really shouldn't be changing for the life of the object. Ditto for the other attributes. So in essence you have (almost) an immutable class, which is a good thing. To make it officially immutable you need to remove all setter methods but leave the getters.


Ok.  I have to admit, I'm still hazy on what the getters and setters are really doing.  I have to read up on that some more.  Now you are talking about removing the setters in my "vehicle" class correct?

Also, what is the best way to rename a program, if I wanted to rename this to "vehicle"?

Thanks Carey.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Houser wrote:
Carey Brown wrote:Your AutoInventory class would more appropriately be named "Vehicle".

Your AutoInventory (Vehicle) class sets all the attributes in the constructor, which is a good way to do it. But once "model" has been set in the constructor it really shouldn't be changing for the life of the object. Ditto for the other attributes. So in essence you have (almost) an immutable class, which is a good thing. To make it officially immutable you need to remove all setter methods but leave the getters.


Ok.  I have to admit, I'm still hazy on what the getters and setters are really doing.  I have to read up on that some more.  Now you are talking about removing the setters in my "vehicle" class correct?

Also, what is the best way to rename a program, if I wanted to rename this to "vehicle"?

Thanks Carey.
A class's fields should all be private so as to keep unauthorized methods from changing their values. The setters (if supplied) become the traffic cops for modifying the field values. Getters perform a similar traffic cop function of controlling who and how access to the fields are granted. Most setters and getters are trivial and only require one line of code to implement, but sometimes including some logic in the method is desired. For instance, you may want a setter to check for a null value.

Yes, I'm talking about removing your setters.

To change to Vehicle (upper case 'V') you'll need to rename the .java file, rename the class, and rename the constructor, and of course anywhere else in your project that you use it.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, last dumb question.  A user needs to be able to enter this vehicle data in and I need to store it in an array list.  How would I supply user data into my program using this vehicle class?  I assume it's done in the inventory program not in my class program?  Sorry for the stupid questions, hard to get my head around this.

Thanks
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to have an ArrayList of Vehicle's, not a list of individual user inputs.

It would be a good idea to put all the user input code into its own method. Something like

There should be code in there to detect when a user is done entering their vehicles. You'll want to return null in that case. Otherwise create a new Vehicle from the input and return it.

In your main...
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Carey, I appreciate all your help. I'm stressing, I've got one more day to figure this out.  Do I add this code in my class or in the program I'm using to pull in my class?  and another thing that's killing me is how do I take this user input into sequential instances of the vehicle class?

So the user enters input, how do I get it to output into my array list as vehicle001, vehicle002, etc... ?

Also, I put in this code in my class:  (Still says AutoInventory not vehicle)



But I get a "need return statement" error, what is causing that?

Lastly, where would the while statement go?

Sorry for all these questions...
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Houser wrote:

If you are creating a new Scanner object on line 2 then you don't need the passed in parameter. I prefer only having one copy of a Scanner created with System.in and then passing it to wherever it needs to be used.

For make, model, and color I suggest using in.nextLine() instead of in.next(), this would allow for spaces in the input.

When you input make the user should have some way to tell the program that there's no move vehicles to be entered. Then return null if that input is entered.
Example:
System.out.print("Please enter the vehicle make (or 'enter' to quit): ");

Lastly, take all your inputs and return a new Vehicle object created with those parameters.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cleaned up the code per your comments:

  

Now I'm choking on the while loop, getting this thing to terminate...
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Get rid of line 23 stuff, you already have a prompt for 'make', just replace it.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:For make, model, and color I suggest using in.nextLine() instead of in.next(), this would allow for spaces in the input.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Get rid of line 23 stuff, you already have a prompt for 'make', just replace it.


I did this and the in.nextLine as you suggested, thanks.  I'm still getting a missing return statement error on line 7

 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Lastly, take all your inputs and return a new Vehicle object created with those parameters.

Also, you just put in the 'make' code but you ripped out all the other prompts. Why?
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repost your current code. I was under the assumption you worked on the code in the order of the posted suggestions. It seems that something is missing.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is how you declare your method, then it need to return an AutoInventory object.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my latest code, I've renamed everything to "Vehicle" and added in returns related to the method.  The only error I'm getting now is on line 89 where I'm getting "illegal start of expression".  What am I doing wrong there?

Thanks




 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Missing a closing brace at line 88.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1) You have a method named Vehicle.  This is bad for two reasons: it looks like a constructor and it starts with an uppercase letter.
2) You declare that the method Vehicle returns an object reference of type Vehicle, but you only return a null if the make is empty.  You need to create and return a Vehicle with a return statement.
3) The method Vehicle has a parameter that you aren't using: scan.  Since you declare and initialize the variable "in" outside of any method, you can (and do) use it in the method.  The parameter scan is unnecessary.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!