Need help with java programming Please only USE JAVA and include a snapshot of r
ID: 3559383 • Letter: N
Question
Need help with java programming
Please only USE JAVA and include a snapshot of running program
Design and implement a set of classes that define various types of reading material: books, novels, magazines, technical journals, textbooks, and so on. Include data values that describe various attributes of the material, such as the number of pages and the names of the primary characters. Include methods that are named appropriately for each class and that print an appropriate message. Create a main driver class to instantiate and exercise several of the classes.
Directions
thank you
Explanation / Answer
Alogorithm:
start
read the type of book
read number of pages
read names of important characters
choose the appropriate book
update the number of pages and important characters to the local variables
display the number of pages and importanat characters
stop
Program Code:
import java.util.Scanner;
public class BooksDemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;
String names;
System.out.println("Choose a type of book: ");
System.out.println("1. Novel");
System.out.println("2. Magazine");
System.out.println("3. Technical Journals");
System.out.println("4. Textbooks");
int ch=sc.nextInt();
System.out.println("Enter number of pages:");
num=sc.nextInt();
System.out.println("Enter names of important characters:(with spaces)");
names=sc.next();
switch(ch)
{
case 1: Novel n=new Novel();
System.out.println("****Novel****");
n.setnumberOfPages(num);
n.setnamesOfPrimaryCharacters(names);
System.out.println("The number of pages are: "+n.getnumberOfPages());
System.out.println("The important characters are:"+n.getnamesOfPrimaryCharacters());
break;
case 2: Magazines m=new Magazines();
System.out.println("****Magazine****");
m.setnumberOfPages(num);
m.setnamesOfPrimaryCharacters(names);
System.out.println("The number of pages are: "+m.getnumberOfPages());
System.out.println("The important characters are:"+m.getnamesOfPrimaryCharacters());
break;
case 3: TechnicalJournals te=new TechnicalJournals();
System.out.println("****Magazine****");
te.setnumberOfPages(num);
te.setnamesOfPrimaryCharacters(names);
System.out.println("The number of pages are: "+te.getnumberOfPages());
System.out.println("The important characters are:"+te.getnamesOfPrimaryCharacters());
break;
case 4: Textbooks tx=new Textbooks();
System.out.println("****Magazine****");
tx.setnumberOfPages(num);
tx.setnamesOfPrimaryCharacters(names);
System.out.println("The number of pages are: "+tx.getnumberOfPages());
System.out.println("The important characters are:"+tx.getnamesOfPrimaryCharacters());
break;
default: System.out.println("Thank you!");
break;
}
}
}
class Novel
{
int numberOfPages;
String namesOfPrimaryCharacters;
Novel()
{
numberOfPages=0;
namesOfPrimaryCharacters="";
}
void setnumberOfPages(int num)
{
this.numberOfPages=num;
}
void setnamesOfPrimaryCharacters(String names)
{
this.namesOfPrimaryCharacters=names;
}
int getnumberOfPages()
{
return numberOfPages;
}
String getnamesOfPrimaryCharacters()
{
return namesOfPrimaryCharacters;
}
}
class Magazines
{
int numberOfPages;
String namesOfPrimaryCharacters;
Magazines()
{
numberOfPages=0;
namesOfPrimaryCharacters="";
}
void setnumberOfPages(int num)
{
this.numberOfPages=num;
}
void setnamesOfPrimaryCharacters(String names)
{
this.namesOfPrimaryCharacters=names;
}
int getnumberOfPages()
{
return numberOfPages;
}
String getnamesOfPrimaryCharacters()
{
return namesOfPrimaryCharacters;
}
}
class TechnicalJournals
{
int numberOfPages;
String namesOfPrimaryCharacters;
TechnicalJournals()
{
numberOfPages=0;
namesOfPrimaryCharacters="";
}
void setnumberOfPages(int num)
{
this.numberOfPages=num;
}
void setnamesOfPrimaryCharacters(String names)
{
this.namesOfPrimaryCharacters=names;
}
int getnumberOfPages()
{
return numberOfPages;
}
String getnamesOfPrimaryCharacters()
{
return namesOfPrimaryCharacters;
}
}
class Textbooks
{
int numberOfPages;
String namesOfPrimaryCharacters;
Textbooks()
{
numberOfPages=0;
namesOfPrimaryCharacters="";
}
void setnumberOfPages(int num)
{
this.numberOfPages=num;
}
void setnamesOfPrimaryCharacters(String names)
{
this.namesOfPrimaryCharacters=names;
}
int getnumberOfPages()
{
return numberOfPages;
}
String getnamesOfPrimaryCharacters()
{
return namesOfPrimaryCharacters;
}
}
Sample Output:
Choose a type of book:
1. Novel
2. Magazine
3. Technical Journals
4. Textbooks
1
Enter number of pages:
23
Enter names of important characters:(with spaces)
john murrah
****Novel****
The number of pages are: 23
The important characters are:john
Screen Shot: