As in previous labs, please write the Java code for eachof the following exercises in BlueJ. For each exercise, write acomment before the code stating the exercise number.
Make sure you are using the appropriate indentation andstyling conventions
Exercise 1 (15 Points)
- In BlueJ, create a new project calledLab6
- In the project create a class calledCompany
- Add instance data (fields) for the company name (aString) and the sales for the current period (adouble)
- Make sure you declare the fields with private visibility.
- Add a constructor to the Company class thataccepts a parameter to set the company name and sets the sales to0.0.
Exercise 2 (20 Points)
- Create a second class in the same project calledCompanyDemo.
- Add a main method to this class. This willserve as the driver of this program.
- In main, create two Companyreference variables named c1 andc2.
- Assign to those variables two Company objects,one with the name Lexcorp and the other with the name DailyPlanet.
- Now print the two objects using separateprintln statements.
- Run the main method – you’ll see the defaultoutput when printing an object. The value returned by the defaultversion of toString is the class name followed bya hash string created based on the memory location of this object.The displayed number is in hexadecimal – not particularlyhelpful.
Exercise 3 (20 Points)
- To improve the output, add a toString methodto the Company class that returns a string of theform
Lexcorp made $2517.85 this period.
- To do the previous step, you need to use theString.format method. Please refer to the StringAPI (Links to an external site.). TheString.format method is the same like theprinf method. The only difference is that, theString.format method doesn't print anything to thescreen. It just returns the formatted String object. Accordingly,it can be used in the toString method to returnthe formatted string.
- Run the main method again to see the newoutput, though sales for both companies will still be 0.0
Exercise 4 (10 Points)
- Add a setter (mutator) and getter (accessor) methods for thecompany name to the Company class.
- Add println statements (for both companies) tothe main method to print just the companyname
- Call the setter on one object to change the name from Lexcorpto Lexcorp, Inc.
- Then print the company name again.
Exercise 5 (20 Points)
- Add a method in Company calledupdateSales that accepts an integer representingthe number of units sold and a double representing the unitprice.
- In that method, multiply the units by the unit price and addthat value to the sales.
- In main, call updateSales forc1, passing in 12 units at $24.53 per unit.
- Then call updateSales for c2, passing in 5units at $14.17 per unit.
- Then print both company objects again.
Exercise 6 (10 Points)
- Add a second constructor to Company with anadditional parameter that gives a company an initial salesfigure.
- In main, change the creation of Daily Planetso that it has initial sales of $255.18.
- Rerun the main method to verify thechange.
Exercise 7 (5 Points)
- Add a resetSales method inCompany that resets the sales value to 0.0
- At the end of main, call resetSales just for Lexcorpand print both companies one more time.