Given main() and a base Book
class, define a derived class called Encyclopedia
. Within the derived Encyclopedia
class, define a printInfo() method that overrides the Book
class' printInfo() method by printing not only the title, author, publisher, and publication date, but also the edition and number of volumes.
Ex. If the input is:
The Hobbit J. R. R. Tolkien George Allen & Unwin 21 September 1937 The Illustrated Encyclopedia of the Universe James W. Guthrie Watson-Guptill 2001 2nd 1
the output is:
Book Information: Book Title: The Hobbit Author: J. R. R. Tolkien Publisher: George Allen & Unwin Publication Date: 21 September 1937 Book Information: Book Title: The Illustrated Encyclopedia of the Universe Author: James W. Guthrie Publisher: Watson-Guptill Publication Date: 2001 Edition: 2nd Number of Volumes: 1
Note: Indentations use 3 spaces.
BookInformation.java
import java.util.Scanner; public class BookInformation { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Book myBook = new Book(); Encyclopedia myEncyclopedia = new Encyclopedia(); String title, author, publisher, publicationDate; String eTitle, eAuthor, ePublisher, ePublicationDate, edition; int numVolumes; title = scnr.nextLine(); author = scnr.nextLine(); publisher = scnr.nextLine(); publicationDate = scnr.nextLine(); eTitle = scnr.nextLine(); eAuthor = scnr.nextLine(); ePublisher = scnr.nextLine(); ePublicationDate = scnr.nextLine(); edition = scnr.nextLine(); numVolumes = scnr.nextInt(); myBook.setTitle(title); myBook.setAuthor(author); myBook.setPublisher(publisher); myBook.setPublicationDate(publicationDate); myBook.printInfo(); myEncyclopedia.setTitle(eTitle); myEncyclopedia.setAuthor(eAuthor); myEncyclopedia.setPublisher(ePublisher); myEncyclopedia.setPublicationDate(ePublicationDate); myEncyclopedia.setEdition(edition); myEncyclopedia.setNumVolumes(numVolumes); myEncyclopedia.printInfo(); } }
Book.java
public class Book { protected String title; protected String author; protected String publisher; protected String publicationDate; public void setTitle(String userTitle) { title = userTitle; } public String getTitle() { return title; } public void setAuthor(String userAuthor) { author = userAuthor; } public String getAuthor(){ return author; } public void setPublisher(String userPublisher) { publisher = userPublisher; } public String getPublisher() { return publisher; } public void setPublicationDate(String userPublicationDate) { publicationDate = userPublicationDate; } public String getPublicationDate() { return publicationDate; } public void printInfo() { System.out.println("Book Information: "); System.out.println(" Book Title: " + title); System.out.println(" Author: " + author); System.out.println(" Publisher: " + publisher); System.out.println(" Publication Date: " + publicationDate); } }
Encyclopedia.java
public class Encyclopedia extends Book { // TODO: Declare private fields: edition, numVolumes // TODO: Define mutator methods - // setEdition(), setNumVolumes() // TODO: Define accessor methods - // getEdition(), getNumVolumes() // TODO: Define a printInfo() method that overrides // the printInfo in Book class }
Encyclopedia.java
public class Encyclopedia extends Book { private String edition; private int numVolumes; public void setEdition(String str) { edition = str; } public void setNumVolumes(int num) { numVolumes = num; } public String getEdition() { return edition; } public int getNumVolumes() { return numVolumes; } public void printInfo() { super.printInfo(); System.out.println(" Edition: " + edition); System.out.println(" Number of Volumes: " + numVolumes); } }
10.14 LAB: Instrument information (derived classes)Given main() and the Instrument class, define a derived class, StringInstrument, for string instruments.Ex. If the input is:Drums Zildjian 2015 2500 Guitar Gibson 2002 1200 6 19the output is:Instrument Information: Name: Drums Manufacturer: Zildjian Year built: 2015 Cost: 2500 Instrument Information: Name: Guitar Manufacturer: Gibson Year built: 2002 Cost: 1200 Number of strings: 6 Number of frets: 19InstrumentInformation.javaimport java.util.Scanner; public class InstrumentInformation { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Instrument myInstrument = new Instrument(); StringInstrument myStringInstrument = new StringInstrument(); String instrumentName, manufacturerName, stringInstrumentName, stringManufacturer; int yearBuilt, cost, stringYearBuilt, stringCost, numStrings, numFrets; instrumentName = scnr.nextLine(); manufacturerName = scnr.nextLine(); yearBuilt = scnr.nextInt(); scnr.nextLine(); cost = scnr.nextInt(); scnr.nextLine(); stringInstrumentName = scnr.nextLine(); stringManufacturer = scnr.nextLine(); stringYearBuilt = scnr.nextInt(); stringCost = scnr.nextInt(); numStrings = scnr.nextInt(); numFrets = scnr.nextInt(); myInstrument.setName(instrumentName); myInstrument.setManufacturer(manufacturerName); myInstrument.setYearBuilt(yearBuilt); myInstrument.setCost(cost); myInstrument.printInfo(); myStringInstrument.setName(stringInstrumentName); myStringInstrument.setManufacturer(stringManufacturer); myStringInstrument.setYearBuilt(stringYearBuilt); myStringInstrument.setCost(stringCost); myStringInstrument.setNumOfStrings(numStrings); myStringInstrument.setNumOfFrets(numFrets); myStringInstrument.printInfo(); System.out.println(" Number of strings: " + myStringInstrument.getNumOfStrings()); System.out.println(" Number of frets: " + myStringInstrument.getNumOfFrets()); } }Instrument.javapublic class Instrument { protected String instrumentName; protected String instrumentManufacturer; protected int yearBuilt, cost; public void setName(String userName) {...
LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information: Name: Dobby Age: 2 Pet Information: Name: Kreacher Age: 3 Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Pet myPet = new Pet(); Dog myDog = new Dog(); String petName, dogName, dogBreed; int petAge, dogAge; petName = scnr.nextLine(); petAge = scnr.nextInt(); scnr.nextLine();...
10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10 flower Hydrangea 30 false lilac flower Rose 6 false white plant Mint 4...
Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...
Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...
(JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...
10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...
JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...
Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LibraryCard { private String id; private String cardholderName; ...
this needs to be in Java: use a comparator class which is passed into the sort method that is available on the ArrayList. import java.util.Scanner; public class Assign1{ public static void main(String[] args){ Scanner reader = new Scanner (System.in); MyDate todayDate = new MyDate(); int choice = 0; Library library = new Library(); while (choice != 6){ displayMainMenu(); if (reader.hasNextInt()){ choice = reader.nextInt(); switch(choice){ case 1: library.inputResource(reader, todayDate); break; case 2: System.out.println(library.resourcesOverDue(todayDate)); break; case 3: System.out.println(library.toString()); break; case 4: library.deleteResource(reader,...