Calling a method in a superclass

Not sure how to get it to work

Page 1 of 1

3 Replies - 6421 Views - Last Post: 21 October 2009 - 07:34 PM Rate Topic: -----

#1 dragonseyeangie  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 20-October 08

Calling a method in a superclass

Post icon  Posted 21 October 2009 - 05:04 PM

Hello,

I'm trying to get this assignment done. I currently have a superclass named Employee, a subclass, named ProductionWorker, and a class named Chapter11Program1 to demonstrate them.

the following is the method i'm having trouble with

private boolean isValidEmpNum(String e)
	{ // Begin private boolean isValidEmpNum(String e)
	
		boolean isValid = true; 
		int i = 0;
		
		// Test the length of the string
		if (e.length() != 5)
			isValid = false;
		
		// Test the first 3 characters for digits
		while (isValid && i < 3)
		{
			if (!Character.isDigit(e.charAt(i)))
				isValid = false;
			i++;
		}
		
		// Test 4th character for hyphen
		while (isValid && i < 4)
		{
			if (e.charAt(i) != '-')
				isValid = false;
			i++;
		}
		
		// Test last character for letter
		while (isValid && i < 5)
		{
			if (!Character.isLetter(e.charAt(i)))
				isValid = false;
			i++;
		}
		
		// Test if last character is an A through M
		while (isValid && i < 6)
		{
			if (e.charAt(4) <= 'A' || e.charAt(4) >= 'M')
				isValid = false;
			i++;
		}
		
		return isValid;
		
	} // End private boolean isValidEmpNum(String e)



I'm not sure how to get this to execute and show an error message, i've tried different things but nothing

here's the code for Employee class

/**
	The Employee class keeps the following information in fields:
	
		- Employee name
		- Employee number in the format XXX-L, 
		  where each X is a digit within the range
		  0-9 and L is a letter within the range A-M.
		- Hire date
		
*/
import javax.swing.JOptionPane;

public class Employee
{ // Begin public class Employee
	
	private String employeeName;
	private String employeeNumber;
	private String hireDate;
	
	/**
		Constructor
		@param name The Employee's name
		@param num The Employee's ID number
		@param date The Employee's hire date
	*/
	
	public Employee(String name, String num, String date)
	{ // Begin public Employee(String name, String num, String date)
	
			// Set the employeeName, employeeNumber, and hireDate fields
			employeeName = name;
			employeeNumber = num;
			hireDate = date;
			
	} // End public Employee(String name, String num, String date)
	
	/**
		Constructor
	*/
	
	public Employee()
	{ // Begin public Employee()
	
		employeeName = "";
		employeeNumber = "";
		hireDate = "";
		
		
	} // End public Employee()
	
	/**
		The setName method stores a value in the 
		employeeName field.
		@param name The employee's name
	*/
	
	public void setName(String name)
	{ // public void setName(String name)
	
		employeeName = name;
		
	} // public void setName(String name)
	
	/**
		The setEmployeeNumber method stores a value
		in the employeeNumber field.
		@param num The employee's ID number
	*/
	
	public void setEmployeeNumber(String num)
	{ // Begin public void setEmployeeNumber(String num)
	
	
		employeeNumber = num;
		
	} // End public void setEmployeeNumber(String num)
	
	/**
		The setHireDate method stores a value
		in the hireDate field.
		@param date The employee's hire date
	*/
	
	public void setHireDate(String date)
	{ // Begin public void setHireDate(String date)

		hireDate = date;
		
	} // End public void setHireDate(String date)
	
	/**
		The getName method returns the value
		of employeeName
		@return The employee's name
	*/
	
	public String getName()
	{ // Begin public String getName()
	
		return employeeName;
		
	} // End public String getName()
	
	/**
		The getEmployeeNumber method returns
		the value of employeeNumber
		@return The employee's ID number
	*/
	
	public String getEmployeeNumber()
	{ // Begin public String getEmployeeNumber()
	
		return employeeNumber;
		
	} // End public String getEmployeeNumber()
	
	/**
		The getHireDate method returns the value
		of hireDate
		@return The employee's hire date
	*/
	
	public String getHireDate()
	{ // Begin public String getHireDate()
		
		return hireDate;
	
	} // Begin public String getHireDate()
	
	/**
		The isValidEmpNum checks whether the employee
		number is in the proper format
		@param e The value entered for the employees ID number
	*/
	
	private boolean isValidEmpNum(String e)
	{ // Begin private boolean isValidEmpNum(String e)
	
		boolean isValid = true; 
		int i = 0;
		
		// Test the length of the string
		if (e.length() != 5)
			isValid = false;
		
		// Test the first 3 characters for digits
		while (isValid && i < 3)
		{
			if (!Character.isDigit(e.charAt(i)))
				isValid = false;
			i++;
		}
		
		// Test 4th character for hyphen
		while (isValid && i < 4)
		{
			if (e.charAt(i) != '-')
				isValid = false;
			i++;
		}
		
		// Test last character for letter
		while (isValid && i < 5)
		{
			if (!Character.isLetter(e.charAt(i)))
				isValid = false;
			i++;
		}
		
		// Test if last character is an A through M
		while (isValid && i < 6)
		{
			if (e.charAt(4) <= 'A' || e.charAt(4) >= 'M')
				isValid = false;
			i++;
		}
		
		return isValid;
		
	} // End private boolean isValidEmpNum(String e)
	
	/**
		The toString method returns a string to 
		be displayed
		@return
	*/
	
	public String toString()
	{ // Begin public String toString()
	
		return "Employee Name: " + employeeName + "\nEmployee ID Number: " +
				employeeNumber + "\nHire Date: " + hireDate;
				
	} // End public String toString()

} // End public class Employee




here's the code for the ProductionWorker class

/**
	The ProductionWorker clas extends the Employee class. 
	It also has the fields to hold the following information:
	
		- Shift (an integer)
		- Hourly pay rate (a double)
		
	The workday is divided into two shifts: day and night. The
	shift field will be an enteger value representing the shift
	that the employee works. The day shift is shift 1 and the 
	night shift is shift 2. 
*/

import java.text.DecimalFormat;

public class ProductionWorker extends Employee
{ // Begin public class ProductionWorker

	private int shift;
	private double payRate;
	final int DAY_SHIFT = 1;
	final int NIGHT_SHIFT = 2;
	DecimalFormat formatter = new DecimalFormat("##0.00");
	/**
		Constructor
		@param name The employee's name
		@param num The employee's ID number
		@param date The employee's hire date
		@param sft The employee's work shift
		@param rate The employee's pay rate
	*/
	
	public ProductionWorker(String name, String num, String date, int sft, double rate)
	{ // Begin public ProductionWorker(String name, String num, String date, int sft, double rate)
	
		// Call the superclass constructor
		super(name, num, date);
		
		// Set the shift and pay rate
		shift = sft;
		payRate = rate;
		
	} // End public ProductionWorker(String name, String num, String date, int sft, double rate)
	
	/**
		Constructor
	*/
	
	public ProductionWorker()
	{ // Begin public ProductionWorker()
	
		payRate = 0.0;
	
	} // End public ProductionWorker()
	
	/**
		The setShift method stores a value
		in the shift field.
		@param s The employee's work shift
	*/
	
	public void setShift(int s)
	{ // Begin public void setShift(int s)
	
		shift = s;
		
	} // End public void setShift(int s)
	
	/**
		The setPayRate method stores a value
		in the payRate field
		@param p The employee's pay rate
	*/
	
	public void setPayRate(double p)
	{ // Begin public void setPayRate(double p)
	
		payRate = p;
		
	} // Begin public void setPayRate(double p)
	
	/**
		The getShift returns the value in the
		shift field
		@return The employee's work shift
	*/
	
	public int getShift()
	{ // Begin public int getShift()
		
		return shift;
		
	} // End public int getShift()
	
	/**
		The getPayRate returns the value in 
		the payRate field
		@return The employee's pay rate
	*/
	
	public double getPayRate()
	{ // Begin public double getPayRate()
	
		return payRate;
	
	} // End public double getPayRate()
	
	/**
		The toString method returns a string
		to be displayed
		@return
	*/
	
	public String toString()
	{ // Begin public String toString()
	
		return "Employee Name: " + getName() + "\nEmployee ID Number: " +
				getEmployeeNumber() + "\nHire Date: " + getHireDate() +
				"\nShift: " + shift + "\nPay Rate: " + formatter.format(payRate);
		
	} // End public String toString()
	
} // End public class ProductionWorker extends Employee



and here's the code for the Chapter11Program1 class

/**
	The Chapter11Program1 class demonstrates the
	Employee superclass and ProductionWorker subclass
*/

import javax.swing.JOptionPane;	// For dialog boxes

public class Chapter11Program1
{ // Begin public class Chapter11Program1

	public static void main(String[] args)
	{ // Begin public static void main(String[] args)
	
		String empName;
		String empNum;
		String hireDate;
		String input;		// For input
		int shift;			// the employee's work shift
		double payRate;		// the employee's payRate
		
		// Get the name of the employee
		empName = JOptionPane.showInputDialog("Enter the name of the employee:");
		
		// Get the employee's ID number
		empNum = JOptionPane.showInputDialog("Enter the employee's ID number\nin the following format - \n\t123-A");
		
		// Get the employee's hire date
		hireDate = JOptionPane.showInputDialog("Enter the employee's hire date:");
		
		// Get the employee's work shift
		input = JOptionPane.showInputDialog("Please enter the work shift for the employee:\n" +
												"\tEnter 1 for the day shift\n\tEnter 2 for the night shift");
		
		// Convert the input to an int
		shift = Integer.parseInt(input);
		
		if (shift < 1 || shift > 2)
		{
			JOptionPane.showMessageDialog(null, "Invalid work shift");
		}
		
		// Get the employee's pay rate
		input = JOptionPane.showInputDialog("Please enter the pay rate for the employee:");
		
		// Convert the input to a double
		payRate = Double.parseDouble(input);
		
		// Create an a Production worker object
		ProductionWorker worker = new ProductionWorker(empName, empNum, hireDate, shift, payRate);
		
		// Display the data 
		JOptionPane.showMessageDialog(null, worker);
	
		System.exit(0);
	
	} // End public static void main(String[] args)
	
} // End public class Chapter11Program1



So far everything works fine putting in input and stuff, but I can't get it to validate the employee number
I feel like a dumb butt having to ask. :( I stayed up half the night last night trying to get it to work.

This post has been edited by dragonseyeangie: 21 October 2009 - 05:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Calling a method in a superclass

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Calling a method in a superclass

Posted 21 October 2009 - 05:52 PM

just call

isValidEmpNum(empNum);

from any the constructors of the 2 subclass
Was This Post Helpful? 0
  • +
  • -

#3 dragonseyeangie  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 20-October 08

Re: Calling a method in a superclass

Posted 21 October 2009 - 07:15 PM

View Postpbl, on 21 Oct, 2009 - 04:52 PM, said:

just call

isValidEmpNum(empNum);

from any the constructors of the 2 subclass


here's what i did to change it

here's the modified code from ProductionWorker
public ProductionWorker(String name, String num, String date, int sft, double rate)
	{ // Begin public ProductionWorker(String name, String num, String date, int sft, double rate)
	
		// Call the superclass constructor
		super(name, num, date);
		isValidEmpNum(num);
		
		// Set the shift and pay rate
		shift = sft;
		payRate = rate;
		
	} // End public ProductionWorker(String name, String num, String date, int sft, double rate)



here's the modified code from Chapter11Program1
// Create an a Production worker object
		ProductionWorker worker = new ProductionWorker(empName, empNum, hireDate, shift, payRate);
		
		if (worker(empNum))
		{
			JOptionPane.showMessageDialog(null, "invalid employee number");
		}
		
		// Display the data 
		JOptionPane.showMessageDialog(null, worker);
	
		System.exit(0);



and here is the error i'm getting when i try to compile 'em

Chapter11Program1.java:51: cannot find symbol
symbol : method worker(java.lang.String)
location: class Chapter11Program1
if (worker(empNum))
^
.\ProductionWorker.java:38: cannot find symbol
symbol : method isValidEmpNum(java.lang.String)
location: class ProductionWorker
isValidEmpNum(num);
^
2 errors

how do i fix the errors?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Calling a method in a superclass

Posted 21 October 2009 - 07:34 PM

first you made isValidEmpNum() private... only the father class can call it
you will have to make it protected or public or nothing for the sub classes to see it
second
		// Call the superclass constructor
		super(name, num, date);
		isValidEmpNum(num);
		
		// Set the shift and pay rate
		shift = sft;


isValidEmpNum() return a boolean specifying if this employeeNum is valid
you do not chack that status
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1