Results 1 to 4 of 4
  1. #1
    ThatPinkSock is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Exclamation Creating Classes!! Help needed badly.

    DESCRIPTION
    You must create 2 new classes, as outlined below.
    PART I

    Create a new class, save the new file as Page.java. The declaration will be as follows:
    public class Page {
    This class will have 2 fields:

    • An array of Pictures called photos

    • An integer called pageNumber

    This class will have 1 constructor, which takes in two parameters to set up your page. The first parameter will be the pageNumber, and the second will be the numPhotos (an integer to represent the number of photos on that page). With these two parameters, be sure to initialize your array

    This class will have 2 methods:
    addPhoto( Picture pic )
    This method will take in a Picture object and add it to the next empty spot in the photos array. The method will return true if it was able to add it to the array, and false otherwise.

    display()

    This method will take no parameters and will return nothing. It will utilize the show() method in the Picture class to display each of the photos in the array to the user, in the order they appear in the array. Before it displays a photo, it will first add a message to the image, at location (10,10), with that images’ page number and its index on the page. See below for examples.

    Be careful to avoid NullPointerExceptions - you will not be able to display a photo if for example the array has not been initialized, or if a photo in the array is null (ie: a case when the page is not full).

    PART II
    Create a new class, save the new file as PhotoAlbum.java. The declaration will be as follows:
    public class PhotoAlbum {
    This class will have 1 field:
    • An array of Pages called pages
    This class will have 1 constructor, which takes in two parameters to set up your photo album. The first parameter will be the numPages (an integer to represent the number of pages in your album), and the second will be the photosPerPage (an integer to represent the number of photos permitted on any page in the album). With these two parameters, again be sure to initialize your array, and be careful to assign the appropriate number to each page (we’d never go looking for the 0th page of a book..).
    This class will have 2 methods:
    getPage( int number )
    This method will take in no parameters, but will return the Page object corresponding to the logical page specified. This means that a parameter of 1 is asking for “page 1” of the album, which correlates to the first item in your array.

    display()
    This method will take no parameters and will not return anything. It will utilize the method in the Page class to display each of the pages in the array to the user, in the order they appear in the array.

    Again, be careful to avoid NullPointerExceptions - if the array has not been initialized, or if a page is null, you should not try to display anything.




    My Questions: How would I declare an array of photos in the fields in the first class?

    How do I create a constructor that takes in two parameters to set up the page?

    What does this method entail? "This method will take in no parameters, but will return the Page object corresponding to the logical page specified. This means that a parameter of 1 is asking for “page 1” of the album, which correlates to the first item in your array."

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ThatPinkSock View Post
    My Questions: How would I declare an array of photos in the fields in the first class?

    an example:
    Java Code:
    private MyType[] myVariable; // this declares an array of MyType


    How do I create a constructor that takes in two parameters to set up the page?

    A constructor for MyClass below takes two parameters, one of FooType the other of BarType. Names have been changed to protect the innocent.
    Java Code:
    public class MyClass {
    
      public MyClass(FooType parameter1, BarType parameter2) {
         // constructor code goes here...
      }
    
      //.... methods...
    
    }


    What does this method entail? "This method will take in no parameters, but will return the Page object corresponding to the logical page specified. This means that a parameter of 1 is asking for “page 1” of the album, which correlates to the first item in your array."

    There's an error in your assignment instructions, and this may be confusing you. The method described is not without parameters, but in fact does take a single parameter, an int.

  3. #3
    ThatPinkSock is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Exclamation

    Thanks!! A couple follow up questions:


    Quote Originally Posted by Fubarable View Post
    an example:
    Java Code:
    private MyType[] myVariable; // this declares an array of MyType
    So if I wanted to create an array of Pictures called photos I would put

    Java Code:
     private Pictures[] photos;
    in my "Page" class?

    Quote Originally Posted by Fubarable View Post
    A constructor for MyClass below takes two parameters, one of FooType the other of BarType. Names have been changed to protect the innocent.
    Java Code:
    public class MyClass {
    
      public MyClass(FooType parameter1, BarType parameter2) {
         // constructor code goes here...
      }
    
      //.... methods...
    
    }
    So for the constructors I could put:

    Java Code:
    public Page(int pageNumber, int numPhotos){}
    If my class is called Page.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ThatPinkSock View Post
    Thanks!! A couple follow up questions:

    So if I wanted to create an array of Pictures called photos I would put

    Java Code:
     private Pictures[] photos;
    in my "Page" class?
    That looks good to me.



    So for the constructors I could put:

    Java Code:
    public Page(int pageNumber, int numPhotos){}
    If my class is called Page.
    Yes, that will work as a skeleton for the constructor, but you'll want to flesh it out buy initializing the class fields inside of it.

    Much luck!

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2010, 12:55 AM
  2. localization....help needed badly
    By paddala in forum Advanced Java
    Replies: 8
    Last Post: 08-27-2008, 04:29 AM
  3. Creating inner classes
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  4. Object ArrayList - increment solution needed badly!!
    By rugbyGeek in forum New To Java
    Replies: 4
    Last Post: 03-08-2008, 12:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •