• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Can you please help me with a Java test?  RSS feed

 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody, and sorry if this post is not very appropriate for this forum, but I'm encountering serious difficulties in completing a test in Java. I need to tell you in advance that I am very new to Java and programming (never done it before), so I am a very beginner.
This is a final test after the third chapter about Methods, Classes and Objects and the exercise is challenging my mind very badly...
The main issue is: I feel I don't have understood the questions and the purpose of the app.

The text of the exercise is the following:

a. Create a class named Lease with fields that hold an apartment tenant’s name,
apartment number, monthly rent amount, and term of the lease in months. Include
a constructor that initializes the name to “XXX”, the apartment number to 0, the
rent to 1000, and the term to 12. Also include methods to get and set each of the
fields. Include a nonstatic method named addPetFee() that adds $10 to the
monthly rent value and calls a static method named explainPetPolicy() that
explains the pet fee. Save the class as Lease.java.

b. Create a class named TestLease whose main() method declares four Lease
objects. Call a getData() method three times. Within the method, prompt a user
for values for each field for a Lease , and return a Lease object to the main()
method where it is assigned to one of main() ’s Lease objects. Do not prompt the
user for values for the fourth Lease object, but let it continue to hold the default
values. Then, in main() , pass one of the Lease objects to a showValues() method
that displays the data. Then call the addPetFee() method using the passed Lease
object and confirm that the fee explanation statement is displayed. Next, call the
showValues() method for the Lease object again and confirm that the pet fee has
been added to the rent. Finally, call the showValues() method with each of the other
three objects; confirm that two hold the values you supplied as input and one holds
the constructor default values. Save the application as TestLease.java.



What I've coded are the following classes:



And the actual program:



You will notice that I've commented out a slice of code, that is just because it is just a repetition of the code to prompt the user two times.
The final result is working, so from a "result oriented" perspective, should be ok, but I don't know why, I feel I've done something wrong, especially for the following reasons:

1. you'll notice that in the first class, I've created the variable within the method addPetFee() named newRent that is actually never used. So, what's the point? It is just "wasting" memory, but the exercise asked me so...
2. in the second class, within the main method, I have duplicated variables to "grab' values from the Scanner to throw into them the user input. Problem is (in my opinion): again I feel I'm wasting memory, there must be another way in order to avoid the repetition of the variables.
3. in general, being this just the third chapter of the book, I feel I wrote too much code, when for the previous exercises it was very little.

In conclusion:
I feel I've done it wrong, I've missed something and the code is not "optimised'.
What do you think guys? I'm asking for your expert opinion to help me understand better.

Than you very much in advance for all precious help ;-)
Maurizio
 
Saloon Keeper
Posts: 4757
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct about newRent, the value gets thrown away. Your requirements don't say anything about returning a value.

You are missing a lot of what has been described in the second half of the requirements, specifically, the implementation of two methods and the calling of those methods.
 
Bartender
Posts: 1876
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, I find this question to be too verbose describing each method names and their modifiers and how to call those methods.
First of call, I suggest that you QuoteYourSources <-- it’s a link
This can give us an idea about the context of the question. I feel that a programming exercise should tell you what are the required inputs and outputs for a program instead of how it should be written.
 
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Honestly, I find this question to be too verbose . . . instead of how it should be written.

Agree. I don't like over‑prescriptive assignments, Part of the skill of programming is working out how many methods to create, which this assignment doesn't test. Another problem is that it is possible to introduce mistakes by being too prescriptive. Do you actually want a tenant named “XXX” or flat No 0? It is probably sensible to have default rents of £1000 for 12 months, however. Do you actually want setXXX() methods that will alter the tenant's name or flat number? Unfortunately, even if whoever set the assignment realises there are mistakes, it is not possible to change the assignment at that stage.
Doubtless the quote above comes from such an assignment which OP has to do.

And . . . Maurizio Gasparro, welcome to the Ranch
 
Maurizio Gasparro
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you, it is too verbose, and that is confusing me. Also, I feel that sometimes it describes things the other way round, for example when she says: "Call a getData() method three times." And then: "Prompt the user to input data for the fields...".
To me it should be the opposite, prompt the user first and then call the getData() methods otherwise they won't return any value, no?
I'm very confused about this assignment but I don't want to give up, I'll keep trying. ;-)

Reference you asked asked for, I don't have a link but this is from the book "Java Programming 8th edition by Joyce Farrell".

Oh and Campbell, thank you for the welcome ;-) much appreciated :-)
 
Carey Brown
Saloon Keeper
Posts: 4757
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the above comments about verbosity. Perhaps this is a test to see if the student can follow instructions. This snippet of the instructions bother me because I would see something named "getData()" to be some form of getter. I might have named it inputLease() or createLease(). Regardless, the instructions can be followed as is. We can already see that if the OP continues on his current path that he'll end up with redundant input code. Making a getData() method would correct that.

Call a getData() method three times. Within the method, prompt a user for values for each field for a Lease , and return a Lease object to the main() method where it is assigned to one of main() ’s Lease objects.

 
Marshal
Posts: 6008
415
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maurizio Gasparro wrote:Also, I feel that sometimes it describes things the other way round, for example when she says: "Call a getData() method three times." And then: "Prompt the user to input data for the fields...".


Just to be slightly on the assignment's side... This is not what assignment requirements say. So you copied/pasted and interpreted (*misinterpreted*) your way. That is a very common thing.


Requirements say:

Call a getData() method three times. Within the method, prompt a user for values for each field for a Lease...


That is very different from what you said.

True story:
I also had in university an assignment similar to that. I think it was from JavaScript. Basically we were given instructions - they were very simple, primitive (and that was on purpose, read further..), and everybody laughed how simply assignment going to be, some of the students packed right away and went home happy. However, lecturer warned, that 100% marks will be given for those who will follow requirements exactly (so long as even smallest tiny piece of details are followed), and will be marked down significantly for shifting from them.

After assignment was finished and marks were distributed, lecturer provided statistics across the years for this very exercise, only up to 10% of people were able to follow requirements (*even tho lecturer made very clear what is going to be tested in this assignment*).

Our group weren't exception. 10/90.

I'll check whether I can publish these requirements (and not to breach anything) just for everybody to see how easy it was, or more precisely, how such an easy instructions were over complicated, misinterpreted, decisions to improvise were made and so on and so forth, so marks were not that good as everybody expected. It was one of my favourite assignments - I've learned from it a lot.
 
Maurizio Gasparro
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've tried to brake it down to ease the understanding of the exercise but honestly, I still struggle a bit...

FIRST PART
do: create a class named Lease.java
do: create fields: aptTenName; aptNum; monRent; terOfLease;
do: create a contractor that initializes the fields: aptTenName = "XXX"; aptNum = 0; monRent = 1000; terOfLease = 12;
do: create setters: setAptTenName(); setAptNum(); setMonRent(); setTerOfLease();
do: create getters: getAptTenName(); getAptNum(); getMonRent(); getTerOfLease();
do: create a non static method addPetFee() that adds $10 to the monthly rent and calls a static method explainPetPolicy() that explains the pet fee (with a string);
SECOND PART
do: create a class named TestLease.java
do: create the main method
do: declare four Lease objects: firstObj, secondObj and so on...
do: prompt the user for values for each field for Lease for all objects
do: call a getData() method three times
// in each call below display the aptTenName as different, e.g. Mary, Lewis, Charlie, etc...
do: in the first call show the data and call the addPetFee() and confirm the fee explaination statement is displayed
do: in the second call show the data and confirm that the pet fee has been added to the rent
do: in the third call show all data
do: confirm that two hold the values supplied and one the default constructor

Any suggestion at all?

p.s. thank to everyone who's helping me, I really appreciate it ;-)
 
Carey Brown
Saloon Keeper
Posts: 4757
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you create getData()?
What is getData() supposed to do?
What, if anything, is getData() supposed to return?
What, if anything, does getData() need to be passed in as parameters?
At what point is getData() called?
Is getData() called more than once?
Where, if anywhere, is the returned value, if any, assigned from getData()?

Similar question for the showValues() method.
 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maurizio Gasparro wrote:. . . do: create fields: aptTenName; aptNum; monRent; terOfLease;

You are going to have to create fields with those names, even though I think they are poor names. They are difficult to read out and they do no t make it clear what they mean. Be sure to use exactly the spelling you have been given.

do: create a contractor that initializes the fields: aptTenName = "XXX"; aptNum = 0; monRent = 1000; terOfLease = 12;. . .

That is an unusual spelling of constructor. Be very careful copying that sort of instruction, And avoid predictive text.

p.s. thank to everyone who's helping me, I really appreciate it ;-)

That's a pleasure Sorry we sound so negative sometimes.
 
Carey Brown
Saloon Keeper
Posts: 4757
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Maurizio Gasparro wrote:. . . do: create fields: aptTenName; aptNum; monRent; terOfLease;

You are going to have to create fields with those names, even though I think they are poor names. They are difficult to read out and they do no t make it clear what they mean. Be sure to use exactly the spelling you have been given.


The requirements don't specify those names. In fact, they refer things like "apartment  number", which in Java camel-case should be "apartmentNumber".
 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . .  The requirements don't specify those names. . . .

That is one of the places where I am relieved to find I was mistaken. MG: please take note and change your variable names.
 
Maurizio Gasparro
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey and Campbell, I've changed variable names as you suggested.
I'm still fighting with the logic and methods though :-( is so annoying...
 
Campbell Ritchie
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Copy your code somewhere else. Delete every method. Add the methods back one by one, and test them as you add them. Then show us what is going wrong.
 
Maurizio Gasparro
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Copy your code somewhere else. Delete every method. Add the methods back one by one, and test them as you add them. Then show us what is going wrong.



Hi Campbell, no sorry, I didn't explain myself very well...
The code above is working and the program is doing what the exercise asks to do.
My concern is about the interpretation and execution, I feel that, even if the program is working, I have done something in a way that I didn't have to.
So, basically, is all about interpretation.
 
Carey Brown
Saloon Keeper
Posts: 4757
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance, according to the instructions, this
should be
per

Create a class named TestLease whose main() method declares four Lease
objects. Call a getData() method three times. Within the method, prompt a user
for values for each field for a Lease , and return a Lease object to the main()
method where it is assigned to one of main() ’s Lease objects.
 
Ranch Hand
Posts: 42
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In defense of the overly prescriptive assignment, it is possible that the sections not used are actually planned to be developed in later lessons. If you had chosen to go in a different direction, the later lessons may require you to recode. Given a particular assignment, 5 experienced programmers will likely come up with 5 perfectly valid but different results. I suspect this is a lesson in working in a collaborative programming environment. Decisions regarding code touch points need to be coordinated by the lead programmer.
 
Maurizio Gasparro
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone who helped me with this test. FYI I've decided to just skip it and go ahead, finish the chapter and start a new one. Then I'll come back to this exercise when I feel more comfortable with Java and see what happens.
I believe that the main issue is that I don't feel very confident with coding at the moment. I noticed that, if anything happens, I do panic a lot ;-) I hope this is normal for a greenhorne :-)


Thank you again!
Maurizio
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!