Hey all.. I have 3 classes in my program but Iam having trouble accesing a method in another class. Iam getting the error message cannot find symbol method printInfo(). Below is the code

class Book{
	
	private String author;
	private String title;
	private int year;
	private Chapter first;
	private Chapter second;
	private Chapter third;
	
	public Book(){
		author="";
		title="";
		year=0;
		first= new Chapter("Chapter 1",0);
		second=new Chapter("Chapter 2",0);
		third=new Chapter("Chapter 3", 0);
	}
	
	public void 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;
	}
	
	public String getTitle()
	{
		return title;
	}
	
	public int getYear()
	{
		return year;
	}
		
	private int TotalPages()
	{
		int total=0;
		total=first.getPages();//+second.getPages()+third.getPages;
		return total;	
	}
	
	public void printInfo()
	{
		System.out.println("Book title: " +getTitle());
		System.out.println("");
		System.out.println("Book Author:" +getAuthor());
		System.out.println("");
		System.out.println("Year:" +getYear());
		System.out.println("");
	//	System.out.println("Number of pages:" +getPages());
		System.out.println("");
	}
	
	public void printContents()
	{
		System.out.println("Contents:");
	//	System.out.println("1." +getTitle()"........." +getPages());
	//	System.out.println("");
	//	System.out.println("2." +getTitle()"...........:" +getPages()"\n");
	//	System.out.println("");		
	//	System.out.println("3."+getTitle()"...........:" +getPages()"\n");
	//	System.out.println("");
	}

}

class Chapter
{
	
	private String title;
	private int numberOfPages;
	
	/*public Chapter()
	{
		title="";
		numberOfPages=0;	
	}*/
	
	public Chapter(String title, int numberOfPages)
	{
		this.title=title;
		this.numberOfPages=numberOfPages;
	}
	
	public String getTitle()
	{
		return title;
	}	
	
	public int getPages()
	{
		return numberOfPages;
	}
	 
}

class mainPrg
{

	public static void main (String[] args)
	{
		Chapter chptobj1=new Chapter("Introduction",100);
		Chapter chptobj2=new Chapter("Main Ideas", 150);
		Chapter chptobj3=new Chapter("Conclusion", 200);
		
		Book bkobj1=new Book();
		
		chptobj1.printInfo();
	}
}

I would also like to get any advice on how I can improve on the code and reasons for the suggestions. Thank you.

Iam also getting the following errors:

1. "( expected in line 68"
2. "cannot find method getPages" in line 61

chptobj1 is an object of class Chapter , whereas printInfo() is a method of class Book . So definitely Java can't find printInfo() method in the Chapter class.

It should have been bkobj1.printInfo() , But you should be able to trace and solve such errors on your own.

I understand that so how can I rectify that and how come if I had only two classes i.e Chapter and mainPrg it would have been able to access it? (Am basing this on the fact that I have tried the latter and it seems to work)

I understand that so how can I rectify that and how come if I had only two classes i.e Chapter and mainPrg it would have been able to access it? (Am basing this on the fact that I have tried the latter and it seems to work)

Sorry but that is not possible, you just haven't declared the said method in the Chapter class, So it doesn't take a java compiler to tell that you have an error there.

Most probably when you are working with only two classes you must have also deleted the code on line 119 and so might have not got the error.

Also here is link on conventions used while writing code in Java.

And the getPages(), getTitle(), methods are not declared in the Book class. That is why you get error. They are defined in the Chapter class so you should do:

public void printContents()
	{
		System.out.println("Contents:");
		System.out.println("1." +first.getTitle()"........." +first.getPages());
		System.out.println("");
		System.out.println("2." +second.getTitle()"...........:" +second.getPages()"\n");
		System.out.println("");		
		System.out.println("3."+third.getTitle()"...........:" +third.getPages()"\n");
		System.out.println("");
	}

It'll be good if u study "object oriented methodology" along with programming conventions of an oop language like java.

And the getPages(), getTitle(), methods are not declared in the Book class. That is why you get error. They are defined in the Chapter class so you should do:

public void printContents()
	{
		System.out.println("Contents:");
		System.out.println("1." +first.getTitle()"........." +first.getPages());
		System.out.println("");
		System.out.println("2." +second.getTitle()"...........:" +second.getPages()"\n");
		System.out.println("");		
		System.out.println("3."+third.getTitle()"...........:" +third.getPages()"\n");
		System.out.println("");
	}

am getting the error ") expected" at line 68..Is there a way I can call the method printInfo from the class mainPrg? maybe something like calling the class Chapter from mainPrg which can call the method printInfo..

You are just missing the additions of the strings here, in all your numbered lines do this

System.out.println("1." +first.getTitle()     [B]+[/B]     "........." +first.getPages());

am getting the error ") expected" at line 68..Is there a way I can call the method printInfo from the class mainPrg? maybe something like calling the class Chapter from mainPrg which can call the method printInfo..

Don't tell me that you couldn't also figure out that out?

Create an instance of the class whose method you want to call.

Sorry for the obvious questions but you know what seems to be obvious to one is not obvious to another. Here is my updated code with a few more hitches.

class Book{
	
	private String author;
	private String title;
	private int year;
	private Chapter first;
	private Chapter second;
	private Chapter third;
	
	public Book(){
		author="";
		title="";
		year=0;
		first= new Chapter("Chapter 1",0);
		second=new Chapter("Chapter 2",0);
		third=new Chapter("Chapter 3", 0);
	}
	
	public 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;
	}
	
	public String getTitle()
	{
		return title;
	}
	
	public int getYear()
	{
		return year;
	}
		
	private int TotalPages()
	{
		int total=0;
		total=first.getPages()+ second.getPages()+ third.getPages;
		return total;	
	}
	
	public void printInfo()
	{
		System.out.println("Book Title: " +getTitle());
		System.out.println("");
		System.out.println("Book Author:" +getAuthor());
		System.out.println("");
		System.out.println("Year:" +getYear());
		System.out.println("");
		System.out.println("Number of pages:" +TotalPages());
		System.out.println("");
	}
	
	public void printContents()
	{
		System.out.println("Contents:");
		System.out.println("1." +first.getTitle()+"........." +first.getPages());
		System.out.println("");
		System.out.println("2." +second.getTitle()+"...........:" +second.getPages());
		System.out.println("");		
		System.out.println("3."+third.getTitle()+"...........:" +third.getPages());
		System.out.println("");
	}

}



class mainPrg
{

	public static void main (String[] args)
	{
		Chapter chpt1=new Chapter("Introduction",100);
		Chapter chpt2=new Chapter("Main Ideas", 150);
		Chapter chpt3=new Chapter("Conclusion", 200);
		
		Book bkobj1=new Book("Joshmo","This is the Book",2008,chpt1,chpt2,chpt3);
		
		bkobj1.printInfo();
		bkobj1.printContents();
	}
}

Iam getting the following errors on line 49
1.Cannot find symbol variable getPages()
2.Operator + cannot be applied to int, Chapter.getPages
3.Incompatible types

You forgot the parenthesis at the end:
total=first.getPages()+ second.getPages()+ third.getPages;
total=first.getPages()+ second.getPages()+ third.getPages();

Errors 1, 2 (and most likely 3) are generated because third.getPages is being parsed as a variable that exists within the Class associated by the object third.

total=first.getPages()+ second.getPages()+ third.getPages; // missing parenthesis after getPages

Edit: 1-upping me, eh javaAddict? =P

This question has already been answered. Start a new discussion instead.