Question

In: Computer Science

1. Look at the following partial class definition, and then respond to the questions that follow...

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.

Solutions

Expert Solution

(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;
}


Related Solutions

Use the following information to respond to the questions that immediately follow. Assume that direct materials...
Use the following information to respond to the questions that immediately follow. Assume that direct materials are added at the beginning of the production process, and conversion costs are added throughout the production process. Degree of completion Costs incurred Item Physical units Materials Conversion Materials Conversion Beginning WIP Inventory 3,000 20% $     30,000 $     12,000 Started during the period 7,000 $     56,000 $     42,000 Transferred out 8,000 Ending WIP Inventory 2,000 80% 50% a. Units started and completed during the...
Review the following scenario and respond to the follow-up questions in an APA formatted report of...
Review the following scenario and respond to the follow-up questions in an APA formatted report of 2400-2600 words. Students will also submit a PowerPoint presentation of no more than 8 slides summarizing the case and your response, to be posted and discussed by peers in Unit 11. Case Tom, Dick, and Harry have been best friends since kindergarten. Tom is the business brain of the group. Dick is the mechanical one and Harry is the creative one. All grown-up, the...
Use the definition of incidence taught in class, and answer the following questions. Be sure to...
Use the definition of incidence taught in class, and answer the following questions. Be sure to show your work. 1.      Spring fever! Epidemiologists and teachers alike have noticed the cyclic occurrence of the dreaded spring fever. Students afflicted with this disease exhibit certain listlessness in class. And they seem to stare out the windows with remarkable tenacity. Why, it’s almost as if the trees growing outside are more interesting than doing algebra. (Gasp!) At Metropolitan High School, teachers have been...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } Question 21 Not yet answered Marked out of 1.00 Flag question Question text D3a - [1 Mark] How many field attributes are there in the TrafficLight class? Answer: Question 22 Not yet answered Marked out of 1.00 Flag...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } 1 :How many field attributes are there in the TrafficLight class? 2 :Name a field attribute for this class. 3 :What is the name of the method that is an accessor? 4 :What is the name of the...
Look at the Data Below then answer/calculate the totals in the questions that follow. (Show Workings)...
Look at the Data Below then answer/calculate the totals in the questions that follow. (Show Workings) Entrepreneur's potential earnings as a salaried worker = $50,000 Annual lease on building = $22,000 Annual revenue from operations = $380,000 Payments to workers = $120,000 Utilities (electricity, water, disposal) costs = $8,000 Entrepreneur's potential economic profit from the next best entrepreneurial activity = $80,000 Entrepreneur's forgone interest on personal funds used to finance the business = $6,000 Answer these questions directly: a) Creamy...
What is the definition of a professional look? Provide a description of a professional look for...
What is the definition of a professional look? Provide a description of a professional look for a job interview. (like clothing and how you can achieve it). What to avoid?
Please make sure that you respond to both passages and follow closely to the three questions...
Please make sure that you respond to both passages and follow closely to the three questions in the prompts (bullets). Do NOT do additional research on the topic. The purpose of the exercise is to analyze the existing passages; not to demonstrate additional research skills. You do not need to provide a Works Cited or Reference page. Note: there is no page limit, however, you need to sufficiently answer the prompts in essay format. Holistic Assessment of Critical Thinking Essay...
in 2 paragraphs, respond to the following questions. 1. Why is it important to have an...
in 2 paragraphs, respond to the following questions. 1. Why is it important to have an independent judiciary (meaning one free from undue political influence)? 2. What are some ways that independence has been maintained by our courts?
For questions the next few questions, please respond to the following questions with regard to the...
For questions the next few questions, please respond to the following questions with regard to the following scenario. Becky would like to sell her 2003 Ford Ranger. She advertised the truck at $3,800 in the local newspaper. She hopes to sell it for $3,500. The lowest price she will accept before selling is $3,300. If she doesn’t receive an offer of at least $3,300, she will donate her truck to charity for a tax deduction. Alec would like to buy...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT