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...
Fix the following codes in JAVA so they can work : public class GeometricObject { private...
Fix the following codes in JAVA so they can work : public class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; public GeometricObject1() { dateCreated = new java.util.Date(); } public GeometricObject1(String Color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return filled; } public void setFilled(boolean filled) { this.filled...
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...
Code in java(eclipse) and please use simple codes Assignment Bigfoot has reportedly been seen in Southeast...
Code in java(eclipse) and please use simple codes Assignment Bigfoot has reportedly been seen in Southeast Texas forests. We have a drone searching for Bigfoot that can take pictures from the air. During a 24-hour period the drone can fly over the forest 3 times. On each flight we estimate Bigfoot will be in range of the camera on the drone for 2 minutes. The camera can take 10 photos per minute. We estimate there is a 30% chance Bigfoot...
This code has to be in java (I code in eclipse). Also these instructions have to...
This code has to be in java (I code in eclipse). Also these instructions have to be followed exactly because if not my output won't match the expected out ( this will be uploaded on zybooks). Write a program that asks the user for their age in days. The program will compute the person's age in years (you can assume that all years have 365 days) and then prints one of the following messages: If the user is 1 year...
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...
OOPDA in java project. in eclipse Instructions: Create a class called Person with the properties listed:...
OOPDA in java project. in eclipse Instructions: Create a class called Person with the properties listed: int id, String firstName, String middleName, String lastName, String email, String ssn, int age. The Person class should have getters and setters for all properties other than id. (You may use Eclipse to help you auto-generate these.) Person class should also have a getter for id Create a no-arg constructor for Person In addition to these getters and setters, Person should have the following...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT