Question

In: Computer Science

As in previous labs, please write the Java code for each of the following exercises in...

As in previous labs, please write the Java code for each of the following exercises in BlueJ. For each exercise, write a comment before the code stating the exercise number.

Make sure you are using the appropriate indentation and styling conventions

Exercise 1 (15 Points)

  • In BlueJ, create a new project called Lab6
  • In the project create a class called Company
  • Add instance data (fields) for the company name (a String) and the sales for the current period (a double)
  • Make sure you declare the fields with private visibility.
  • Add a constructor to the Company class that accepts a parameter to set the company name and sets the sales to 0.0.

Exercise 2 (20 Points)

  • Create a second class in the same project called CompanyDemo.
  • Add a main method to this class. This will serve as the driver of this program.
  • In main, create two Company reference variables named c1 and c2.
  • Assign to those variables two Company objects, one with the name Lexcorp and the other with the name Daily Planet.
  • Now print the two objects using separate println statements.
  • Run the main method – you’ll see the default output when printing an object. The value returned by the default version of toString is the class name followed by a hash string created based on the memory location of this object. The displayed number is in hexadecimal – not particularly helpful.

Exercise 3 (20 Points)

  • To improve the output, add a toString method to the Company class that returns a string of the form

Lexcorp made $2517.85 this period.

  • To do the previous step, you need to use the String.format method. Please refer to the String API (Links to an external site.). The String.format method is the same like the prinf method. The only difference is that, the String.format method doesn't print anything to the screen. It just returns the formatted String object. Accordingly, it can be used in the toString method to return the formatted string.
  • Run the main method again to see the new output, though sales for both companies will still be 0.0

Exercise 4 (10 Points)

  • Add a setter (mutator) and getter (accessor) methods for the company name to the Company class.
  • Add println statements (for both companies) to the main method to print just the company name
  • Call the setter on one object to change the name from Lexcorp to Lexcorp, Inc.
  • Then print the company name again.

Exercise 5 (20 Points)

  • Add a method in Company called updateSales that accepts an integer representing the number of units sold and a double representing the unit price.
  • In that method, multiply the units by the unit price and add that value to the sales.
  • In main, call updateSales for c1, passing in 12 units at $24.53 per unit.
  • Then call updateSales for c2, passing in 5 units at $14.17 per unit.
  • Then print both company objects again.

Exercise 6 (10 Points)

  • Add a second constructor to Company with an additional parameter that gives a company an initial sales figure.
  • In main, change the creation of Daily Planet so that it has initial sales of $255.18.
  • Rerun the main method to verify the change.

Exercise 7 (5 Points)

  • Add a resetSales method in Company that resets the sales value to 0.0
  • At the end of main, call resetSales just for Lexcorp and print both companies one more time.

Solutions

Expert Solution

Exercise 1:

class Company
{
   private String companyName;
   private double sales;
  
   public Company(String companyName)
   {
       this.companyName = companyName;
       this.sales = 0.0;
   }
  
  
}

Exercise 2:

class CompanyDemo
{
   public static void main (String[] args)
   {
       Company c1 = new Company("Lexcorp");
      
       Company c2 = new Company("Daily Planet");
      
       System.out.println(c1);
       System.out.println(c2);
      
   }
}

Output:

Company@5caf905d
Company@27716f4

Exercise 3:

public String toString()
   {
       return String.format("%s made $%.2f this period.", companyName, sales);
   }

output:

Lexcorp  made $0.00 this period.
Daily Planet   made $0.00 this period.

Exercise 4:

public void setCompanyName(String companyName)
   {
       this.companyName = companyName;
   }
   public String getCompanyName()
   {
       return companyName;
   }
  

System.out.println(c1.getCompanyName());
       System.out.println(c2.getCompanyName());
      
       c1.setCompanyName("Lexcorp, Inc.");
       System.out.println(c1.getCompanyName());
      

Output:

Lexcorp
Daily Planet
Lexcorp, Inc.

Exercise 5:

public void updateSales(int units, double price)
   {
       sales = units*price;
   }

   c1.updateSales(12,24.53);
       c2.updateSales(5,14.17);
      
       System.out.println(c1);
       System.out.println(c2);

Output:

Lexcorp, Inc.  made $294.36 this period.
Daily Planet  made $70.85 this period.

Exercise 6:

public Company(String companyName, double sales)
   {
       this.companyName = companyName;
       this.sales = sales;
   }

Company c2 = new Company("Daily Planet",255.18);

Output:

Daily Planet  made $255.18 this period.

Exercise 7:

public void resetSales()
   {
       sales = 0.0;
   }

   c1.resetSales();
      
       System.out.println(c1);
       System.out.println(c2);

Output:

Lexcorp, Inc.  made $0.00 this period.
Daily Planet  made $70.85 this period.

Complete code:

class Company
{
   private String companyName;
   private double sales;
  
   public Company(String companyName)
   {
       this.companyName = companyName;
       this.sales = 0.0;
   }
   public Company(String companyName, double sales)
   {
       this.companyName = companyName;
       this.sales = sales;
   }
  
   public String toString()
   {
       return String.format("%s made $%.2f this period.", companyName, sales);
   }
  
   public void setCompanyName(String companyName)
   {
       this.companyName = companyName;
   }
   public String getCompanyName()
   {
       return companyName;
   }
  
   public void updateSales(int units, double price)
   {
       sales = units*price;
   }
   public void resetSales()
   {
       sales = 0.0;
   }
  
}
class CompanyDemo
{
   public static void main (String[] args)
   {
       Company c1 = new Company("Lexcorp");
      
       Company c2 = new Company("Daily Planet",255.18);
      
       System.out.println(c1);
       System.out.println(c2);
      
       System.out.println(c1.getCompanyName());
       System.out.println(c2.getCompanyName());
      
       c1.setCompanyName("Lexcorp, Inc.");
       System.out.println(c1.getCompanyName());
      
       c1.updateSales(12,24.53);
       c2.updateSales(5,14.17);
      
       System.out.println(c1);
       System.out.println(c2);
      
      
       c1.resetSales();
      
       System.out.println(c1);
       System.out.println(c2);
      
   }
}

Output:

Lexcorp  made $0.00 this period.
Daily Planet  made $255.18 this period.
Lexcorp
Daily Planet
Lexcorp, Inc.
Lexcorp, Inc.  made $294.36 this period.
Daily Planet  made $70.85 this period.
Lexcorp, Inc.  made $0.00 this period.
Daily Planet  made $70.85 this period.

Do ask if any doubt. Please up-vote.


Related Solutions

Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
IN C++ As in previous labs, RESIST THE URGE TO CODE! Determine what data will be...
IN C++ As in previous labs, RESIST THE URGE TO CODE! Determine what data will be necessary in order to complete this task. Then design the steps necessary to process that data. Do you need if statements? Do you need loops? Do you need if statements within your loops? Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s...
Write Java code for each of the following problem a. Two players, a and b, try...
Write Java code for each of the following problem a. Two players, a and b, try to guess the cost of an item. Whoever gets closest to the price without going over is the winner. Return the value of the best bid. If both players guessed too high, return -1. example: closestGuess(97, 91, 100) → 97 closestGuess(3, 51, 50) → 3 closestGuess(12, 11, 10) → -1 b. Given a non-empty string, return true if at least half of the characters...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT