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 field.


c. Draw a UML diagram for the class, including the methods you have written.

Respuesta :

Solution :

a.

public Book([tex]$\text{String title}$[/tex], String author, [tex]$\text{String publisher}$[/tex], int [tex]$\text{copiesSold}$[/tex]) {

 this.[tex]$\text{title}$[/tex] = [tex]$\text{title}$[/tex];

 this.[tex]$\text{author}$[/tex] = [tex]$\text{author}$[/tex];

 this.[tex]$\text{publisher}$[/tex] = [tex]$\text{publisher}$[/tex];

 this.[tex]$\text{copiesSold}$[/tex] = [tex]$\text{copiesSold}$[/tex];

b). [tex]$\text{public String}$[/tex] getTitle() {

 return [tex]$\text{title}$[/tex];

}

[tex]$\text{public void}$[/tex] setTitle([tex]$\text{String title}$[/tex]) {

 this.[tex]$\text{title}$[/tex] = [tex]$\text{title}$[/tex];

}

[tex]$\text{public String}$[/tex] getAuthor() {

 return author;

}

[tex]$\text{public void}$[/tex] setAuthor(String author) {

 this.[tex]$\text{author}$[/tex] = [tex]$\text{author}$[/tex];

}

[tex]$\text{public String}$[/tex] getPublisher() {

 return [tex]$\text{publisher}$[/tex];

}

[tex]$\text{public void}$[/tex] setPublisher(String [tex]$\text{publisher}$[/tex]) {

 this.[tex]$\text{publisher}$[/tex] =[tex]$\text{publisher}$[/tex];

}

public int get[tex]$\text{copiesSold}$[/tex]() {

 return [tex]$\text{copiesSold}$[/tex];

}

[tex]$\text{public void}$[/tex] set[tex]$\text{copiesSold}$[/tex](int [tex]$\text{copiesSold}$[/tex]) {

 this.[tex]$\text{copiesSold}$[/tex] = [tex]$\text{copiesSold}$[/tex];

}