In: Computer Science
1. Look at the following partial class definition, and then respond to the questions that follow it: public class Book { private String title; private String author; private String publisher; private int copiesSold; } a. Write a constructor for this class. The constructor should accept an argument for each of the fields. b. Write accessor and mutator methods for each of the fields. c. Draw a UML diagram for the class, including the methods you have written. 2. Consider the following class declaration: public class Addition { public int num1, num2; public String s1; public int add(int num1, int num2) { int sum = num1 +num2; return sum; } } a. Write a no argument constructor for the class. It should assign the value 0 to num1 and num2 fields. b. Write an overload constructor for this class that will concatenate the string values passed as arguments to it.
(1)
class Book {
private String title;
private String author;
private String publisher;
private int copiesSold;
}
(a)public Book(String title,String author,String publisher,int
copiesSold){
this.title = title;
this.author = author;
this.publisher = publisher;
this.copiesSold = copiesSold;
}
(b)
void setTitle(String title){this.title = title;}
void setAuthor(String author){this.author = author;}
void setPublisher(String publisher){this.publisher=
publisher;}
void setCopiesSold(int copiesSold){this.copiesSold =
copiesSold;}
String getTitle(){return title;}
String getAuthor(){return author;}
String getPublisher(){return publisher;}
int getCopiesSold(){return copiesSold;}
(c)UML diagram
Book
- title : String
- author : String
- publisher : String
- copiesSold : int
+ Book(String , String , String , int)
+ setTitle(String) : void
+ setAuthor(String) : void
+ setPublisher(String) : void
+ setCopiesSold(String) : void
+ getTitle() : String
+ getAuthor() : String
+ getPublisher() : String
+ getCopiesSold() : int
(2)
public class Addition {
public int num1, num2;
public String s1;
public int add(int num1, int num2) {
int sum = num1 +num2;
return sum;
}
}
(a) No argument constructor
public Addition(){
num1 = 0;
num2 = 0;
}
(b)
public Addition(int n1 , int n2){
num1 = n1;
num2 = n2;
}