This is my first time writing an array of objects and I’m having an issue putting it all together.
I have written two classes and the third program that is supposed to test the classes.
***PROBLEM***
My primary issue at the moment is that my program to test the classes has to read data from an external file and place it into an array.
The program to test my classes creates an instance of an array of objects that is initially empty.
Next it needs to pull what data is in the external file and place it in the array (for loop)
I’m not sure how to loop each line form the file into the array.
My second problem is going to be once i have the external file into my array how do i pull each array value out of the index and write it back to the same file
again i know it needs to be a loop but I'm not sure how to write a for statement like this
for(int index = 0; index < DVDcollection(indexvalue of array); index++)
because the size of the array is stored in the class how do i say for given array index?
Below is a description of what my array of objects will require followed by what code I have so far.
Program 7: Creating an Array of Objects - Part I
Using the Library Books program as a guide, write your own program that allows you to create a database of your own.
Include the following in your program:
1. Create a Class representing the information in one item of your database. The information for one item must include a minimum of 3 fields. Within the Class for one item, include the following methods:
a. A constructor that accepts the information about one item and assigns it to the instance data of the method.
b. A method that prints the information about one item of your collection. Name this method toString. NOTE: It is important that you name this method toString because it's one of Java's built-in methods that allows you print out your Object.
*******MY CODE FOR THIS SECTION*********
//Prologue Section
/**********************************************************************
*Program Name: CSC 111 Program 7
*Author: Peter Welch
*Date: 4/24/11
*Course/Section: CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVD
*
* Class constructor will initialize the following:
* Instance Data
* 1. Title
* 2. Genre
* 3. Rateing
* 4. Description
* Class will include a separate method to perform each of the following:
* 1. toString() will return instance data for object.
*
*Initial Algorithm:
*
* BEGIN Program 7
*
* Public DVD()
* Public toString()
*
* END Program 7
*********************************************************************/
//Pre-Processor Declaration Section
public class DVD
{
private String Title;
private String Genre;
private double Rateing;
private String Description;
String Result;
public DVD(String dvdname, String type, double quality, String info) //******CONSTRUCTOR TO INITIALIZE TITLE, GENRE, RATEING, AND DESCRIPTION******//
{// START CONSTRUCTOR
Title= dvdname;
Genre= type;
Rateing=quality;
Description=info;
} // END CONSTRUCTOR
public String toString() //***** METHOD PROVIDES A STRING REPRESENTATION OF CLASS OBJECT******//
{//START toSTRING
String Result="Title :\t"+Title+ "\nGenre :\t"+Genre+"\nRateing :\t"+Rateing+"\nDescription:\n\t"+Description;
return Result;
}//END toSTRING
}//END DVD CLASS
2. Create another Class with an array of Objects of the Class of one item with the following methods:
a. Create an array of the Objects from the Class of one item.
b. Create a constructor to initialize the number of items in the array to zero.
c. Create a method to add one item at a time to the array.
d. Create a method to display the contents of the entire array. Call this method toString.
e. Create a method to increase the size of the array if it is getting full.
*******MY CODE FOR THIS SECTION*********
//Prologue Section
/**********************************************************************
*Program Name: CSC 111 Program 7
*Author: Peter Welch
*Date: 4/24/11
*Course/Section: CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
* Initial Algorithm:
*
* public Class DVDBOOK-Array of objects.
* {
* Instance data:
*
*
* DVDBOOK() - constructor
* {
* Initializes number of items in the array to zero.
* }
*
* ADDDVD()
* {
* Method Adds one Item to the collection.
* }
*
* toString()
* {
* Method displays contents of the entire array.
* }
*
* arrayPlus()
* {
* Mehtod to increase the size of the array if ti is gettign small.
* }
* } end DVDBOOK
*
*********************************************************************/
//Pre-Processor Declaration Section
public class DVDBOOK // START DVDBOOK CLASS
{
//***INSTANCE DATA***//
final int MAX_DVDS= 100; // SIZE OF DVDBOOK ARRAY
private DVD[] BOOK = new DVD[MAX_DVDS]; //DELCARATION OF ARRAY DVD
private int count;
//******CONSTRUCTOR: CREATES AN INITIALLY EMEPTY ARRAY******//
public DVDBOOK()
{
count=0;
}
//******ADDDVD() METHOD ADDS ONE DVD ITEM TO THE COLLECTION****//
public void ADDDVD(String Title, String Genre, double Rateing, String Description)
{
if (count == BOOK.length)
arrayPlus();
BOOK[count] = new DVD(Title, Genre, Rateing, Description);
count++;
}
//******toSTRING() METHOD TO DISPLAY CONTENTS OF ENTIRE ARRAY******//
public String toString()
{
String report="MY DVDBOOK \n\nNumber of DVD's: "+count+"\n\n";
for(int dvd = 0; dvd < count; dvd ++)
report+=BOOK[dvd]+"\n";
return report;
}
//******arrayPLUS()METHOD INCREASES ARRAY SIZE IF IT IS FULL******//
public void arrayPlus()
{
DVD[] temp = new DVD[BOOK.length*2];
for(int dvd = 0; dvd < BOOK.length; dvd ++)
temp[dvd] = BOOK[dvd];
BOOK = temp;
}
}//END DVDBOOOK CLASS
;
3. Create a Driver Class with a Menu with the option to add an item, to print out a list of all items, and exit.
4. When your program is first executed, read the data from an external file and place it in your array. Submit the external file with program.
5. Save the data back to the external file when the Exit option is chosen from the menu in the Drive.
NOTE: You MUST read your data from an external file! You MUST save your data back to that external file when done.
*******MY CODE FOR THIS SECTION*********
//Prologue Section
/**********************************************************************
*Program Name: CSC 111 Program 7
*Author: Peter Welch
*Date: 4/24/11
*Course/Section: CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
* Initial Algorithm:
*
* public Class DVDCOLLECTION
* {
* Instance data:
* CREATE INSTANCE OF DVDBOOK
*
* OPEN FILE:
*
* FILE READER
* BUFFERED READER
* BUFFERED WRITER
*
* PLACE FILE TOKENS INTO INSTANCE OF DVDBOOK
*
*
* do
* {
* DISPLAY MENUE WITH OPTIONS
* 1. ADD A DVD
* 2. DISPLAY COLLECTION
* 3. EXIT
*
* READ INPUT FROM USER
*
* }
* IF (INPUT1)
* {
* ADD A OBJ.DVD() TO DVDBOOK[]
* }
* IF (INPUT2)
* {
* TOSTRING()
* }
* IF (INPUT3)
* {
* EXIT
* }
* WHILE(INPUT!=3)
*
* WRITE DVDBOOK TO FILE
*
* CLOSE FILE
*
* } END DVDCOLLECTION
*
*
*
*
*********************************************************************/
//Pre-Processor Declaration Section
import java.io.*;
import java.util.*;
public class DVDCOLLECTION
{
public static void main (String args[])
{// BEGIN MAIN METHOD
//*****INSTANCE DATA******//
DVDBOOK DVDcollection = new DVDBOOK(); //***INSTANCE OF DVDBOOK CLASS***//
int menuItem;
String Title,Genre,Description;
double Rateing;
char sure;
//**********************READS EXTERNAL FILE INTO ARRAY*******************************//
//*******PROBLEM STARTS HERE*******//
for (int index = 0; index < DVDBOOK.length; index++)//
{
try
{//***START TRY***//
String inPutFile="TESTDATA";
FileReader fRead = new FileReader(inPutFile);
BufferedReader bRead = new BufferedReader(fRead);
String Line = bRead.readLine();
StringTokenizer Tokenizer;
while(Line !=null)
{
Tokenizer = new StringTokenizer(Line);
DVDcollection[index] = Tokenizer.nextToken()+Tokenizer.nextToken()+Tokenizer.nextToken()+Tokenizer.nextToken();
Line = bRead.readLine();
}
bRead.close(); //***CLOSE FILE***//
}//***END TRY***///
catch (IOException exception)
{// START CATCH
System.out.println(exception.getMessage());
}//END CATCH
//************************************************************************************//
//*******************************DISPLAY MENU*****************************************//
do
{
System.out.println ("\n\n\nSelect from the following: \n");
System.out.println ("1. Add a DVD\n");
System.out.println ("2. Display List of DVD's\n");
System.out.println ("3. Exit\n\n");
menuItem = Keyboard.readInt( );
if (menuItem == 1)//******USER CHOOSES TO ADD A DVD******//
{
//******USER INPUT DVD INFO******//
System.out.println("Enter the title of the DVD: ");
Title=Keyboard.readString();
System.out.println("Enter the genre of the DVD: ");
Genre=Keyboard.readString();
System.out.println("Enter the rateing for the DVD 1 to 10");
Rateing=Keyboard.readDouble();
System.out.println("Enter a brief descriptoin of the DVD: ");
Description=Keyboard.readString();
//******NEW DVD ADDED TO DVDBOOK() DVDcollection******//
DVDcollection.ADDDVD(Title, Genre, Rateing, Description);
}
else if (menuItem == 2)
{
System.out.println (DVDcollection.toString()); //**PRINTS toSting()*****//
}
else
{
System.out.println ("Are you sure you want to quit? Y/N: \n");
sure = Keyboard.readChar( );
if (sure == 'n' || sure == 'N')
menuItem = 1;
else
menuItem = 3;
}
}// end while
while (menuItem != 3); //while exit hasn't been selected
}//end main
}//end class
}