Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSCI 1933 Lab 4

IntelliJ Intro, OOP, File I/O, and Databases

Procedures for Lab

Labs will be run synchronously in person at the normally scheduled time.  Attendance at your scheduled lab section is strongly encouraged, but not required in order to accommodate for those who may not be comfortable meeting in person.  You are strongly encouraged to work with a partner within your lab section.  The TAs will assist you in finding one, if needed.  Have a TA evaluate your progress on each milestone before you move onto the next.  The labs are designed to be mostly completed by the end of lab.  If you are unable to complete all of the milestones by the end of lab, you will have until the last office hours on the following Monday to get them checked off by any of the course TAs. We suggest you get your milestones checked off as soon as you complete them since Monday office hours tend to become crowded.  You will only receive credit for the milestones you have checked off by a TA. Regrades of a milestone are available but only if submitted before the last office hours on the following Monday. There is nothing to submit to Canvas for this lab.

Setting up IntelliJ

If you are already comfortable using IntelliJ, you may skip this section, although it is recommended to ensure you are following best practices.

You’ve already received your first project in the class. When creating larger code bases, it can be tedious to switch back and forth from the text editor to the terminal and in between all the code files. This is where an Integrated Development Environment (IDE) like IntelliJ comes in handy.

We recommend using IntelliJ IDEA by Jetbrains for this class, and for later projects and labs this will be the required IDE. IntelliJ is an industry standard that also has a free community version. Instructions for downloading IntelliJ Community Edition can be found here.  Make sure to scroll down to the ”Standalone Installation” section to download just IntelliJ.


Note: Click here to find a PDF explaining the initial setup in more depth.


Now that IntelliJ is downloaded and you have your license registered, you can create a new project from scratch. IntelliJ will find your Java SDK and ask if you want any external libraries. For this lab, we will use native Java, so just click next. Rename the project to lab4 and change the project location to your labs/lab4 directory.

Paste the provided  .java files into the new lab4/src/ folder, and the .csv files into lab4/.  For any IntelliJ project you create, all code should be located in the src folder; this is where the IDE ”looks for”code to be compiled and run. Any external files will be located in the parent directory.

Now you have all your files in place, but IntelliJ still needs to know which main to run. In the top right corner, there is a button to add a new configuration.  Select that then select Application option, and your main class will be Owl for Milestone 1 then OwlPopulation for the rest.  You may add multiple configurations and switch between them on the fly using this button as well. Alternatively, you can go to the Run menu and select Run... to pull up a menu that will create a configuration for your currently open file, as long as it has a

public  static  void main(String[]  args).

Now that IntelliJ knows where your main is, hitting the green hammer and green play button will build and run your project respectively.


Tip: Sometimes when starting a project via opening a folder with IntelliJ or other import methods, the sources module will not be properly defined for the project.   Naming the folder containing your code src” lets IntelliJ identify the folder as containing source code. If the sources of the project are not defined, IntelliJ doesn’t know what files it will need to compile in order to run the configuration you setup earlier.  To fix this, go to File  Project Structure.  Then, navigate to the Modules tab of Project Settings.  Select the folder containing your .java files and click Mark as:  Sources, then apply. You can tell that sources are properly recognized if the symbol next to the file in the IntelliJ file exporer is a blue C (with a green arrow, if it has a main method).


Introduction

Now that you have your IntelliJ all set up, welcome to the fourth lab of CSCI 1933!  One of the tracks offered by the Computer Science Department is Data-Driven Computing.  An important aspect of this is being able to process a large dataset and perform some sort of analysis on it. For this lab, we’re going to be doing some statistical analysis on populations of owls.

1    Owl Class

Let’s start by making an Owl class. Each Owl has its own name, age, and weight, so we need class variables (types String, int, and double respectively) to reflect this.  The first method we need to make is a constructor for the Owl class:

• public  Owl(String  name,  int  age,  double  weight) - this initializes the class variables of each Owl object to the arguments passed in.

It is good practice to make all your class variables private. Once we do this, we also need to make getter and setter methods to access these private variables from outside the Owl class:

• public  String  getName()

• public  void  setName(String  name)

• public  int  getAge()

• public  void  setage(int  age)

• public  double  getWeight()

public  void  setWeight(double  weight)

Finally, include the method:

• public  boolean  equals(Owl  other) - This returns true if the name, age, and weight of the Owl are equal to other’s name, age, and weight. Otherwise, this returns false. Remember to use the equals method to compare Object types like String, and use == to compare primitive types such as double and int.


Milestone 1:

In the main method, instantiate a few owls, then demonstrate your equals method working. For example, if your main method contains this code:

Owl  owl1  =  new  Owl("owl1",  5,  12 .0);

Owl  owl2  =  new  Owl("owl2",  5,  12 .0);

Owl  owl3  =  new  Owl("owl1",  5,  12 .0);

System .out .println(owl1 .equals(owl2));

System .out .println(owl1 .equals(owl3));

Then your output should be:

false

true


2    OwlPopulation Class

Next, we’ll need to create an Object type to model a population of owls.  So far in class, you’ve probably been dealing primarily with arrays of primitive types, such as int[].  However, Java arrays are capable of storing any type, including Object types defined in classes.  For example, if we have a collection of Owl objects that we would like to consider as a single population, we can store them in an array. This array’s type will be Owl[]. Each index in that array will store a single Owl object, and you will be able to access each owl in the array using array notation. For example, if we have an array of owl objects called owlArray and would like to change the age of the owl in index 0, we can use owlArray[0] .setAge().

For this milestone, modify the given OwlPopulation class.   This class will have the variables private  String  fileName and private  Owl[]  data.   The OwlPopulation constructor should take in a file name as a parameter and call the following method.

• public  int  populateData() - this will read in the CSV file and construct the Owl objects to put into the OwlPopulation’s data attribute. populateData should return the number of Owls in the OwlPopulation.

In order to read in the CSV file, we will again utilize the Scanner class. The populateData method is partially filled out for you. It already reads in a file line by line. You may modify this code to use in your OwlPopulation class.


Hint: A CSV file (comma-separated-value file), as its name suggests, separates each value with a comma.  In this file, each line contains the  [name, age, weight] attributes for an individual owl.

The following methods may be useful to you in parsing the file:

•  split(String  delimiter) is called on a String.  It splits the string into an array of substrings delimited by an argument passed in.

•  Integer .parseInt(String  num) and Double .parseDouble(String  num) both take in a String object and return an int and double version of the String input respec- tively.



Note: The two CSV files can be found here. Make sure to open both CSV files in IntelliJ and observe their layout. Microsoft Excel, Google Sheets, and the file preview in Canvas do not show them accurately!  Additionally, remember to put the CSV files in the top level folder for this lab, lab4. This will be important for interpreting the file path correctly.



Milestone 2:

Show an OwlPopulation being constructed correctly using the provided CSV files. Print the number of owls in the population to the console. For reference, Population 1 should contain 89 members, while Population 2 should contain 67.


3    Taking Statistics on an OwlPopulation

Fill in the following methods to collect statistics on the OwlPopulation.

• public  double  averageAge() - calculates and returns the average age of all the owls in the population.


Note: This function returns a double, while an Owl’s age attribute is of type int. Make sure you aren’t rounding the average age to an int.


• public  Owl  getYoungest() - returns the youngest Owl in the OwlPopulation. If no owl is found, return null.

• public  Owl  getHeaviest() - returns the heaviest Owl in the OwlPopulation.  If no owl is found, return null.

• public  String  toString() - returns a summary of the population.  The summary should include the name and age of the youngest Owl, the name and weight of the heaviest Owl, and the average age of the owls in the population. The exact format of the summary is up to you.


Milestone 3:

Make a main method in your OwlPopulation class and demonstrate your statistic collection working - construct OwlPopulation objects from the two files supplied and print the result of calling toString() on each object.

So that you can determine the accuracy of your methods, the output for population 1 should look something like this (although formatting may differ depending on your implementation):

The  youngest  owl  is  Owl1,  which  is  1  years  old .

The  heaviest  owl  is  Owl89  ,  which  weighs  9 .9  pounds .

The  average  age  of  the  population  is  45 .0 .

Population 2 should look something like this:

The  youngest  owl  is  Hedwig,  which  is  3  years  old .

The  heaviest  owl  is  Buckbeak,  which  weighs  96 .87  pounds .

The  average  age  of  the  population  is  32 .88059701492537 .



Reflection:   What  are  the  time  complexities  of  averageAge(),  getYoungest(),   and getHeaviest()?


4    Merging OwlPopulations

Write the method in the OwlPopulation class

• public  void merge(OwlPopulation  other)

which takes in an OwlPopulation and merges it with the population it is called on. Take care not to include duplicate owls” in your population.


Hint: The equals() method written for the first milestone may be useful in implementing the helper containsOwl(Owl  other) method.



Milestone 4:

In your main method, merge OwlPopulations and print the new statistics of the merged population to the console.  Where do the original populations go?  Where is the ”merged” population stored?

So that you can determine how well your merge method is working, your output should look something like this:

The  youngest  owl  is  Owl1,  which  is  1  years  old .

The  heaviest  owl  is  Buckbeak  ,  which  weighs  96 .87  pounds .

The  average  age  of  the  population  is  38 .81060606060606 .

This merged population should have 132 members.



Reflection: Assuming that the size of the data set is n  (the total number of owls in the two databases combined), try to quantify the time complexity of your merge() algorithm in terms of n. You may wish to approach this by comparing to the three sort methods given in lecture which are all n2  complexity.


Writing OwlPopulation to CSV (Honors Section)

This section and milestone are only required for students in the honors section.  However, students in other sections are still encouraged to work on this if they are interested and time permits .

Until now you have been reading in OwlPopulations from provided CSV files. In this milestone, you will be asked to write a new version of the merge method that merges the owl population and then writes the new population and statistics back to a CSV file. To do so, we will fill in the method   public  void merge(OwlPopulation  other,  String  fileName) which takes in an                     OwlPopulation and the name of the file to create.  Note that the fileName string must end with " .csv".

Since we already have a merge(OwlPopulation  other), this is an example of method  over- loading. This means that in Java multiple methods can have the same name as long they take in different arguments.

To create a new file, we first create a File object:  File  f  =  new  File(fileName); Next, we call the File object’s createNewFile() method.  This method will actually create a file that you can write to.  This method returns false if the file name already exists, and returns true if the fileName does not exist and has been successfully created. You do not need to store the result of createNewFile(), just be aware that it is there.

We will use the FileWriter class from java .io to write to the CSV file. Instantiate a FileWriter object like this: FileWriter  fw  =  new  FileWriter(f); to get an object that can append to the file. You can do so using the append(String  str); method. Create a CSV file that matches the format of the provided CSV files with each Owl’s name, age, and weight. After you’re done writing, make sure to call fw .flush(); and fw .close();.


Milestone 4:

Create the overloaded merge method to write the name, age, and weight of each Owl to a CSV file. Show a TA your new merge method and the CSV file that it creates.