Colorado State University Logo | CS 150: Culture and Coding (AUCC 3B/GT-AH3) Colorado State University Logo | CS 150: Culture and Coding (AUCC 3B/GT-AH3)
CS 150: Culture and Coding (AUCC 3B/GT-AH3)
Computer Science

Lab 08 - Salary Data Analysis

In this lab you will review and implement a light analysis of majors from their salary data. The purpose of this lab is to practice writing a loop and your code tracing/reading skills. In the second portion of this lab, you will identify various issues within the program and correct them yourself.

You will learn:

  • How to write a loop
  • How to read a program

Step 0 - Understand the Purpose

Your first step is to go through the program and understand the flow and purpose of it. Read the documentation provided above each method. You don’t need to understand every line but understand the general objective of each method.

SalaryAnalysis.java is the class you will be working in to preform calculations and to display results. SalaryData.java is a program we wrote for you that extracts all the data from a file so you can use it in your SalaryAnalysis.java. The file that contains the data is salary_data.csv.

Step 1 - Write the listAll() Method

This method will cycle through all salaries in order. To allow reading to be easier, you should call prettyPrint(int) on every major. It is useful to know that calling data.size() will return the total number of majors that are being tracked in the salary data. If you have 27 majors, it will return 27, even though you should loop from 0 to 26.

Step 2 - Write the listAll() Method Signature

listAll() is a method that is public non-static that returns nothing and has no parameters. Don’t forget your beginning and ending curly braces! It should look something like this:

public void listAll()

When a method is non-static you do not include a static or non-static keyword after public.

Step 3 - Implement the listAll() Method

First, print out getHeader() with a new line so when you run listAll() the user will see a key to the info displayed. This will look like:

DataID.	Major	Avg. Male Salary	Avg. Female Salary	Avg. White Salary	Avg. PoC Salary	Salary	Male %	Female %	White %	PoC %

Now, write a for loop to start at the beginning of our collection of elements called data, stop at the end of data and increments one at a time.

Collection of data is a new topic we are lightly introducing now (we will go over it more later) so to help you out data.size() will return the length of data. This return value is very similar to .length() for Strings. Keep in mind that length is not zero based so a collection of 5 elements will return 5, not 4 (the last index).

NOTE: data does not need to be re-declared since it is a class variable which can be used anywhere in the program with having to be passed as a parameter.

For each iteration of the for loop(inside the for loop) you will simply call prettyPrint() with your for loop variable as the parameter.

prettyPrint() takes an int that represents which individual salary the method will print to the console.

For more help writing for loops reference this link.

Testing listAll()

You will not be able to test listAll yet because there are several bugs in the program that you will need to find and fix before listAll can run correctly. Once you have figured out all the bugs select the option to run listAll and test your method.

More on Salary Analysis of Majors – Debugging!

In the rest of this lab you will complete your analysis of majors, their demographics and salary data. The purpose of this portion is to imagine you finished writing an entire program but when you run it, you get a lot of errors. Now, your job is to debug the program by reviewing and testing each method one by one.

You will learn:

  • How to debug your own program
  • How to read program errors
  • How to test
  • More practice with conditionals and for loops

Step 4 - Understand the Purpose

Your first step is to go back through the program and understand the flow and purpose of it. Read the documentation provided above each method. You don’t need to understand every line but understand the general objective of each method. Doing so will help you identify when a bit of code isn’t doing what the method it resides in intended to do.

Step 5 - Find the Five Bugs

There are five bugs dispersed throughout the program. Each method will have one or zero bugs. The methods with // NO BUGS IN HERE next to its definition will not have a bug in it. All other methods are fair game.

Types of Bugs

There are logic bugs and syntax bugs. In this program, there are two syntax and three logic bugs.

Syntax bugs are simply improperly typed statements. According to webopedia, syntax refers to the spelling and grammar of a programming language. Computers are inflexible machines that understand what you type only if you type it in the exact form that the computer expects. The expected form is called the syntax. An example of this are missing semi-colons after java statements, wrong casing of letters, forgetting to cast or …

System.out.println(Hello World);

This program would not run or compile due to the syntax error. To properly run this code and have it print Hello World there would need to be double quotes around the Hello World like so…

System.out.println("Hello World");

Unlike syntax errors, logic bugs are ones that allows the program to run and not crash but the output is not what your expected. An example of this is…

int bigNum = 100;
int smallNum = 1;
if(bigNum < smallNum){
    System.out.println("bigNum is bigger than smallNum.");
}

This snippet of code wouldn’t print "bigNum is bigger than smallNum." but ran with no error. However, as the programmer I would have expected it to print but I used the less than equality sign < so it printed nothing. To fix the bug all I would need to do is change the if statement to be if(bigNum > smallNum) for the print statement I was expecting.

Your fun fact for the day is that computer scientist and U.S. Navy Rear Admiral Grace Hopper coined the term “bug” relative to computer errors after finding a moth inside a Mark II calculator between two relays. Naturally, “debugging” followed soon after.

Step 6 - Fix the Bugs

Once you have found the bugs, fix one at time and then test that method by using the testMethod() or just running your program.

If you haven’t found and fixed every bug, you may still have some error messages. Your error messages may be hard to read but they can be helpful. The line number in the message provides you with a line number that can help you pin point where the bug is (on or near the line number). Here’s a helpful resource for understanding your error messages!

Step 7 - Running Your Program

Let’s just verify that our program runs and compiles just fine to determine if you fixed all the bugs. When you hit run you should see this…

Salary Data Viewer 1.0
Main Menu
1. List All
2. Find Highest
3. Print Sublist
4. Find differences
0. Exit program
Select an option: 

Type in 1 and hit the enter key to navigate to the list all method on the menu. If you haven’t implemented the method listAll yet it will just print out the same menu again.

Step 8 - Test your program

Test your implementation of listAll(). Your output now for listAll will start like this…

DataID.	Major	Avg. Male Salary	Avg. Female Salary	Avg. White Salary	Avg. PoC Salary	Salary	Male %	Female %	White %	PoC %
0.	Agriculture / Animal Sciences	$57,028.99	$47,437.97	$55,829.54	$47,817.34	$54,624.16	74.93%	25.07%	84.96%	15.04%	
1.	Architecture	$65,491.80	$54,785.70	$62,433.97	$58,735.77	$61,601.40	63.66%	36.34%	77.49%	22.51%	
2.	Arts	$50,186.12	$40,908.64	$44,028.95	$46,841.09	$44,447.16	38.14%	61.86%	85.13%	14.87%
[more not shown!]

We implemented the Find Highest with our findHighestSalary() method. To see what that method does type in 2 and hit enter. You should see:

DataID.	Major	Avg. Male Salary	Avg. Female Salary	Avg. White Salary	Avg. PoC Salary	Salary	Male %	Female %	White %	PoC %
10.	Engineering	$69,974.04	$59,073.86	$68,760.73	$66,598.52	$68,105.95	82.86%	17.14%	69.72%	30.28%	

The output for Print Sublist will start like this in the console for input 60000 ($60,000)…

Select an option: 3
Enter a whole dollar amount (10000) to see salaries above that amount: 60000


DataID.	Major	Avg. Male Salary	Avg. Female Salary	Avg. White Salary	Avg. PoC Salary	Salary	Male %	Female %	White %	PoC %
1.	Architecture	$65,491.80	$54,785.70	$62,433.97	$58,735.77	$61,601.40	63.66%	36.34%	77.49%	22.51%	
5.	Chemistry	$65,215.35	$58,228.48	$63,011.91	$59,692.27	$62,183.92	56.61%	43.39%	75.06%	24.94%

The output for Find differences will look like this in the console for input 1 (men and women) and 6 (computer science)…

The difference between Gender Salaries for Computer Science is 15%

Now to exit the program type 0 and then hit the enter.

Step 9 - Turning in

Once you have run the program and are satisfied it is working, you may submit for grading. Note that you only get five submission attempts, so make sure it is mostly working before you submit. We will bypass the main method in our tests, running our own tests on each method you wrote.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

CS 150: Culture and Coding (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.