Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Driver Program (Demo1)

Code Block
languagejava
/** Driver *to Driverdemonstrate classpolymorphism tousing testthe Textbook and Magazine Class 
 */
that inherit
from Publication 
 */import java.time.LocalDate;
import java.util.ArrayList; // import the ArrayList class
import java.time.LocalDate; //needed for librarya fordate LocalDatevaraible Class

public class Demo1Demo4 {

	public static void main(String[] args) {
	
		//create an instanceArray List of TextbookPublications
		TextBookArrayList<Publication> myTextBookmyPublications = new TextBookArrayList<Publication>("Learn Java", "Bart Simpson", 759, "8th Edition", "Computer Science");
		System.out.println(myTextBook.PrintInformation());	
);
		
		//now add some Magazines and textbook to the Publication ArrayList		
		//createadding a date that will be used for the Magazine 
		LocalDate myDate = LocalDate.of(2022, 8, 30);   
textbooks
		myPublications.add(new TextBook("Learn Java", "Bart Simpson", 759, "8th Edition", "Computer Science") ); 
		myPublications.add(new TextBook("Learn C++", "Fred Flintstone", 759, "7th Edition", "Computer Science") ); 
		
		//create an instance of Magazinea date
		LocalDate myDate = LocalDate.of(2022, 8, 30); 
		Magazine
myMagazine = 		//adding magazines
		myPublications.add(new Magazine("Time Magazine", "Time Life Publisher", 83, "Monthly", myDate)); 
		SystemmyPublications.out.println(myMagazine.PrintInformation());add(new Magazine("Alamo College Magizine", "ACCC", 65, "Quarterly", myDate)); 
		
		
	}//end of Main
} //end of Class DriverOne

Example output:

...

This Java code is a driver program that demonstrates how to use the TextBook and Magazine classes that extend the Publication class.

Here's a detailed breakdown:

  1. LocalDate Import: This line imports the LocalDate class from the java.time package. LocalDate is a class that represents a date (year, month, day).

  2. main Method: The main method is the entry point for any Java application. The Java Virtual Machine (JVM) calls this method when the program starts.

  3. Creating a TextBook Object: It creates a new TextBook object named myTextBook by calling the TextBook constructor with the parameters "Learn Java", "Bart Simpson", 759, "8th Edition", and "Computer Science".

  4. Printing TextBook Information: The System.out.println(myTextBook.PrintInformation()); statement prints the information of the myTextBook object to the console by calling the PrintInformation method of the myTextBook object.

  5. Creating a LocalDate Object: It creates a LocalDate object named myDate representing the date August 30, 2022. This date object will be used as an argument while creating a Magazine object.

  6. Creating a Magazine Object: It creates a new Magazine object named myMagazine by calling the Magazine constructor with the parameters "Time Magazine", "Time Life Publisher", 83, "Monthly", and myDate.

  7. Printing Magazine Information: The System.out.println(myMagazine.PrintInformation()); statement prints the information of the myMagazine object to the console by calling the PrintInformation method of the myMagazine object.

...

now iterate through the array and display the magazines and textbooks 
		for(Publication pPubs : myPublications)
		{
			System.out.println(pPubs.PrintInformation());
		}		
		
	}//end of Main

}//end of class