Inheritance Help

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 857 Views - Last Post: 25 June 2013 - 10:11 PM Rate Topic: -----

#1 mentoes178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-June 13

Inheritance Help

Posted 24 June 2013 - 08:03 AM

I have the Employee class and the ProductionWorker class done but am having problems with the rest. any help is appreciated.

Assignment Description:
A Java program should be created to read an employee text file (Information.txt) and
decide from the employee number and shift which department and shift the employee is
assigned. A list of the employees and their information should be listed to the monitor as
well as written to a text file named Department.txt.

Create a class named Employee including a constructor and the necessary accessor and
mutator methods. The class should keep the following information in fields:

o Employee Name
o Employee Number in the format NNN-X, where each N is a digit and X is a
letter which can be one of the following:
H - represents Human Resources
A - represents Accounting
P - represents Production
S - represents Shipping
o Hire Date

Next, write a class named ProductionWorker that inherits from the Employee class. Include
a constructor and the necessary accessor and mutator methods. The ProductionWorker class
should keep the following information in fields:

o Shift Number which is an integer and can be one of the following:
1 - represents Morning Shift
2 - represents Swing Shift
3 - represents Night Shift
o Hourly Pay Rate

Input: The input file will be named Information.txt. One record (line) will contain the
employee name, employee number, hire date, shift number and pay rate each separated by
spaces. A sample text file is below.

Jane Rivers 902-A 05/16/2001 1 16.25
Bob Cox 823-S 06/21/1990 2 17.50
Ann Ramsey 715-A 02/12/1998 1 16.25
Joseph Chandler 723-P 12/22/2000 3 14.35
Arnold Kennedy 133-S 08/10/1999 2 18.20
Larry Huber 198-P 02/12/2000 3 17.25
Annette Wilson 501-H 04/04/1995 1 20.25
Robert Ferguson 674-H 04/10/2002 2 16.50
Ava Gaines 434-H 01/05/2000 3 15.65

Output: Output should consist of a listing of the employee name, employee number,
employee's department, hire date, shift name and pay rate This information should be
displayed on the monitor as well as written to a text file named Department.txt.


I am getting a couple of errors in my Employees and production worker classes...

This is what I have for Employees:

import java.io.*; 
import java.util.StringTokenizer;

public class Employees
	{
	public static void main (String[] args) //throws IOException
   {
		String line, inFile = "Information.txt", outFile = "Department.txt";
		StringTokenizer tokens;
		File file = new File(filename);
		
		try
		{	
			//build input stream
			FileReader fr = new FileReader (inFile);
			BufferedReader fileIn = new BufferedReader (fr);
		
		
			//build output stream
      	FileWriter fw = new FileWriter (fileOut);
      	BufferedWriter bw = new BufferedWriter (fw);
      	PrintWriter outFile = new PrintWriter (bw);
		
			line = inFile.readLine();
			while (line != null)
			{
				tokens = new StringTokenizer(line);
				employeeName = null;
				employeeNumber = null;
				hireDate = null;
				shiftNumber = null;
				hourlyPayRate = null;
			
				if(tokens.hasMoreTokens())
					employeeName = tokens.nextToken();//tokenize part employeeName
					employeeNumber = tokens.nextToken();//tokenize part employeeNumber
					hireDate = tokens.nextToken();//tokenize part hireDate
					shiftNumber = tokens.nextToken();//tokenize part shiftNumber
					hourlyPayRate = tokens.nextToken();//tokenize part hourlyPayRate
					
				//output to file
				outFile.print(employeeName + " ");
				outFile.print(employeeNumber + " ");
				outFile.print(hireDate + " ");
				outFile.print(employeeName + " ");
				outFile.print(shiftNumber + " ");
				outFile.print(hourlyPayRate);
				
				//output to screen
				System.out.println(employeeName + " " + employeeNumber + " " + hireDate + " " + employeeName +
											" " + shiftNumber + " " + hourlyPayRate);
			
			catch(FileNotFoundException exception)
			{
				System.out.println("Exception: The file " + fileIn + " was not found.");
			}
         finally
         {
         	System.out.println(line + " \n");
         }


         // read next line
         line = inFile.readLine();
		}


        // close files
        inFile.close();
        outFile.close();
	}
		 catch (IOException exception)
      {
        System.out.println(exception);
      }

      finally
      {
       System.out.println("Done");
      }
	}




and for ProductionWorker

//ProductionWorker class that inherts from Employee class
public class ProductionWorker extends Employee 
{
	private int ShiftNumber; 
	private double HourlyPayRate; 
	
	public void setShiftNumber(int s) 
	{
		ShiftNumber = s;
	}
	public void setHourlyPayRate(double h) 
	{
		HourlyPayRate = h;
	}
	public int getShiftNumber() 
	{
		return ShiftNumber; 

	}
	public double getHourlyPayRate() 
	{
		return HourlyPayRate;
	}
}




And for Employee

//Employee Class
public class Employee
{
	private String EmployeeName, EmployeeNumber, HireDate;
	private String type;
	 
   public void setEmployeeName(String e)
	{
		EmployeeName = e;
	}
	public void setEmployeeNumber(String n) 
	{
		EmployeeNumber = n; 
	}
	public void setHireDate (String h)
	{
		HireDate = h; 
	}
	public String getEmployeeName() 
	{
		return EmployeeName; 
	}
	public String getEmployeeNumber() 
	{
		return EmployeeNumber;
	}
	public Employee(String type)
	{ 
		this.type = type; 
	}
	public String toString()
	{ 
		if(type.equals("H"))
		return "Human Resources";
		if(type.equals("A"))
		return "Accounting";
		if(type.equals("P"))
		return "Production";
		if(type.equals("S"))
		return "Shipping";
		return type;
	}

	public String getHireDate()
	{
		return HireDate;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance Help

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Inheritance Help

Posted 24 June 2013 - 08:05 AM

Please post your errors exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#3 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Inheritance Help

Posted 24 June 2013 - 08:05 AM

View Postmentoes178, on 24 June 2013 - 03:03 PM, said:

I am getting a couple of errors in my Employees and production worker classes...


Would you care to elaborate?


edit: Was beaten to the punch haha

This post has been edited by schutzzz: 24 June 2013 - 08:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Inheritance Help

Posted 24 June 2013 - 08:05 AM

Quote

I am getting a couple of errors in my Employees and production worker classes...


Please post your errors - just copy the compiler output and paste it here.
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Inheritance Help

Posted 24 June 2013 - 08:05 AM

Come on now, don't just tell us you have problems - we know that, you posted here - tell us what problems you are having. What errors?
Was This Post Helpful? 0
  • +
  • -

#6 mentoes178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-June 13

Re: Inheritance Help

Posted 24 June 2013 - 08:09 AM

For the ProductionWorker class I get the error constructor Employee in class Employee cannot be applied to given types;
Required:java.lang.Sting
found: no arguments
reason actual and formal argument lists differ in length

in Employees I get "catch" without "try"
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Inheritance Help

Posted 24 June 2013 - 08:11 AM

The child class must have a matching constructor, or explicitly invoke one from the super class. From your Employee class:
 public Employee(String type)
28	    {
29	        this.type = type;
30	    }



You don't explicitly define a constructor for ProductionWorker, so by default, the following constructor is provided:
public ProductionWorker(){
     super();
}



So it tries to invoke a super constructor with no arguments. However, no such constructor exists in Employee.

Thus, you must either define a constructor in ProductionWorker with matching arguments as Employee, or invoke said constructor explicitly with a super call.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Inheritance Help

Posted 24 June 2013 - 08:15 AM

The catch without try is due to poorly nested blocks. Removing the code, you have

try {

  while (condtion) {
    takeSomeSteps();
    catch (SomeException se) {

    }
  }
}


The catch block must be on the same level of nesting as the try that it corresponds to:

try { 
  while (condition) {
    takeSomeSteps();

  }
}
catch (SomeException se) {
}


This post has been edited by jon.kiparsky: 24 June 2013 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#9 mentoes178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-June 13

Re: Inheritance Help

Posted 24 June 2013 - 08:22 AM

View Postjon.kiparsky, on 24 June 2013 - 08:15 AM, said:

The catch without try is due to poorly nested blocks. Removing the code, you have

try {

  while (condtion) {
    takeSomeSteps();
    catch (SomeException se) {

    }
  }
}


The catch block must be on the same level of nesting as the try that it corresponds to:

try { 
  while (condition) {
    takeSomeSteps();

  }
}
catch (SomeException se) {
}




Okay now updated Employees is :

import java.io.*; 
import java.util.StringTokenizer;

public class Employees
    {
    public static void main (String[] args) //throws IOException
   {
        String line, inFile = "Information.txt", outFile = "Department.txt";
        StringTokenizer tokens;
        File file = new File(filename);
        
        try
        {   
            //build input stream
            FileReader fr = new FileReader (inFile);
            BufferedReader fileIn = new BufferedReader (fr);
        
        
            //build output stream
        FileWriter fw = new FileWriter (fileOut);
        BufferedWriter bw = new BufferedWriter (fw);
        PrintWriter outFile = new PrintWriter (bw);
        
            line = inFile.readLine();
            while (line != null)
            {
                tokens = new StringTokenizer(line);
                employeeName = null;
                employeeNumber = null;
                hireDate = null;
                shiftNumber = null;
                hourlyPayRate = null;
            }
                if(tokens.hasMoreTokens())
                    employeeName = tokens.nextToken();//tokenize part employeeName
                    employeeNumber = tokens.nextToken();//tokenize part employeeNumber
                    hireDate = tokens.nextToken();//tokenize part hireDate
                    shiftNumber = tokens.nextToken();//tokenize part shiftNumber
                    hourlyPayRate = tokens.nextToken();//tokenize part hourlyPayRate
                    
                //output to file
                outFile.print(employeeName + " ");
                outFile.print(employeeNumber + " ");
                outFile.print(hireDate + " ");
                outFile.print(employeeName + " ");
                outFile.print(shiftNumber + " ");
                outFile.print(hourlyPayRate);
                
                //output to screen
                System.out.println(employeeName + " " + employeeNumber + " " + hireDate + " " + employeeName +
                                            " " + shiftNumber + " " + hourlyPayRate);
             }
            catch(FileNotFoundException exception)
            {
                System.out.println("Exception: The file " + fileIn + " was not found.");
            }
         finally
         {
            System.out.println(line + " \n");
         }


         // read next line
         line = inFile.readLine();
        }


        // close files
        inFile.close();
        outFile.close();
    }
         catch (IOException exception)
      {
        System.out.println(exception);
      }

      finally
      {
       System.out.println("Done");
      }
    }




with the error identifier <> expected on the line for the inFile.close();

View Postmacosxnerd101, on 24 June 2013 - 08:11 AM, said:

The child class must have a matching constructor, or explicitly invoke one from the super class. From your Employee class:
 public Employee(String type)
28	    {
29	        this.type = type;
30	    }



You don't explicitly define a constructor for ProductionWorker, so by default, the following constructor is provided:
public ProductionWorker(){
     super();
}



So it tries to invoke a super constructor with no arguments. However, no such constructor exists in Employee.

Thus, you must either define a constructor in ProductionWorker with matching arguments as Employee, or invoke said constructor explicitly with a super call.


Can you explain to me a little more, I'm sorry, just beginning and I read the post a couple of times and am still confused...
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Inheritance Help

Posted 24 June 2013 - 08:25 AM

See the code sample. If A's constructor accepts a String, each of B's constructors must invoke the super constructor in A. If they have matching parameter lists (such as the second B constructor), this is done implicitly. Otherwise, you must explicitly do this as with the first B constructor.
class A{

    public A(String str){}
}

class B extends A{

   public B(){
       super("some string");
   }

   public B(String str){}
}


Was This Post Helpful? 0
  • +
  • -

#11 mentoes178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-June 13

Re: Inheritance Help

Posted 24 June 2013 - 08:31 AM

View Postmacosxnerd101, on 24 June 2013 - 08:25 AM, said:

See the code sample. If A's constructor accepts a String, each of B's constructors must invoke the super constructor in A. If they have matching parameter lists (such as the second B constructor), this is done implicitly. Otherwise, you must explicitly do this as with the first B constructor.
class A{

    public A(String str){}
}

class B extends A{

   public B(){
       super("some string");
   }

   public B(String str){}
}


class Employee{
     public Employee(String str){}
}
class ProductionWorker extends Employee{

public Production worker90{
     super();
}
public ProductionWorker(String str){}
}



correct?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Inheritance Help

Posted 24 June 2013 - 08:34 AM

Nope. And your compiler should give you a lot of complaints. This section is invalid syntax:
public Production worker90{
07	     super();
08	}


Was This Post Helpful? 0
  • +
  • -

#13 mentoes178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-June 13

Re: Inheritance Help

Posted 24 June 2013 - 08:47 AM

View Postmacosxnerd101, on 24 June 2013 - 08:34 AM, said:

Nope. And your compiler should give you a lot of complaints. This section is invalid syntax:
public Production worker90{
07	     super();
08	}


import java.text.DecimalFormat;


public class ProductionWorker extends Employee 
{
    private int ShiftNumber; 
    private double HourlyPayRate; 
    DecimalFormat formatter = new DecimalFormat("##0.00");
    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
        ShiftNumber = sft;
        HourlypayRate = rate;
    } // End public ProductionWorker(String name, String num, String date, int sft, double rate)
    
    public ProductionWorker()
    { // Begin public ProductionWorker()
        HourlypayRate = 0.0;
    }
    public void setShiftNumber(int s) 
    {
        ShiftNumber = s;
    }
    public void setHourlyPayRate(double h) 
    {
        HourlyPayRate = h;
    }
    public int getShiftNumber() 
    {
        return ShiftNumber; 

    }
    public double getHourlyPayRate() 
    {
        return HourlyPayRate;
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Inheritance Help

Posted 24 June 2013 - 08:49 AM

Okay. And?
Was This Post Helpful? 0
  • +
  • -

#15 mentoes178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-June 13

Re: Inheritance Help

Posted 24 June 2013 - 08:49 AM

This is the this is the updated ProductionWorker....I still get errors saying

constructor Employee in class Employee cannot be applied to given types;
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3