Maurizio Gasparro

Greenhorn
+ Follow
since Aug 31, 2018
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Maurizio Gasparro

Thank you guys for enlightening me! Thanks to you I managed to fix it and it worked like a charm ;-)
I've changed it like this:

@Tim first of all, thanks for the welcome :-) I do agree with you, getters and setters should only set and get values (as far as I know) but I was asked to do this, so I had to find a way to pass the value to the setter from the constructor.

Thank you again Tim, Carey and Knute!
Maurizio
5 years ago
Hi everybody!
I have a question that is causing me itching all across my body...
The code I wrote is the following (part of it):

Then I have a program that instantiates two object of this class. One object calls the default constructor and the second object calls the two parameters constructor.
Problem is: when I test the the program the setGuests() method doesn't work. I mean, it doesn't compute the price.
I assume the issue sits in the fact that, somehow, I need to pass the values from the constructor to the set method, but I have no clue on how to do this.
Am I assuming it right or I am completely wrong? (I wouldn't be surprised if you tell me is the second option)

Thanks in advance for your help as usual!
Maurizio
5 years ago
Thank you guys for all the support, it is very much appreciated!
I know I don't have to hide behind the fact that I'm a novice, but actually I am so that doesn't mean I'm not eager to learn more, but sometimes going off track creates some kind of distance from the subject I'm studying at the moment.
It is always good though to read your comments and your expert advice. I couldn't find a better community than this, I'm very happy for it ;-)

Just to reply few specific questions:

@Ritchie yes, I did read the document you linked, very helpful and I've actually started using enum by myself, even though only limited to simple tests like days of the week, etc... but it worked. Thanks a lot for that!

@Junilu even though I can be considered a student, just because I'm actually studying Java, but in reality I'm not. I left school many many years ago and just a month ago I decided to embark the Java ship only for "fun". I just bought a book and started learning by myself, with the help of this community, youtube videos, tutorials on the web, etc...


Thank you guys again!
Maurizio
5 years ago

Campbell Ritchie wrote:Unfortunately I don't like that constructor. A blood group isn't a char and vice versa. Nor is the Rhesus type. And how are you going to model people with group AB? Blood groups looks like a good use for an enum, and so would Rhesus type. Junilu has already shown you how to do that.


Hi Ritchie, and thank you very much for your reply!
I must say you are definitely right about the char because then I will not be able to use the AB group, maybe I should use a String instead. I am aware that there are "better" ways to write my program but, as I explained in my very first topic, I'm a very beginner, for example I don't even know what enum is, hence I can not use it ;-)
5 years ago
Thank Junilu, Carey, Andy and Knute for your precious help!
Junilu, your explaination about naming the fields or variables and the DRY principle it's veru useful for me because I'm a novice, so I need to know these things.

I have managed to fix the program and the solution was actually simplier than I thought.
Following the "Composition" I don't know why I was invoking methods instead of creating an object to pass the values.
So, in the Patient class I've modified the overloaded constructor in the following way and it works like a charm ;-)



Thank you again to everybody for the help! I hope one day I will be able to help other beginners like me :-)

Maurizio
5 years ago
How can I rectify the post above? I just realised that I didn't copy the first line of the code in all classes, which is actually an empty line, so if you read the above, the errors actually resides in line 9 and 7, so the error would be:

Exception in thread "main" java.lang.NullPointerException
at Patient.<init>(Patient.java:9)
at TestPatient.main(TestPatient.java:7)


Sorry for the inconvenience :-(
5 years ago
Hi everybody!
I was wondering if you can help me to understand why my application doesn't compile. I think the issue is that I got confused with "composition", which is the theme of the chapter I'm currently studying.
When I compile the program "TestPatient" it throws the following error:

Exception in thread "main" java.lang.NullPointerException
at Patient.<init>(Patient.java:10)
at TestPatient.main(TestPatient.java:8)


I've checked the line 10 in the Class Patient and the line 8 in the TestPatient Class but, to my greenhorn eyes, nothing seems wrong.
Here is the code for the classes and the program:



Or maybe the issue is with the "this" reference? I'm trying to solve the riddle but is hard...
In addition, to give you more information, I've tested the Class BloodData with another program and everything's is fine, so the problem resides in both Patient Class and TestPatient program (I think...).
Thanks in advance for your precious help as always!

Maurizio
5 years ago
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
5 years ago

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.
5 years ago
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...
5 years ago
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 ;-)
5 years ago
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 :-)
5 years ago
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
5 years ago
Sorry Harry, I didn't reply your question: I'm studying with the book Programming Java 8th edition by Joyce Farrel
To be honest I find it fairly good for my level (beginner).
5 years ago
Thank you very much to both S Fox and Harry Kar, your help is much appreciated.
Well, after your replies it is definitely clearer than before, it is not very easy to understand at the beginning because some terms get "overlapped" liked Method/Function etc...

Thank you again!
Maurizio
5 years ago