public class BookTeater public static void main (string 1 args) Book bl new Book
ID: 3810671 • Letter: P
Question
public class BookTeater public static void main (string 1 args) Book bl new Book ("Java programming a & Loftus Pearson 2014) System-out-print in (b1 +"In"): Book b2 new Book ("Gone with the Wind "Margaret Mitchell Amazon 1960) System out-printin (b2): public static class Book private String title, author, publisher private int copyright public Book (String t String a String p int cR) title author a: publisher p: copyright CR: public String toString return "Title: title "InAuthor: author "AnPublisher: publisher "AncopyRight t copyright:Explanation / Answer
I have added the getter setter functions and also re-wrote the code as follows :- (i have written the getter setter functions in bold form)
Code:-
import java.util.*;
import java.lang.*;
import java.io.*;
class BookTester
{
public static void main(String []args) throws java.lang.Exception
{
Book b1=new Book("Java programming","Lewis & Loftus","Pearson",2014);
System.out.println(b1 + " ");
Book b2=new Book("Gone with the Wind","Margaret Mitcvhell","Amazon",1960);
System.out.println(b2);
}
static class Book{
private String title,author,publisher;
private int copyright;
public void setTitle(String t){
title=t;
}
public void setAuthor(String t){
author=t;
}
public void setPublisher(String t){
publisher=t;
}
public void setCopyright(int t){
copyright=t;
}
public Book(String t,String a,String p,int cR){
setTitle(t);
setAuthor(a);
setPublisher(p);
setCopyright(cR);
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public String getPublisher(){
return publisher;
}
public int getCopyright(){
return copyright;
}
public String toString()
{
return "title : "+getTitle()+" Author: "+getAuthor()+" Publisher "+getPublisher()+" CopyRight: "+getCopyright();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.