• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code help  RSS feed

 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
Have been going around trying different languages for some time now, but always return to Java.
Wrote something simple and tested some new things, but now i wonder if i have been doing it right with classes and methods(Edit: Maybe named constructors? :P).

Would be great if someone had the time
Link to github: https://github.com/phunlife/My_Java_Code/blob/main/main.java

Thank you for reading!

//Andreas
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not bad for a start. Change type of lampOn to boolean. Change breaks(int x) to setBreaksOn(boolen b). Car should have a SoundSytem. Boose (Bose actually) should inherit from SoundSystem. In Main create a new Audi and assign it to a Car variable. Also, class names should begin with a capital letter.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Tchikatchika wrote:Wrote something simple and tested some new things, but now i wonder if i have been doing it right with classes and methods...

About the only thing I can add to Carey's points is that, by convention, a method called 'isLampOn()' would normally be a "getter" for the 'lampOn' variable (which, as he says, should be a boolean).

A better name for the method you've written might be displayLampStatus().

Winston
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Not bad for a start. Change type of lampOn to boolean. Change breaks(int x) to setBreaksOn(boolen b). Car should have a SoundSytem. Boose (Bose actually) should inherit from SoundSystem. In Main create a new Audi and assign it to a Car variable. Also, class names should begin with a capital letter.


Thank you for the help, Made the changes you mentioned and it looks much better i think. What i dont get is the "SoundSystem", should i make a class of its own or can you inherit from a method??

//Andreas
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Andreas Tchikatchika wrote:Wrote something simple and tested some new things, but now i wonder if i have been doing it right with classes and methods...

About the only thing I can add to Carey's points is that, by convention, a method called 'isLampOn()' would normally be a "getter" for the 'lampOn' variable (which, as he says, should be a boolean).

A better name for the method you've written might be displayLampStatus().

Winston


That is indeed a much better name for it. Thank you
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, btw here is the updated files: https://github.com/phunlife/My_Java_Code

I moved the Car class to its own file, hope it is right :P

//Andreas
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would propose to go for an even cleaner design by swapping out the boolean for an enum so that the code can read naturally...

See for yourself:
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you can write "if (lampStatus)" instead of "if (lampStatus == true)". Actually, you *should* use that, because all other Java developer are used to that, and thus can more quickly read your code. (And, totally unrelated, I think you mean "brakes" instead of "breaks". Although I've never heard of the ability to turn brakes off. )

swapping out the boolean for an enum

I'm torn on using an enum for a boolean. Personally, I don't think it adds much to readability or clarity - (lampStatus ? "on\n" : "off\n") is shorter than (lampStatus == Status.ON ? "on\n" : "off\n"). As soon as there are more options (like Status.BROKEN) I would agree.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Car and Audi should each be in their own file and the classes should be declared public.
 
Andreas Tchikatchika
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Note that you can write "if (lampStatus)" instead of "if (lampStatus == true)". Actually, you *should* use that, because all other Java developer are used to that, and thus can more quickly read your code. (And, totally unrelated, I think you mean "brakes" instead of "breaks". Although I've never heard of the ability to turn brakes off. )

swapping out the boolean for an enum

I'm torn on using an enum for a boolean. Personally, I don't think it adds much to readability or clarity - (lampStatus ? "on\n" : "off\n") is shorter than (lampStatus == Status.ON ? "on\n" : "off\n"). As soon as there are more options (like Status.BROKEN) I would agree.


Aah, thats a much better way to do it
---------------------------------------------

Made changes to the code and added new stuff, will try to continue tomorrow.
Have commented some stuff in the code too.
Code: https://github.com/phunlife/My_Java_Code

Thank you all for all the help and hints I've gotten so far, really encouraging!

//Andreas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!