View previous topic :: View next topic |
Author |
Message |
Doomwinner Advanced Cheater Reputation: -1
Joined: 07 Oct 2009 Posts: 79 Location: USA
|
Posted: Sat Apr 13, 2013 7:55 pm Post subject: Error in Java Programming |
|
|
Making a program involving Super and sub classes for a college course.
But I don't understand the error.
Can't figure out what the problem is.
This was pre-made, there shouldn't be any errors here:
Code: | /**
The Employee class stores data about an employee
for the Employee and ProductionWorker Classes
programming challenge.
*/
public class Employee
{
private String name; // Employee name
private String employeeNumber; // Employee number
private String hireDate; // Employee hire date
/**
This constructor initializes an object with a name,
employee number, and hire date.
@param n The employee's name.
@param num The employee's number.
@param date The employee's hire date.
*/
public Employee(String n, String num, String date)
{
name = n;
setEmployeeNumber(num);
hireDate = date;
}
/**
The no-arg constructor initializes an object with
null strings for name, employee number, and hire
date.
*/
public Employee()
{
name = "";
employeeNumber = "";
hireDate = "";
}
/**
The setName method sets the employee's name.
@param n The employee's name.
*/
public void setName(String n)
{
name = n;
}
/**
The setEmployeeNumber method sets the employee's
number.
@param e The employee's number.
*/
public void setEmployeeNumber(String e)
{
if (isValidEmpNum(e))
employeeNumber = e;
else
employeeNumber = "";
}
/**
The setHireDate method sets the employee's
hire date.
@param h The employee's hire date.
*/
public void setHireDate(String h)
{
hireDate = h;
}
/**
The getName method returns the employee's name.
@return The employee's name.
*/
public String getName()
{
return name;
}
/**
The getEmployeeNumber method returns the
employee's number.
@return The employee's number.
*/
public String getEmployeeNumber()
{
return employeeNumber;
}
/**
The getHireDate method returns the
employee's hire date.
@return The employee's hire date.
*/
public String getHireDate()
{
return hireDate;
}
/**
isValidEmpNum is a private method that
determines whether a string is a valid
employee number.
@param e The string containing an employee
number.
@return true if e references a valid ID number,
false otherwise.
*/
private boolean isValidEmpNum(String e)
{
boolean status = true;
if (e.length() != 5)
status = false;
else
{
if ((!Character.isDigit(e.charAt(0))) ||
(!Character.isDigit(e.charAt(1))) ||
(!Character.isDigit(e.charAt(2))) ||
(e.charAt(3) != '-') ||
(!Character.isLetter(e.charAt(4))))
status = false;
}
return status;
}
/**
toString method
@return A reference to a String representation of
the object.
*/
public String toString()
{
String str = "Name: " + name + "\nEmployee Number: ";
if (employeeNumber == "")
str += "INVALID EMPLOYEE NUMBER";
else
str += employeeNumber;
str += ("\nHire Date: " + hireDate);
return str;
}
}
|
This creates an instance of the class(es), this is where the error is picked up:
Code: | //This Runs the Super and Sub Class
import java.util.*;
public class EmployeePROG
{
public static void main(String[] args)
{
ProductionWorker one = new ProductionWorker("John Doe", "123-A", "April 1st");
System.exit(0);
}
} |
The extension I made: the error is either here or in the previous code.
Code: | //extension for Employee.java
import java.util.*;
public class ProductionWorker extends Employee
{
public static void main(String n, String num, String date)
{
int Shift = 0; //Day or Night Day = 1 Night = 2
double HPR = 0.0; //Hourly pay rate
Shift = 1;
HPR = 2.5;
}
} |
_________________
Do I frustrate you? How about now? |
|
Back to top |
|
|
Innovation Grandmaster Cheater Reputation: 12
Joined: 14 Aug 2008 Posts: 617
|
|
Back to top |
|
|
Doomwinner Advanced Cheater Reputation: -1
Joined: 07 Oct 2009 Posts: 79 Location: USA
|
Posted: Sun Apr 14, 2013 8:27 am Post subject: |
|
|
That certainly covers what I'm learning right now, but doesn't quite clarify where I personally screwed up. To me, this code makes perfect sense (Appearently not to java though).
_________________
Do I frustrate you? How about now? |
|
Back to top |
|
|
shitposter Newbie cheater Reputation: 5
Joined: 12 Nov 2008 Posts: 13
|
Posted: Sun Apr 14, 2013 9:20 am Post subject: |
|
|
What's the error?
|
|
Back to top |
|
|
Innovation Grandmaster Cheater Reputation: 12
Joined: 14 Aug 2008 Posts: 617
|
Posted: Sun Apr 14, 2013 9:58 am Post subject: |
|
|
Doomwinner wrote: | That certainly covers what I'm learning right now, but doesn't quite clarify where I personally screwed up. To me, this code makes perfect sense (Appearently not to java though). |
Read about subclass constructors.
|
|
Back to top |
|
|
deviluc Cheater Reputation: 1
Joined: 02 Jun 2010 Posts: 28
|
Posted: Mon Apr 15, 2013 1:26 pm Post subject: |
|
|
Isn't there missing a Code: | super(n, num, date); | call in the constructor of ProductionWorker?
P.S.: It would be usefull to know which error is being thrown by your compiler
|
|
Back to top |
|
|
jucce Advanced Cheater Reputation: 1
Joined: 02 Apr 2013 Posts: 99
|
Posted: Mon Apr 15, 2013 2:23 pm Post subject: |
|
|
Change this line in the class ProductionWorker:
Code: | public static void main(String n, String num, String date) |
to
Code: | public ProductionWorker(String n, String num, String date) | You aren't defining the constructor correctly.
|
|
Back to top |
|
|
Doomwinner Advanced Cheater Reputation: -1
Joined: 07 Oct 2009 Posts: 79 Location: USA
|
Posted: Tue Apr 16, 2013 2:35 pm Post subject: |
|
|
Thank you, jucce.
I have been told repeatedly how ugly my code is, but your the first one to simply tell me my problem and solution instead of telling me to break it up into parts, or just read the section on so-and-so. (No offense Innovation, I am thinking of my teacher and classmate{who had good intent}, not you.)
And thanks to you too, deviluc, I was, but it wasn't what was throwing my particular error, and the way the book I used read, I didn't think I needed it.
+Karma to you both!(Please give time for cooldown)
_________________
Do I frustrate you? How about now? |
|
Back to top |
|
|
|