Easy Java Question

Danimal1209

Senior member
Nov 9, 2011
355
0
0
So, I am beginning to program in Java and I am learning accessors and modifiers. In this example, I am trying to set an employee class to where a user can input first name, last name, employee Id, and rate of pay.

Here is what I have so far. Now, I know it is not even near correct, but I am somewhat confused on what to do. I have not modified the code to use the console for input yet. Initially, for this example I just had a constructor input the code, now I want to input in via the console. I just want to get the firstname right so I can do the rest afterwards.

public class Employee {

private String firstName;
private String lastName;
private int employeeId;
private double hourlyRate;

public Employee (String firstNameIn, String lastNameIn, int idIn, double hourlyRateIn)
{
setFirstName(firstNameIn);
lastName
employeeId
hourlyRate
}

private void setFirstNameIn(String first)
{
if (first.length() > 20)
{
System.out.println("The name you entered needs to be less than 20 characters long");
System.exit(-1);
}
firstNameIn = first;
}

public String getFirstNameIn()
{
return firstNameIn;

firstnamein in the modifier says that it cannot find the symbol. So, I'm not sure why.

Any help is appreciated.
 

KLin

Lifer
Feb 29, 2000
29,557
166
106
You're calling SetFirstName. Method name is SetFirstNameIn.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
Ok, fixed that. But I'm still confused, where do I put in the console so people can enter their information?
 

ioni

Senior member
Aug 3, 2009
619
11
81
Ok, fixed that. But I'm still confused, where do I put in the console so people can enter their information?

Do you know how to run your program from the console? If you do, run your program from there and get input from stdin.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
Ok, last question lol.

Why is firstNameIn still saying it has no symbol? In the set modifier.
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,361
4,067
75
"accessors and modifiers"? Sounds like somebody's been coming up with buzzwords. I always called these "getters" and "setters".

As for your current problem, it's similar to your last problem. Descriptive, consistent naming is very important in programming. I'm gonna suggest you write very simple getters and setters to start with, and leave the error checking to later. It may help to see everything on one screen.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
The book we're using for class calls them accessors and midifiers. My proffesor calls them as you call them, getters and setters.

The thing that bothers me in class is that he doesn't use an overhead to actually right the code. He just explains what does what and we're supposed to figure out what goes where. It's frustrating.

I think I need to do a bit more reading to understand this better. Too bad this homework is due tonight. It doesn't look like I'll be finishing it.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Your variable names are a mess. I would consider using function names that won't confuse you with your variable names.

Consider below and you can figure it out.

Code:
public class Employee {

private String [B]firstName;[/B] 
...............
...............
System.exit(-1);
}
[U]firstNameIn[/U] = first;
}
...............
...............
...............
return [U]firstNameIn[/U];
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,361
4,067
75
Let me try to spell it out for you, then.

First, classes have members. If they're public, anyone can read or write them.
Code:
public class C {
  public int i;
}

If they're private, no one outside the class can read them, so to read and write them it may be convenient to write a getter method and a setter method.

Code:
public class C {
  private int i;
  public void set_i(int i) { this.i=i; }
  public int get_i() { return i; }
}

Now, why is this better than just having the public variable? Two reasons. One, you can leave out the setter, if you want the property to be read-only. And two, you can modify the setter to check for errors and/or modify the getter to interpret the value. Example:

Code:
public class C {
  private int money;
  public void set_money(float fmoney) { money=(int)((fmoney*100.0)+0.5); }
  public float get_money() { return ((float)money)/100.0; }
}

That there is a "fixed-point" storage system, by the way.

I suggest you build up your homework along the lines of this progression. First write the class member variables. Then write very simple getters and setters. Make sure those work. Then add the verification steps, or whatever else you need.
 
Last edited:

quikah

Diamond Member
Apr 7, 2003
4,104
672
126
Another possible issue (depends on the requirements), your setFirstNameIn method is private, so you cannot call it from outside the class.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
Ok, I think I am getting a little closer. But the variable in the () in the Employee function are still underlined red saying there is no symbol. Not sure why since it is in the code.

<quote>
public class Employee {

private String firstName;
private String lastName;
private int id;
private double rate;
public Employee (String firstNameInput, String lastNameInput,
int idInput, double rateInput)
{
setFirstName(first);
setLastName(last);
setId(employeeId);
setRate(hourlyrate);
}

private void setFirstNameInput(String first)
{
if (first.length() > 20) // I used 100 as an example for error checking!
{
System.out.println("First name needs to be 20 characters or less");
System.exit(-1);
}
firstName = first;
}
public String getfirstName()
{
return firstName;
}
private void setLastNameInput(String last)
{
if (last.length() > 20) // I used 100 as an example for error checking!
{
System.out.println("Last name needs to be 20 characters or less");
System.exit(-1);
}
lastName = last;
}
public String getlastName()
{
return lastName;
}
private void setidInput(int employeeId)
{
//if (id.length() > 20) // I used 100 as an example for error checking!
{
System.out.println("First name needs to be 20 characters or less");
//System.exit(-1);
}
id = employeeId;
}
public int getemployeeId()
{
return id;
}
private void setRateInput(double hourlyRate)
{
//if (first.length() > 20) // I used 100 as an example for error checking!
{
System.out.println("First name needs to be 20 characters or less");
//System.exit(-1);
}
rate = hourlyRate;
}
public double getrate()
{
return rate;
}
}
</quote>
 

purbeast0

No Lifer
Sep 13, 2001
53,027
5,912
126
setFirstName(first)

should be

setFirstName(firstNameInput)

since that is what the variable in the parameters of the constructor are. you should be able to figure out the next 3 based on what i just told you.

you really do need to go back and figure all this out yourself and learn it, rather than having people on here tell you how to do it. not trying to be a dick, but that is about as basic as you could possibly get as far as programming goes. it's just simply understanding variables and scope.
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
Yeah, I know I need to read more on the subject. Our teacher isn't teaching how I would suggest. He doesn't really explain much. I don't even know what some of these variables in parens mean. lol. Time to get my book out.

My teacher for Ruby was great and I understood everything without a problem.

Thanks.

LAST THING LOL

In this code:

if (first.length() > 20 );
{
System.out.println("First name needs to be 20 characters or less. you've entered "
+ "a name that is " + first.length() + " characters long");
System.exit(-1);
}

When I enter the name Dan it runs the if statement and tells me that it's 3 characters long. Why is it still running it when first.length is < 20?

GOT IT! Damng semicolon.
 
Last edited:
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |