Java application that reads from an input file Solved

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment

Description

5/5 - (2 votes)
  • The purpose of this is to practice writing a class, creating objects of this class, and storing data in an array of objects of this class.
    1. Java application that reads from an input file, which contains your data from CSV (Comma Separated Values) format, stores the data in an array of objects, and outputs the array to the screen.
    2. The 1st step is to make your CSV file. Open up jGRASP. Cick: File, New, Plain Text, to create a new text file. Then, save the file as a CSV file by adding the .csv file extension when you save it: filename.csv Otherwise, open up a CSV excel file and type in your data. The 1st row should be the column names, separated by commas.
    3. The 2nd step is to write your program! Here is starter code: javaYou will have two classes – your public class LastnameFirstname08 and class HawaiianTheme.

      ** The class HawaiianTheme is the class you choose from Assignment #2, such as class HawaiianPlantsclass HawaiianBirdsclass HawaiianRoyalFamily, or class HawaiianStarCompass. **

      ** The class HawaiianTheme does NOT have the public modifier. **

      ** The class HawaiianTheme is NOT nested inside public class LastnameFirstname08. **

      ** The class HawaiianTheme is a class definition, which contains the variables (data fields) and methods to define objects, which store data. **

    4. class HawaiianThemeis the type of data that you stored in your input file. For example, I am using “Marine Mammals of Hawai’i”, so my class is: class MarineMammalsOfHawaii
    5. The 1st commandline argument (args[0]) is the name of your input file that you just created. Have some error checking to make sure there is only one command argument.
    6. In your main() method, make an array of objects of class class HawaiianTheme.

      Here is an example of an array of String objects:
      String itemNames[] = new String[MAX_SIZE];
      Here is an example of an array of Integer objects:
      Integer itemNumbers[] = new Integer[MAX_SIZE];
      Here is an example of an array of President objects:
      President array[] = new President[MAX];
      Here is an example of an array of MarineMammalsOfHawaii objects:
      MarineMammalsOfHawaii array[] = new MarineMammalsOfHawaii[MAX];

      Display the array on the screen – it should be all nulls at this point.

    7. The next step is to initialize the array with the data from the file. Earlier this semester, we read a file and stored it in an array with this program: java
    8. Before we store data in our array of objects, we have to create data fields, a constructor, and toString() method for class HawaiianTheme.
    9. Below your public class LastnameFirstname08class, create data fields, a constructor, and toString() method for class HawaiianTheme.
    10. Create three data fields – one data field for each of the attributes, which is the same as the three column names, in your class HawaiianTheme. For example, for my class MarineMammalsOfHawaii, I have these data fields: name, population, and length.
    11. Write the constructor for your class HawaiianTheme. You should have a three parameters, which initialize your three data fields. For example, I have three parameters of type String, Integer, and Double in my constructor.
    12. While you read the data from the input file, store the data from each row in the data fields of each object in the array by calling the constructor to initialize each array element.

      Here is an example of initializing an array element for an array of Integers:
      array[i] = new Integer(10);
      Here is an example of initializing an array element for an array of Strings:
      array[i] = new String(“ten”);
      Here is an example of initializing an array element for an array of Presidents:
      array[0] = new President(“George”, “Washington”, 1);
      Here is an example of initializing an array element for an array of MarineMammalsOfHawaii:
      array[0] = new MarineMammalsOfHawaii(“Hawaiian monk seal”, 1100, 2.4);

    13. Write the toString()method for your class HawaiianTheme. The return value should return a String with the three data fields, so they can be displayed.
    14. Here is an example of using the toString() method to output data for an array element:
      out.println(array[i].toString());
    15. See javafor an example of a class, constructor, toString() method, and an array of objects.
  1. Make sure your code follows the ICS 211 Java Coding Standard, in particular the Java documentation (Javadoc) comments that go above each method.
  • Write your original commentsevery 3-5 lines of code.

 

SAMPLE OUTPUT

 

Display MarineMammalsOfHawaii array[] without initializing elements:

index   element

0     null

1     null

2     null

3     null

4     null

5     null

6     null

7     null

8     null

9     null

10     null

11     null

12     null

13     null

14     null

15     null

16     null

17     null

 

Read from input file: mammals.csv

 

Display MarineMammalsOfHawaii array[] after initializing elements:

index   name                        population   length

0     Hawaiian monk seal               1100      2.40

1     humpback whale                  10000     16.00

2     spinner dolphin                  3351      2.35

3     common bottlenose dolphin         235      3.50

4     Risso’s dolphin                 85000      4.00

5     rough-toothed dolphin          150000      2.83

6     striped dolphin               2000000      2.60

7     pygmy killer whale                817     20.50

8     false killer whale                150      2.80

9     melon-headed whale               2950      3.00

10     short-finned pilot whale         8850      3.70

11     sperm whale                      7082     17.30

12     dwarf sperm whale               19000      3.00

13     pygmy sperm whale                  50      3.50

14     orca                               50     10.70

15     Blainville’s beaked whale        2200      5.00

16     Cuvier’s beaked Whale           13000      8.30

17     pantropical spotted dolphin   3000000      2.50

Startercode

/*** Short description of program.* * @author Last Name, First Name* ICS 211 Assignment 8 * Today’s Date*/ public class LastnameFirstname08 {   public static void main(String[] args) {    //error checking for commandline input       //make an array of Nature objects    //output array of Nature objects to the screen (should be “null” for all elements)        //read from file and store data from file in your Nature array of objects    //by initializing each array element using the constructor        //use toString() to display the array again with data from input file         }//end of main() method}// end of class LastnameFirstname08 /** * Class HawaiianTheme stores and displays the data for each HawaiianTheme object *  * @author Your Name */class HawaiianTheme {   //data field #1   //data field #2   //data field #3   //constructor – used to initialize the three data fields   //toString() method – returns a String with the three data fields}//end of class HawaiianTheme

  • Answer-12.zip