Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program, which prints a table of contents. Use the following data str

ID: 3748074 • Letter: 1

Question

1. Write a program, which prints a table of contents. Use the following data structure: public class TocEntry // Specify the needed methods private String chapter private int page In your driver/test program define: public final int TOCSIZE 100; TocEntry toc[]-new TocEntry[TOCSIZE] int toc curlen 0 Next develop the necessary code, in your TocEntry class, to read in a chapter name and page number until "****is entered. From this generate an output, e.g.: Sample run: % java useTocEntry Enter chapter title: Camelot Enter starting page number: 1 Enter chapter title: King Arthur' s Court Enter starting page number: 3 Enter chapter title: Knights of the Table Round Enter starting page number: 8 Enter chapter title: Sir Dinadan the Humorist Enter starting page number: 12 Enter chapter title: An Inspiration Enter starting page number: 14 Enter chapter title: The Eclipse Enter starting page number: 23 Enter chapter title: A Postscript by Clarence Enter starting page number: 274 Enter chapter title: Camelot King Arthur's Court Knights of the Table Round Sir Dinadan the Humorist An Inspiration The Eclipse A Postscript by Clarence... 12 14 23 274

Explanation / Answer

Welcome to the Chegg !! Its been my pleasure to help you with your queries.

For the code to run, two java files would be created.

MainClass.java and TocEntry.java

TocEntry,java would contain the TocEntry class having the mentioned variables and the getter setter member functions for getting and setting the values of ChapterName and Page Number.

MainClass.java would contain the main() method and it would create the object of the TocEntry class and calls the member functions using dot(.) operator.

Code is mentioned below.I have mentioned the comments in the code to identify the code for the separate files MainClass.java and TocEntry.java

Also, other comments are mentioned throughout the code so that you would better understand the code written.

//This class needs to be defined in a file TocEntry.java

//Code for TocEntry.java [starts]

public class TocEntry {

//setter method for assigning value of Chapter Name

public void setChapName(String chap)

{

this.chapter=chap;

}

//setter method for assigning value of Page Number

public void setPageNo(int PageNo)

{

this.page=PageNo;

}

//getter method for getting value of Chapter Name

String getChapName()

{

return chapter;

}

//getter method for getting value of Page Number

int getPageNo()

{

return page;

}

private String chapter;

private int page;

}

//Code for TocEntry.java [ends]

//This class needs to be defined in a file MainClass.java

import java.util.Scanner;

public class MainClass

{

public static void main(String[] args)

{

final int TOCSIZE=100;

TocEntry toc[]=new TocEntry[TOCSIZE];

int toc_curlen=0;

Scanner s = new Scanner(System.in);//Scanner object is used for getting user input

int flag=1;

String chapName="";

int pageNo;

while(flag==1)

{

TocEntry tocObject=new TocEntry();//creating object of Class TocEntry

chapName="";

  

System.out.print("Enter chapter title: ");

chapName= s.next();//getting the user input for Chapter name. nextLine() is used for getting String type input

tocObject.setChapName(chapName);//method calling for setting the value of chapter name in the current object tocObject

if(chapName.equals("****"))

{

//when if condition satisfies , we print the chapter names alongwith their page numbers

  

flag=0;//value of flag=0 denotes that the while loop would not run further.

for(int i=0;i<toc_curlen;i++)

{

System.out.println(toc[i].getChapName()+".................."+toc[i].getPageNo());

}

}

else

{

System.out.print("Enter starting page number: ");

pageNo= s.nextInt();//getting the user input for page number. nextInt() is used for getting Int type input

tocObject.setPageNo(pageNo);//method calling for setting the value of page no in the current object tocObject

toc[toc_curlen]=tocObject;//storing the object in the array toc at the index toc_curlen

toc_curlen=toc_curlen+1;//incrementing the value of the variable toc_curlen

}

}

}

}

Hope you understood the code mentioned above.

Hoping to see you again on Chegg !!!