Home » Java Basics » 05 - Files, Collections, and Exceptions

Screenshot 5a: Running createFruits.class with a command line parameter
Screenshot 5a: createFruits Output
5
Classes - Exercize
How classes interact with each other
Recall the Fruit.java class that was covered in the previous chapter. This class was a non-display class that simply described a Fruit Object's state (through its properties) and behavior (through its methods). A createFruits class will interact with the Fruit class. Sections of code in the createFruits class that have something to do with a concept we will discuss in this chapter will be covered in detail in a latter section.
Broadly, the createFruits.java class does the following
- It takes input from the fruits.txt file. This file contains a path to the fruit images in its first line. After this initial line, it may contain an arbitrary number of lines. Each of these lines contains the fruit name, its valid colors in comma-separated form, the name of its image file, and its calorific value as a semi-colon (';') separated list.
- The input file is passed to the main method of createFruits.java as a command line argument. That is, the class should run in the following way:

Screenshot 5a: Running createFruits.class with a command line parameter
- Note that the path to the input text file is specified after 'createFruits'. The main method may access this path using its input parameter's first element (args[0]).
- The file reads one line of the input file at a time. It sets the image path equal to the initial line.
- Program Execution is halted if the input file format is incorrect. A custom Exception object InputFileFormatException.java handles the error.
- A Fruit Object is created for every subsequent line of the input file.
- A list of all the fruit objects is maintained.
- The list of fruits is iterated and the 'showDetails' method is invoked to display each fruit's information.
Follow these steps to run the createFruits.java file:
- Open the createFruits.java file in the NetBeans 5.5 IDE and go through the source. The code may seem complex but will become clearer as we proceed.
- Replace your copy of Fruit.java with this version.
- Save the InputFileFormatException.java and createFruits.java files in the same directory as Fruit.java.
- Compile all three files using Build -> Compile Filename ->
- Copy the files banana.PNG, apple.PNG, and orange.PNG into a suitable folder (it makes sense to save these in the same directory as the fruits.txt input file)
- Save the fruits.txt input file in a suitable directory
- Open the saved input file and change the first line to reflect the path to the directory where you saved the images. Do not add a '\' or any other path separator to the end of this path.
- Use the fruits.txt input file as a command line parameter and run createFruits.java using the command shown in screenshot 5a (Java createFruits <FullPathtoInputFile>\fruits.txt).
- You should run the command from the MySamples\build\classes subdirectory. You will see the following output after you hit enter.
Screenshot 5a: createFruits Output