Lab 5: Copy Constructors, Static Variables and Methodsdue Monday, May 18 at 11:59pm on Canvas Pair Programming Required or No Credit
Butterfly Database
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 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 ""; } }
/** * 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; } }
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
How You Will Be Graded:
Source of butterfly information |