• Post Reply Bookmark Topic Watch Topic
  • New Topic

My base code.  RSS feed

 
Greenhorn
Posts: 4
IntelliJ IDE Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all!

I've come to the huge realization that learning Java will be so much easier if I have a goal set on a application =] . The final application that I am looking to make is somewhat of a know all vehicle toolkit. I know I could look on how to make this work, but I rather get input from others on hints and what not. I am still learning how the constructors work and many other odds and ends.

Please leave any feedback on what I should change/add/omit or just ideas that I can try and implement on my own. I'm trying to make this learning process as fun as possible haha.
Here's my base code:



Here's the output: Why does it print the "Currently....." line each time? I also want to make it show the amount of Makes there are each time I add a Cars object.
Currently you can choose between 6 different Makes
Toyota
1991
Currently you can choose between 5 different Makes
Lexus
1998
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Hope wrote:
Here's the output: Why does it print the "Currently....." line each time?


Because you're telling it to. Why would you not expect it to?

On the other hand, if you're asking why it prints 6 the first time and 5 the second time, what class is Make? (Date? List? Integer?) What do the javadocs for that class's length() method say?

I also want to make it show the amount of Makes there are each time I add a Cars object.


So then you need to either keep a separate data structure that just contains the individual makes, or, each time you add an object, you need to iterate over all the existing ones, pull out its make and add it to the number of makes if it's not already been counted.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: you ought not to give fields names starting with capital letters. To avoid the confusion between the field and the parameter, you need the this. keyword.
And fields should normally have private access. You can provide access via getXXX methods:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it would make more sense if your class were named Car, singular, instead of Cars, plural. When you create an instance, it makes more sense to say you have a Car object, as opposed to "a Cars object."
 
Michael Hope
Greenhorn
Posts: 4
IntelliJ IDE Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

On the other hand, if you're asking why it prints 6 the first time and 5 the second time, what class is Make? (Date? List? Integer?) What do the javadocs for that class's length() method say?

So then you need to either keep a separate data structure that just contains the individual makes, or, each time you add an object, you need to iterate over all the existing ones, pull out its make and add it to the number of makes if it's not already been counted.

Also, it would make more sense if your class were named Car, singular, instead of Cars, plural. When you create an instance, it makes more sense to say you have a Car object, as opposed to "a Cars object."


Thanks for the feedback! "Make" (now make), is a field. I was looking for a way to keep a number of all the individual makes.

Campbell Ritchie wrote:By the way: you ought not to give fields names starting with capital letters. To avoid the confusion between the field and the parameter, you need the this. keyword.
And fields should normally have private access. You can provide access via getXXX methods:


Thank you also for the feedback. I made corrections as you stated.

 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Hope wrote:Thanks for the feedback! "Make" (now make), is a field. I was looking for a way to keep a number of all the individual makes.

One way would be to make 'Make' a class (possibly even an enum).

Another thing: What's to stop someone creating any of the following?:
Car combo = new Car("Toyota", "GS300", 1991, "White");
Car chippo = new Car("Athlon", "64-X2", 2010, "Green");
Car oldie = new Car("Lexus", "GS300", 1934, "Silver");
Car loud = new Car("Lexus", "GS300", 1998, "Screaming Orange");


Have a thunk about it.

Winston
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to keep track of the number of Car objects with a particular make field, you need some sort of counting mechanism. If you find the Java Tutorials section about Maps, you will find a similar counter there. It might take you a bit of time to understand how it works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!