Question

In: Computer Science

This is Java class working on the eclipse. I include some of the codes just so...

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

Solutions

Expert Solution

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());
   }

}


Related Solutions

use eclipse give me java codes Task III: Write a classCarInsurancePolicy The CarInsurancePolicy class will...
use eclipse give me java codes Task III: Write a class CarInsurancePolicy The CarInsurancePolicy class will describe an insurance policy for a car. 1. The data members should include all the data members of an Insurance Policy, but also the driver’s license number of the customer, whether or not the driver is considered a “good” driver (as defined by state law) , and the car being insured (this should be a reference to a Car object -- write a separate...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Hello, I need this done with Java and ran in Eclipse if possible. A university wants...
Hello, I need this done with Java and ran in Eclipse if possible. A university wants to demonstrate its political correctness by applying the Supreme Court’s “Separate but equal is inherently unequal” doctrine to gender as well as race. As such, the university has decided that both genders will use the same bathroom facilities. However, in order to preserve some tradition, it decrees that when a woman is in the bathroom, only other women may enter, and when a man...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT