Homework Programming Assignment, Five A) Homework assignment submission requirement/policy Each homework assignment may contain a set of problems with a due date specified usually one week from the date the problem set is assigned. Assignments will be posted on my website and handed out in class. Each problem in an assignment shall receive a separate grade. Each homework assignment should be turned in before the specified due time. In case you cannot complete a problem by the beginning of class on the due date, you can take another two days to work on the problem and turn it. The penalty for such a period late submission will be 20 percent. If you hand the problem in two class periods late, the penalty shall be 40 percent for that problem. Beyond one week from the specified due date the problem shall NOT be graded for any reason. Please assemble all homework in an envelope or folder of your choice. I shall not accept loose homework. The folder should keep the contents from falling out and contain: 1. 2. 3. 4. 5.

6. 7.

A clear header including your name (and your partner name in case some lab assignments), assignment number and problem number. A copy of the homework assignment sheet. A printout of all source code and supporting comments. A printout of the output from each program (you may use Alt + PrtSc to copy the console output, and use Ctrl + v to paste it to the project report, say a Word document). A 3 ½ inch DOS-format floppy (or a CD-ROM, or use a USB drive) containing all source code needed to compile and run your program. This diskette must not contain any files unrelated to the problem set. I shall compile and run each program that you submit as well as examine your source code. This source code must be nicely and consistently formatted. Unformatted or difficult to decipher code shall result in a grade reduction for the particular problem being graded. If your problem is one or two class periods late you must clearly state this on top of the first sheet. A self-assessment of each problem. This should indicate whether you believe you have completed the problem successfully. It may also discuss any special difficulties that you have had in solving the problem.

Programs will be graded by compiling and running them on a PC configured like the lab computers. Make sure that the programs can be tested at the DOS console, by the use of javac and java commands. I will NOT use any IDE to grade the programs, though you can use any IDE such as NetBeans to develop the programs. Please verify the contents of your disk before turn in. It is not uncommon to receive disks that contain nothing. That is the grade that is awarded. If a program does not compile at the DoS console, it shall receive an automatic grade of 0. If a program produces run-time or logical errors you shall receive only partial credit. * Note: The homework assignment is to be individually completed by yourself. Copying the work of another student whether that work is a homework program or an exam problem is cheating. Obtaining code via the Internet is cheating. You must write your own programs completely and not modify some other student’s work to disguise that the work has not originated from you. It is usually quite easy to see through such disguises. You are always welcome to discuss concepts with fellow students. You must draw a sharp line between discussing a concept and its implementation in a program. The former cooperation is allowed the latter is cheating.

B) Problems using Inheritance, polymorphism, and ArrayList (total 240 pts + 40 bonus) * For full credit your solution must make use of key methods and have minimal code redundancy. Think about your answers. 1. Textbook exercise 10.1 (50pts): The Triangle class 2. Textbook exercise 10.3 (30pts): Subclasses of Account 3. Texbook exercise 10.5 (30 pts): The Course class 4. Using ArrayList (40 pts): write a program that creates an ArrayList, adds a Date object, a string, a Character object, and a Circle object to the list, and use a loop to display all the elements in the list by invoking the object’s toString() method. 5. Application of ArrayList (90 pts + 40 bonus pts) * Clue/Notes: ArrayList takes all elements as objects, instead of primitive data types. Part A: you may take each score as a Double object using the wrapper class. Part B: you may want to declare a Student class that implements a compareTo() method (even more generic, Comparable interface), so that the student objects can be compared based on the implementation of compareTo() method – you may need it for sorting. You may refer to textbook chapter 11.10 “Example: sorting an array of objects” for reference (you have to use an ArrayList in this example, instead of an array though). You may do this part after our lectures covering Chapter 11.

Part A: Suppose that we wish to store the grades of students in a class and then determine the following statistics: highest grade, lowest grade, average grade, and provide descendingly sorted score list. Suppose further that the grades are held in an text file, one grade per line. The name of this file is score.txt (see attached file). The format is as follows: 96.0 95.0 0.0 81.0 38.0 78.0 …

Your task is to write a complete application that reads the input file, stores the information in one ArrayList and displays the maximum score, the minimum score, the average score, and the sorted score list (descending). Design of Solution

1. You may want to read the raw data from the file, and load the raw data values into an ArrayList using wrapper objects of type Double (the raw data will be input as type String and converted to Double). 2. Design methods that do the required operations 3. You may try “divide and conquer”. The following presents the structure of the Grades class, you should complete the methods implementation. /** * Grades class. * A class that manages the computation of several grading statistics. */ import java.io.*; import java.util.*; public class Grades { // Fields private ArrayList list;

// list is what you want to manipulate

/* Constructor method: use the fileName as the argument, load the raw data into the list; it throws IOException since text I/O may get exceptions. You may try to write a separate program to load the raw data into the list and test it first, then, try to embed it into the Grades class. */ public Grades(String fileName) throws IOException { list = new ArrayList(); … //read data from the file into the array-list //treat each value as a Double object } public double maximumGrade() { … } public double minimumGrade() { … } public double averageGrade() { … } public void descendingSorting() { … } }

The following is an application that tests Grades class. /** * A test class that exercises class Grades */

import java.text.*; import javax.swing.*; public class GradeApp { public static void main(String [] args) throws Exception { DecimalFormat df = new DecimalFormat ("0.##"); Grades g = new Grades ("grades.txt"); System.out.println ("Maximum grade = " + g.maximumGrade()); System.out.println ("Minimum grade = " + g.minimumGrade()); System.out.println ("Average grade = " + df.format (g.averageGrade())); JOptionPane.showInputDialog("Done"); } }

Part B: Further suppose that the grades are held in another text file, one student name together with the grade per line. The name of this file is studentScores.txt; (see attached file). The format is as follows: Jack 96.0 Steven 95.0 Mark 0.0 Adams 81.0 Jason 38.0 Kevin 78.0 …

Your task is to write a complete application that reads the input file, stores the information in one ArrayList and displays the maximum score with the student name, the minimum score with the student name, the average score, and the sorted score list (descending) with both score and student name information. Design of Solution 4. Design a Student class, with name field and score field, and those accessor methods and toString method. 5. You may want to read the raw data from the file, and load the raw data values into an ArrayList using objects of type Student. The following presents the structure of the StudentGrades class, you may want to complete the methods implementation. /** * Grades class. * A class that manages the computation of several grading statistics. */

import java.io.*; import java.util.*; public class StudentGrades { // Fields private ArrayList list;

// list is what you want to manipulate

/* Constructor method: use the fileName as the argument, load the raw data into the list; it throws IOException since text I/O may get exceptions */ public StudentGrades(String fileName) throws IOException { list = new ArrayList(); … } public Student maximumGrade() { … } // return the student object with the maximum grade public Student minimumGrade() { … } // return the student object with the minimum grade public double averageGrade() { … } public void descendingSorting() { … } }

Then, write an application, similar to the GradeApp class in part A, to test the StudentGrades class’s functions.