4 Replies - 229 Views - Last Post: 06 December 2012 - 12:19 PM Rate Topic: -----

#1 Runner13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-December 12

I have a question on a specific area of my code. (DrJava)

Posted 05 December 2012 - 07:12 PM

This is my code but my issue is my second method
The program is to create an Album of pictures. The class Album is suppose to set up how your pictures are arrange, etc...

public class Album
{
   int nPictsInAlbum;
   int capacity;
   Picture pictArray[];
   
  public Album(int capacityParam )
  {
    this.pictArray = new Picture[capacityParam];
    capacity = capacityParam;
    int nPictsInArray = 0;  
    System.out.println("Album( " + capacityParam + " ) constructor called.");
  }
  
  public boolean addPicture( Picture thePicture )
  {
    if(nPictsInAlbum >= capacity)
    {
      return false;
    }
    System.out.println("addPicture( p ) called.  The Picture object param. printed as a String is");
    System.out.println( thePicture );
    return true;
  } 

 I am lost for this method. My professor confuse me with his instructions. Can someone explain to me what he means?
 --> Instead of adding the given Picture at the end, this method inserts it at the position given by the where parameter value. That value should be between 0 and nPictsInAlbum
When it is 0, the given Picture is inserted at the beginning and the other Pictures are shifted right to make the space for it. When it is nPictsInAlbum , it just adds the given Picture at the right end. (When inbetween, it adds the given Picture inbetween the whereth Picture and the Picture after that one.)

I think what I have kind of right but, not right exactly. 
[b]public boolean addPicture( Picture thePicture, int where )[/b]
  {
    int w;
    w = where; 
    while( w < pictArray.length)
    {
      nPictsInAlbum = pictArray[nPictsInAlbum];
      nPictsInAlbum = nPictsInAlbum + 1;
    }
    System.out.println("addPicture( p , " + where + " ) called.  The Picture object param. printed as a String is");
    System.out.println( thePicture );
    return true;
  }[/b]
  public void explore()
  {
    System.out.println("explore called");
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: I have a question on a specific area of my code. (DrJava)

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I have a question on a specific area of my code. (DrJava)

Posted 05 December 2012 - 09:04 PM

This code does not make sense and won't even compile

   while( w < pictArray.length)  
   {  
       nPictsInAlbum = pictArray[nPictsInAlbum];  
       nPictsInAlbum = nPictsInAlbum + 1;  
   }  


nPicsInAlbum must be a int has its name lets supposed and as you perform a + 1 on it
pictArray[] is a Picture
so how can you

int = Picture
int = int + 1

and what is the use of doing

nPictsInAlbum = pictArray[nPictsInAlbum];

if you add 1 to nPictsInAlbum just the following line ?


If you have code logic problem, better yto post code that compiles
Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: I have a question on a specific area of my code. (DrJava)

Posted 05 December 2012 - 11:15 PM

int w;
w = where;


What do you intend to do with the above code?Where is 'where' getting initialized? Please show the calling function of
public boolean addPicture( Picture thePicture, int where )
.

And please use meaningful names as variables. I almost thought that you are initializing an integer variable with a string value.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#4 Runner13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-December 12

Re: I have a question on a specific area of my code. (DrJava)

Posted 06 December 2012 - 11:28 AM

This is the code my professor gave us --->
He gave us instructions onto how to figure out what each method is supposed to do. We have to code in what the method has to do.
Once we have the methods done correctly within this program then we have to compile it along with his (professor) demo program to make sure this program is running correctly.

public class Album
{
  private int nPictsInAlbum;
  private Picture[] pictArray;
  private int capacity;
  public Album(int capacityParam )
  {
    System.out.println("Album( " + capacityParam + " ) constructor called.");
    //Remove the above line for Project 5.
    capacity = capacityParam;
    //You must add more..
  }
  public boolean addPicture( Picture thePicture )
  {
    System.out.println("addPicture( thePicture ) called.  The Picture object param. printed as a String is");
    System.out.println( thePicture );
    return true;
  } 
 
 [b]The instruction my professor gave for this method confuse me:
  public boolean addPicture(Picture thePicture, int where)
  Instead of adding the given Picture at the end, this method inserts it at the posi­tion
  given by the where parameter value. That value should be between 0 and nPictsInAlbum
  When it is 0, the given Picture is inserted at the beginning and the other Pic­
  tures are shifted right to make the space for it.  When it is nPictsInAlbum , it just
  adds the given Picture at the right end.  (When in­between, it adds the given Picture 
  in­be­tween the whereth Picture and the Picture after that one.)[/b]
   
  public boolean addPicture( Picture thePicture, int where )
  {
    System.out.println("addPicture( thePicture , " + where + " ) called.  The Picture object param. printed as a String is");
    System.out.println( thePicture );
    return true;
  }
  public void explore()
  {
    System.out.println("explore called");
  }
}

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I have a question on a specific area of my code. (DrJava)

Posted 06 December 2012 - 12:19 PM

Fine :whistling:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1