• Post Reply Bookmark Topic Watch Topic
  • New Topic

member data acceptance  RSS feed

 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a new class named TrippleString. Then I entered the private member data. My compiler informs me that private String string1, private String string2, and private String string3 states: The field TripleString.string1 is never read locally. Same message for string2 and string3. I have fiddled around trying to get my toString() method to accept them, but toString() really turns into a mess. I think my output looked OK before I fiddled with it. However I would like to get rid of these warnings. The spec calls for these three strings to be its basic data. I need to get it right because I have to use this TrippleString class in another application. Here is my class.


Output

Any suggestions would be greatly appreciated.


 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are those three Strings supposed to be used?
 
Carey Brown
Bartender
Posts: 3022
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Where are those three Strings supposed to be used?

Do you want them to show up in toString()? Do you want them accessible via getters? What purpose do they serve?

The name "string1" is not descriptive.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:My compiler informs me that private String string1, private String string2, and private String string3 states: The field TripleString.string1 is never read locally. Same message for string2 and string3.
Worth to mention, that it is not a compile time error. It is a warning. And it means what it says, that you have declared those variables: string1, string2... but you never use them in your class (any setter or getter or any other method). That I'd consider as a smallest issue you're facing now.

1. What happened with code indentation in your second constructor? (the one with parameters). It seems you're missing curly braces everywhere.
2. The content of that constructor is most likely incorrect too. Never seen that before.

Don't know, probably it is not worth listing errors anymore till you get that bit fixed.

Tip: every class can have main method. That means, you can take this advantage and test each individual class units instantly within your class you write. How that would look? You add main method at the end of class, you add couple of fields in your class, you add 1 constructor, and you try to create an instance of that class. If no exceptions, no other errors - you add 1 setter and 1 getter method. Then you do the same, create an instance, set some value, trying to get that. If all as expected - moving onto another method. One by One.

Once you finish that - you remove main method and leave class on its own rights.

i.e.:
Don't move too fast. Think about every line well. When you reach your second constructor testing, you'll notice some weird things.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And since the class is named TripleString, what instance variables: name, age, gender, occupation have to do with that class?

Please explain a bit more about your project, so people could understand your actual intention, so the help would be more accurate
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your constructyor

never uses any of the arguments. Then it calls

Which has several problems. First, it creates a local variable called "name" that hides the instance variable.
Second, there is the redundant cast to String.
Third, getname() dereferences the instance variable "name" that hasn't been set yet, so will
cause a NullPointerException.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . Third, getname() dereferences the instance variable "name" that hasn't been set yet, so will cause a NullPointerException.
No it doesn't. It does not dereference name but “this”. I will not cause an exception to be thrown, but will return null and assign that to name. In fact it is equivalent to
name = this.name;

[added later]That statement does nothing useful, as FK implies.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getName calls toString on name, so it will cause a null pointer exception.
Of course it should simply return name.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:getName calls toString on name, so it will cause a null pointer exception.
Of course it should simply return name.

A prime example of a violation of the Principle of Least Astonishment

BTW, "POLA" is very similar to the word "pula" which in my native tongue means "red" as will be faces when your code doesn't do what one would normally expect it to do. Violating POLA is not a victimless crime.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once TripleString() is defined, it is to be used to insantiate TripleString objects that will be used in my main() method. The main() is to instantiate four or more TripleString objects. The objects I selected are string name, int age, char gender, and string occupation. Some should use the default constructor and some using the constructor that has parameters. Next, immediately display all objects. Mutate one or more members of every object. Display all objects a second time. After that, do two explicite mutator tests for each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails. Make two accessor calls to demonstrate that they work. No user input should be done in this app. Later, class TrippleString will be used to instantiate TripleString objects that can be used in a main() method and/or the static methods that main() invokes to simulate a slot machine project. Here is my main(). It seems to do all of the above tests OK. At any rate, I would like to get rid of those warnings in my member data section. Thanks for all of your feedback. The comments are helpful.
 
Junilu Lacar
Sheriff
Posts: 11169
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
I'm just trying to understand your thinking but it seems very strange. What you're doing when you use the name "TripleString" is kind of like calling a Person a "DoubleEye" because a person normally has two eyes.

It's weird how you have these three variables that you simply call "string1, string2, and string3". Looking more closely at the code around it, in fact, these "TripleString" things seem to each represent a person. Each of them has a name, age, and gender. That sure looks like a Person to me so why not just change the name from "TripleString" to "Person" then?

What you're doing with these names is just confusing and makes my brain do this
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar: I used TripleString (string1, string2, string3) because the spec. said, "...create a class called TripleString. TripleString will consist of three private member Strings as its basic data: string1, string2, and string3...." The spec. wanted me to instantiate some objects (at least four) to test various aspects of the app. I picked these objects because I wanted some variety to test (string, int, char). I could have picked a duck or a donkey. I was free to select the objects I wanted to use. I hope the above clears up some of the confusion. I value your input and would appreciate further suggestions from you. THANKS!
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:The spec. wanted me to instantiate some objects (at least four) to test various aspects of the app. I picked these objects because I wanted some variety to test (string, int, char). I could have picked a duck or a donkey. I was free to select the objects I wanted to use. I hope the above clears up some of the confusion. I value your input and would appreciate further suggestions from you.

OK, well mine is: Show us the exact instructions you were given, so we don't go off at tangents.

The class you're creating makes no sense to a real-world programmer, but may be being used to teach you something about the language, so rather than us trying to guess why you've done something, show us the instructions you were given so we're in no doubt.

Cheers.

Winston
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To WINSTON GUTKOWSKI: Hi Winston, you helped me on a dating app a couple of years. Good to hear from you again. What follows are the instructions.

The assignment is to first create a class called TripleString. TripleString will consist of three private member Strings as its basic data. There will be a few instance methods to support that data.

Once defined, we will use it to instantiate TripleString objects that can be used in our main() method. In a later assignment, the TripleString class will help us create a more involved application.

TripleString will contain three private member Strings as its main data: string1, string2, and string3. We will also add a few public static members such as final int MAX_LEN and MIN_LEN. These represent the maximum and minimum length that our class will allow any of its strings to be set to. We can use these static members in the TripleString method whose job it is to test for valid strings (see below).

The Program Spec.

Class TripleString Spec
Private Class Instance Members:

String string1
String string2
String string3

All legal strings should be between 1 and 50 characters.

As stated in the modules, we never want to see a literal in our methods. So the class should have static members to hold values for the limits described above, as well as default values for any field that is constructed using illegal arguments from the client. These are put in the public static section. Public Class Static Constants (declare to be final):

MAX_LEN = 50

DEFAULT_STRING = " (undefined) "

Public Instance Methods
Default Constructor

TripleString() -- a default constructor that initializes all members to DEFAULT_STRING.

Parameter-Taking Constructor

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.

Mutators/Accessors

set()s and get()s for these members. Mutators in our course are named using the convention as follows: setString1( ... ), setString3( ... ), etc. Likewise with accessors: getString2(). We need an accessor and mutator for each injdividual String member, so three pairs of methods in this category. When a mutator detects an invalid String, no action should be taken. The mutator returns false and the existing String remains in that member. Not a new default String.

String toString() - a method that returns a String which contains all the information (three strings) of the TripleString object. This String can be in any format as long as it is understandable and clearly formatted.

Private Static Helper Methods

boolean validString(String str) -- a helper function that the mutators can use to determine whether a String is legal. This method returns true if the string is not null and its length is between MIN_LEN and MAX_LEN (inclusive). It returns false otherwise.

Where it all goes

TripleString should be a class distinct from (and not contained within) your main() method (which we call Foothill). You can create the TripleString class as a non (word too blurry to read) class directly in your client Foothill.java file. This makes it a sibling class, capable of being used by any other classes in the file (of which there happens to be only one: Foothill.

You type it directly into that file; do not ask Eclipse to create a new class for you or it will generate a second.java file which we don't want right now. In other words, Foothill.java will look like this:

import java.util.*;
import java.lang.Math;

public class Foothill
{
//main class stuff
}

class TripleString
{
//TripleString class stuff
}

As you can see, TripleString is to be defined after, not within, the Foothill class definition. This makes it a sibling class, capable of being used by any other classes in the file (of which there happens to be only one: Foothill).

The Foothill main()

Instantiate four or more TripleString objects. Some of them using the default constructor and some using the constructor that takes parameters.
Immediately display all objects.
Mutate one or more members of every object.
Display all objects a second time.
Do two explicit mutator tests for each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails.
Make two accessor calls to demonstrate that they work.

More:

Be sure that all output is descriptive. Use labels to differentiate your output sections and full sentences in your mutator/accessor tests.
No user input should be done in this program.

I am not supplying a sample output this week -- the above description is adequate.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting your code after incorporating the suggestions above would be a good thing.

As stated before, you are not using string1, string2, or string3 in your code, not in any constructor or method. This is why you are getting the warning.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code originally posted does not meet the requirements given, IMO. It seems to me that the given requirements need to be interpreted very literally. You have interpreted them as "guidelines" and added your own semantics to string1, string2, and string3 by taking them to fields that represent name, age, and gender. There is nothing in your given requirements that says anything about name, age, or gender. You're supposed to have only the string1, string2, and string3 fields, and getters/setters for each of these three fields.

Also, it seems to me that you misinterpreted this part: Do two explicit mutator tests for each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails.

The if-statement and the printing of the message should be OUTSIDE any of the TripleString methods, not as part of them.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:Default Constructor
TripleString() -- a default constructor that initializes all members to DEFAULT_STRING.
Parameter-Taking Constructor
The requirements has some inaccuracy, as some stated above for instance. Keep in mind for the future, that based on such requirements you can't have default constructor. You can have at best "no argument" and "with parameters" constructor. Default constructor being provided by compiler only if there is no user defined constructor (ok, I could sound as being too picky, but that is how actually is). Since you were asked to provide one constructor with some parameters, the other could be no argument constructor (created also by you, not a compiler anymore), but it is no longer called default. I am telling you this in case you'd be up to go for a some certain certifications, so you wouldn't slip there

Now back to reality. As you see, and probably noticed yourself, you created excessive amount of instance variables you were not asked for. You should be having only 3 instance variables which should be named "string1", "string2", "string3". That is it. Also you should have some constants. The note to take is, that you need to create exactly what you are being asked for, no more, no less.

Keep your already written class as a reference and start over following exact requirements. Many parts you got correct
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JUNILU LACAR & LUTAURAS VILDA: In the instructions I posted there is a section called The Foothill main() wherein it asks me to instantiate four or more TripleString objects. I selected, the objects name, age, gender, and occupation. Since that is not correct, what TripleString objects ( four or more) should I be instantiating? I am confused.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:JUNILU LACAR & LUTAURAS VILDA: In the instructions I posted there is a section called The Foothill main() wherein it asks me to instantiate four or more TripleString objects. I selected, the objects name, age, gender, and occupation. Since that is not correct, what TripleString objects ( four or more) should I be instantiating? I am confused.

Of course you're confused. The names in the "requirements" don't make much sense. You're confusing yourself even more by doing this:

You are using the same names for the variables in your main method as the names of the fields in the TripleString class. Imagine walking around using only "thingA", "thingB", and "thingC" to talk about anything. You'd end up saying things like "Let me put on my thingA before we get into the thingB", "Let's watch thingC and then we can eat some thingA and drink some thingB, or maybe thingC." That's kind of ridiculous, right? That's not very different from what you've written in your program though.

Use meaningful names so that your code makes sense. I think that's why you are really wanting to use the name, age, and gender names so you can make sense of it, too. Talk to your instructor and tell him that the names "string1, string2, string3" don't make sense. If you can't do that, then use different names in your main method, like maybe "triple1, triple2, triple3" -- that's still quite ridiculous but maybe it will help you clear your thoughts long enough to think of even better names.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:JUNILU LACAR & LUTAURAS VILDA: In the instructions I posted there is a section called The Foothill main() wherein it asks me to instantiate four or more TripleString objects. I selected, the objects name, age, gender, and occupation. Since that is not correct, what TripleString objects ( four or more) should I be instantiating? I am confused.

I have printed out those instructions and started crossing off with pencil what is not on requirements. Charlotte even offered me coloured markers to make it easier, but I decided to stay with pencil. The important parts I squared and wrote numbers above them, so would be clear what is needed.
Apart from the poorly written instructions, here is below what I found..

  • Create one of two classes. Both classes needs to be defined within the same .java file (one public class Foothill and one non-public TripleString). First class:
  • 1. Class which is called TripleString
    2. Three instance variables in it: string1, string2, string3
    3. Three constants: MAX_LEN, MIN_LEN, DEFAULT_STRING
    4. No argument constructor
    5. Constructor with 3 parameters (strings)
    5. Methods: setString1, setString2, setString3, getString1, getString2, getString3, toString, validString(<- static).

  • Second class to create:
  • 1. Class which is called Foothill
    2. Instantiate 4 or more TrippleString objects. Instance variable names could be chosen any, since class TrippleString are not that clear, you can use names for object instance as "threeHeadedDragon1", "threeHeadedDragon2", that would fit to whole context.
    3. Some of those 4 has to be created with no-argument constructor, some of them with constructor which takes parameters.
    4. Once objects created, print their content out (remember, toString method needs to be implemented in TrippleString class).
    5. Use setString1, setString2, setString3 methods in order to change the state of created objects.
    6. Repeat step 4.
    7. Try to use setString1, setString2... with invalid inputs to prove or negate correctness of method, where validString should do all the job (it should be used in setString* methods). All that 7 part should be demonstrated in if/else statements.

    Any methods, which are called setAge(..), setGender(..) or similar should be removed based per my understanding. You can use setString("male"), setString("20") to see if they are valid inputs or not.
     
    Campbell Ritchie
    Marshal
    Posts: 55793
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:. . .
    4. No argument constructor
    5. Constructor with 3 parameters (strings)
    . . .
    I have seen that sort of thing before. A class with three fields should have all three initialised in its constructor. A no‑arguments constructor is at variance with that principle. Unfortunately if that sort of thing appears in the assignment instructions, you are stuck with it
     
    Junilu Lacar
    Sheriff
    Posts: 11169
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Liutauras Vilda wrote:. . .
    4. No argument constructor
    5. Constructor with 3 parameters (strings)
    . . .
    I have seen that sort of thing before. A class with three fields should have all three initialised in its constructor. A no‑arguments constructor is at variance with that principle. Unfortunately if that sort of thing appears in the assignment instructions, you are stuck with it

    Semantics of "default constructor" aside, the instructions do specify this:

    TripleString() -- a default constructor that initializes all members to DEFAULT_STRING.

    Overall though, I think we all agree that this is a poorly thought-out exercise to be giving students.
     
    lee shotwell
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    LIUTAURAS VILDA: I rewrote my app., but I still have issues.
    1. The member data (str1, str2, str3) are still not read locally.
    2. I can load three strings, but not four.The instructions call for four or more. I fiddled around with the app. But no luck. I am sure there is a way to do it.
    3. When I test one of the strings it comes back null, it should read DEFAULT_STRING.
    4. When I load the constructor with parameters the if/else blocks are not tested and I get DEFAULT_STRING in the three strings.





    output


    Now, when I set the data in the strings all is OK except in str3 where I tested it, I got null instead of (undefined).


    output


    Any suggestions would be greatly appreciated. Thanks also for your most recent comments. They were very helpful.


     
    Liutauras Vilda
    Marshal
    Posts: 4670
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is exemplary how you're 100% devoted in solving this, but you gone a bit too fast and too far. You should have posted your code after you got constructor implemented. Don't rush

    Requirements 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.

    Requirements wrote:All legal strings should be between 1 and 50 characters.

    Requirements wrote:MAX_LEN and MIN_LEN...use these static members in the TripleString method whose job it is to test for valid strings...


    Please read carefully bold parts from your requirements and compare with your each line in constructor. Please tell us, what do you think you're missing or doing not quite correct?
     
    Campbell Ritchie
    Marshal
    Posts: 55793
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The constructor on line 52 looks very confused, and I am sure it doesn't do what you think it does.

    It is no good “rewrit[ing]” things, or “fiddl[ing] around with the app”. You can do that for millions of times and, most probably, one of those million will be correct. Or you can plan a strategy and design and get it to work first time. That constructor I mentioned looks as though you had been guessing. There are also a few minor style points which you might do well to correct:-
  • 1: Don't use \n. Use printf and use the %n tag for new lines. Use an additional %n at the end of your format String.
  • 2: In the constructor I mentioned you have if statements without braces. Either add {} or convert them to ?: expressions.

  • I am afraid you will probably find it easier to start from scratch. Give your variables names which say what they mean; Liutauras has told you several times that str1 is not a good name. When you have good names, “expose” those names by using them as constructor parameters, and disambiguate them from your fields with the this. construct. Make sure that all your fields are initialised to sensible values before your constructors complete. Write as few constructors as you can get away with, but be sure to write a constructor in every class you create.Don't mess around with default values or error messages if you get incorrect input. Throw Exceptions. The Objects method will throw an Exception is you pass it null. Read about it in the documentation, and read it carefully because last time I quoted that method I made a mistake writing it down and the code wouldn't compile.
     
    Liutauras Vilda
    Marshal
    Posts: 4670
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also check your line 55. Surely it is not how it should be.
    You assign myString1 to null (because string is your instance variable which is null initially).

    That line 55 not needed at all. You should check: str1, str2 and str3 for validity and in case they are valid, assign accordingly:
    this.str1 = str1;
    this.str2 = str2;
    ...

    In case they are not valid, assign to DEFAULT_STRING. This is what requirements says, right?
    Be super careful when reading requirements, it has to be 100% accurate as been specified.

    Print out instructions, get some colored markers, some people find them handy. Color important parts and check your code against them if it does what it asks for.
     
    Campbell Ritchie
    Marshal
    Posts: 55793
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have found you can assign make like this:-
    this.make = Objects.requireNotNull(make);
    It is all in the documentation link I gave you earlier.
     
    Liutauras Vilda
    Marshal
    Posts: 4670
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look, lee, you're doing well, you're quite close to.

    1. Remove that variable int length from line 29 and variable String string from line 40 - these are not needed, nowhere specified in instructions, so shouldn't be used, just delete.
    2. You got method isValid(), check if it is correct, and use it in constructor with 3 parameters in the same way you're using it in set... methods.
    3. If valid, assign instance variable to passed argument variable, in case it is not valid, assign to DEFAULT_STRING.

    Once you done with constructors, go onto setters and getters.
     
    Junilu Lacar
    Sheriff
    Posts: 11169
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    It is no good “rewrit[ing]” things, or “fiddl[ing] around with the app”. You can do that for millions of times and, most probably, one of those million will be correct. Or you can plan a strategy and design and get it to work first time. That constructor I mentioned looks as though you had been guessing.

    Unfortunately, in real-world software development, rewriting programs generally turns out to be a poor choice and often is a bad choice. Just randomly fiddling around with the code an design is not good either. Yes do need a plan and an idea of a design that would be better. This is where the skill of being able to refactor separates the men (/women) from the boys (/girls). This hints at a few keys to being able to refactor effectively: 1 - being able to recognize poor designs (code/design smells), 2 - knowing what a better design would be, and 3 - how to get to that better design through small, incremental changes.

    For the constructor in question, there are a few things that OP probably does not know about instance members and constructors:

    1. Instance members, which is what str1, str2, and str3 are, will always be given a default value if none is explicitly specified. For object references, the default value upon instantiation is null.

    2. An instance member can be assigned an initial value where it is declared. For example, in the code below, the field bar will have an initial value of "(none)" instead of null when you do this:

    You can take advantage of this mechanism to simplify the code in both the constructors.
     
    Junilu Lacar
    Sheriff
    Posts: 11169
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good job formatting your code properly. However, you need to be careful to use braces and not let your indentation fool you. The code below does not behave exactly the way the indentation appears to imply it behaves.

    Lines 89 and 90 are not bound to the else clause. That is, the above code is syntactically equivalent to this:

    and NOT this:

    In practical terms, you got lucky in that there's no difference in the end result of what the method does. However, this is just by pure luck. In many other situations, this kind of error can lead to a logic bug that is very difficult to find because your indentation is misleading you.
     
    Liutauras Vilda
    Marshal
    Posts: 4670
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    lee shotwell wrote:2. I can load three strings, but not four.The instructions call for four or more.
    You misinterpreting that part.
    Instructions wrote:Instantiate four or more TripleString objects.
    That means:

    But the amount of passed arguments never be more than 3. You have been asked to create 2 constructors: no argument, and with 3 parameters. So you have 2 options only when creating an instance:
     
    Junilu Lacar
    Sheriff
    Posts: 11169
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I already pointed this out before but I'll reiterate since you still haven't corrected it. The code below and others like it do not correctly satisfy one of your requirements.

    The relevant requirement:
    The Foothill main()
    ...
    Do two explicit mutator tests for each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails.

    The if/else statement(s) should be in the main() method, not in the setter methods.

    There's a common coding pattern that applies to this: the Guard Clause

    Basically, if R is some return type, maybe an int or boolean or whatever, and T is also a type:

    R could also be void, in which case you'd just have this:

     
    lee shotwell
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have had difficulty creating the two mutator tests for each in the main() by calling a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails. Then make two "accessor calls" to demonstrate that they work. Here you will see the method which I created in my main(), but I cannot seem to make it 100% correct so I can test it. In fact, I have written several methods and none were 100% correct.
    When the instructor says to use a mutator, he means a setter and when he talks about an accessor he is refering to getters. So the method I wrote is using a setter. I am in a deep hole. Here is my main(). Any help would be greatly appreciated.


     
    Knute Snortum
    Sheriff
    Posts: 4091
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have a method (or something) inside main() that starts boolean String setStr1. It's in the wrong place. You can't have methods inside methods.
     
    Junilu Lacar
    Sheriff
    Posts: 11169
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, Lee, you are in a big hole and that seems to be because you keep writing code without a good understanding of what you're writing.

    For instance, this is totally wrong:

    printf() does not work the same way as println(). Your code is what one might expect to see with println(). Google for examples of System.out.printf() and understand how to use it.

    What are you trying to do on line 28 - 42? That looks like a method but it's inside the main() method. Does that code even compile? Google for how to write methods in Java and understand that concept.

    Also, I can't make sense of what you're doing on lines 12 - 19. It seems to me that you're confused as to what you're saying here as well.

    Are you aware that:
    Line 12 and 17 affect the same object, str1?
    Line 13 and 18 affect the same object, str2?
    Line 14 and 19 affect the same object, str3?

    You have been told many times before that the variable names you are using in main() will just make your code confusing because you are using very similar names in the TripleString class.

    Line 12 declares the str1 variable as a reference to a TripleString object, then makes it reference a new instance of TripleString.
    Line 17 then sets the str1 field of the TripleString object referenced by the variable str1 to the value "male".

    Very similar things are happening on Lines 13 & 18 and Lines 14 & 19:

    Line 13 declares the str2 variable as a reference to a TripleString object, then makes it reference a new instance of TripleString.
    Line 18 then sets the str2 field of the TripleString object referenced by the variable str2 to the value "snake".

    Line 14 declares the str3 variable as a reference to a TripleString object, then makes it reference a new instance of TripleString, with its fields str1, str2, str3 initialized to "1", "duck", and "rabbit", respectively.
    Line 19 then sets the str3 field of the TripleString object referenced by the variable str3 to the value "buick".

    All of this can be very confusing even for an experienced programmer. I doubt this what you actually meant to do though.

    Describe the three objects referenced by str1, str2, st3 to us. Then we'll know for sure what you do or do not understand. That is, tell us your understanding of the following:

    Regarding the variable str1 declared on line 9:
    1. What kind of object does this variable reference?
    2. What are that object's fields and their values when line 22 is executed?

    Regarding the variable str2 declared on line 9:
    1. What kind of object does this variable reference?
    2. What are that object's fields and their values when line 22 is executed?

    Regarding the variable str3 declared on line 9:
    1. What kind of object does this variable reference?
    2. What are that object's fields and their values when line 22 is executed?
     
    Liutauras Vilda
    Marshal
    Posts: 4670
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    lee, I want to point out something from me too (it is similar to what Junilu said once again).

    Reference variables you chose str1, str2, str3 confuses you.

    Remember, that each TripleString instance has 3 instance variables no matter how many of them you initialize/not initialize explicitly within constructor.

    str3 you could change to

    when you writeThat means you're changing "Language". You can also change "Object", by writing
    The question is, is it what you want? I don't know. But it is almost for sure, that you somehow in your head connecting those str1, str2, str3 reference variables with respective method names, while these are not connected at all. That is probably because you pushed yourself into the corner with poorly chosen reference variable names. Change them to something different.

    Note it won't affect instance variable names in TripleString class, because these needs to be str1, str2, str3 - this is what your instructions ask.

    Now again, why have you got that setStr1 (line 28, line 51) method declared in your Foothill class? It has to be in TripleString class as per requirements. Same with validString (line 45) and why you chose such parameter type there, Object

    Have you tried to discuss with your instructor in case that you find this task too challenging? Maybe he'd advice to work in group with other fellow students, so you could discuss and think in group how you understand this task and try to accomplish it together.

    We want to help, but it is difficult with current code you have. You most likely need to start over again. I'd recommend once again. Take by very small amount of requirements, 1 line or 2 of them and try to replace in Java code. Once you do that, lets look if you got it right, and only then you move forward. Currently when you post lots of code which is almost all incorrect, very difficult to give you a hint, as hint basically would sound - all not good, need to start over. That can demotivate you fairly quickly. We don't want that

    When is your assignment due?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!