In: Computer Science
This is Java class working on the eclipse. I include some of the codes just so you know what needed.
create and work with interfaces
you'll create the DepartmentConstants interface presented. In
addition, you'll implement an interface named Displayable that's
similar to the Printable interface Create the interfaces
1- Import the project named ch12-ex1_DisplayableTest and review
the codes
package murach.test;
public interface Displayable {
String getDisplayText();
}
2 . Note that this code includes an interface named Displayable
that contains a single method named getDisplayText that returns a
String.
3. open the Displayable TestApp class. Then, note that it includes
a method named display that accepts a Displayable object as an
argument
package murach.test;
public class DisplayableTestApp {
public static void main(String args[]) {
System.out.println("Welcome to the Displayable Test
application\n");
Employee e = new
Employee(2, "Smith", "John");
// TODO: add code that
passes this object to the display method below
Product p = new
Product("java", "Murach's Java Programming", 57.50);
// TODO: add code that
passes this object to the display method below
System.out.println();
}
private static void display(Displayable d)
{
System.out.println(d.getDisplayText());
}
}
4 - Add an interface named Departmen that contains the three
constants: ADMIN, EDITORIAL, and MARKETING.
Implement the interfaces
5 open the Product class. Then, edit it so it implements the
Displayahle inler. face. To do that, add a getDisplay Text method
that returns a description of the product.
package murach.test;
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 getPriceFormatted() {
NumberFormat currency =
NumberFormat.getCurrencyInstance();
return
currency.format(price);
}
}
6- open the Employee class. Then, edit it so it implements the
DepartmentConstants and Displayable interfaces. To do that add a
get Display Text method that uses the constants in the
DepartmentConstants interface to include the department name and
the employee's name in the string that it retuns.
package murach.test;
public class Employee {
private int department;
private String firstName;
private String lastName;
public Employee(int department, String lastName,
String firstName) {
this.department =
department;
this.lastName =
lastName;
this.firstName =
firstName;
}
}
Use the classes that implement the
interfaces
7- open the Displayable TestApp class. Then, modify the variable
that stores the Employee object so it is of the Displayable
type.
8- Add code that passes the Displayable object to the static
display method that's coded at the end of this class.
9- Run the application to make sure that it displays the employee
information
10 -. Repeat the previous three steps for a Product object When
you're done. the console should look like this:
Welcome to the Displayable rest application
John Smith(Editorial)
Murach's Java Programming
Use a default method
11. in the Employee and Product classes, rename the getDisplay Text
methods to toString methods so that they override the toString
method of the object class. This should prevent the class from
compiling and display an error message that indicates that the
classes don't implement the getDisplay Text method.
12. In the Displayable interface, modify the getDisplayText method
so it's a default method. The code for this method should return
the String object that's returned by the tosting method. This
should allow the Employeee and Product classes to compile since
they can now use the default method.
13. Run the application to make sure it works as before
public interface Department {
public static final String admin = "ADMIN";
public static final String editorial =
"EDITORIAL";
public static final String marketing =
"MARKETING";
}
**********************************************************************************
public interface Displayable {
String getDisplayText();
}
********************************************************************************
public class Employee implements Displayable, Department {
private int department;
private String firstName;
private String lastName;
public Employee(int department, String lastName,
String firstName) {
this.department = department;
this.lastName = lastName;
this.firstName = firstName;
}
@Override
public String getDisplayText() {
String deptname = editorial;
return "employee name is :" +
firstName + lastName + "(" + deptname
+ ")";
}
}
**************************************************************************************************************
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 getPriceFormatted() {
NumberFormat currency =
NumberFormat.getCurrencyInstance();
return
currency.format(price);
}
@Override
public String getDisplayText() {
return description;
}
}
*******************************************************************************************************************
public class DisplayableTestApp {
public static void main(String args[]) {
System.out.println("Welcome to the
Displayable Test application\n");
Displayable e = new Employee(2,
"Smith", "John");
// TODO: add code that passes this
object to the display method below
Displayable p = new
Product("java", "Murach's Java Programming", 57.50);
// TODO: add code that passes this
object to the display method below
display(e);
display(p);
System.out.println();
}
private static void display(Displayable d) {
System.out.println(d.getDisplayText());
}
}