...
Driver Program (Demo1)
Code Block | ||
---|---|---|
| ||
/** 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:
LocalDate
Import: This line imports theLocalDate
class from thejava.time
package.LocalDate
is a class that represents a date (year, month, day).main
Method: Themain
method is the entry point for any Java application. The Java Virtual Machine (JVM) calls this method when the program starts.Creating a
TextBook
Object: It creates a newTextBook
object namedmyTextBook
by calling theTextBook
constructor with the parameters"Learn Java"
,"Bart Simpson"
,759
,"8th Edition"
, and"Computer Science"
.Printing TextBook Information: The
System.out.println(myTextBook.PrintInformation());
statement prints the information of themyTextBook
object to the console by calling thePrintInformation
method of themyTextBook
object.Creating a
LocalDate
Object: It creates aLocalDate
object namedmyDate
representing the date August 30, 2022. This date object will be used as an argument while creating aMagazine
object.Creating a
Magazine
Object: It creates a newMagazine
object namedmyMagazine
by calling theMagazine
constructor with the parameters"Time Magazine"
,"Time Life Publisher"
,83
,"Monthly"
, andmyDate
.Printing Magazine Information: The
System.out.println(myMagazine.PrintInformation());
statement prints the information of themyMagazine
object to the console by calling thePrintInformation
method of themyMagazine
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
|