• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help Please

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this project, you must complete the definition of a class hierarchy consisting of Person, Customer, Employee, and SalesRep classes. Then, you must complete the coding of a Project02 class that tests the hierarchy by loading an array of Person references with a mix of Customer, Employee, and SalesRep objects and uses polymorphism to display what the array contains.
Start with the following source statements. The program compiles but has two major sections of missing code.
// Make it possible to reference required packaged code.

import java.io.*;
import java.text.*;
import java.util.*;

// This class defines an application that uses polymorphism to process an
// array of Employee, SalesRep, and Customer objects.


public class Project02 {
public static void main(String[] args) {
Person[] persons = new Person[10];
persons[0] = new Employee("Barb Webb", "123 Maple", 24000);
persons[1] = new SalesRep("Ted Na", "654 Elm", 18000, 9000, 6);
persons[2] = new Customer("Todd Kent", "4321 Oak", 150);
for (int i = 0; i < persons.length; i++) {
if (persons[i] != null) {
System.out.println(persons[i].toString());

}
}
}
} //-----------------------------------------------------------------------
// YOUR CODE TO TEST THE CLASS HIERARCHY GOES HERE
//-----------------------------------------------------------------------

// This abstract class encapsulates the data and processing of a person.
//

abstract class Person {
private String name;
private String address;

// This constructor receives the person's name and address as parameters.

public Person(String pName, String pAddress) {
setName(pName);
setAddress(pAddress);
}

// This method can be called to set the person's name.

public void setName(String pName) {
name = pName;
}

// This method can be called to set the person's address.

public void setAddress(String pAddress) {
address = pAddress;
}

// This method can be called to get the person's name.

public String getName() {
return name;
}

// This method can be called to get the person's address.

public String getAddress() {
return address;
}

// This method can be called to get the object's runtime class and the
// person's name and address as a String.

public String toString() {
return getClass().getName() + ": " + name + ", " + address;
}
}

// This interface forces implementing classes to define a pay method.
//

interface Payable {
public double pay();
}

class Customer extends Person { ////MY CODE CLASSES
private double balance;
public Customer(String pName, String pAddress, double iBalance) {
super(pName);
super(pAddress);
setBalance(iBalance);
}
public boolean setBalance(double nBalance) {
if (nBalance >= 0) {
balance = nBalance;
return true;
}
else
return false;
}
public double getBalance() {
return balance;
}
public String toString() {
return this.getName() + this.getAddress() + this.getBalance();
}
}

class Employee extends Person implements Payable {
private double salary;
public Employee(String pName, String pAddress, double iSalary) {
super(pName);
super(pAddress);
setSalary(iSalary);
}
public boolean setSalary(double nSalary) {
if (nSalary > 0) {
nSalary = salary;
return true;
}
else
return false;
}
public double getSalary() {
return salary;
}
public String toString() {
return this.getName() + this.getAddress() + this.getSalary;
}
}

class SalesRep extends Person implements Payable {
private double salary;
private double sales;
private double commission;
public SalesRep(String pName, String pAddress, double iSalary, double iSales, double iCommission) {
super(pName);
super(pAddress);
setSalary(iSalary);
setSales(iSales);
setCommission(iCommission);
}
public boolean setSalary(double nSalary) {
if (nSalary > 0) {
nSalary = salary;
return true;
}
else
return false;
}
public double getSalary() {
return salary;
}
public boolean setSales(double nSales) {
if (nSales > 0) {
nSales = sales;
return true;
}
else
return false;
}
public double getSales() {
return sales;
}
public boolean setCommission(double nCommission) {
if (nCommission > 0) {
nCommission = commission;
return true;
}
else
return false;
}
public double getCommission() {
return commission;
}
public String toString() {
return this.getName() + this.getAddress() + this.getSalary() + this.getSales() + this.getCommission;
}
}
//---------------------------------------------------------------------------
// YOUR CODE TO COMPLETE THE CLASS HIERARCHY CODE GOES HERE
//---------------------------------------------------------------------------

// This class contains custom methods that support application processing.


class Utility {

// This method can be called to prompt the user to press the ENTER key to
// continue.

public static void pressEnter() {
System.out.println("Press ENTER to continue...");
try {
System.in.read();
System.in.skip(System.in.available());
}
catch (Exception err) {}
}

// This method can be called to convert a double value to a properly
// formatted currency string. For example: (1234.56) returns "$1,234.56"

public static String moneyFormat(double pAmount) {
NumberFormat nf = NumberFormat.getCurrencyInstance();
return nf.format(pAmount);
}

// This method can be called to convert a double value to a formatted
// percentage string having the number of decimal places indicated by the
// second parameter. For example: (.01234, 2) returns "1.23%"

public static String percentFormat(double pValue, int pDecimalPlaces) {
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMaximumFractionDigits(pDecimalPlaces);
return nf.format(pValue);
}
}

Completing the class hierarchy
Use the following information as a guide in defining the classes needed to complete the class hierarchy.
• A Customer is a Person who has a balance (double). Provide a single constructor that will receive the customer's name, address, and balance. The name and address must be passed through to the superclass constructor while the balance can be passed to the "set" method that will store it. Be sure to code the necessary "set" and "get" methods for balance but do NOT be concerned with editing the data. Provide a toString method that overrides the one inherited from the superclass. This method must return a concatenated string consisting of the string returned by the toString method of the superclass and the customer's balance in currency format (using the moneyFormat method of the Utility class).
• An Employee is a Person who is Payable and has a salary (double). Provide a single constructor that will receive the employee's name, address, and salary. The name and address must be passed through to the superclass constructor while the salary can be passed to the "set" method that will store it. Be sure to code the necessary "set" and "get" methods for salary but do NOT be concerned with editing the data. Provide a toString method that overrides the one inherited from the superclass. This method must return a concatenated string consisting of the string returned by the toString method of the superclass and the employee's salary in currency format (using the moneyFormat method of the Utility class). Also provide a pay method that will return the value of the employee's salary divided by 24 as a double.
• A SalesRep is an Employee who has sales and a commission rate (both double). Provide a single constructor that will receive the sales rep's name, address, salary, sales, and commission rate. The name, address, and salary must be passed through to the superclass constructor while the sales and commission rate can be passed to the "set" methods that will store them. Be sure to code the necessary "set" and "get" methods for sales and commission rate but do NOT be concerned with editing the data. Provide a toString method that overrides the one inherited from the superclass. This method must return a concatenated string consisting of the string returned by the toString method of the superclass, the sales rep's sales (in currency format using the moneyFormat method of the Utility class), and the sales rep's commission rate (in percent format using the percentFormat method of the Utility class). Also provide a pay method that overrides the one inherited from the superclass. The method must return the sum of the value returned by the superclass pay method and the sales rep's commission (their sales times their commission rate).

Completing the Project02 class
Complete the test code as follows:
• Declare an array of Person object references capable of holding 10 elements.
• Create an Employee object with the employee's name, address, and salary and add the object's reference to the array.
• Create a Customer object with the customer's name, address, and balance and add the object's reference to the array.
• Create a SalesRep object with the sales rep's name, address, salary, sales, and commission rate and add the object's reference to the array.
• Code a for loop to process the array. For each non-null element, use the object's toString method to display information about the Customer, Employee, or SalesRep. Then, if the object is Payable, use the object's pay method to display their pay in currency format (using the moneyFormat method of the Utility class).
If you do this correctly, the output of your program should look SOMETHING like this:
Employee: Barb Webb, 123 Maple, Salary: $24,000.00
Pay is: $1,000.00
Customer: Todd Kent, 4321 Oak, Balance: $150.00
SalesRep: Ted Na, 654 Elm, Salary: $18,000.00, Sales: $9,000.00, Comm Rate: 6%
Pay is: $1,290.00
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So where's your code? What problems are you having with it?

Also please read: UseCodeTags, DoYourOwnHomework and UseAMeaningfulSubjectLine. Thanks.
 
Steven Frontiers
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Resolved
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you care to tell us the solution? Others may benefit from it in the future.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic