aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Implementation for item 3.3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Implementation for item 3.3" Watch "Implementation for item 3.3" New topic
Author

Implementation for item 3.3

Aleks V. Pascoal
Ranch Hand

Joined: Apr 21, 2002
Posts: 73
Guys,
I made an example about item 3.3, trying to cover all it asks.

Here is what I've tried to say with the code:


There is an association between Book and Author;
Each book may have 0..2 authors;
Just books know about its authors;
An author may be in many books;

There is a composition between Book and Page;
Each book must have at least 1 page;
Since pages are creates inside the book, each one may only be in one book;
Books know about its pages, but pages don't know about the book they're in;


Please validade it!!!
If someone has more examples please post it!


class Book{

private Author[] authors;
private Page[] pages;

Book(Author[] authors, int n) throws LengthException {
if (authors.length > 2) throw new LengthException("A Book may have 2 authors maximum");
this.authors = authors;
if (n < 1) throw new LengthException("A Book must have at least 1 page");
this.pages = new Page[n];
}

public Author[] getAuthors(){ return authors;}

}


class Author{

private String name;

Author(String name){ this.name = name; }

public String getName(){ return name;}

}

class Page{

private String content;

Page(String content){
this.content = content;
}

public String getContent(){ return content;}

}

class LengthException extends Exception{
LengthException(String msg){
super(msg);
}
[ June 27, 2005: Message edited by: Aleks V. Pascoal ]
Mahesh Kumaraguru
Ranch Hand

Joined: Jun 01, 2005
Posts: 95
Originally posted by Aleks V. Pascoal:
...
pages are creates inside the book

[ June 27, 2005: Message edited by: Aleks V. Pascoal ]


I see that in your Book class you are cleating the "pages" array, but where are you creating the individual pages ? Your Book class should have the following code after pages = new Page[n];

[ June 27, 2005: Message edited by: Mahesh Kumaraguru ]

Regards,<br />K. Mahesh<br /> <br />SCJP 1.2 Aug-2001 : 79%
Aleks V. Pascoal
Ranch Hand

Joined: Apr 21, 2002
Posts: 73
You're absolutely right... As a matter fact I've just wanted to make the shortest code, but it's important to show the creation of the pages.

But, what about my goals... I mean the relations, multiplicity, etc... are right?
Mahesh Kumaraguru
Ranch Hand

Joined: Jun 01, 2005
Posts: 95
1) There is an association between Book and Author;
1.1) Each book may have 0..2 authors;
1.2) Just books know about its authors;
1.3) An author may be in many books;

2) There is a composition between Book and Page;
2.1) Each book must have at least 1 page;
2.2) Since pages are creates inside the book, each one may only be in one book;
2.3) Books know about its pages, but pages don't know about the book they're in;


The relations and multiplicity are right. I checked them as follows :-

1) is covered when the code passes Author[] to Book constructor and Book class stores Author[].
1.1) is covered when the code checks authors.length > 2
1.2) is covered because the Book class does not callback Author. For the Author to know about Books that he/she has written, Author class must have a Book[] variable + setter methods and Book ( or the code that instantiates Book ) has to inform Author of the new Book which was created.
1.3) This is implicit, there is nothing in your code which prevents an Author object from being passed as parameter to multiple Books

2) is covered when the code declares Page[] and creates it using new Page[n].
2.1) is covered when the code checks n < 1
2.2) is covered when new Page( "Page Content" ) is used to create new Pages. The Page is created inside the Book ( Contrast : Author is created outside Book and passed as constructor parameter ).
2.3) Page class does not have a Book variable + refer 1.2 above
Mallesham Karnati
Ranch Hand

Joined: May 11, 2005
Posts: 40
Mahesh,
Your analytical summarization is excellent! Thanks a lot.
I have just a small suggestion. Can you modify the code slightly and make Book-Author association as 1..* (One or More) since it will make more realistic? (Because, it is realistic to assume that a book has 1 or more authors).

Great Job!
Regards,
Mallesham
[ June 27, 2005: Message edited by: Mallesham Karnati ]
Mahesh Kumaraguru
Ranch Hand

Joined: Jun 01, 2005
Posts: 95
To change Book to Author to have 1..* Association, change the check of

authors.length > 2

to

authors.length == 0.

Since there is no upper limit on the number of authors, this check for equals zero is sufficient.

Realistic scenario :-The code is checking only the length of the array, its not checking if the elements of the array are not null. In real project code, this would be checked probably before calling Book constructor. If I were developing only the Book class ( and not the class(es) which is creating instance(s) of Book(s)), I would prefer to check the authors received as constructor parameter are not null and if any are null, throw a different business Exception to alert the code which is calling constructor of Book that author(s) received is/are null.
[ June 27, 2005: Message edited by: Mahesh Kumaraguru ]
Aleks V. Pascoal
Ranch Hand

Joined: Apr 21, 2002
Posts: 73
Thanks guys!!

You've said all I wanna hear! I made the code but need someone to check it for me!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementation for item 3.3
 
Similar Threads
Return mutable collection?
UML guide for the SCJA exam available!
EJB 3.0 and ManyToMany relationship
Class as attribute of another class, displaying values
can I ask a big favor?