473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Programming with Inner Classes

Hi everyone,

My classes are: Employee, Date and HourlyEmployee. I'm supposed to write my Employee class with Date class as an Inner class and then HourlyEmployee is supposed to extend Employee. Class Date has 3 attributes, day, month, and year are all int values except month may be written as a string, these values are set to default of "January 1, 1000" when the default constructor is invoked. Class Employee had 2 attributes, name and Date. name is inputted by the user, date is also inputted by the user but once input is received, date values are sent to other methods to verify if the date information is correct. HourlyEmployee is exactly like Employee except with additional attributes or wageRate and hoursWorked, both int values.
I have rewritten Date inside Employee and when i create an Employee object in my main, everything works perfectly. My Date class has a method to prompt user for input, called readInput(), and will automatically set the Employee date to the input values given by the user. Where i am having trouble is when i create an HourlyEmployee object and try to access the same readInput() method, the values entered do not actually get set to the object, so i am able to input values for HourlyEmployee object but when i print my object's attributes, the default values of Date object show up instead. Maybe i'm not calling the readInput() from HourlyEmployee correctly? (my classes all have mutator methods, just not shown here)

public class Employee
{

protected class Date
private String month;
private int day;
private int year; //a four digit number.

public Date( )
{
month = "January";
day = 1;
year = 1000;
} //sets the default date to January 1, 1000


other methods...
public void readInput( ) //method that prompts the user for input
//automatically verifies if the date is correct by
//calling other functions is class Date
{
boolean tryAgain = true;
Scanner keyboard = new Scanner(System.in);
while (tryAgain)
{
System.out.print("Enter month, day, and year, do not use a comma: ");
String monthInput = keyboard.next( );
int dayInput = keyboard.nextInt( );
int yearInput = keyboard.nextInt( );
if (dateOK(monthInput, dayInput, yearInput) )
{
setDate(monthInput, dayInput, yearInput);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");
}
}

}//end of class Date
}//end of class Employee

public class HourlyEmployee extends Employee
{

private double wageRate;
private double hours; //for the month

public HourlyEmployee( )
{
super( );
wageRate = 0;
hours = 0;
}

more methods...
}//end of class HourlyEmployee

So basically, im not quite sure how i should be calling Date class when i create an HourlyEmployee Object.

The way that i'm currently doing it is:

HourlyEmployee e = new HourlyEmployee(); //creating an hourlyemployee

//i am trying to call readInput() from Date class so i'm saying:

e.getHireDate().readInput(); //which is the only way i can get the function
// to work and its supposed to prompt user
//for input and set the object to those values
//and it's not

am i calling the inner class Date correctly using HourlyEmployee object? it compiles without problem but the code is not working as it should

any suggestions?
thank you VERY much
Mar 18 '09 #1
3 1419
r035198x
13,262 8TB
Use code tags when posting code.
How is it "not working as it should"? You need to give more details about the behavior of the code.
Mar 19 '09 #2
chaarmann
785 Expert 512MB
@rotaryfreak
So Employee contains nothing except definition for date-class? No variables, no methods, no constructor? (Java will make default constructor if you don't give)
You should have at least a instance (object) of Date inside employee somewhere, if you want to access that date in your derived class! For example:

Expand|Select|Wrap|Line Numbers
  1. protected myDate = new Date();
Then you can access with:
Expand|Select|Wrap|Line Numbers
  1. HourlyEmployee e = new HourlyEmployee();
  2. e.myDate.setDate(month, day, year);
Mar 19 '09 #3
thank you chaarmann for the reply, but i ended up going through every single line of code and i realized my accessor function was returning a copy of the object and not the object itself. Under normal circumstances, i know that returning the object directly is dangerous because there is a privacy leak but for my application, that's exactly what i needed to happen, and that's why i said the code wasn't working as it should. In the end, i was in fact calling the function properly but because my output was not correct, i assumed i was not calling the function correctly.

a big thank you again.
Mar 21 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: devu | last post by:
Currently I'm working on a class that performs a batch process as per scheduling for all employees in a company. Being a batch process load is obviously a huge factor. The process first needs to...
10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
1
by: Mark Paskin | last post by:
Hello, I'm having difficulty getting template matching to work with inner classes. I've tried to look up the way to fix this in my references, but I haven't found the fix yet. When I try to...
6
by: Marco | last post by:
Howdy! Given: public abstract class A { public abstract int A1(int i); private class B { private int B1(int i) { int j;
18
by: Frankie | last post by:
I have been hired to go to a former client of mine and train their staff programmers on ASP.NET. These guys have only Mainframe, MS Access, SQL Server, and VB6 desktop application development...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
6
by: Philip Potter | last post by:
I have a graph data structure. It goes something like this: class GraphNode { private: friend class Graph; // successor GraphNodes stored as indices of the Graph's array int value; int next1,...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.