GIDForums

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Apr-2006, 11:19
postage postage is offline
New Member
 
Join Date: Apr 2006
Location: chicago.
Posts: 1
postage is on a distinguished road

a tester class and then some.


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:
JAVA 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.
  #2  
Old 06-May-2006, 15:48
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 872
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: a tester class and then some.


I must have forgot to return to this thread!...

How is this project coming along? Still need help?
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.