In: Computer Science
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)
Exercise 2 (20 Points)
Exercise 3 (20 Points)
Lexcorp made $2517.85 this period.
Exercise 4 (10 Points)
Exercise 5 (20 Points)
Exercise 6 (10 Points)
Exercise 7 (5 Points)
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.