Use this link to download the file : https://www.murach.com/shop/murach-s-java-p
ID: 3672426 • Letter: U
Question
Use this link to download the file : https://www.murach.com/shop/murach-s-java-programming-detail
Step by step solutions please. this is begining java so help me make it simple.
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 .
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.
Explanation / Answer
DisplayableTestApp.java
import java.util.Scanner;
public class DisplayableTestApp
{
private static String displayMultiple(Displayable d, int count){
int x = 0;
String s = "";
while (x < count){
String t = d.getDisplayText();
s = s + t + " ";
++x;
}
return s;
}
public static void main(String args[])
{
System.out.println("Welcome to the Displayable Test application ");
// create an Employee object
Displayable e1 = new Employee(1, "testName", "testLast", 45.9);
// display the employee information
//System.out.println(e1.getDisplayText() + " ");
// String s = DisplayableTestApp.displayMultiple(e1, 3);
System.out.println(DisplayableTestApp.displayMultiple(e1, 10));
// create a Product object
Displayable p2 = new Product("xyz", "this is a product", 45.555);
Displayable p3 = new Product("aaa", "this is a product", 45.555);
// display the product information
System.out.println(DisplayableTestApp.displayMultiple(p2, 2) + " " + p2.equals(p3));
// create another Employee object
Displayable e2 = new Employee(2, "secondTest", "secondTestTest", 54.657);
// compare the two Employee objects
System.out.println();
// create another Product object
// compare the two Product objects
}
}
DepartmentConstants.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public interface DepartmentConstants {
int ADMIN = 1;
int EDITORIAL = 2;
int MARKETING = 3;
}
Displayable.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public interface Displayable {
String getDisplayText();
}
Employee.java
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;
}
public String getDisplayText(){
String dept = "";
if (department == ADMIN){
dept = "Administration";
}
else if (department == EDITORIAL){
dept = "Editorial";
}
else if (department == MARKETING){
dept = "Marketing";
}
return firstName + " " + lastName +" "+
Double.toString(salary) + " " + dept;
}
}
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 + " " + description + " " + Double.toString(price);
}
@Override
public boolean equals(Object o){
if (o instanceof Product){
Product p1 = (Product) o;
if (getDisplayText().equals(p1.getDisplayText())){
return true;
}
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.