Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp.
Also uses Super in the TextBook Class section.
Here I am providing the answers for above questions:
Problem 1:
class Book
{
String title;
double price;
public Book() //first constructor(default consructor)
{
this.title="";
this.price=0.0;
}
public Book(String title,double price) //second constructor with
parameters
{
this.title=title;
this.price=price;
}
public String getTitle() //getTitle method to return the
title
{
return this.title;
}
public void setTitle(String title) //setTitle method to assign the
title
{
this.title=title;
}
public double getPrice() //getPrice method to return the
price
{
return this.price;
}
public void setPrice(double price) //setTitle mehtod to assign the
price
{
this.price=price;
}
public String toString() //toString method to print details
{
return "\nTitle:"+this.title+"\nPrice:"+this.price;
}
}
public class BookApp
{
public static void main(String args[])
{
Book b1=new Book(); //creating the first object b1 by calling
default constructor
b1.setTitle("John Doe");
b1.setPrice(8.5);
System.out.println(b1.toString()); //calling toString method to
print details of book1
Book b2=new Book("Ann Smith",9.7); //creating the second object b2
by calling parameter constructor.
System.out.println(b2.toString()); //calling toString method to
print details of book2
}
}
Output:
Problem 2:
class TextBook extends Book //TextBook class extending the Book
Class
{
int courseID;
public TextBook() //first constructor(default consructor)
{
super("",0.0);
this.courseID=0;
}
public TextBook(String title,double price,int courseID) //second
constructor with parameters
{
super(title,price);
this.courseID=courseID;
}
public int getCourseID() //getCourseID method to return the
courseID
{
return this.courseID;
}
public void setCourseID(int courseID) //setCourseID method to
assign the courseID
{
this.courseID=courseID;
}
public String toString() //toString method to print details
{
return super.toString()+"\nCourseID:"+this.courseID;
}
}
public class TextBookApp
{
public static void main(String args[])
{
Book b=new Book("Ann Smith",9.7); //creating the book object b by
calling parameter constructor
System.out.println(b.toString()); //printing the details by calling
toString method.
TextBook tb1=new TextBook(); //creating the first object tb1 by
calling default constructor
tb1.setTitle("John Doe");
tb1.setPrice(8.5);
tb1.setCourseID(2050);
System.out.println(tb1.toString()); //printing the details
TextBook tb2=new TextBook("Ann Smith",9.7,3090); //creating the
second object t2 by calling parameter constructor.
System.out.println(tb2.toString()); //printing the details
}
}
Output:
Hoping that the above answers will help you...Thank you...
Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp....
Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double +setPrice(double): void +toString(): String The class has two attributes, title and price, and get/set methods for the two attributes. The first constructor doesn’t have a parameter. It assigns “” to title and 0.0 to price; The second constructor uses passed-in parameters to initialize the two attributes. The toString() method returns values for the two attributes. Notation: - means private, and + means public. 1....
C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...
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...
Programming language: Java Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...
Code should be in C# Create a class called SavingsAccount. Use a static variable called annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance. Provide static method setAnnualInterestRate to set the annualInterestRate to a new value....
In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...
Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...
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...
Below is a class definition (.h) for the Bookclass. class Book { private: int numPages; string author; string isbn; double price; public: string title; public: Book (); void setAuthor(string); string getAuthor(); void setIsbn(string); string getIsbn (); void setPrice(double); double getPrice (); string getTitle (); }; Assume you are writing code within the maindriver program: declare variable of type Book. Using the Bookvariable, set your book’s author to Edgar Allan Poe Using the Bookvariable, set your book’s title to The...