• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

instantiating object using interface and object file

I have a VideoObj object:

/**

 * Implementation of Video interface.

 * @see Data

 */

final class VideoObj implements Video {

  private final String _title;

  private final int    _year;

  private final String _director;



  /**

   * Initialize all object attributes.

   */

  VideoObj(String title, int year, String director) {

    _title = title;

    _director = director;

    _year = year;

  }



  public String director() {

    // TODO

    return _director;

  }



  public String title() {

    // TODO

    return _title;

  }



  public int year() {

    // TODO

    return _year;

  }

Open in new window


This object implements a Video interface.  I am tasked to finish a factory method in a separate class called Data.  

Data class (factory method):
  /**

   * Factory method for Video objects.

   * Title and director are "trimmed" to remove leading and final space.

   * @throws IllegalArgumentException if Video invariant violated.

   */

  static public Video newVideo(String title, int year, String director) {

    if (  (title == null)

 	       || (director == null)

 	       || (year <= 1800)

 	       || (year >= 5000)) {

      throw new IllegalArgumentException();

    }

    title = title.trim();

    director = director.trim();

    year();

    if (  ("".equals(title))

 	       || ("".equals(director))) {

      throw new IllegalArgumentException();

    }

    return newVideo;

  }

Open in new window


My IDE is telling me the year = year is

"Multiple markers at this line

- The method year() is undefined for the type

Data

- The assignment to variable year has no effect"



Also it is complaining on return newVideo:

"newVideo cannot be resolved to a

 variable"


I am pretty lost and very rusty on java.  I am most confused about accessing private variables (title, year, etc are private in VideoObj).  Also, I am not sure what to return.  Any guidance /advice?
0
dazedandconfused69
Asked:
dazedandconfused69
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Is this for a homework assignment? If so, you should state that in your question so that the experts on here can give the right level of help, ie. not just give you full code solutions, and actually try and help you to write your own code. For the moment, I will assume that it IS homework, and just provide some pointers...

The method year() is undefined for the type
This would have been from line 29 of Data.java above (this will be a different line in your code, you should really post your whole class if you can to give us the right context). The method call on this line would be trying to call a method named 'year' and it would need to be in the Data class itself. Am guessing that this isn't at all what you want and you can probably just remove this line.

The assignment to variable year has no effect
I'm guessing that you may have posted different code to what gave these errors, because I can't see where this error would be thrown from.

newVideo cannot be resolved to a variable
Basically, as it says, newVideo isn't a variable and so it can't work out what you want to return. The general idea of a factory method is that is creates an object, sometimes initialising it (as in your case) and returns that object to the caller. Hints: the 'new' keyword is what creates objects in Java, the VideoObj constructor is the only way you have of initialising the object so you will need to use it, and the object that is created is what you will 'return' to the caller of your factory method.

I am most confused about accessing private variables (title, year, etc are private in VideoObj).
Fear not then, as you don't need to access them. You pass in the values that you want the VideoObj to have via the constructor (line 25 of VideoObj.java) and the constructor sets up the private variables. Also, when you want to retrieve the values in those variables, again you don't access the private variables directly but via the 'getter' methods (line 37, 47 & 57 of VideoObj.java). Note: to conform to the general standard, you probably should change the names of those methods to getDirector(), getTitle() and getYear(), possibly in the Video interface too if they are part of that interface.
0
 
dazedandconfused69Author Commented:
Thank you for the comments.  It is a HW assignment.  I will follow your advice going forward.  Is it appropriate for me to suggest a solution based on an Expert's advice and for that expert to confirm that solution is correct?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Yes, that is actually the preferred way. If you post your solution and ask specific questions about certain parts, we can definitely help. I mean, pretty much what you posted in the original question is good, it shows that you attempted something yourself, and you asked specific questions. I just said that stuff about HW assignments as some experts on here might have just replied with code only (no explanation) and that just doesn't really help you in the long run! As I said what you did was good, a lot of the time though we see other people just posting the HW question verbatim and hoping someone will code everything for them!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now