Lab 5: Copy Constructors, Static Variables and Methods

due Monday, May 18 at 11:59pm on Canvas

Pair Programming Required or No Credit
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your Lab code to Canvas.
  • Only ONE partner submits the Lab code on Canvas.
  • BOTH partners submit the contract.
  • Please make sure both your names are on all the files.
  • If you need help finding a partner, please contact me as soon as possible.

Butterfly Database

  • Write a program that acts as a database for butterfly species that offers 4 menu options - View, Add, Remove and Quit
  • You will create two classes - a Butterfly class and a Butterfly database class - and work with a text file of butterflies.
  • Along the way, you will practice writing and calling a copy constructor, using this to avoid shadowing, and work with static member variables and methods.
  • Start by copying and pasting the below data into a text file called butterflies.txt:
butterflies.txt text file:

Swallowtail
Black Swallowtail
Black
Swallowtail
Western Tiger Swallowtail
Yellow
Swallowtail
Pipevine Swallowtail
Blue
Brush-footed Butterfly
Viceroy
Orange
Brush-footed Butterfly
Monarch
Orange
Brush-footed Butterfly
Julia Heliconian
Orange
Brush-footed Butterfly
Variegated Fritillary
Tan
Pieridae
Checkered White
White
Pieridae
Clouded Sulphur
Yellow
Pieridae
Cabbage White
White
Gossamer Wing
Gray Hairstreak
Gray
Gossamer Wing
Reakirt's Blue
Blue

  • Next, copy and paste the below starter code into a file named Butterfly.java

Butterfly.java starter code:

/**
 * Butterfly class
 * @author
 * @author
 * CIS 36B, Lab 5
 */

public class Butterfly {
    private String family;
    private String species;
    private String color;
    private int speciesNumber;
    private static int speciesNum = 550;
   
    /**
     * Default constructor for Butterfly
     * Assigns the following default values
     * family: "Family unknown"
     * species: "Species unknown"
     * color: "Color unknown"
     * speciesNumber: 0
     */
   
    public Butterfly() {
       
    }
   
    /**
     * Multi-argument constructor for Butterfly
     * @param family the family of butterfly
     * @param species the butterfly species
     * @param color the principle wing color
     * speciesNumber assigned to 0
     */
    public Butterfly(String family, String species, String color) {
       
    }
   
    /**
     * Creates a new butterfly that is a copy
     * of b. Except speciesNumber is assigned 0
     * @param b another Butterfly
     */
    public Butterfly(Butterfly b) {
       
    }
   
    /**
     * Accesses the family of butterfly
     * @return the family of butterfly
     */
    public String getFamily() {
        return "";
    }
   
    /**
     * Accesses the species of butterfly
     * @return the species of butterfly
     */
    public String getSpecies() {
        return "";
    }
   
    /**
     * Accesses the color of butterfly
     * @return the color of butterfly
     */
    public String getColor() {
        return "";
    }
   
    /**
     * Accesses the assigned species number
     * @return the species number
     */
    public int getSpeciesNumber() {
        return -1;
    }
   
    /**
     * Assigns a new family of butterfly
     * @param family the butterfly family
     */
    public void setFamily(String family) {
       
    }
   
    /**
     * Assigns a new species of butterfly
     * @param species the butterfly species
     */
    public void setSpecies(String species) {
       
    }
   
    /**
     * Assigns a new color of butterfly
     * @param color the principle wing color
     */
    public void setColor(String color) {
       
    }
   
    /**
     * Assigns a new species number
     * @param speciesNumber the number generated
     * by generateSpeciesNumber variable
     */
    public void setSpeciesNumber(int speciesNumber) {
       
    }
   
    /**
     * Used to generate a new species number
     * for each butterfly. To be called
     * each time a new buttefly object is
     * created (constructor is called).
     * Increments speciesNum and returns it
     * @return the species number
     */
    public static int generateSpeciesNumber() {
        return -1;
    }
   
    /**
     * Creates a String of butterfly information
     * Species: <species>
     * Family: <family>
     * Color: <color>
     * Species Number <speciesNumber>
     * @return the butterfly information
     */
    @Override public String toString() {
        return "";
    } 
}
  • Note that you are not allowed to change the signatures of any of the given methods.
    • You are only allowed to write the method bodies for the given methods
    • No additional methods or other alterations allowed or you will not receive credit for this assignment
  • Lastly, copy and paste the below starter code into a file named ButterflyDatabase.java:
/**
 * ButterflyDatabase.java
 * @author
 * @author
 * CIS 36B, Lab 5
 */
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class ButterflyDatabase {

    private ArrayList<Butterfly> butterflies = new ArrayList<Butterfly>();
   
    public static void main(String[] args) throws IOException{
        File file = new File("butterflies.txt");
        Scanner input = new Scanner(file);
        String species, family, color, choice = "";
       
        //read in data from the file here
       
        input.close();
        input = new Scanner(System.in);
        System.out.println("Welcome to the Butterfly Database!\n");
        while (!choice.equalsIgnoreCase("q")) {
            System.out.println("Menu:");
            System.out.println("A. Add a butterfly");
            System.out.println("R. Remove a butterfly");
            System.out.println("V. View butterfly family members");
            System.out.println("Q. Quit");
            System.out.print("\nEnter your choice: ");
            choice = input.nextLine();
            if(choice.equalsIgnoreCase("A")) {
                System.out.println("\nSelect from one of our accepted families of butterfly: ");
                System.out.println("\tBrush-footed Butterfly");
                System.out.println("\tGossamer Wing");
                System.out.println("\tPieridae");
                System.out.println("\tSwallowtail");
                System.out.print("\nEnter your choice: ");
                choice = input.nextLine().toUpperCase();
                int index = linearFamilySearch(butterflies, choice);
                if(index != -1) {
                    //call the copy constructor here!
                    System.out.print("Enter the species: ");
                    species = input.nextLine();
                    b.setSpecies(species);
                    System.out.print("Enter the color: ");
                    color = input.nextLine();
                    b.setColor(color);
                    //generate the new species number here by *correctly* calling static method
                    //set the species number of b to be this new number
                    //add b to the butterflies ArrayList
                    System.out.println("\nThe following butterfly has been added:\n" + b + "\n");
                } else {
                    System.out.println("Sorry, " + choice + " is not an accepted family!\n");
                }   
            } else if (choice.equalsIgnoreCase("R")) {
                //fill in here
            } else if (choice.equalsIgnoreCase("V")) {
                //fill in here
            } else if (choice.equalsIgnoreCase("Q")) {
                File outfile = new File("butterflyList.txt");
                //fill in here to write to file
                System.out.println("\nGoodbye!");
            } else {
                System.out.println("\nInvalid choice.\n");
            }
        }
        input.close();
    }
   
    /**
     * Searches an ArrayList of Butterfly objects for
     * all Butterfly objects of the given family
     * @param al the ArrayList of Butterfly objects
     * @param family the family to search for
     * @return an ArrayList containing ALL butterfly objects
     * belonging to the given family.
     */
    public ArrayList<Butterfly> findAFamily(ArrayList<Butterfly> al, String family) {
        return null;
    }
   
    /**
     * Searches an ArrayList of Butterfly objects for
     * the first Butterfly object of the given family
     * @param al the ArrayList of Butterfly objects
     * @param family the family to search for
     * @return the index in the ArrayList of the first Butterfly
     * belonging to the given family
     */
    public int linearFamilySearch(ArrayList<Butterfly> al, String family) {
        return -1;
    }
   
    /**
     * Searches an ArrayList of Butterfly objects for the
     * Butterfly whose species number matches the given
     * speciesNumber
     * @param al the ArrayList of Butterfly objects
     * @param speciesNumber the species number
     * @return the index of the Butterfly with
     * the matching speciesNumber in the ArrayList
     */
    public int linearSpeciesNumberSearch(ArrayList<Butterfly> al, int speciesNumber) {
        return -1;
    }
}

  • Fill in the missing lines of code as directed above, as well as the method bodies for the 3 given methods.
  • Note that you are not allowed to alter the given starter code, except for the following cases:
    • You may fill in the indicated missing lines, and method bodies.
    • You may make changes to method calls.
    • However, please do not change the method signatures themselves.
  • When your program is completed, it should work as shown in the sample output below.
  • Note that as a final step, the program should write out the butterflies to a text file called butterflyList.txt
  • Please pay careful attention to any input validation and error checking as demonstrated in the sample output.
Sample Output:

Welcome to the Butterfly Database!

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: H

Invalid choice.

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: I

Invalid choice.

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: V

Select from one of our accepted families of butterfly:
    Brush-footed Butterfly
    Gossamer Wing
    Pieridae
    Swallowtail

Enter your choice: Pierswakkis
Sorry, PIERSWAKKIS is not an accepted family!

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: V

Select from one of our accepted families of butterfly:
    Brush-footed Butterfly
    Gossamer Wing
    Pieridae
    Swallowtail

Enter your choice: PieRIdaE

Butterflies of the PIERIDAE family:

Species: Checkered White
Family: PIERIDAE
Color: White
Species Number: 558

Species: Clouded Sulphur
Family: PIERIDAE
Color: Yellow
Species Number: 559

Species: Cabbage White
Family: PIERIDAE
Color: White
Species Number: 560

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: Z

Invalid choice.

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: A

Select from one of our accepted families of butterfly:
    Brush-footed Butterfly
    Gossamer Wing
    Pieridae
    Swallowtail

Enter your choice: PIERIddhksjf
Sorry, PIERIDDHKSJF is not an accepted family!

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: A

Select from one of our accepted families of butterfly:
    Brush-footed Butterfly
    Gossamer Wing
    Pieridae
    Swallowtail

Enter your choice: pieridae
Enter the species: Sleepy Orange
Enter the color: Orange

The following butterfly has been added:
Species: Sleepy Orange
Family: PIERIDAE
Color: Orange
Species Number: 563

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: V

Select from one of our accepted families of butterfly:
    Brush-footed Butterfly
    Gossamer Wing
    Pieridae
    Swallowtail

Enter your choice: Pieridae

Butterflies of the PIERIDAE family:

Species: Checkered White
Family: PIERIDAE
Color: White
Species Number: 558

Species: Clouded Sulphur
Family: PIERIDAE
Color: Yellow
Species Number: 559

Species: Cabbage White
Family: PIERIDAE
Color: White
Species Number: 560

Species: Sleepy Orange
Family: PIERIDAE
Color: Orange
Species Number: 563

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: R
Enter the Species Number: 1
Sorry, we don't have a butterfly of that number!

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: R
Enter the Species Number: 559
Removing...
Species: Clouded Sulphur
Family: PIERIDAE
Color: Yellow
Species Number: 559

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: V

Select from one of our accepted families of butterfly:
    Brush-footed Butterfly
    Gossamer Wing
    Pieridae
    Swallowtail

Enter your choice: Pieridae

Butterflies of the PIERIDAE family:

Species: Checkered White
Family: PIERIDAE
Color: White
Species Number: 558

Species: Cabbage White
Family: PIERIDAE
Color: White
Species Number: 560

Species: Sleepy Orange
Family: PIERIDAE
Color: Orange
Species Number: 563

Menu:
A. Add a butterfly
R. Remove a butterfly
V. View butterfly family members
Q. Quit

Enter your choice: Q

Goodbye!

Matching butterflyList.txt out file:

Species: Black Swallowtail
Family: SWALLOWTAIL
Color: Black
Species Number: 551

Species: Western Tiger Swallowtail
Family: SWALLOWTAIL
Color: Yellow
Species Number: 552

Species: Pipevine Swallowtail
Family: SWALLOWTAIL
Color: Blue
Species Number: 553

Species: Viceroy
Family: BRUSH-FOOTED BUTTERFLY
Color: Orange
Species Number: 554

Species: Monarch
Family: BRUSH-FOOTED BUTTERFLY
Color: Orange
Species Number: 555

Species: Julia Heliconian
Family: BRUSH-FOOTED BUTTERFLY
Color: Orange
Species Number: 556

Species: Variegated Fritillary
Family: BRUSH-FOOTED BUTTERFLY
Color: Tan
Species Number: 557

Species: Checkered White
Family: PIERIDAE
Color: White
Species Number: 558

Species: Cabbage White
Family: PIERIDAE
Color: White
Species Number: 560

Species: Gray Hairstreak
Family: GOSSAMER WING
Color: Gray
Species Number: 561

Species: Reakirt's Blue
Family: GOSSAMER WING
Color: Blue
Species Number: 562

Species: Sleepy Orange
Family: PIERIDAE
Color: Orange
Species Number: 563



What to Submit:

  • When you are finished, upload Butterfly.java, ButterflyDatabase.java and your pair programming contract to Canvas.
  • Please make sure that your names are on both source files in a Javadoc comment which correctly uses the @author tag.
  • Please remove all package statements before submitting (-5 pts)
  • Please do not submit as a zip file (-5 pts)
  • Please make sure your code is properly formatted (-5 pts)
    • Go to Source->Format on Eclipse to autoformat your code
  • -10 for any warnings in your code (which will indicate incorrect use of static methods/variables)


How You Will Be Graded:

  • 100 points: The assignment works as shown in the sample output. All required features are implemented and implemented correctly.
  • 80-100 points: The assignment works mostly as shown in the sample output. Most required features are implemented and implemented correctly. Wording or spacing choices may differ from those shown.
  • 40-80 points: Some of the assignment works as shown in the sample output. Some required features are implemented and implemented correctly.
  • 10-40 points: Serious problems in implementation and little correspondence between sample output and what is shown. Few of the features implemented or implemented correctly.
  • 0 points: Code does not compile or outputs an error at runtime, or the starter code was altered from what was provided (rather than added on to).

Source of butterfly information