CIS 3309 Lab Assignment 4 – Chapters 8 and 9
(Last revised: June 16, 2018. Do only Exercises 8.1 X and 9.2. The other two exercises relate to material that is more detailed and less conceptual. Although you will use this material later in the course, it is not essential that you work on these labs now.)
Note: 25% of your grade is based on
1. Proper and consistent naming of variables, and class instances (objects)
2. Meaningful comments (not over done nor “under done”) used primarily to explain the purpose of your software components (classes and procedures).
The purpose of any classes you create should be clearly described, and the purpose of each procedure (function or sub) should also be documented.
An additional 25% is for clean, well organized code, including well thought out class and procedure structures.
If your program works but has sloppy code, bad variable names, and no comments, you will only get 50% of the maximum grade.
VALIDATION: This is as good a time as any to build good habits related to data validation. Every piece of data entered for which the entry is not “idiot proof” should be validated (unless otherwise indicated). We should not have to remind you about this anymore. It should be automatic. It is good to develop good habits early.
~~~~~~~~~~~~~~~~~~~~~~~
Please read Chapters 8 and 9, and understand the material before Lab 4 begins. Chapter 8 introduces arrays and collections and Chapter 9 is Strings and Dates.
- Chapter 8 covers array and collection objects and their respective data and methods. You can skip the material on untyped collections, pp. 242-243 as well as the material on array lists, pp. 254-255.
- Chapter 9 covers data and methods for String and Date data types
The lab assignment is to complete exercises related to Chapter 8 and 9, as shown next.
Exercise 8.1 X Accumulate Test Score Data Using an Array and a List
In this exercise, you’ll create a form that accepts one or more scores from the user. Each time a score is added, the score total, score count, and average score are calculated and displayed.
- Start a new project named ScoreCalculator.
- Add labels, text boxes, and buttons to the default form and set the properties of the form and its controls so they appear as shown above. When the user presses the Enter key, the Click event of the Add button should fire. When the user presses the Esc key, the Click event of the Exit button should fire.
- Declare two class variables to store the score total and the score count.
- Create an event handler for the Add button Click event. This event handler should get the score the user enters, calculate and display the score total, score count, and average score, and reset the focus to the Score text box. You can assume that the user will enter valid integer values and that they will be positive.
- Create an event handler for the Click event of the Clear Scores button. This event handler should set the two class variables to zero, clear the text boxes on the form, and move the focus to the Score text box.
- Create an event handler for the Click event of the Exit button that closes the form.
- Test the application to be sure it works correctly.
Now we are going to modify what has been done so farto store the entered data in an array so that it can be sorted. We will modify the form slightly and following the sort produce a dialog box containing the sorted data. (see below)
- Go ahead and declare a class variable myDatafor an array that can hold up to 20 scores.
- Modify the Click event handler for the Add button so it inserts each score that is entered by the user into the next element in the array. To do that, you can use the score count variable to refer to the next element.
- If you have not done so already, add a Display Scores button that with a Click event that sorts the scores in the array (using a separate method), displays the scores in a dialog box (such as the one shown below), and moves the focus to the Score text box. Be sure that only the array elements that contain scores are displayed.
- Test the application to be sure it works correctly.
Finally: we are going to modify what has been done so far to store the entered data in list. We do this in order to emphasize some of the differences between a list and an array.
- Replace the declaration for the array variable with a declaration for a List<int> object, and delete the class variable for the score count. This count is not needed anymore, why not?
- Modify the Click event handler for the Add button so it adds to the list the score that’s entered by the user. In addition, delete the statement that increments the score count variable you just deleted.
- Modify the Click event handler for the Display Scores button so it sorts the scores in the list and then displays them in a dialog box. Modify the dialog box so that it shows the count of the number of elements in the list. How can you determine the count of these elements given that we are no longer tracking this count.
- Test the application to be sure it works correctly.
NOT REQUIRED FOR Fall 2018 – Go on to Exercise 9.2
Exercise 8.2
I am going to have you skip this one in the interests of time. But if you want to do it, for your own benefit, here are the added directions. The required dialog box for input is shown below on the left. When you have finished the project described in the book, modify it to produce the output dialog box shown at the right (for 5 years worth of calculations).
Exercise 9.1: Work with Dates and Times
For this problem, DO NOT assume your input values are correct. Use try-catch structures to validate all input. Be sure you look over the type and method tables in Chapter 9 carefully. Specifically:
a. Don't forget to use parse to convert a string to a date. Be sure to use a try-catch structure to ensure that the user enters a valid date.
b. When subtracting one DateTime value from another you get a result of type TimeSpan. If you want to see only the days portion of the TimeSpan structure (and not the hours, minutes, or seconds) you have to use the Days property to do so.
c. In computing the age, don't forget to apply the Year property of a DateTime value as needed.
Exercise 9.2: Work with Strings
For this problem we will assume that every name has either 2 or 3 parts each separated from the other by one or more blanks. You may assume each name contains only letters. Every phone number is assumed to have exactly 3 parts each separated from the other by exactly two dashes. Use the split() method to separate the name parts and the phone number parts. If the number of parts for either is not correct tell the user, and ask them to reenter the required data. Set the focus to the data element that is in error. A phone number must contain exactly 10 digits. All other characters will be removed by your software. If the number does not contain 10 digits, catch the error and allow the user to re-enter the number.