RE: java basics exercises with solution !! 07-23-2014, 01:05 PM
#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.
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.
You can shorten this and avoid unneccessary checks as well by doing:
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:
And try this to see the difference:
Same as with the other if-statements:
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:
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.
Expressed feelings are just an attempt to simulate humans.
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)


Thumbs up
![[Image: MUJ8qSW.png]](http://i.imgur.com/MUJ8qSW.png)
(its written good inside the code)