Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this excerise, you\'ll create & implment the DepartmentConstants interface pr

ID: 3881752 • Letter: I

Question

In this excerise, you'll create & implment the DepartmentConstants interface presented in this chapter. You'll also create & implement an interface named Displayable thats similar to the Printable interface.

Create the interfaces

1.Open the project named ch09_ex1_DisplayableTest in the ex_starts directory [Starting Code at Bottom]

2. Add an interface named DepartmentConstants that contains the three constants shown in the

Figure 9-4:

public interface DepartmentConstants { int ADMIN = 1; int EDITORIAL = 2; int MARKETING = 3; }

3. Add an interface named Displayable. This interface should contain a single method named getDisplayText that returns a String.

IMPLEMENT THE INTERFACES

4. Edit the Product class so it implements the Displayable interface. The getDisplayText method in this class should format a string that can be used to display the product information.

5. Edit the Employee class so it implements the DepartmentConstants and Displayable interfaces. The getDisplayText method in this class should work like the one in the Product class, and it should use the constants in the DepartmentConstants interface to include the department name in the return value.

USE THE CLASSES THAT IMPLEMENT THE INTERFACES

6. Open the DisplayableTestApp class and add code to it that creates an Employee object, assigns it to a Displayable variable, and displays the information in the Employee object at the console. To get the information for an employee, you'll need to use the getDisplayText method of the Displayable interface.

7. Run the application to make sure that it displays the employee information

8. Repeat steps 6 and 7 for a Product object.

USE AN INTERFACE AS A PARAMETER

9. Open DisplayableTestApp class and add a method with this signnature: private static String displayMultiple(Displayable d, int count)

Write the code for this method so it returns a string that contains the Displayable parameter the number of times specified by the int parameter.

10. Modifiy the code in the main method so it uses the displayMultiple method to display the employee information once and the product informaton twice

11. Run the application to make sure it works correctly.

Note 1: in question 4, getDisplayText() method returns a string, but it does not print out anything.

Note 2: in question 5, getDisplayText() method returns a string, but it does not print out anything.

Note 3: for question 6 regarding "add code to this class that creates an Employee object," when you create an Employee object, you don’t need to ask the user for input and get the values from keyboard by using the Scanner class. Instead, you can simply provide hard-code values such as the following. new Employee(2, "Smith", "John", 50000);

Note 4: in question 9, the method displayMultiple() returns a string, but it does not print out anything.

Starting Code:

DisplayableAppTest Class

public class DisplayableTestApp
{
public static void main(String args[])
{
System.out.println("Welcome to the Displayable Test application ");

// create an Employee object

// display the employee information

System.out.println();

// create a Product object

// display the product information
}
}

Product Class

import java.text.NumberFormat;

public class Product
{
private String code;
private String description;
private double price;

public Product()
{
this.code = "";
this.description = "";
this.price = 0;
}

public Product(String code, String description, double price)
{
this.code = code;
this.description = description;
this.price = price;
}

public void setCode(String code)
{
this.code = code;
}

public String getCode(){
return code;
}

public void setDescription(String description)
{
this.description = description;
}

public String getDescription()
{
return description;
}

public void setPrice(double price)
{
this.price = price;
}

public double getPrice()
{
return price;
}

public String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}

}

Employee Class

public class Employee
{
private int department;
private String firstName;
private String lastName;
private double salary;

public Employee(int department, String lastName, String firstName,
double salary)
{
this.department = department;
this.lastName = lastName;
this.firstName = firstName;
this.salary = salary;
}
}

Welcome to the Displayable Test application Name: Joey Aramaki Department: ADMIN Salary: $100000.00 Code: 3049 Description: Black iPhone 10 Price: $1146.00 Code: 3049 Description: Black iPhone 10 Price: $1146.00 BUILD SUCCESSFUL (total time: 1 second)

Explanation / Answer


Given below are the modified and new files.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
DepartmentConstants.java
==========================
public interface DepartmentConstants {
int ADMIN = 1;
int EDITORIAL = 2;
int MARKETING = 3;
}


Displayable.java
==================
public interface Displayable {
public String getDisplayText();
}


Employee.java
==============
import java.text.NumberFormat;
public class Employee implements DepartmentConstants, Displayable {
private int department;
private String firstName;
private String lastName;
private double salary;
public Employee(int department, String lastName, String firstName,
double salary)
{
this.department = department;
this.lastName = lastName;
this.firstName = firstName;
this.salary = salary;
}
@Override
public String getDisplayText() {
String str = "Name: " + firstName + " " + lastName + " ";
str += "Department: ";
if(department == ADMIN)
str += "ADMIN";
else if(department == EDITORIAL)
str += "EDITORIAL";
else if(department == MARKETING)
str += "MARKETING";
str += " ";
str += "Salary: " + NumberFormat.getCurrencyInstance().format(salary);
return str;
}
}


Product.java
===========
import java.text.NumberFormat;
public class Product implements Displayable
{
private String code;
private String description;
private double price;
public Product()
{
this.code = "";
this.description = "";
this.price = 0;
}
public Product(String code, String description, double price)
{
this.code = code;
this.description = description;
this.price = price;
}
public void setCode(String code)
{
this.code = code;
}
public String getCode(){
return code;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
public String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
@Override
public String getDisplayText() {
return "Code: " + code +" " +
"Description: " + description + " " +
"Price: " + getFormattedPrice() + " ";
}
}


DisplayableTestApp.java
======================
public class DisplayableTestApp
{
private static String displayMultiple(Displayable d, int count)
{
String s = "";
for(int i = 1; i <= count; i++)
s += d.getDisplayText() + " ";
return s;
}
public static void main(String args[])
{
System.out.println("Welcome to the Displayable Test application ");
// create an Employee object
Displayable emp = new Employee(DepartmentConstants.ADMIN, "Aramaki", "Joey", 100000);
// display the employee information
System.out.println(displayMultiple(emp, 1));
// create a Product object
Displayable prod = new Product("3049", "Black iPhone 10", 1146);
// display the product information
System.out.println(displayMultiple(prod, 2));
}
}
output
======
Welcome to the Displayable Test application
Name: Joey Aramaki
Department: ADMIN
Salary: £100,000.00
Code: 3049
Description: Black iPhone 10
Price: £1,146.00
Code: 3049
Description: Black iPhone 10
Price: £1,146.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote