Results 1 to 4 of 4
- 11-10-2010, 02:59 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
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 (wed 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."
-
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.
- 11-10-2010, 03:28 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 5
- Rep Power
- 0
Thanks!! A couple follow up questions:
So if I wanted to create an array of Pictures called photos I would put
in my "Page" class?Java Code:private Pictures[] photos;
So for the constructors I could put:
If my class is called Page.Java Code:public Page(int pageNumber, int numPhotos){}
-
That looks good to me.
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.So for the constructors I could put:
If my class is called Page.Java Code:public Page(int pageNumber, int numPhotos){}
Much luck!
Similar Threads
-
classes and packages needed to connect any mailserver
By nyk976 in forum NetworkingReplies: 1Last Post: 02-10-2010, 12:55 AM -
localization....help needed badly
By paddala in forum Advanced JavaReplies: 8Last Post: 08-27-2008, 04:29 AM -
Creating inner classes
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:07 PM -
Object ArrayList - increment solution needed badly!!
By rugbyGeek in forum New To JavaReplies: 4Last Post: 03-08-2008, 12:47 AM


LinkBack URL
About LinkBacks


Bookmarks