CS 305J Assignment, Parameters

Programming Assignment 4 Individual Assignment. You must complete this assignment by yourself. You cannot work with anyone else in the class or with someone outside of the class. You are encouraged to get help from the instructional staff.

Placed online: Wednesday, September 17
20 points, ~2% of total grade
Due: no later than 11 pm, Thursday, September 25
General Assignment Requirements

Introduction The purposes of this assignment are:
  1. Decompose a problem into simpler parts.
  2. Create a structured solution based on those simpler parts.
  3. Practice getting input from a program user.
  4. Practice using parameters.
  5. To practice writing and using methods that return a value.

Create a program that calculates a student's final grade in a course based on their homework and exam grades. The following is one example log of execution of the program. User input is in bold and underlined.

This program accepts a student's homework
and exam scores as input and then computes
their grade in the course based on the
weight assigned to each category.

Enter the number of homework assignments: 4
Enter the number of exams: 2

Enter the weight for homeworks: 35

Please enter homework scores.
Enter student's homework number 1 score : 20
Enter student's homework number 2 score : 18
Enter student's homework number 3 score : 20
Enter student's homework number 4 score : 15
Total points = 73 / 80
Weighted score = 31.94

Please enter exam scores.
Enter student's exam number 1 score : 88
Enter student's exam number 2 score : 95
Total points = 183 / 200
Weighted score = 59.48

Course average: 91.42

This program calculates the course average for a student based on input from the program user.

A student's course average is a weighted average of homework and exam scores. To compute the weighted average for a category, the student's points earned in a category is divided by the total points for that category and multiplied by that category's weight.

Each homework is worth 20 points, although students may earn more than 20 points on a homework.

Each exam is worth 100 points. Students may not earn more than 100 points on an exam.

Consider the example from above: There are 4 homeworks and 2 exams. The student's homework scores are 20, 18, 20, and 15. The students exam scores are 88 and 95. The homework is worth (weighted) 35% of the final course grade and the exams are worth 65% of the final course grade. It is only necessary to enter the homework weight. The exam weight will be calculated based on the homework weight.

The following calculations produce the student's course grade from the above log of execution:

Note that the preceding equations are not Java expressions.  In Java, an integer expression such as (88 + 95)/(100 * 2) = 183 / 200 would evaluate to 0. We can ensure floating point division occurs by make one of the terms a double.

For example the Java expression (88 + 95) / (100.0 * 2) => 183 / 200.0 => 0.915. Floating point division occurs here because one of the operands of the division operator is a floating point number.

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 Description
  • The program asks for the number of homework assignments and exams.
  • The program asks for the weight out of 100 for the homework. It then deduces the weight of the exams.
  • You may assume the user enters valid input. For example the number of homework assignments will be greater than or equal to 1 and the homework weight will be greater than 0 but less than 100.
  • Homework assignments are worth 20 points and exams are worth 100 points. You should use global constants to make these easy to change from one run of the program to the next.
  • You must handle 1 special case of input. Students can earn more than 20 points per assignment. For example a homework score of 25 is valid. But the total points for homework is capped at the number of homework assignments times 20. So if there are 3 homework assignments a student could earn more than 60 points but when figuring their grade they would only get 60 points. You should use the Math.min method to handle this case.
  • 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 sample program runs 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 a student's total homework points and exam points 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 and can use parameters and return values.  Therefore, use static methods that accept parameters and return values where appropriate for structure and to eliminate redundancy. Not all of the methods you create will actually reduce redundancy. Some of them will simple add structure to the program even though they are only called once. For full credit, you must use at least 3 non-trivial methods other than main and round2.

  • 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.  Each method should perform a coherent task and should not do too large a share of the overall work.  For reference, my solution is 83 lines long (including blank lines, comments, and lines with a single curly brace. } ) with 5 methods other than main and round2.
  • When handling numeric data, you are expected to choose appropriately between types int and double.

  • For this assignment you are limited to the language features in Chapters 1 through 3, plus Section 4.1's cumulative sum technique.  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 not make the Scanner object that gets input from the keyboard a global variable. You should create it in the main method and pass it as a parameter.
  • 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.

Tips
  • Start early. This assignment is not as straight forward as the first three assignments. (Although I do think it is not as algorithmically difficult as the rocket assignment. There is not a lot of figuring out odd patterns in this assignment.) Start early so you have time to get help from the teaching staff if you need it.
  • Make a plan before you start coding. The decomposition (way to break up) this program is not as obvious as assignments 2 and 3. There are multiple reasonable ways to break up the program. Do not sit down and start coding! Instead read the problem statement and look at the sample outputs.  Then sit down with pencil and paper and think about how you are going to break up the problem. Write down what you want the main method to do. Then think about what other methods you want and what they will do. Think about the parameters they will need. Do this on paper first! Once you have a fairly detailed plan then start coding your solution.
  • When you start coding don't try and code all the methods first and then test your program. Instead code a method and test it. Write code to test the method even though this isn't going to be part of your final program. Which method do you do first? There are two approaches called top-down and bottom-up. In a top-down approach you start with the high level methods like main and then work your way down. In a bottom-up approach you start with the simple methods like round2 or a method to calculate a weighted score. Test each method after your finish it and be sure it is working before you move on.
  • Get help if you need it. If you get stuck go to the teaching staff during lab hours or the ACP tutors. This is individual work. You are not to use anyone else's code or have anyone else walk you through how to solve the problem.
  • EXTRA: The current version of the program requires us to re run the program for every student. How would you change the program to be able to handle multiple students? Assume the homework and test weights are the same for every student. If you want an extra challenge make you program able to handle multiple students by running the program once. This is just something to try on your own. What you turn in should be a program that does need to be re run for every student, not one that can handle multiple students in one run.

When finished turn in your Grades.java program using the turnin program.

Files
File Responsibility
sampleOutput.html. Sample Execution log. Various runs of the program showing behavior. Given the same input, your program should provide the exact same output. Provided by me.
Grades.java. A basic shell of  the program. Provided by me and you. (Okay, mostly you.)
Checklist Did you remember to:
  • review the general assignment requirements?
  • work on the assignment individually?
  • fill in the header in your file Grades.java
  • create a logical, structured solution that is easy to understand and reduces redundancy?
  • use loops and parameters to structure the problem and remove redundancy?
  • ensure your program does not suffer a compile error or runtime error?
  • use a class (global) constant for points per homework assignment and points per exam?
  • Only use concepts and tools from chapters 1 - 3 and 4.1 of the textbook?
  • put a comment on each method describing in broad terms, what it does?
  • send your TA email if you are planning on using slip days?
  • turn in your Java source code in a file named Grades.java to the proper account in the Microlab via the turnin program before 11 pm, Thursday, September 25?

Based on an assignment by Marty Stepp. Thanks to Marty for letting me borrow his ideas.

Back to the CS 305j homepage.