Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Write a driver program implementing an ArrayList of CD object

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a driver program implementing an ArrayList of CD object

    I have written the CDException class and the CD class, and now I'm trying to write a driver class to run the other classes. I have to

    Read the artist name from user
    Read the album name from user
    Read the price from user
    Read how many in stock from user

    And print what they wrote but only the valid entries. Other entries will be ignored upon printing if they just enter blanks.

    Here's what I have for my CD class

    import java.text.*;
    public class CD {
     
    private String sArtist;
    private String sAlbum;
    private double dPrice = 0;
    private int inStock = 0;
    String decimalFormats = "$##.##";   
     
        public CD(String sArtist, String sAlbum, double dPrice, int inStock) throws Exception{//constructors
     
            this.setArtist(sArtist);
            this.setAlbum(sAlbum);
            this.setPrice(dPrice);
            this.setinStock(inStock);
        }
            public String toString( ){//to string method
     
                DecimalFormat df = new DecimalFormat(decimalFormats);   //Formats the price
                String s = "Album: " + sAlbum + "\nArtist: " + sArtist +
                    "\nPrice: " + df.format(dPrice);
     
                if(inStock == 0)
                    return "Album: " + sAlbum + "\nArtist: " + sArtist +
                    "\nPrice: $" + dPrice + "\nNot in Stock"; 
                else
                    return s + "\nCurrently " + inStock + " in stock";
            }
     
            //mutator methods
            public void setArtist(String newArtist)throws Exception{
                if(newArtist.length( ) == 0){
                    CDException cde = new CDException( );
                    cde.setMessage("Artist name contains no characters" + "\nCannot create CD");
                    throw cde;
                }
                else{
                    this.sArtist = newArtist;
                }
            }
            public void setAlbum(String newAlbum) throws Exception{
                if(newAlbum.length( ) == 0){
                    CDException cde = new CDException( );
                    cde.setMessage("Album name contains no characters" + "\nCannot create CD");
                    throw cde;
                }
                else{
                    this.sAlbum = newAlbum;
                }
            }
            public void setPrice(double newPrice)throws Exception{
                if(newPrice < 0){
                    CDException cde = new CDException( );
                    cde.setMessage("CD Price cannot be negative" + "\nCannot create CD");
                    throw cde;
                }
                else{
                    this.dPrice = newPrice;
                }
            }
            public void setinStock(int newInStock)throws Exception{
                if(newInStock < 0){
                    CDException cde = new CDException( );
                    cde.setMessage("In stock cannot be negative" + "\nCannot create CD");
                    throw cde;
                }
                else{
                    this.inStock = newInStock;
                }
            }
     
     
     
            public String getsArtist( ){
                return this.sArtist;
            }
            public String getsAlbum( ){
                return this.sAlbum;
            }
            public double getdPrice( ){
                return this.dPrice;
            }
            public int getinStock( ){
                return this.inStock;
            }
     
        }   
    }

    And here is my Driver class, It needs to be modified so that way I can ask the user to enter the Artist name, album name, price of CD and how many in stock and wrap it up to add it into an ArrayList. If the user enters blanks, those will be ignored upon printing them. Only valid entries will be printed. I also have a CDException class that I wrote as well. This is frustrating me, any help would be much appreciated! I am very new to Java, so please bear with me as I may seem like I dont know what I'm talking about or doing.

     public class CDStore{
     
      public static void main (String[ ] arg) throws Exception{
     
       ArrayList (CD) CD = new ArrayList( );
     
       try{
             CD cd1 = new CD("Muse", "The Resistance", 11.99, 20);
             System.out.println(cd1.toString( ));
             System.out.println("=========================");
       }
       catch(CDException cde){
            System.out.println(cde.getMessage( ));
            System.out.println("=========================");
       }
     
      try{
           CD cd2 = new CD("Yanni", "The Live at the Acropolis", 11.99, 0);
           System.out.println(cd2.toString( ));
           System.out.println("=========================");
       }
      catch(CDException cde){
          System.out.println(cde.getMessage( ));
          System.out.println("=========================");
      }
     
     
      try{
           CD cd3 = new CD("Muse", "Black Holes and Revelations", 10.99, 15);
           System.out.println(cd3.toString( ));
           System.out.println("=========================");
       }
      catch(CDException cde){
           System.out.println(cde.getMessage( ));
           System.out.println("=========================");
     }
     
     try{ 
          CD cd4 = new CD("Paul Mc Cartney", "All the Best", -0.99, 12);
          System.out.println(cd4.toString( ));  
          System.out.println("=========================");
       }
     catch(CDException cde){
          System.out.println(cde.getMessage( ));
          System.out.println("=========================");
      }
     
      try{
          CD cd5 = new CD("Lady Gaga", "The Fame Monster", 14.99, 44);
          System.out.println(cd5.toString( ));
          System.out.println("=========================");
       }
      catch(CDException cde){
          System.out.println(cde.getMessage( ));
          System.out.println("=========================");
      }    
     
     
     
      try{
          CD cd6 = new CD("", "California Gurls", 1.29, 4);
          System.out.println(cd6.toString( ));
          System.out.println("=========================");
       }
      catch(CDException cde){
          System.out.println(cde.getMessage( ));
          System.out.println("=========================");
      }    
     
    try{   
          CD cd7 = new CD("Pitbull", "", 11.99, 12);
          System.out.println(cd7.toString( ));
          System.out.println("=========================");
       }
     catch(CDException cde){
          System.out.println(cde.getMessage( ));
          System.out.println("=========================");
       }    
     
      }//main method ends
     
     } //program ends


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    way I can ask the user to enter ...
    What have you tried? What class do you want to use to read the data from the user?
    Scanner is one
    JOPtionPane is another.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    I haven't learned what the JOptionPane is. I know I have to use the scanner. But I have three different classes, the exception, CD class, and the driver class. And I have my constructors in the CD class, but when I attempted to enter the questions in the driver such as

    System.out.print("Enter Artist name: ");
    sArtist = reader.nextLine( );

    sArtist is the constructor in the CD class, and when I try to use it in the driver, it says it hasnt been declared or something like that. How do I link these all three classes, or how would I go about assigning the sArtist to what the user enters? Do i have to re-declare it in the driver as well even tho I done so in the CD class?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    or something like that.
    Copy the full text of the error messages and post it here. Fixing a something like that error is hard to do.

    The reading of the data should be done in the method that is going to create the object. Read all the needed data and then create the object by calling its constructor with that data.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    This is what pops up. I thought that all the classes would be linked? Hmm.

    CDStore2.java:12: cannot find symbol
    symbol : variable sArtist
    location: class CDStore2
    sArtist = reader.nextLine( );
    ^
    Note: CDStore2.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error

    ----jGRASP wedge2: exit code for process is 1.

    But my attempted modification looks like this, but isn't working. Should I declare new variables in this class and then wrap it up and then add it to the ArrayList?

    import java.util.*;
    public class CDStore2{
     
       public static void main (String[ ] arg) throws Exception{
     
    		Scanner reader = new Scanner (System.in);
    		ArrayList <CD> CD = new ArrayList( );
     
    		System.out.print("Enter artist name: ");
    		sArtist = reader.nextLine( );
     
     
     
       }//main method ends
     
    } //program ends

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    cannot find symbol
    symbol : variable sArtist
    The compiler can not find where the symbol in the error message is defined. You need to add a definition for that variable to the code.

    BTW the variable name should NOT be the same as the class name. That will be confusing and a problem. Give the variable a name that describes what it contains:
    myFavCDs.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    Well, my class names are CD class, CDException and CDStore

    The constructor names are sArtist, sAlbum, dPrice, and inStock

    I cannot change the names, per my instructor.

    Which is why I asked, those variables are declared in my CD class, and I'm trying to assign them a new name in the driver class with the user's input. Am i supposed to re-declare them in the driver class as well?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    The constructor names are sArtist, sAlbum, dPrice, and inStock
    I'm not sure what that means. Constructor names must be the same as the name of the class.
    Do you mean class variable names?
    The variables that the data is read into mist be define as local to the method where the reading is done.
    Their values will be passed to a constructor which will save their values in class variables.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    Well, like I stated before as well, I am very new to Java, this being my first course in it. So please bear with me as I may seem like I don't know what I'm doing or saying for that matter.

    My two codes are posted up above.

    So, what I would just have to do is declare the variables inside the driver class as well?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    Take a look at the tutorial about how to define variables:
    Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Since the variables used to receive user input are temporary variables, define them in the method where they are used.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Write a driver program implementing an ArrayList of CD object

    Also posted at Write a driver program implementing an ArrayList of CD objects - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Writing a Driver Program to interact with a Class?
    By ViewtifulAaron in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2013, 06:12 AM
  2. Implementing a Driver to following code
    By Twoacross in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 11th, 2011, 11:17 PM
  3. Implementing .txt readfrom and write to in a current program
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2011, 09:01 PM
  4. write object to sql db
    By wolfgar in forum JDBC & Databases
    Replies: 3
    Last Post: May 18th, 2010, 10:03 AM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM

Tags for this Thread