Using the 'new' keyword/calling method from another class

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 778 Views - Last Post: 13 October 2011 - 10:13 AM Rate Topic: -----

#1 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Using the 'new' keyword/calling method from another class

Posted 12 October 2011 - 08:04 AM

Hello,

Basically I need to create objects for each field Dvd1, Dvd2, Dvd3 in the class constructor for the collection class using the new keyword. I have done this but is this correct way in which to do it?

Also, there needs to be a method in the collection class which gets the details of the collection using a method from the Dvd class three times, once for each Dvd. Could you please help me understand what needs to be done and know.

Here's my code at the moment any help would be much appreciated

public class Dvd
{
    // List of instance variables
    private String title;
    private String director;
    private String leadActor;
    private int runTime;
    private int releaseYr;
    private int rating;
    private String format;
    private String category;

    /**
     * Constructor for objects of class Dvd
     */
    public Dvd()
    {
       
        
    }

    /**
     * Method to get the title of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public String getTitle()
    {
        return title;
    }
    
    /**
     * Method to get the Director of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public String getDirector()
    {
       return director;
    }
     
    /**
     * Method to get the Lead Actor of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public String getLeadActor()
    {
       return leadActor;
    }
    
    /**
     * Method to get the Running Time of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public int getRunTime()
    {
       return runTime;
    }
    
    /**
     * Method to get the Release Year of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public int getReleaseYr()
    {
       return releaseYr;
    }
    
    /**
     * Method to get the Rating of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public int getRating()
    {
       return rating;
    }
    
    /**
     * Method to get the Format of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public String getFormat()
    {
       return format;
    }
    
    /**
     * Method to get the Category of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public String getCategory()
    {
       return category;
    }
    
    /**
     * Method to set the Title of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setTitle()
    {
       //Prompt user for the title of the DVD
       System.out.print("Title: ");
       
       //Get make using the Genio method
       title = Genio.getString();
    }
    
    /**
     * Method to set the Director of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setDirector()
    {
       //Prompt user for the Director of the DVD
       System.out.print("Director: ");
       
       //Get make using the Genio method
       director = Genio.getString();
    }
    
    /**
     * Method to set the Lead Actor of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setLeadActor()
    {
       //Prompt user for the Lead Actor of the DVD
       System.out.print("Lead Actor: ");
       
       //Get make using the Genio method
       leadActor = Genio.getString();
    }
    
    /**
     * Method to set the Running Time of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setRunTime()
    {
       //Prompt user for the Running Time of the DVD
       System.out.print("Running Time: ");
       
       //Get make using the Genio method
       runTime = Genio.getInteger();
    }
    
    /**
     * Method to set the Release Year of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setReleaseYr()
    {
       //Prompt user for the Release Year of the DVD
       System.out.print("Release Year: ");
       
       //Get make using the Genio method
       releaseYr = Genio.getInteger();
    }
    
    /**
     * Method to set the Rating of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setRating()
    {
       //Prompt user for the Rating of the DVD
       System.out.print("Rating: ");
       
       //Get make using the Genio method
       rating = Genio.getInteger();
    }
   
    /**
     * Method to set the Format of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setFormat()
    {
       //Prompt user for the Format of the DVD
       System.out.print("Format: ");
       
       //Get make using the Genio method
       format = Genio.getString();
    }
    
    /**
     * Method to set the Category of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setCategory()
    {
       //Prompt user for the Category of the DVD
       System.out.print("Category: ");
       
       //Get make using the Genio method
       category = Genio.getString();
    }
    
    /**
     * Method to set all the attributes of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setAttributes()
    {
       //Calls each individual methods which sets all the attributes of the DVD
       setTitle();
       setDirector();
       setLeadActor();
       setRunTime();
       setReleaseYr();
       setRating();
       setFormat();
       setCategory();
    }
    
    /**
     * Method to print out all DVD's attributes to the screen
     * 
     * @param  No parameters
     * @return None 
     */
    public void allAttributes()
    {
       //Ouputs each attributes to the screen
       System.out.println("Title: "+title+"");
       System.out.println("Director: "+director+"");
       System.out.println("Lead Actor: "+leadActor+"");
       System.out.println("Running Time: "+runTime+"");
       System.out.println("Release Year: "+releaseYr+"");
       System.out.println("Rating: "+rating+"");
       System.out.println("Format: "+format+"");
       System.out.println("Category: "+category+"");
    }
}



/**
 * Description of Collection class
 * 
 * @author Mark Goddard
 * @version 11/09/2011
 */
public class Collection
{
    // List of instance variables
    private String Dvd1;
    private String Dvd2;
    private String Dvd3;
   
    /**
     * Constructor for objects of class Collection
     */
    public Collection()
    {
        // initialise instance variables
        
        Dvd Dvd1 = new Dvd();
        Dvd Dvd2 = new Dvd();
        Dvd Dvd3 = new Dvd();
        
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Using the 'new' keyword/calling method from another class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Using the 'new' keyword/calling method from another class

Posted 12 October 2011 - 08:22 AM

private String Dvd1;  

     public Collection() {
     // initialise instance variables  

     Dvd Dvd1 = new Dvd();


Your comments is complety false.
Dvd Dvd1 = new Dvd();
does not initialize at all an instance variable. It creates a variable Dvd1 that is local to the constructor and will stop to exist as soon as you will exit the constructor.

You probably want
private Dvd Dvd1;  

     public Collection() {
     // initialise instance variables  

     Dvd1 = new Dvd();


Was This Post Helpful? 0
  • +
  • -

#3 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 04:44 AM

Hey, Thank you much appreciated :)
Next I need to create a method to get details of the collection. I therefore need to use a method from the Dvd class three times which I have attempted. Is this the right way because I called each individual 'get' method, however when I wanted to call allAttributes or setAtributes an error always appeared.


/**
 * Description of Collection class
 * 
 * @author Mark Goddard
 * @version 11/09/2011
 */
public class Collection
{
    // List of instance variables
    private Dvd Dvd1;
    private Dvd Dvd2;
    private Dvd Dvd3;
   
    /**
     * Constructor for objects of class Collection
     */
    public Collection()
    {
        // initialise instance variables
        Dvd1 = new Dvd();
        Dvd2 = new Dvd();
        Dvd3 = new Dvd();
    }
    
    /**
     * Method to set all the attributes of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setDvd1()
    {
       String temp;
       int temp1;
       temp = Dvd1.getTitle();
       temp = Dvd1.getDirector();
       temp = Dvd1.getLeadActor();
       temp1 = Dvd1.getRunTime();
       temp1 = Dvd1.getReleaseYr();
       temp1 = Dvd1.getRating();
       temp = Dvd1.getFormat();
       temp = Dvd1.getCategory();
    }
    
    /**
     * Method to 
     * 
     * @param  No parameters
     * @return None 
     */
    public void setDvd2()
    {
       String temp;
       int temp1;
       temp = Dvd2.getTitle();
       temp = Dvd2.getDirector();
       temp = Dvd2.getLeadActor();
       temp1 = Dvd2.getRunTime();
       temp1 = Dvd2.getReleaseYr();
       temp1 = Dvd2.getRating();
       temp = Dvd2.getFormat();
       temp = Dvd2.getCategory();
    }
    
    /**
     * Method to 
     * 
     * @param  No parameters
     * @return None 
     */
    public void setDvd3()
    {
       String temp;
       int temp1;
       temp = Dvd3.getTitle();
       temp = Dvd3.getDirector();
       temp = Dvd3.getLeadActor();
       temp1 = Dvd3.getRunTime();
       temp1 = Dvd3.getReleaseYr();
       temp1 = Dvd3.getRating();
       temp = Dvd3.getFormat();
       temp = Dvd3.getCategory();
    }
}



Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 07:43 AM

    public void setDvd1()
    {
       String temp;
       int temp1;
       temp = Dvd1.getTitle();
       temp = Dvd1.getDirector();
       temp = Dvd1.getLeadActor();
       temp1 = Dvd1.getRunTime();
       temp1 = Dvd1.getReleaseYr();
       temp1 = Dvd1.getRating();
       temp = Dvd1.getFormat();
       temp = Dvd1.getCategory();


This method doesn't do much of anything. You create a localized String and int. You then continually override the temp String never doing anything with it, and it disappears at the end of the method since it was create in the method.

What were you attempting to do with this method? There is a logical flaw we need to work through.
Was This Post Helpful? 0
  • +
  • -

#5 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 07:54 AM

Basically, I need a method to get the details of the collection. All I have worked out is I need to use a method from the Dvd class three time, once for each Dvd. I understand what you are getting at about it overriding where the data is stored, so I've done it this way instead. What do you think?

/**
 * Description of Collection class
 * 
 * @author Mark Goddard
 * @version 11/09/2011
 */
public class Collection
{
    // List of instance variables
    private Dvd Dvd1;
    private Dvd Dvd2;
    private Dvd Dvd3;
   
    /**
     * Constructor for objects of class Collection
     */
    public Collection()
    {
        // initialise instance variables
        Dvd1 = new Dvd();
        Dvd2 = new Dvd();
        Dvd3 = new Dvd();
    }
    
    /**
     * Method to set all the attributes of the DVD
     * 
     * @param  No parameters
     * @return None 
     */
    public void setDvd1()
    {
       String temp;
       temp = Dvd1.allAttributes();
    }
    
    /**
     * Method to 
     * 
     * @param  No parameters
     * @return None 
     */
    public void setDvd2()
    {
       String temp;
       temp = Dvd2.allAttributes();
    }
    
    /**
     * Method to 
     * 
     * @param  No parameters
     * @return None 
     */
    public void setDvd3()
    {
       String temp;
       temp = Dvd3.allAttributesTitle();
    }
    
     /**
     * Method to locate a Dvd by title
     * 
     * @param  No parameters
     * @return None 
     */
    public String locateByTitle()
    {
       String temp1 = "Braveheart";
       String temp2 = "Friends";
       if (temp1.equals(temp2))
       { 
           System.out.println("Same title");
       } 
       else 
           System.out.println("Different title");
       return
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 07:58 AM

Your DVD allAttributes() method doesn't return anything. it actually just prints it out like it is supposed to via the methods code. Also, even if it does return something you still don't do anything with the String temp. I understand that you want to get all of the attributes.. but what do you want to do with them? Print them? Make a copy of the DvD with them? This is what we need to know.
Was This Post Helpful? 0
  • +
  • -

#7 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 08:04 AM

Hey,

Sorry for not being clear but yes I want the details of the entire collection to be printed on screen.
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 08:11 AM

Alright, well then I do not understand why you call it setDVD if that is all you want to do, and to do that, the allAttributes() method does that for you so just call that method from the object like so:
public void setDvd1()
{
   Dvd1.allAttributes();
}

Was This Post Helpful? 0
  • +
  • -

#9 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 08:19 AM

The reason why I'm doing it is because not many people have just one DVD so I need somewhere to create a collection of just three DVD's. I need a method so that the user can the details of the entire DVD collection and also a method so these details can be printed to the screen.
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 08:33 AM

Alright, well this is going to play into a design thing that you have going on.

In your collections class, you should create an ArrayList<DVD> this will store DVD objects in an ArrayList. Then you can create a method called displayDVDs() that will loop through the ArrayList and call the allAttributes() method for each object in the arraylist. The method will print out all of the information so you do not need to do anything other than call that method. This will print out all of the information for every DVD they have entered.

By using an ArrayList you do not restrict the number of DVDs that they can have. Like you said, a majority of people have more than 1, and definitely more than 3.

Basically the design that you should be going for is this:
DVD class - have getters and setters and a toString() (which is your allAttributes(); however, a toString() returns a compiled String of the variables rather than printing them. You generally print the toString()

DVDCollection Class - You will have this class to store DVD objects in an ArrayList, which means you need to have an addDVD(Params for new DVD) method in it, removeDVD(DVD) method in it, and also a displayDVD(DVD) and then finally a displayAll() method. Alternatively, you could have a search(String) that will search for a DVD by name.

DVDData Class - This will be the driver class. It will create a DVDCollection object, and then it will have a printMenu() function, printing options like add DVD search for a DVD and remove and View single DVD and view All DVDs. Assign each option to a number, use a switch statement and perform the code accordingly to the number they selected, so if they selected 1, add a DVD, this is where you prompt for all of the fields, then call the DVDCollection object.add(Params for DVD) and it will create the DVD, and add it to the ArrayList.

I know that it is a lot of work but once you get started it isn't too hard. Class design is an imperative thing to have. A design like this is one of the most common. To do what you are wanting to do this is probably the best route for a fully functional DVD Collection Program.

Does this make sense?
Was This Post Helpful? 1
  • +
  • -

#11 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 08:48 AM

I see what you are getting at. Thank you for the help!!
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 08:50 AM

Anytime, try that and since you will have a new problem and a different code you might as well create a new thread if you run into an issue. Make sure that all of the class files are in the same location(folder, directory.. whatever you wanna call it) because only then can they have access to each other through inheritance.

Happy Coding!
Fuzzy
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,594
  • Joined: 27-December 08

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 08:58 AM

In general, when you see yourself naming variables dvd1, dvd2, dvd3, etc., that is a good indicator that you should be using an array or Collection of some sort. They are easier to manage than all those variables.
Was This Post Helpful? 0
  • +
  • -

#14 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 09:00 AM

Sorry for the hassling but if I wanted to do what I'm trying to do but by limiting how many DVD's I could have, would it still work. I know that the array list is more of a structured and overall better way of doing this. I just want to know if this is possible?!
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Using the 'new' keyword/calling method from another class

Posted 13 October 2011 - 09:04 AM

Yes it is possible by using an array with a fixed amount and then you need a int dvdCounter object that will increase after each addition to the array. Then in the add() method before you try to add it you will have to check and see if the dvdCounter == array.length.

Might I ask why you are wanting to restrict the user to a certain amount of DVDs?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2