Question 4.18: Write a Java class Author with following features:
• Instance variables :
o firstName for the author’s first name of type String.
o lastName for the author’s last name of type String.
• Constructor:
o public Author (String firstName, String lastName): A constructor with
parameters, it creates the Author object by setting the two fields to the passed values.
• Instance methods:
o public void setFirstName (String firstName): Used to set the first name of author.
o public void setLastName (String lastName): Used to set the last name of author.
o public double getFirstName(): This method returns the first name of the author.
o public double getLastName(): This method returns the last name of the author.
o public String toString(): This method printed out author’s name to the screen
Write a Java class Book with following features:
• Instance variables :
o title for the title of book of type String.
o author for the author’s name of type String.
o price for the book price of type double.
• Constructor:
o public Book (String title, Author name, double price): A constructor with
parameters, it creates the Author object by setting the the fields to the passed values.
• Instance methods:
o public void setTitle(String title): Used to set the title of book.
o public void setAuthor(String author): Used to set the name of author of book.
o public void setPrice(double price): Used to set the price of book.
o public double getTitle(): This method returns the title of book.
o public double getAuthor(): This method returns the author’s name of book.
o public String toString(): This method printed out book’s details to the screen
Write a separate class BookDemo with a main() method creates a Book titled
“Developing Java Software” with authors Russel Winderand price 79.75. Prints the
Book’s string representation to standard output (using System.out.println).
Solution:
package com.company;
import java.util.Scanner;
public class lab4problem18 {
public static void main(String[] args) {
Scanner Obj = new Scanner(System.in);
Author A1 = new Author();
String f = Obj.nextLine();
A1.setFirstName(f);
A1.getFirstName();
String g =Obj.nextLine();
A1.setLastName(g);
A1.getLastName();
System.out.println(A1);
System.out.println("title of the name:");
String t = Obj.nextLine();
System.out.println("author is :");
String a = Obj.nextLine();
System.out.println("price is :");
double p = Obj.nextDouble();
Book B1 = new Book(t,a,p);
B1.setTitle(t);
B1.getTitle();
B1.setAuthorn(a);
B1.getAuthorn();
B1.setPrice(p);
B1.getPrice();
System.out.println(B1);
}
}
public class Author {
String firstName;
String lastName;
public void setFirstName(String firstName){
this.firstName =firstName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String LastName){
this.lastName =LastName;
}
public String getLastName(){
return lastName;
}
public String toString()
{
return "authoer:" + firstName +" "+ lastName;
}
}
public class Book {
String title;
String authorn;
double price;
Book(String title,String authorn , double price){
this.title =title;
this.authorn =authorn;
this.price = price;
}
public void setTitle(String title){
this.title =title;
}
public String getTitle()
{
return title;
}
public void setAuthorn(String author){
this.authorn = authorn;
}
public String getAuthorn()
{
return authorn;
}
public void setPrice(double price){
this.price =price;
}
public double getPrice()
{
return price;
}
public String toString()
{
return "book title:" + title +" athor : "+ authorn+ " price:" + price;
}
}