1,105,380 Community Members

return type

Member Avatar
krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

hi,

public Record get(VideoObj v) {
    // TODO
if((v ==null)  || (v==""))
    return null;
else
 return ??;
  }

Recor and VideoObj are classes. if the methos return type is Record, can someone tell me what should I return?

thanks

Member Avatar
stultuske
Posting Expert
5,298 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

an instance of Record

Member Avatar
krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

an instance of Record

So do i have to create a instance in the method
like
Record r = new Record(VideoObj video, int numOwned, int numOut, int numRentals);

so in the else part it will be return r;
is it? but what about the parameters for the Record metho? that is the only method that class has

thanks

Member Avatar
stultuske
Posting Expert
5,298 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

I don't really understand what you mean. you need to create a valid instance of Record (since I don't know your record class, I am not making assumptions of how that looks like) by calling (on of) the constructor(s) of that class, and returning that instance.

what do you mean by 'the parameters for the Record metho? that is the only method that class has'.

can you post that code here? and put it between Code tags, it makes it a lot easier to read.

Member Avatar
krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

I don't really understand what you mean. you need to create a valid instance of Record (since I don't know your record class, I am not making assumptions of how that looks like) by calling (on of) the constructor(s) of that class, and returning that instance.

what do you mean by 'the parameters for the Record metho? that is the only method that class has'.

can you post that code here? and put it between Code tags, it makes it a lot easier to read.

the Record Class method

final class Record {
  /**
   * The video.
   * @invariant <code>video != null</code>
   */
  VideoObj video;
  /**
   * The number of copies of the video that are in the inventory.
   * @invariant <code>numOwned > 0</code>
   */
  int numOwned;
  /**
   * The number of copies of the video that are currently checked out.
   * @invariant <code>numOut <= numOwned</code>
   */
  int numOut;
  /**
   * The total number of times this video has ever been checked out.
   * @invariant <code>numRentals >= numOut</code>
   */
  int numRentals;

  /**
   * Initialize all object attributes.
   */
  Record(VideoObj video, int numOwned, int numOut, int numRentals) {
    this.video = video;
    this.numOwned = numOwned;
    this.numOut = numOut;
    this.numRentals = numRentals;
  }
  /**
   * Return a shallow copy of this record.
   */
  public Record copy() {
    return new Record(video,numOwned,numOut,numRentals);
  }
  /**
   * Return a string representation of the object in the following format:
   * <code>"video [numOwned,numOut,numRentals]"</code>.
   */
  public String toString() {
    StringBuilder buffer = new StringBuilder();
    buffer.append(video);
    buffer.append(" [");
    buffer.append(numOwned);
    buffer.append(",");
    buffer.append(numOut);
    buffer.append(",");
    buffer.append(numRentals);
    buffer.append("]");
    return buffer.toString();
  }
}
Member Avatar
stultuske
Posting Expert
5,298 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 
Record(VideoObj video, int numOwned, int numOut, int numRentals) {
    this.video = video;
    this.numOwned = numOwned;
    this.numOut = numOut;
    this.numRentals = numRentals;
  }

this is your constructor. you'll have to create a new instance of Record and return that, since you specified a constructor, your compiler will no longer find a default constructor (except if you add one yourself) so you'll have to pass all the parameters.

you have to have something like this:

return new Record(video,numOwned,numOut,numRentals);

and I don't mean: just copy paste this. this is the return of the copy method in your Record class. I don't know your project well enough to know where to get the other information.
A good way would be to keep an array(List) of Records somewhere. when you need to return that Record, go search this array for a Record having that video object attached to it, and return that instance.
if there isn't one: create it, add it to the list, and return it.

Member Avatar
krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 
Record(VideoObj video, int numOwned, int numOut, int numRentals) {
    this.video = video;
    this.numOwned = numOwned;
    this.numOut = numOut;
    this.numRentals = numRentals;
  }

this is your constructor. you'll have to create a new instance of Record and return that, since you specified a constructor, your compiler will no longer find a default constructor (except if you add one yourself) so you'll have to pass all the parameters.

you have to have something like this:

return new Record(video,numOwned,numOut,numRentals);

and I don't mean: just copy paste this. this is the return of the copy method in your Record class. I don't know your project well enough to know where to get the other information.
A good way would be to keep an array(List) of Records somewhere. when you need to return that Record, go search this array for a Record having that video object attached to it, and return that instance.
if there isn't one: create it, add it to the list, and return it.

there is

private final Map<VideoObj,Record> _data = new HashMap<VideoObj,Record>();

in the class where the method
public Record get(VideoObj v) is

can i get the details for the return new Record(video,numOwned,numOut,numRentals); from "Map<VideoObj,Record> _data = new HashMap<VideoObj,Record>" ???

Member Avatar
stultuske
Posting Expert
5,298 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

yes. Just get the Record linked to the VideoObj and return that one

Member Avatar
krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

yes. Just get the Record linked to the VideoObj and return that one

i didn't get it???

Member Avatar
stultuske
Posting Expert
5,298 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

you have a HashMap filled with combinations of Video_obj Objects and Record Objects.
you have a Video_obj object, just search in your Map if there is an element which has that Video_object as a key.

if so, use that element, if not, create a new instance of Record, store that new combination in the HashMap and return that Record instance.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: