JAVA PROGRAMMING_____FINAL PROJECT ( THANK YOU FOR YOUR HELPING) I made 7 java c
ID: 3810649 • Letter: J
Question
JAVA PROGRAMMING_____FINAL PROJECT ( THANK YOU FOR YOUR HELPING)
I made 7 java classes of ReadingMaterial, newspaper, manga, book, textbook, novel and rectangular in the below.
From these 7 java class, Can you create a class "ReadingFactory."
"ReadingFactory."
ReadingFactory should create a newspaper, a manga, a textbook, and a novel.
a. ReadingFactory should contain a method that demonstrates polymorphism by displaying the object sent to it.
b. ReadingFactory should have an arraylist of all the instantiated objects - sort the collection, and then use the display method.
c. Demonstrate casting an object back to its original data type (e.g. cast a ReadingMaterial back to a Novel.)
Be sure all methods have been tested.
Be sure to include JavaDocs! (Generate and save them)
------------------------------------------------------------------------------------------------------
// 1. ReadingMaterial Class
public class ReadingMaterial {
private String language;
String publisher;
//Constructor
public ReadingMaterial(String publisher, String language)
{
setPublisher(publisher);
setLanguage(language);
}
//Getter method for language
public String getLanguage()
{
//Returning language
return language;
}
//Getter method for publisher
public String getPublisher()
{
//Returning publisher
return publisher;
}
//Setter method for language
private void setLanguage(String language)
{
//Setting language
this.language = language;
}
public void setLanguage() {
this.language = "English";
}
//Setter method for publisher
private void setPublisher(String publisher)
{
//Setting publisher
this.publisher = publisher;
}
//Overriding toString method
@Override
public String toString()
{
return " Publisher: " + getPublisher() + " Language: " + getLanguage();
}
}
-----------------------------------------------------------------------------------------------------
//2. Newspaper Class
// Create a subclass of ReadingMaterial called Newspaper - should call superclass constructors
public class Newspaper extends ReadingMaterial
{
//Instance variables
private String name;
private Rectangle size;
private int numPages;
//Constructor
public Newspaper(String publisher, String name, Rectangle size, int numPages)
{
//Calling super class constructor
super(publisher,"English");
setName(name);
setSize(size);
setNumPages(numPages);
}
//Getter method for name
public String getName()
{
//Returning name
return name;
}
//Getter method for size
public Rectangle getSize()
{
//Returning size
return size;
}
//Getter method for number of pages
public int getNumPages()
{
//Returning number of pages
return numPages;
}
//Setter method for name
private void setName(String name)
{
//Setting name
this.name = name;
}
//Setter method for size
private void setSize(Rectangle size)
{
//Setting size
this.size = size;
}
//Setter method for number of pages
private void setNumPages(int numPages)
{
if (numPages <= 0)
numPages = 1;
//Setting numPages
this.numPages = numPages;
}
//Overriding toString method
@Override
public String toString()
{
return " Name: " + getName() + " Size: " + getSize() + " Number of Pages: " + getNumPages() + super.toString();
}}
---------------------------------------------------------------------------------------------------------------------
//3. Manga Class
//Create a subclass of ReadingMaterial called Manga- should call superclass constructors
public class Manga extends ReadingMaterial {
private String name;
private String author;
private Rectangle size;
private int numPages;
private boolean typicalReadingOrder;
public Manga(String publisher,String name, String author, Rectangle size, int numPages, boolean typicalReadingOrder) {
super(publisher, "English");
setName(name);
setAuthor(author);
setSize(size);
setNumPages(numPages);
setTypicalReadingOrder(typicalReadingOrder);
}
/*Setters and Getters */
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
private void setAuthor(String author) {
this.author = author;
}
public Rectangle getSize() {
return size;
}
private void setSize(Rectangle size) {
this.size = size;
}
public int getNumPages() {
return numPages;
}
private void setNumPages(int numPages) {
if (numPages <= 0)
numPages = 1;
this.numPages = numPages;
}
public boolean getTypicalReadingOrder() {
return typicalReadingOrder;
}
private void setTypicalReadingOrder(boolean typicalReadingOrder) {
this.typicalReadingOrder = typicalReadingOrder;
}
@Override
public String toString() {
return "Manga{" + " publisher=" + publisher +" name=" + name + ", author=" + author + ", size=" + size + ", numPages=" + numPages + ", typicalReadingOrder=" + typicalReadingOrder + " }";
} }
------------------------------------------------------------------------------------------------------------------------
//4. Book Class
//Create a subclass of ReadingMaterial called Book- should call superclass constructors
public class Book extends ReadingMaterial {
private String name;
private String author;
private Rectangle size;
private int numPages;
private String ISBN;
public Book(String publisher,String name, String author, Rectangle size, int numPages,String ISBN)
{
super(publisher,"English");
setName(name);
setAuthor(author);
setSize(size);
setNumPages(numPages);
setISBN(ISBN);
}
public String getName(){
return name;
}
private void setName(String name){
this.name=name;
}
public String getAuthor(){
return author;
}
private void setAuthor(String author){
this.author=author;
}
public int getNumPages(){
return numPages;
}
private void setNumPages(int numPages){
this.numPages=numPages;
if (numPages <= 0)
numPages = 1;
}
public Rectangle getSize(){
return size;
}
private void setSize(Rectangle size){
this.size=size;
}
private void setISBN(String ISBN){
this.ISBN=ISBN;
}
public String getISBN(){
return ISBN;
}
@Override
public String toString() {
return "Book{" + " publisher=" + publisher +" name=" + name + ", author=" + author + ", size=" + size + ", numPages=" + numPages +" ISBN=" + ISBN + " }";
}
public void display(){
System.out.println("Display method of Book is called");
}}
--------------------------------------------------------------------------------------------------
//5. Textbook class
//Create a subclass of Book called Textbook- should call superclass constructors
public class Textbook extends Book {
private String subject;
private String course;
public Textbook(String publisher, String name, String author, Rectangle size, int numPages, String ISBN,
String subject, String course) {
super(publisher,name,author,size,numPages,ISBN );
setSubject(subject);
setCourse(course);
}
public String getSubject() {
return subject;
}
private void setSubject(String subject) {
this.subject = subject;
}
public String getCourse() {
return course;
}
private void setCourse(String course) {
this.course = course;
}
@Override
public String toString() {
return "Textbook [subject=" + subject + ", course=" + course + "]";
}
@Override
public void display() {
System.out.println("Display method of Textbook is called");
}}
---------------------------------------------------------------------------------------------------
//6. Novel class
//Create a subclass of Book called Novel- should call superclass constructors
public class Novel extends Book {
private String genre;
public Novel(String publisher, String name, String author, Rectangle size, int numPages, String ISBN,
String genre) {
super(publisher,name,author,size,numPages,ISBN );
setGenre(genre);
}
public String getGenre() {
return genre;
}
private void setGenre(String genre) {
this.genre = genre;
}
@Override
public String toString() {
return "Novel [genre=" + genre + "]";
}
@Override
public void display() {
System.out.println("Display method of Novel is called");
}
}
------------------------------------------------------------------------------------------------------
//7. Rectangle class
public class Rectangle {
private double width;
private double length;
Rectangle() {
width=1;
length=1;
}
Rectangle(double length, double width) {
setWidth(width);
setLength(length);
}
public double getWidth() {
return width;
}
private void setWidth(double width) {
if (width <= 0)
width = 1;
this.width = width;
}
public double getLength() {
return length;
}
private void setLength(double length) {
if (length <= 0)
length = 1;
this.length = length;
}
@Override
public String toString() {
return width + " * " + length;
}
}
Explanation / Answer
//ReadingFactory class
import java.util.ArrayList;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.html.HTMLDocument.Iterator;
public class ReadingFactory {
public static void showObject(ReadingMaterial obj)
{
if(obj instanceof Newspaper)
System.out.println("The sent object is of type Newspapaer");
if(obj instanceof Manga)
System.out.println("The sent object is of type Manga");
if(obj instanceof Novel)
System.out.println("The sent object is of type Novel");
}
public static void main(String agrs[])
{
Rectangle size1 = new Rectangle(100.0, 200.0);
//Newspaper(String publisher, String name, Rectangle size, int numPages)
Newspaper news = new Newspaper("N_Navneet", "N_Naman", size1, 100);
Rectangle size2 = new Rectangle(20.0, 10.0);
//Manga(String publisher,String name, String author, Rectangle size, int numPages, boolean typicalReadingOrder)
Manga mang = new Manga("M_Navneet", "M_Naman", "M_Ronaldo", size2, 200, true);
Rectangle size3 = new Rectangle(30.0, 20.0);
//Textbook(String publisher, String name, String author, Rectangle size, int numPages, String ISBN,String subject, String course)
Textbook text = new Textbook("T_Navneet", "T_Naman","T_Ronaldo", size3, 300, "T_ISBN", "T_MATHS", "T_Algebra");
Rectangle size4 = new Rectangle(40.0, 30.0);
//Novel(String publisher, String name, String author, Rectangle size, int numPages, String ISBN, String genre)
Novel nov = new Novel("N_Navneet", "N_Naman", "N_Ronaldo", size4, 400, "N_ISBN", "N_Fiction");
// testing 2 objects by polymorphism
showObject(news);
showObject(mang);
showObject(nov);
//Array list to add objects and call display method
ArrayList<ReadingMaterial> alist=new ArrayList<ReadingMaterial>();
alist.add(news);
alist.add(mang);
alist.add(text);
alist.add(nov);
//Collections.sort(alist);
for(ReadingMaterial rm:alist)
{
System.out.println();
rm.display();
//rm.toString();
System.out.println(rm.getPublisher());
}
}
}
//To generate JavaDocs in Eclipse Follow the below steps:
//1. Go To Project> Generate Javadoc
//2. In JavaCommand field, browse to find javadoc.exe
//3. Check the box next to project/package/file for which you are creating javadoc
//4. In destination field, browser the desired destination
//5. Finish
// Moreover you need to add display method as below in ReadingMaterial.java
public void display() {
this.display();
}
Newspaper.java
public void display(){
System.out.println("Display method of Newspaper is called");
}
Manga.java
public void display(){
System.out.println("Display method of Manga is called");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.