• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

member data acceptance

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question was asked of me: When is the assignment due? It was due mid-December 2015. I was just starting the assignment when I was involved in a road accident that almost killed me. I had to withdraw from the online class. I spent months in hospital. Now I am 99% OK. I plan on taking the class again in the Fall of this year. Concerning this assignment, I have written two methods in class TripleString to meet the requirement of calling two members of objects to test them in if/else statements. Here are my methods. Each one has a different parameter.

Here is my main() which includes the calls with the if/else messages.

Here are some calls: First, display all the objects.

Next I have mutated one or more members of every object.

Finally, I do two explicit mutator tests where I call each mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails. I do two accessor calls to demonstrate that they work.

Here is one more call with a successful outcome.

Your comments would be appreciated concerning all of the above code.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to hear about your troubles. It's good that you have recovered well.

It's also good to see that you're improving your code. The indentation looks great

Those two methods are exactly the same except for the names so there really is no need to have them both. One of them will do. You just need to call it with a different parameter each time. I know you had to write separate methods because they validate different fields but when you see duplication like this, it's a sign that your approach is flawed. Rethink your approach by asking yourself what is different and what is the same in both sections of code. Then make the code that is similar as a separate method and parameterize the parts that are different.
 
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are glad you recovered.

While Junilu sees an improvement in code indentation, I have to agree with him here, but the rest, I personally don't see an improvement. I'm sorry.

I re-read your posted instructions once again, and it looks to me either you don't understand them or decided to do not follow them.
I extracted for you even step by step what needs to be done as instructions crammed in an essay form could be difficult to read.

Can you post TripleString class in full?
And please answer, I am very interested, why you don't do exactly as instructions say, but rather your way, which is totally different?
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutauras Vilda: In response to your question: Why (don't you) do exactly as (the) instructions say, but rather your way, which is totally different? I believe it is because the course modules that I studied and referred to for this assignment led me to do it the way I did. I noticed that I received a couple of e-mails (while I was in the hospital) from the instructor concerning changes to the spec in that students were having trouble getting output. One of the changes, I believe had to do with using str rather than string and the other e-mail stated that students could use "this" but that they needed to read ahead to understand how to use it in that it was not part of the modules for this assignment. I will look up the e-mails and post them to you. Here is the class TripleString

Here is the main() also, I changed it by using the no arguments constructor to set TripleString string3 in that the assignment instructions ask one to instantiate some objects by using default constructor (ie. the no argument constructor)
and using the constructor that takes parameters.

I did some mutating and I see that TripleString str3, str1 ("Alli") did not set. In the output you will see that it is (undefined). I do not know why.

Here is the output without any mutations.

 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain this part, if possible line by line, I'm trying to understand what are you doing here. Thank you.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks to me to be the same as the much simpler:
 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:Looks to me to be the same as the much simpler:

But is it correct? I'd try to get correct first, then look for the ways how to simplify.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutauras Vilda: In regards to my constructor that takes parameters and establishing the string's validity. I have had a problem when I declare local variables, objects or references in a method or a constructor, in this case, if they have the same name as class or instance members, the locals "shadow" the class or instance members inside my constructor. So I handled this problem by changing the spelling of string and using the keyword this. I have tried other ways and I get output that has a lot of null and the format looks terrible. Furthermore, what Fred Kleinschmidt suggested does not work.

Here are the changes to spec made by the two e-mails from the instructor.

#1 e-mail: Formal parameter names can be the following in the constructor that takes parameters: TripleString(String str1, String str2, String str3). Formal parameters can be named anything (desciptive). Another solution is using the keyword this. Then you can keep the parameters string1, string2, string3.

#2 e-mail: The next assignment uses the TripleString class and you want to ensure that the TripleString class includes any needed corrections.
You may even submit my " TripleString class solution" for this current assignment as part of your next assignment (I do not know what he is talking about when he mentions his TripleString class solution.).
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your instructor probably provided his own solution to the previous current assignment and he's saying it's OK if you use that solution as the basis for the current next assignment. (Edited: fixed off-by-one error )
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lee shotwell wrote:In regards to my constructor that takes parameters and establishing the string's validity. I have had a problem when I declare local variables, objects or references in a method or a constructor, in this case, if they have the same name as class or instance members, the locals "shadow" the class or instance members inside my constructor. So I handled this problem by changing the spelling of string and using the keyword this. I have tried other ways and I get output that has a lot of null and the format looks terrible.


Lee,

I have to admit not having read the entire thread (48 posts now), but I suspect that part of your problem may be that you are making changes to your design simply to get things to compile. And that's usually a bad idea.

A few general points for you:
1. DON'T define static members of any kind (ie, fields or methods) unless you have a very good reason - or you've been told - to do so. The best Java objects are self-contained, and so should work without any static members at all.

2. NEVER define static variables (ie, fields that can change).

3. You may find that some methods don't actually need an object to work, but make it a habit to start out by defining them as instance methods, and then adding the static keyword only when you (a) know they work, and (b) know they can be made static.

4. Before you write your first line of Java code, make sure you understand WHAT your class needs to do - Note: "what", not "how". A lot of this will come from the requirements, but resist the urge to simply start coding because you think you know what is needed. That approach may work for "Hello World", but it simply doesn't scale as soon as problems become more complex.

You may find this article worth reading to help you get out of the "static trap".

HIH

Winston
 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lee shotwell wrote:I have had a problem when I declare local variables, objects or references in a method or a constructor, in this case, if they have the same name as class or instance members, the locals "shadow" the class or instance members inside my constructor. So I handled this problem by changing the spelling of string and using the keyword this.

Don't write anything else after constructor if you haven't got it right. That means if it doesn't do what the instructions says it to do. Getting rid of compiler errors doesn't mean handling situation.

lee shotwell wrote:Here are the changes to spec made by the two e-mails from the instructor.

Did he change the instructions in the middle of assignment Luckily no, he just tried to clarify you something, but since you don't understand those instructions well, it looks that these got changed.

Lets step back to instructions part, what these say

Instructions wrote:TripleString(String string1, String string2, String string3) -- a constructor that initializes all members according to the passed parameters. It has to make use of the private helper method validString() to be sure each String satisfies the class requirement for a member String. If any passed parameter does not pass the test, a default String should be stored in that member.


Since it's been a while, does that code makes sense to you lee?Could you add comments on each line of the TripleString constructor body to see what you think it does and re-post it here?

Same instructions above says you have to use private helper method validString().
What you got? Method becomes private when you add keyword private to its declaration as a first word. private boolean validString(....)

lee shotwell wrote:

What the line 3 does there? Please explain so we could understand your reasoning why you added it there.

See lee? It is important to read instructions and understand them. We see that you know Java syntax how construct methods, constructors, classes, but you're failing on logical part of exercise. You need to READ instructions AND UNDERSTAND them first before you move on.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston Gutkowski: Yes, I was "told to do so" in regards to the static members in the app.
Liutauras Vilda: const. w/ parameters
if(validString(string1)----------------If string is a valid string
{
this.string1 = string1;-------------I wanted to refer to the member string, rather than the local string.
}
else
{
this.string1 = DEFAULT_STRING;---------This does not make any sense at all.
}

Yes, I made a mistake by not putting private before validString().

What (does line 3 do?) The compiler told me that I had to declare and initialize String str = null; Generally, when I enter null, it is the "kiss of death" to my app. so I make sure something else takes its place. I made a bad choice. I have changed it to String str = string;
 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Instructions wrote:If any passed parameter does not pass the test, a default String should be stored in that member.


lee shotwell wrote:This does not make any sense at all.

Please explain why?
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LIUTAURAS VILDA: In regards to your last post concerning . The code is not written correctly. If I was after a hidden instance variable I should write and if I wanted a hidden static variable I would write the ClassName.StaticVariable. For example, . What I had written does not fall within either of those camps. Furthermore, I looked at another app. that I had written a few years ago, it did not have Public Class Static Constants declared to be final and I was able to set the data and mutate the data without any problems.


Here is the output.

All worked well, but I think the current app. I am working on has these static members and I do not know how to deal with them correctly.
 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lee shotwell wrote:LIUTAURAS VILDA: In regards to your last post concerning
The code is not written correctly. If I was after a hidden instance variable... and if I wanted a hidden static variable... few years ago, it did not have Public Class Static Constants declared to be final and I was able to set the data and mutate the data...


You didn't answer to my question. However, you're using very clever terminology in too many places, you don't need that in order to solve this problem.

In case you're still willing to solve this exercise, my suggestion would be, write down for yourself once again the instruction you have been given, go through this thread multiple times and carefully read the hints you have been given by all the contributors in this thread and write them down.

Don't use any of those clever words and don't try to make any assumption based per your guesses, that doesn't work - you need to follow instruction you have been given, don't improvise, just do what you have been asked to do, it is important.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic