Google Answers Logo
View Question
 
Q: JAVA PROGRAMMING CODINGS ( Answered,   0 Comments )
Question  
Subject: JAVA PROGRAMMING CODINGS
Category: Computers > Programming
Asked by: aldaweesh-ga
List Price: $50.00
Posted: 22 Sep 2005 02:45 PDT
Expires: 22 Oct 2005 02:45 PDT
Question ID: 570827
The course "Programming in Oak" has 3 tests. The weights of the test
are 20%, 30% and 50%, Students receive an 'A', 'B', 'C', 'D', 'E' or
'F' for each test. To compute the final grade for the course, these
letter grades are first converted to numbers according to the table
below, next the weighted sum is computed, and finally this weighted
sum is converted back to a letter according to the table below.
F	E	D	C	B	A
0	1	2	3	4	5
Let us look at an example. Franck received for the first test an 'A',
for the second one an 'F', and for the third test a 'C'. His results
give rise to the weighted sum 0.20 × 5 + 0.30 × 0 + 0.50 × 3 = 2.50,
which is rounded up to 3. Therefore, his final grade for the course is
a 'C'. Had he scored an 'A', 'E' and 'D' for the three tests, his
weighted sum 0.20 × 5 + 0.30 × 1 + 0.50 × 2 = 2.30 would have been
rounded down to 2 and, hence, would have resulted in a 'D' as the
final grade.
We want to develop an app that starts with the letter grades for the
three tests and outputs the corresponding final grade. A template for
the sought app is shown below.
Scanner input = new Scanner(System.in);
PrintStream output = System.out;

char firstGrade = 'B';
char secondGrade = 'C';
char thirdGrade = 'B';
double weightedSum = ???;
int roundOff = ???;
char finalGrade = ???;
output.println(finalGrade);
You need to complete the development by replacing "???" with
expressions (not literals). You may not add or remove statements; only
complete the above three statements. Develop and eCheck Check02C.
Hint: For the conversion you may want to use casting 
. Note: Casting from a double to an int always rounds down. For
example, (int) 2.60 results in 2.
Answer  
Subject: Re: JAVA PROGRAMMING CODINGS
Answered By: leapinglizard-ga on 22 Sep 2005 06:25 PDT
 
Dear aldaweesh,

Below is a complete program, named Oak.java, in which the occurrences
of "???" in the above template have been replaced with correct Java
expressions.

To convert a letter grade to its numerical equivalent, we subtract the
char value of the grade from the constant 'F'. The weighting is
performed by multiplying the score on each test by .2, .3, and .5
respectively.

Rounding is accomplished by the Math.round() method. Note that it
returns a long when passed a double, so we cast the long to an int.

Finally, to convert the weighted numerical average back to a letter
grade, we subtract its char value from the constant 'F'.

I have tested this program with a Java 5 compiler and runtime
environment. It has been a pleasure to address this question on your
behalf.


Regards,

leapinglizard



//=====begin Oak.java

import java.lang.*;
import java.util.*;
import java.io.*;

public class Oak {
    public static void main(String[] argv) {
        PrintStream output = System.out;

        char firstGrade = 'A';
        char secondGrade = 'F';
        char thirdGrade = 'C';


        double weightedSum = .2 * ('F' - firstGrade) +
                             .3 * ('F' - secondGrade) +
                             .5 * ('F' - thirdGrade);

        int roundOff = (int) Math.round(weightedSum);

        char finalGrade = (char) ('F' - roundOff);


        output.println(finalGrade);
    }
}

//=====end Oak.java

Request for Answer Clarification by aldaweesh-ga on 01 Oct 2005 07:34 PDT
Class should be named Check02C
Values for firstGrade, secondGrade and thirdGrade should be as
  in the question.
There is no need for all the empty lines in your code.
import java.lang.* is not needed.
Often it is better to import individual classes than importing
  a whole package.

Clarification of Answer by leapinglizard-ga on 04 Oct 2005 18:10 PDT
//=====begin Check02C.java
import java.util.*;
import java.io.*;
public class Check02C {
    public static void main(String[] argv) {
        PrintStream output = System.out;
        char firstGrade = 'B';
        char secondGrade = 'C';
        char thirdGrade = 'B';
        double weightedSum = .2 * ('F' - firstGrade) +
                             .3 * ('F' - secondGrade) +
                             .5 * ('F' - thirdGrade);
        int roundOff = (int) Math.round(weightedSum);
        char finalGrade = (char) ('F' - roundOff);
        output.println(finalGrade);
    }
}
//=====end Check02C.java
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy