Help On Java Code
807603Nov 12 2007 — edited Nov 14 2007Hey everyone! I'm writing this Java Code, and I need help on this one part.
So the instruction says:APCS A
GRADES
Program Description:
This assignment will give you practice with parameters, returning values, and interactive programs. Turn in a Java class named Grades in a file named Grades.java, which will be submitted electronically on the course web site. You will be using a Scanner object for console input, so you will need to import java.util.*; into your program.
This program uses a student's grades on homework and exams to compute an overall course grade. The following is one example log of execution of the program (user input is underlined).
This program accepts your homework scores and
scores from two exams as input and computes
your grade in the course.
Homework and Exam 1 weights? _50 20_
Using weights of 50 20 30
Homework:
Number of assignments? _3_
Assignment 1 score and max? _14 15_
Assignment 2 score and max? _16 20_
Assignment 3 score and max? _19 25_
Total points = 49 / 60
Weighted score = 40.83
Exam 1:
Score? _81_
Curve? _0_
Total points = 81 / 100
Weighted score = 16.2
Exam 2:
Score? _95_
Curve? _10_
Total points = 100 / 100
Weighted score = 30.0
Course grade = 87.03
The course grade is a weighted average. To compute a weighted average, the student's point scores in each category are divided by the total points for that category and multiplied by that category's weight.
In the log of execution shown, the course has 50% weight for homework, 20% weight for exam 1, and 30% weight for exam 2. There are 3 homework assignments worth 15, 20, and 25 points respectively. The student received homework scores of 14, 16, and 19. The student received an exam 1 score of 81. The student earned an exam 2 score of 95; the exam was curved by +10 points, but exam scores are capped at 100, so the student was given 100 for exam 2.
The following calculations produce the student's course grade from the above log of execution:
Note that the preceding equations are not Java math. In Java, an integer expression such as 81/100 would evaluate to 0, but above the value intended is 0.81.
The program behaves differently depending on the user input it receives; you should look at all of the example logs of execution on the course web site to get a more comprehensive example of the program's behavior.
Program Behavior Details:
The program asks the user for the weights of the homework and exam 1. Using this information, the program can deduce the weight of exam 2 as 100 minus the other two weights.
You may assume that the user enters valid input. For example, assume that the user enters a number of homework assignments no less than 1, and that the sum of category weights entered will be no more than 100. The weight of a particular category (homework, exam 1, or exam 2) will be non-negative but could be 0.
You should handle the following two special cases of user input:
A student might receive extra credit on a particular assignment, so for example 22 / 20 is a legal assignment score. But the total points for homework are capped at the maximum possible. For example if a student receives 63 total points out of a maximum of 60, your program should cap this to 60 / 60.
The maximum score for an exam is 100. If the curved exam score exceeds 100, a score of 100 is used.
Use the Math.max and Math.min methods to constrain numbers to a given range.
Notice that all weighted scores and grades printed by the program are shown with no more than 2 digits after the decimal point. To achieve this, you may type the following method into your program and call it to round a double value to the nearest hundredth:
// Returns the given double value rounded to the nearest hundredth.
public static double round2(double number) {
return Math.round(number * 100.0) / 100.0;
}
The following is an example usage of this method to print a variable named x:
System.out.println("The rounded value of x is " + round2(x));
See the logs of execution on the course web site. Your program's output should match these examples exactly when the same input is typed. Please note that there are some blank lines between sections of output and that input values typed by the user appear on the same line as the corresponding prompt message.
The code to compute the student's homework scores requires you to compute a cumulative sum. See the lecture examples and section 4.1 of the textbook for more information on this technique.
Stylistic Guidelines:
A major part of this assignment is demonstrating that you understand parameters and return values. Therefore, use static methods that accept parameters and return values where appropriate for structure and to eliminate redundancy. For full credit, you must use at least 3 non-trivial methods other than main and round2.
Unlike on previous assignments, you can have println statements in your main method on this program. However, your main method should still represent a summary of the overall program; the majority of the behavior should come from other methods. To fully achieve this goal, some of your methods will need to return values back to their caller. Each method should perform a coherent task and should not do too large a share of the overall work. For reference, our solution is 91 lines long with 5 methods other than main.
When handling numeric data, you are expected to choose appropriately between types int and double.
You are not allowed to use more advanced features (such as if/else statements) or features not covered in class or the textbook to solve the problem.
You should properly indent your code and use whitespace to make your program readable. Give meaningful names to methods and variables in your code. Follow Java's naming and capitalization standards as described in Section 1.2 of the book. Localize variables whenever possible; declare them in the shortest possible scope.
Include a comment at the beginning of your program with basic information and a description of the program. Also include a comment at the start of each method explaining its behavior.
This is the code I got so far:
//
// Author:
// Teacher:
// Date:
//
import java.util.Arrays;
import java.util.*; // for Scanner
// This program prompts the user to enter various student scores
// and outputs the student's grade in the course.
public class Grades {
// the students don't need to have these constants
private int numAssignments;
public static void main(String[] args) {
giveIntro();
getHomeworkScores();
//your code goes here
}
// a welcome message to start the program
public static void giveIntro()
{
System.out.println("This program accepts your homework scores and");
System.out.println("scores from two exams as input and computes");
System.out.println("your grade in the course.");
System.out.println();
}
// asks about student's homeworks and returns weighted HW score
public static void getHomeworkScores()
{
Scanner console = new Scanner(System.in);
System.out.println("Please enter your Homework Weight:");
int homeworkWeight = console.nextInt();
System.out.println("Please enter your Exam 1 Weight:");
int exam1Weight = console.nextInt();
int exam2Weight = 100 - homeworkWeight - exam1Weight;
System.out.println("Using the following Weights:");
System.out.println("Homework Weight:" + homeworkWeight);
System.out.println("Exam 1 Weight:" + exam1Weight);
System.out.println("Exam 2 Weight:" + exam2Weight);
System.out.println("Homework:");
System.out.println("Please enter the Number of Assignments:");
int numAssignments = console.nextInt();
for (int i = 0; i < numAssignments; i++)
{
System.out.println("Assignment " + numAssignments + " Score?");
int numHomework = console.nextInt();
System.out.println("Assignment " + numAssignments + " Max?");
int numHomeworkMax = console.nextInt();
}
}
public static double homework(int weight) {
//your code goes here
return 0.0;
}
// asks about student's exam and returns weighted exam score
public static double exam(Scanner console, int number, int weight)
{
//your code goes here
return 0.0;
}
// helper method to compute and print a weighted category score
public static double weightedScore(int weight, int earned, int
possible) {
//your code goes here
return 0.0;
}
// returns the given double value rounded to the nearest hundredth
public static double round2(double number) {
return Math.round(number * 100.0) / 100.0;
}
}
What I need help on is when you enter the Number of Assignments you have. On the example, whatever Number of Assignments you enter returns the Assignment # plus the Score you got and the Max Score. I dont get how you can do that...I tried a "for" loop, but I do not know how to make it go:
Number of Assignments: 2
Assignment 1 Score:
Assignment 1 Max:
Assignment 2 Score:
Assignment 2 Max:
I cannot figure out to make it ascend in order. Do I use a "for" loop? Arrays?