Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Input into another class (I think)  RSS feed

 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so my latest assignment has turned me into a mind melted mess...
some highlights from the brief;
you have been provided with a program "Employee" no explanation will be given, it must not be edited, and must be used to fulfil your programs critera.
utilise the functionality of Employee to so that when a variable is change your code updates automatically

Create your own program called Manage; Manage will have 4 functions
1. change the employees income by applying a bonus or penalty.
2. take an array, of employees returning a boolean if two names match
3. return the total sum of all salaries in an array
4. print the name which is first alphabetically
the Employees details such as name income and any relavant bonuses or penaties shall be added via command line arguments

The Employee code is provided below, my program is separate but in the same directory, and i am stuck at the first hurdle... i can not get my program to change the names, incomes, or bonues, from default values.
the next question once that is solved, is how do i then take an array from Employee



Please any and all help will be greatly appreciated as i am really struggling with this :'(
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Austin wrote: it must not be edited

It needs to be edited as line 42 is wrong. It should be:
That apart, you have been asked to:
Create your own program called Manage; Manage will have 4 functions

So you need a file called manage.java, which does some "stuff" with employees
1. change the employees income by applying a bonus or penalty.

Sounds like this involves creating some employee instances and then changing the initial income value (which appears to be 0 or some constructor value). You would change the salary by running the bonus or penalty method on the instances you want to change i.e. like the employee class does in its own main section (presumably there for unit testing)
2. take an array, of employees returning a boolean if two names match

There is an override of the Object equals method to do this.
3. return the total sum of all salaries in an array
4. print the name which is first alphabetically

Create an Employee array variable
Populate with some Employee instances
Process it within an enhanced for loop and sum the salaries retrieved via the Salary method
Sort list and display first entry

All looks straight forward. First and foremost, create your manage.java file and go from there.

 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is horrible code and that is a horribly formulated "problem".  If you still can, you should ask for your money back.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief! What poorly‑formatted code. What bad method names! Did you really copy that code without changes? You will have to create a method to increase the salary and put it in a class called Manage (a poor name for a class, by the way). Once you have done that, consider the rest of the assignment.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Good grief! What poorly‑formatted code. What bad method names! Did you really copy that code without changes? You will have to create a method to increase the salary and put it in a class called Manage (a poor name for a class, by the way). Once you have done that, consider the rest of the assignment.


No I have changed the method and variable names. but just simply using synonyms.

anyways i have actually written some code... but it doesnt work at all I have

so my starting point was trying to get it to print a persons name...



you might have guessed it does nothing!
then i was going to move onto adding the salary...



Does nothing...

I will then follow by giveing a bonus or penalty... and start creating functions but dont see the point in doing functions until i can get some part of the main working to test or do something...
but I am just left scratching my head on this one :'(

 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your starting to get lost between the P1, and E1's and ther persons and employees i do appolagise it is where i have edited/manipulated the code (hastily) to post in the public domain...
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so would the employee instances be held in my own program? and not need to call the Employee program/File?
i ask because the print should be formatted as it is on line 33.
when this program is run it works, new employee, "mike" is then printed as
also what is this assert that is in each function, I haven't seen that before.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan, keep it simple. One step at a time i.e.

Create manage.java
Within it create an instance of an Employee
Apply a Bonus to the instance created
Show the result
Apply a Penalty to the instance created
Show the result

Build slowly on something which works. Don't get lost thinking too far ahead.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:Stan, keep it simple. One step at a time i.e.
...
Build slowly on something which works. Don't get lost thinking too far ahead.


so i have Manage.Java
and i can hard code



Thus entering the default salary, by hand but im certain this isnt what is being asked for, however if i try to print "Employee.salary" it tells me salary is private!
same if i tried to add or subtract from it
this also happened if i attempted to add a name... hence my confusion why has this program been given to us if its not useable... or what am I doing wrong...
Thanks
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Austin wrote:however if i try to print "Employee.salary" it tells me salary is private!

Yes. That's called "encapsulation". It means keeping instance variable private and only allowing manipulation though the classes own methods i.e. what are commonly known as setters and getters. The getter on 'Salary' is salary(). Therefore if from within manage. java you create an Employee instance called e1 then e1.salary() should return what's held in 'Salary'.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Stan Austin wrote:however if i try to print "Employee.salary" it tells me salary is private!

Yes. That's called "encapsulation". It means keeping instance variable private and only allowing manipulation though the classes own methods i.e. what are commonly known as setters and getters. The getter on 'Salary' is salary(). Therefore if from within manage. java you create an Employee instance called e1 then e1.salary() should return what's held in 'Salary'.


Sorry im not sure i get this, been toying with it for the last 10 mins and I'm getting nothing
Is how i have defined an Employee an Employee instance as per your definition, or do i need a different way of creating an instance of employee too?
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the error i get is:
non-static method salary() cannot be referenced from a static context
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:Create manage.java
You have mentioned it twice. That is a bad name for a class source file. Campbell told that already. Another thing is, that it should start with an upper case - very important to avoid further mess (a bit too late, probably OP will need to press and hold CTRL then A and most likely DEL).

@ Stan Austin, best choice here would be to start over in my opinion. It may sound bad, but it isn't. You learning, it is kind of normal, maybe expected a bit less chaos, but then for everybody is different.

For the past 2 days I was working on my simple practice project called Conway's Game of Life. I have bought book recommended by one Ranch member, so found it there some interesting stuff. I have present about 20-30 lines of code so far (after 2 days!), and I already managed to delete everything about 10 times and rewrite everything and re-test and again and again... Problem is, that I'm struggling to come up with something I could show to somebody, so looking for the ways how I could improve - you need to do the same - and often best option to get closer to something better is to release something bad you have (delete in simple).

Problem is, that you gone too far with that mess you have shown us here. Code isn't tested, code most likely doesn't even compile - why you write further lines of code if previuos ones don't compile? It isn't what you have been taught probably, right? Take some rest, put it away for day or two and come back to it later fresh, with fresh project.

Do you have somewhere clearly written instructions? Post 1 or 2 sentences of them, so we could have a look what you have been asked for.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Austin wrote:Sorry im not sure i get this, been toying with it for the last 10 mins and I'm getting nothing

If manage.java contained this:
it would return:
1000
1010
990

Do you understand this code and output?
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Do you understand this code and output?


yes, simple. lol
but that doesn't do what i require...
getting to increase and decrease salary seems like 20 steps away.
first i just need it to print as specified in Employee on line 33. surely?
at the moment i cant get the default salary to print yet alone manipulate it...
also i need to enter the name using the command line and for

the input isnt working :/
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that suggestion will do what is required of you. Why not post the code as it really shou‍ld be, rather than messing up the method names?
What is wrong with line 33? It looks all right to me. I suggest you go through the documentation for (System.out).println() and keep following the links untill you find what method is called in the argument object.
Did you only have an equals method? In that case it has been overridden incorrectly; if you look here, you will find out that you must override hashCode() too.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, good news i have got one stage further.
I am now getting the name and it is even printing as it should



printing e1 and e1.salary cant be done in the same println statement or it falls over seemingly, so that has cured that issue, also the increase decrease, do work.
not that i can get it to do it via the function but im one step closer thank you!!
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: I think that suggestion will do what is required of you. Why not post the code as it really shou‍ld be, rather than messing up the method names?

because I'm certain my lecturer does google searches of his own assignment to find plagerism, and so i dont risk getting accused if someone posts a very specific answer here that i think is the only way to code the solution this will help.

Campbell Ritchie wrote:What is wrong with line 33? It looks all right to me. I suggest you go through the documentation for (System.out).println() and keep following the links untill you find what method is called in the argument object.

nothing the line 33 is how i want my print formatted, i now have this working as for what method is being called, that has been confusing me but i think it is the Employee method of the Employee class, isnt it?


Campbell Ritchie wrote:
Did you only have an equals method? In that case it has been overridden incorrectly; if you look here, you will find out that you must override hashCode() too.

what now? the equals method, it compares two name to find out if they are the same right? some how we need to use that method, provided in the Employee class in the function we write. or that is how i have interpreted it, I have only just got to the first function now...
change the employees income by applying a bonus or penalty. (this is to be taken from the 3rd argument entered in the command line) our function should call the bonus and penalty methods, and it should return false if there is overflow or underflow of int.
currently working on this... using Pauls Previous comment as a bit of a guide line
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I'm not sure if we are now moving on as the original question has been solved, if so i will raise a new question but since there is substantial background here i thought this would be easier...

so you guys are propably going to laugh as i think i have made a real dogs dinner of it... I'm Pretty tired (and a little stressed) now after being at it for hours tonight, Issue is I'm on a very tight dead line
but my code for changing the salary (in the main)



and my code for the function.

should be able to see what i am trying to do
but the issue i am having is that it is trying to Find my method, change_salary as a variable, when i want it to be a method being called, am i being really daft here? (dont answer that lol)
Thanks in advance
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Austin wrote:the issue i am having is that it is trying to Find my method, change_salary as a variable, when i want it to be a method being called

I don't quite get this statement. You are not calling change_salary from main(). Where is change_salary being treated as a variable?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Austin wrote:. . . because I'm certain my lecturer does google searches of his own assignment to find plagerism, and so i dont risk getting accused if someone posts a very specific answer . . .
We have policies against very specific answers, and would do our best to remove them before you have a chance to see them. If you do see such an answer, you would have to be careful not to copy it.
Campbell Ritchie wrote:What is wrong with line 33? . . . you find what method is called in the argument object.

nothing the line 33 is how i want my print formatted . . . i think it is the Employee method of the Employee class, isnt it?
You didn't follow enough links; it is toString that is called. You find that from where it said it behaves as if String#valueOf() had been called, and that link will tell you it calls toString.
. . . the equals method, it compares two name to find out if they are the same right? . . . using Pauls Previous comment as a bit of a guide line
You seem to be getting that to work nicely so far
Whenever you override equals, you must override hashCode too. You can't sort out that mistake, however. Look on it as an SEP. (Somebody Else's Problem.)
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you recognise code below?

Stan Austin, try to pay attention to code formatting and indentation, these are tremendously important, it simply helps to you to follow your own code. It helps for the people whoever reads or going to read your code (instructor probably).
Your code could be compared to the essay which has no periods, commas, upper cases at the beginning of sentence, hyphens. Can you imagine how difficult would be to read such essay? So think about your code, it gives similar effect if you don't pay attention to it.

In some of your code you're using:

better just write

in such way you wouldn't risk to re-assign value unintentionally, i.e.:

noticed the difference? It is hard to notice, and that mistake happens quite often. Earlier or later, and when the time comes it can be very difficult to find it.
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking to the formatted and well indented code you can see some drawbacks of it. What you can say about 'if' statements? Are they really need to be 'if' in all cases? At least one I see a good candidate to swap it to 'else'.

I also changed variable name variable_name to variableName (<- later follows Java naming convention).
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:]What you can say about 'if' statements? Are they really need to be 'if' in all cases? At least one I see a good candidate to swap it to 'else'

Couldn't you remove one of the 'If' statements completely:
can become
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Between these two statements the method will always return false.
The first if() will only be skipped if modifiedSalary == Integer.MAX_Value, then it will be caught by the second if() and return false.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hang on, isn't that always going to return false?

(and I see Carey has beaten me to it!)
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:

Not sure why you were using not a short-circuit operator, but rather '&'. Nevertheless, both would be wrong. There should be a different operator.

[edit] I've been slowest it seems.
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting enough, but nobody of us noticed that until we got formatting and indentation right!
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Not sure why you were using not a short-circuit operator, but rather '&'.

Because I thought the OP was attempting to check within a range i.e. BETWEEN two numbers. As such a short circuit operator would be wrong as it would not check both sides.  I thought he had somehow set his own MIN and MAX. Time to move on I think.
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Liutauras Vilda wrote:Not sure why you were using not a short-circuit operator, but rather '&'.

Because I thought the OP was attempting to check within a range i.e. BETWEEN two numbers. As such a short circuit operator would be wrong as it would not check both sides.  I thought he had somehow set his own MIN and MAX. Time to move on I think.

I think better would have been to admit mistake as this explanation isn't correct too
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:I think better would have been to admit mistake as this explanation isn't correct too

Min = 5
Max = 20
Value given is x
x >= Min AND x <= Max is valid
x >= Min on its own is not enough as x >= Min could be 100000
Need to check both sides for a BETWEEN

If I've misunderstood the ops question fine. However, I'm only trying to help, Stan. Perhaps that should be the focus here.


 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean

I think OP originally wanted to check if values aren't out of int range, and if it is - return false. For that || would be appropriate. If OP's intention is the same but just logic inverted, then 'true' would need to be returned and used your proposed operator (short circuit would work there too). But once again, we don't know what is the idea behind OP's intention. So let's assume we both semi-right
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:Because I thought the OP was attempting to check within a range i.e. BETWEEN two numbers. As such a short circuit operator would be wrong as it would not check both sides.  I thought he had somehow set his own MIN and MAX. Time to move on I think.

Sorry, but this is incorrect. The && operator only short circuits if the left side expression evaluates to false. That would make the entire Boolean expression false. If the left side expression evaluates to true, then the right side expression gets evaluated. So, there is no difference between && and & as far as logical correctness is concerned. The only difference is the short-circuiting, which does not affect logical correctness. I'm not sure how you came to think that it does.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The && operator only short circuits if the left side expression evaluates to false
My mistake. You are of course correct.
Junilu Lacar wrote:I'm not sure how you came to think that it does.
Human fallibility.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More importantly, OP's logic for checking the range of the value is obviously wrong, for reasons that Carey has already pointed out.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Junilu Lacar wrote:I'm not sure how you came to think that it does.
Human fallibility.

A perfectly acceptable reason  
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP: why do all your methods start with an assert Salary >= 0? Is this something you were taught to do? It is very poor style. First, you can't always rely on assert being active. Second, to have all your methods assert the same thing shows a lack of thought put into the design and/or a lack of coherence in the design. Third, assertions like that belong in unit tests, IMO, not in production code. If you're peppering your production code with assertions, then it's time for you to learn how to write proper unit tests.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:@OP: why do all your methods start with an assert Salary >= 0? Is this something you were taught to do? It is, very poor style.

If those asserts were included in the code your instructor provided, then I really think you should ask for your money back. That is horrific, IMO.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, im not sure what assert even does, but they are there. and i've just ignored them.
My course is sponsored by my employer, i would have to fail then have to pay my employer, then try to get money back after the fact... im going to stick with passing in the first instance lol.
and yes i was trying to get find int overflow, and no that code didnt work... it does now you will be pleased to hear.
I am not stuck further on in the assignment that is due in under 12 hours :'(

this code just runs on no compiler errors, no prints just a complete blank until its manually stopped... any idea why?


Thanks
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code indentation is terrible. And you should have spent 2 minutes on fixing that before you post. No excuse, at all 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!