Login Register






java basics exercises with solution !! filter_list
Author
Message
RE: java basics exercises with solution !! #11
@blackeagle Great thing to do this thread for others. I think it is a good reference for programming exercises.

I still have suggestions for the solutions.

In general: The formatting is not the best and contains some violations of the Java code conventions. The fastest way to solve this is to run an automated formatter from an IDE, e.g. Eclipse.



Code:
int nbr1=in.nextInt(); Double kilometers=in.nextDouble();

My advice is to not use nextInt or nextDouble to read from stdin. The user may always write something that won't be convertable to int or double and this will break the program. Prefer readLine() to get a string as input and convert the string safely to the desired data type; meaning you can tell the user that the input was invalid instead of causing the program to break.



Code:
if (average >= 90) { System.out.println("grade=A"); } if ((average >= 70) && (average < 90)) { System.out.println("grade=B"); } if ((average >= 50) && (average < 70)) { System.out.println("grade=C"); } if (average < 50) { System.out.println("grade=F"); }

You can shorten this and avoid unneccessary checks as well by doing:

Code:
if (average >= 90) { System.out.println("grade=A"); } else if (average >= 70) { System.out.println("grade=B"); } else if (average >= 50) { System.out.println("grade=C"); } else { System.out.println("grade=F"); }



Code:
average = (quiz + midterm + finalscore) / 3;

This has some minor rounding issues. As you perform an integer division, decimal numbers will be cut instead of rounded correctly and might result in the wrong value for corner cases like an average of 89.7.
To avoid this behaviour it should rather look like this:

Code:
average = (int) Math.round((quiz + midterm + finalscore) / 3.0);

And try this to see the difference:

Code:
System.out.println((269 / 3)); System.out.println((269 / 3.0)); System.out.println((Math.round(269 / 3.0)));



Code:
if ((quantity >= 100) && (quantity <= 120)) { discount = (revenue * 10) / 100; } if (quantity > 120) { discount = (revenue * 15) / 100; } if (quantity < 100) { discount = (revenue * 0) / 100; }

Same as with the other if-statements:

Code:
if (quantity > 120) { discount = (revenue * 15) / 100; } else if(quantity >= 100){ discount = (revenue * 10) / 100; } else { discount = (revenue * 0) / 100; }

It is generally adviseable to avoid magic numbers and use variables instead as well as avoiding repition as much as possible. So based on this here is the next step of correction:

Code:
final int UPPER_DISCOUNT_QUANTITY = 120; final int LOWER_DISCOUNT_QUANTITY = 100; int discountPercentage = 0; if (quantity > UPPER_DISCOUNT_QUANTITY) { discountPercentage = 15; } else if(quantity >= LOWER_DISCOUNT_QUANTITY){ discountPercentage = 10; } discount = revenue * discountPercentage / 100.0;
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: java basics exercises with solution !! #12
@Deque ty very much for ur reply i learned a lot from it and i think that it will really help every1 who try to solve those exercises thank you very much deque i really hope someday i'll be as good as you are !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #13
Nice one. Some more problems should be added to it. Smile Thumbs up
[Image: MUJ8qSW.png]
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------

Reply

RE: java basics exercises with solution !! #14
Thanks for the share. Did you make this by yourself? Where is credit?
Java Java day Smile

Reply

RE: java basics exercises with solution !! #15
(07-23-2014, 06:12 PM)Sidatar Wrote: Thanks for the share. Did you make this by yourself? Where is credit?
Java Java day Smile

well i don't think i need to give credit to any1 i search the net for the exercises but the solutions are made by me !! the exercises there are solved in more complicated ways i made it easier fo people who just started to learn java !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #16
last time i checked, 1 mile isnt 1609km Wink (its written good inside the code)
small typing mistake, anyway this is nice, gj and ty!

Reply

RE: java basics exercises with solution !! #17
Updated again (Added 2 exercises on while and do while loops)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #18
thread updated 5 more exercices added !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #19
thread updated 5 more exercices added !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: java basics exercises with solution !! #20
thread updated 5 more exercices added !!
[Image: blackeagle_zps6ad86521.gif]

Reply