You are on page 1of 6

bAndrew Cordell

Elizabeth Aquino

CS 116 Spring 2020 Lab #05


Due: Wednesday, February 26th, 7:00 PM
Points: 20

Instructions:
1. Use this document template to report your answers and create separate java files
for your classes. Enter all lab partner names at the top of first page.
2. You don’t need to finish your lab work during the corresponding lab session.
3. ZIP your Java files and lab report into a single file. Name the file as follows:

LastName_FirstName_CS116_Lab5_Report.zip

4. Submit the final document to Blackboard Assignments section before the due
date. No late submissions will be accepted.
5. ALL lab partners need to submit a report, even if it is the same document.

Objectives:
1. (5 points) Design, code and test programs that require arrays.
2. (7 points) Design, code and test a user-defined object containing an array.
3. (8 points) Design, code and test a user-defined object containing an array.

Problem 1 [5 points]:
Write a program that:
 asks the user how many exam scores there are (and verifies that the user
entered a positive integer),
 prompt the user for each real-valued score, one by one (as shown below in
Sample program behavior / output: box),

1
 Calculate and output the average of the scores and count and output how many
scores are greater than the average (as shown below in Sample program
behavior / output: box).

Sample program behavior / output:

How many exams scores do you have to enter? 5


Enter score #1: 95.0
Enter score #2: 92.0
Enter score #3: 68.0
Enter score #4: 72.0
Enter score #5: 70.0

The average score is: 79.4


There are 2 scores larger than the average.

Your tasks:
 Provide basic design of your program (inputs/outputs, their format, valid ranges,
etc. along with all necessary computation steps) in the box below [1 out of 5
points]:

Program design:

2
 Complete the Test Plan table below (the number of test cases is up to you, but
should be fairly exhaustive) [1 out of 5 points]:

Test plan
Test case Sample data Expected result Verified?

 Code your program [3 out of 5 points].

Problem 2 [7 points]:
Write a DailySales class to store a collection of a company's daily sales records
for a single month (up to 31 days). All daily sales are of integer data type. You do
not have to worry about verifying the number of days actually in the month. Please
code a class with the following methods:

 DailySales() - default constructor,

 DailySales (int daysInMonth) - constructor,

 public boolean addSales(int dayNumber, int sales) - add


"sales" to the current sales for "dayNumber". Return true of successful, else
return false (if invalid sales amount or invalid dayNumber),

 public int maxDay() - return the day number with the maximum sales,

 public int[] daysBelowGoal() - return an array of day numbers that


have less than 100 units sold,

Your tasks:
 Provide basic design of your program (define the fields/attributes and methods,
include data type and valid ranges for attributes, and access control, arguments
and return types for methods) in the box below [1 out of 7 points]:

3
Program design:
public class DailySales {

private int [] salesByDay;


private int [] daysBelow;

public static void DailySales() {


}

public DailySales(int daysInMonth){


}

public boolean addSales(int dayNumber, int sales){


}

public int maxDay() {


}

public int [] daysBelowGoal(){

 Complete the Test Plan table below (the number of test cases is up to you, but
should be fairly exhaustive) [1 out of 7 points]:

Test plan
Test case Sample data Expected result Verified?
Days in month 32 Invalid-input Yes
message
Day number 32 Invalid day message Yes
Sales -1 Invalid sales message Yes

 Code your program [5 out of 7 points].

Problem 3 [8 points]:
This problem will explore some basic traits of an unsorted list of data. We will not go
to the extent of sorting the data, but try to understand some ideas leading to sorting.
Write a class, UnsortedData, that has two "public static" methods:

4
 A method called countOutOfPosition that

 accepts as parameter an array of doubles, and


 returns a count of how far "out of position" all the items are. Do this by
summing up (for each item "i" in the array) how many items after item "i"
in the unsorted list are smaller than item "i".

 A method called bubble that

 takes as parameter and array of doubles, and


 performs the following operation once on every item in the unsorted list: If
an item is larger than the item after it, swap the items.

Write a UnsortedDataApp program, that

 prompts the user for a name of an input file (all the data files are in Blackboard
-> Class Notes -> Lab Code -> Lab #05 section),
 reads 100 real numbers from that file. You can assume the file exists, and
contains 100 valid real numbers.
 Then:
 call "countOutOfPosition method and display the result.

 call "bubble" and your first function again and see if there items are not as
far "out of position" as before.

Here is a sample run:

Sample run:

What is the name of the input file? dataRandom.txt


Total Out of Order BEFORE Bubble = 2604
Total Out of Order AFTER Bubble = 2508

Your tasks:

5
 Complete the Test Plan table below (the number of test cases is up to you, but
should be fairly exhaustive) [1 out of 8 points]:

Test plan
Test case Sample Expected result Verified?
data
dataRando dataRando Total Out of Position BEFORE Bubble = 2604 Yes
m.txt m.txt
Total Out of Position AFTER Bubble = 2508
dataSorted. dataSorted. Total Out of Position BEFORE Bubble = 0 Yes
txt txt
Total Out of Position AFTER Bubble = 0
dataReverse dataReverse Total Out of Position BEFORE Bubble = 4950 Yes
Sorted.txt Sorted.txt
Total Out of Position AFTER Bubble = 4851

 Code your program [7 out of 8 points].

You can see with each run of Bubble, we are removing "Out of Position" counts
(except for the already sorted input of course). If we keep executing "Bubble", we
will eventually sort the data, this is called BubbleSort. The "Out of Position" count is
sort of a measure of how much work the sort needs to do on that input data set.

Make sure you add try-catch sections that handle FileNotFoundException where
appropriate.

You might also like