Java Programming Book Model

This programming assignment is designed to evaluate your knowledge, comprehension and application of some Java programming constructs - simple methods, string manipulation, expression and exposure to object interaction which has been structurally provided. It is expected that a program can be written that compiles and runs correctly. You should work on this programming assignment on your own. However, collaboration with others on general logic flow, techniques and Java Core API methods available may be useful. It is most beneficial if you do not share code.

Write a Java program that models a book. A book will have an author, a title, the year of publication and three (3) chapters. A chapter will have a title and the number of pages in the chapter.

The class Book must include the following public methods:

	1. Chapter 1 must always start on page 1.
	2. If Ch 1 is 100 pages long and Ch 2 is 200 pages long then:
	   Ch 1 starts on page 1 and ends on 100
	   Ch 2 starts on page 101 and ends on 300
	   Ch 3 starts on page 301 and ends on 301 + (length of Ch 3) 

The class Book must have one private method:

The class Chapter should include the following public methods:

The main method should do the following:


The following are sample initial values you could use for partially testing your program:

Book title: A Good Book

Book author: Somebody-Very-Famous

Year: 1999

Chapter 1:

Title: Introduction

Number of Pages: 100

Chapter 2:

Title: Main Ideas

Number of Pages: 150

Chapter 3:

Title: Conclusions

Number of Pages: 200


Using the previous initial values, the output of your program will look like this:


Title: A Good Book
Author: Somebody-Very-Famous
Year: 1999
Number of pages: 450

Contents:
   1. Introduction...................1
   2. Main Ideas...................101
   3. Conclusions..................251       


IMPORTANT: Place Book, Chapter and Ex2 classes in the same file. The file for this program must be named Ex2.java. (Not EX02, ex2, or any other variation). ). Your file must be should placed in a subdirectory for this project. I suggest something like C:\javacode\Ex2\

 

Code to start from in the single source code file Ex2.java:

class Book
{
	private String author;
	private String title;
	private int year;
	private Chapter first;
	private Chapter second;
	private Chapter third;

Book() //default constructor
{
}

Book(String author, String title, int year, Chapter first, Chapter second, Chapter third)
{
	this.author = author;
	this.title = title;
	this.year = year;
	this.first = first;
	this.second = second;
	this.third = third;
}

public String getAuthor()
{
	return author;
}

//other getís


public void printInfo()
{
//print output according to description of method
//and sample output given above
}

private int totalPages()
{
	return(first.getPages()+add more code here);
}

public void printContents()
{
//add lots of logic here to do the printContents method
//format the output just as in the sample output
//note the number of pages could be up to 9,999 pages
//therefore, the number of dots ....... after the chapter
//title and before the page number will vary. This means
//you will have to use logic, most likely the substring method
//of a String object variable, to output the correct number
//of dots. Good Luck!

}

} //end class Book


class Chapter
{
//insert the private instance variables needed for the
//chapter object. Look at the book object for a pattern
//of what to do.

Chapter(String title, int numberOfPages)
{
	//initialize private instance variable
	//with parameters passed into this constructor
	//of the chapter object.
}

//insert the method described above that are provided by
//the chapter object.


} //end class Chapter


//this is the class that gets run. It is complete as coded below.
//you are free to change the values of the chapter and book
//object creation parameters.  These are the values passed to the
//objects constructor. The changeable items are in bold & italic.

public class Ex2
{

Ex2()
{
}

public static void main(String args[])
{
	Chapter c1, c2, c3;
	c1 = new Chapter("title 1", 25);
	c2 = new Chapter("title 2", 40);
	c3 = new Chapter("title 3", 33); //total=98

	Book b1;
	b1 = new Book("author", "bookname", 1999, c1, c2, c3);
	b1.printInfo();
	b1.printContents();
}

} //end class Ex2