Design a class called Writer
, which is designed to model a book's writer. It contains:
private
instance variables: name
(String
), email
(String
), and gender
(char
of either 'm'
or 'f'
);name
, email
and gender
with the given values;public Writer(String name, String email,char gender) {......}
public
getters/setters: getName()
, getEmail()
, setEmail()
, and getGender()
;(There are no setters for name
and gender
, as these attributes cannot be changed.)
toString()
method that returns "Writer[name=?,email=?,gender=?]
", e.g., "Writer[name=Girdhar Gopal,[email protected],gender=m]
".Also, design a class called Book
to model a book written by one writer. It contains:
private
instance variables: name
(String
), writers
(an array of the class Writer
you have just created, assume that a book can have any number of writers), price
(double
), and qty
(int
);publicBook (String name, Writer[] writer,double price) { ...... }
publicBook (String name, Writer[] writer,double price,int qty) { ...... }
getName()
, getPrice()
, setPrice()
, getQty()
, setQty()
.toString()
that returns "Book[name=?,Writers={Writer[name=?,email=?,gender=?],........},price=?,qty=?
". You should reuse Writer
’s toString()
.class Writer
{
private String name;
private String email;
private char gender;
public Writer(String name,String email,char gender)
{
this.name=name;
this.email=email;
this.gender=gender;
}
public String getName()
{
return name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email=email;
}
public char getGender()
{
return gender;
}
public String toString()
{
return "Writer[name="+name+",email="+email+",gender="+gender+"]";
}
}
class Book
{
private String name;
private Writer[]writers;
private double price;
private int qty;
public Book(String name,Writer[]writers,double price)
{
this.name=name;
this.writers=writers;
this.price=price;
}
public Book(String name,Writer[]writers,double price,int qty)
{
this.name=name;
this.writers=writers;
this.price=price;
this.qty=qty;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price=price;
}
public int getQty()
{
return qty;
}
public void setQty(int qty)
{
this.qty=qty;
}
public String toString()
{
StringBuilder sb=new StringBuilder();
sb.append("{");
for(int i=0;i<writers.length;i++)
{
sb.append(writers[i].toString());
if(i<writers.length-1)
{
sb.append(",");
}
}
sb.append("}");
return "Book[name="+name+",Writers="+sb.toString()+",price="+price+",qty="+qty;
}
}