codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  tester class and then some.  postage at 17:30 on Wednesday, April 26, 2006
 

Hi everyone, I am new on this forum, I've been reading it for a while, and decided to try my luck asking a few questions. I've got this large program I'm supposed to put together, but I'm running into a few problems. Here is the 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.
// ----- Written by: Ken Zemaitis.

public class Project03 {
public static void main(String[] args) {

//-----------------------------------------------------------------------
// code for testing the class hierarchy.
//-----------------------------------------------------------------------


}
}



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

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.
//JH

interface Payable {
public double pay();
}
//---------------------------------------------------------------------------
// here is where my code to complete the class hierarchy needs to go.
//---------------------------------------------------------------------------



class Customer extends Person {
public double balance; //Balance variable defined.
public Customer(String pName, String pAddress, double pBalance) {
super(pName, pAddress);
setBalance(pBalance);
}

public double setBalance(double pBalance) {
return pBalance;
}

public double getBalance() {
return balance;
}


public String toString() {
return getClass().getName() + ": " + getName() + ", " + getAddress() + ", " + Utility.moneyFormat(balance);
}

}

class Employee extends Person {
public boolean payable;
public double salary;
public Employee(String pName, String pAddress, double pSalary) {
super(pName, pAddress);
setSalary(pSalary);
}

public double setSalary(double pSalary) {
return pSalary;
}

public double getSalary() {
return salary;
}


public String toString() {
return getClass().getName() + ": " + getName() + ", " + getAddress() + ", " + Utility.moneyFormat(salary);
}



}

class SalesRep extends Employee {
public double sales;
public double commission;
public SalesRep(String pName, String pAddress, double pSalary, double pSales, double pCommission) {
super(pName, pAddress, pSalary);
setSales(pSales);
setCommission(pCommission);
}

public double setSales(double pSales) {
return pSales;
}

public double setCommission(double pCommission) {
return pCommission;
}

public double getSales() {
return sales;
}

public double getCommission() {
return commission;
}

public String toString() {
return getClass().getName() + ": " + getName() + ", " + getAddress() + ", " + Utility.moneyFormat(salary) + ", " + Utility.moneyFormat(sales) + ", " + Utility.moneyFormat(commission);
}

}

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

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



I am having trouble completing the "test" section of my code, as well as completing the hierarchy all together. The way I have the program coded as of right now compiles, but produces no results, which is why I need help with the "test" code. There are a few other things I might be able to figure out on my own after writing the test code, but if not I will just post again.

These are some guidelines I was provided with:

Completing the class hierarchy...

A guide in defining the classes needed to complete the class hierarchy.

1. 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).
2. 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.
3. 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 Project03 class...

Complete the test code as follows:
1. Declare an array of Person object references capable of holding 10 elements.
2. Create an Employee object with the employee's name, address, and salary and add the object's reference to the array.
3. Create a Customer object with the customer's name, address, and balance and add the object's reference to the array.
4. 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.
5. 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).

Output 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


Any help or suggestions would be appreciated, and thanks a lot for even taking the time to look at this. I know it's a lot.

  Re: tester class and then some.  crwood at 19:24 on Wednesday, April 26, 2006
 

Some suggestions about your program:
1 — This method must return a concatenated string consisting of the string returned by the toString method of the superclass and the customer's balance
The way to get the superclass toString is by using the "super" key word. So you could try

// possible Customer "toString" implementation
return super.toString() + ", Balance: " + Utility.moneyFormat(getBalance());

2 — An Employee is a Person who is Payable
This is telling you that the Employee class must implement the Payable interface and, by contract, must then implement every method defined by the interface — here, only one method.
3 — a "set" method usually does not return anything to the caller so its return type is "void." It has an argument (a local variable) which is used to set the value of the instance variable (in class scope) that it is "setting." In java

public void setBalance(double pBalance) {
balance = pBalance;
}


public static void main(String[] args) {
Person[] people = new Person[10];
people[0] = new Employee("Barb Webb", "123 Maple", 24000.00);
people[1] = new Customer("Todd Kent", "4321 Oak", 150.00);
people[2] = new SalesRep("Ted Na", "654 Elm", 18000.00, 9000.00, 6.0);
for(int j = 0; j < people.length; j++)
if(people[j] != null)
System.out.println(people[j]);
}


  Re: tester class and then some.  postage at 18:04 on Thursday, April 27, 2006
 

Thank you very much. This information has helped me greatly.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: do static member are inherited
•  Databaser in java
•  convert ascii grided data file in to binary data file and vice versa
•  Re: Help with Using DataReader with StoredProcedure
•  Perl Script Output (w3c validator)
•  Re: HashMap question
•  focus management?
•  Looping Issue...Please help!
•  regarding hibernate


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006