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...
Consider the following class definition: class circle                                  &nb
Consider the following class definition: class circle                                                       class cylinder: public circle {                                                                      { public:                                                             public: void print() const;                                           void print() const; void setRadius(double);                                  void setHeight(double); void setCenter(double, double);                     double getHeight(); void getCenter(double&, double&);               double volume(); double getRadius();                                        double area(); double area(); circle();                                     cylinder(); circle(double, double, double);                       cylinder(double, double, double, double); private:                                                private: double xCoordinate;                                       double height; double yCoordinate;                         } double radius; } Suppose that you have the declaration: cylinder newCylinder;...
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...
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT