• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor return value code help  RSS feed

 
Karen Freeman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code for an assignment is not working the way I want it to. Here it is:

The question asked us to make a constructor and gave us two of the three side values for the triangles. I created a constructor and assigned it names of the three sides. Then I inserted the two values we were given into the constructor when I made the new Triangle (newTriangle1 and newTriangle2). I'm trying to access the getB() method and get the program to work on finding the missing value/side and print them out but the program doesn't display any value for side B. Any suggestions would be VERY appreciated. I am a complete newbie so I'm sorry if I am doing something stupid.. I just don't know any better.. :-) Thank you!!
 
Steve Kedzie
Greenhorn
Posts: 22
Mac Netbeans IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a good look at your constructor for starters, particularly the assignment statements. Please edit your post with code tags so that we can read it much easier.

Steve
 
Pallavi Sadit
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karen,
You want to initialize the private variables when you are creating an object , but are you actually doing that in your constructor? Take a close look at the constructor to see what you are actually doing there. When this statement will be executed :" Triangle newTriangle1 = new Triangle(48.00, 0, 80.00); " which variables will be assigned the values 48.00,0 and 80.00. Are you using those variables anywhere? Which variables should actually been assigned these values instead? Please try answering these questions and you will get to the root of the problem.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Karen, Welcome to the Ranch!

As Steve suggested, using code tags when posting your code makes it much much easier to read and therefore makes it more likely that folks will stick around to read it and help you out. I've added them for you this time and I'm sure you'll agree that it looks much better now. You can read about using code tags here -> UseCodeTags

Cheers
Tim (with his Moderator hat on)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I think you have fallen in to the trap of “These things are a lot simpler than you think.”
I suggest you have a look at the Java Tutorials and look at the example there. I would say that although it is not compulsory under the rules of the grammar to provide a constructor, as a good design principle you should write a constructor for every class you write. There are two bits of syntax I shall tell you about, which you are probably not familiar with.

Why are you using Pythagoras in the getA method? There is no evidence that you have a rightangled triangle, so Pythagoras won't work. Also use x * x in preference to Math.pow(x, 2) for squares. You will find the getA method much simpler than you think. I think you need to slow down. One thing at a time. If you try too much all at once, you will find nothing works and you end up srting out half of one problem and half of the other and things will look even worse. So leave out the getA method alone until you have got the constructors sorted out.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get your three‑paremeter constructor working first. I shall explain “this.i” later, so you may have to read these posts in reverse order.
Earlier, I wrote: . . . There are two bits of syntax I shall tell you about, which you are probably not familiar with.
. . .
Both of them involve the keyword this.

If you do not write a call to another constructor at the beginning of your constructor, the compiler will add it for you. Usually what happens is that a constructor like this:-… is treated by the compiler as if you had written this:-Those two constructors would produce exactly the same bytecode output.
The super() call tells the JVM to look for the superclass' constructor, which in this case means this constructor. The JVM calls that constructor, too, when it initialises you object.
Now, you can call another constructor instead if you wish. You might say super(i); if you have a superclass' constructor which can take i as an argument. But in this case you don't have such a constructor you can call, so you can't. But what you can do (as the Java Tutorials link tells you), is have several constructors in the same class. Now you can call one constructor from another. And you do it with the keyword this and round brackets/parentheses. Like this();

If you write super(...); or this(...); that bit has to be the very first part of the constructor, so you can only ever fit one of those things in a constructor.

What this(...); means is, “Look for another constructor in the same class which can take those arguments, call it, then come back here.” You can write a constructor which contains this(...); and nothing else. You can write a constructor in my Foo class like this:-What you are doing is saying, “If nobody wishes to pass any values, I'll permit that and provide default values 3, 4 and 5.”

Obviously you can have a constructor with two parameters which calls a three‑parameter constructor and copies the values of two of the parameters and provides the value of the third, too. You can include a formula in a this(...); call.In that case I would set k to half the product of i and j. You can even have a method call… and use that √ for k.
I would draw your attention to methods of the Math class. Have close look at its methods, especially those beginning with H‑Y (hint! hint!). I don't think I should say any more.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, you want to know about declarations. You should never declare anything in a constructor. If you say String s = ...;, that is a declaration and you are declaring s as a local variable. You don't usually want local variables in constructors. They get their values, get to the end of the constructor, go out of scope and vanish in to a sort of cyber‑limbo never to be seen again. So my s vanishes, which is bad. If there is a field called s, the compiler will ignore that, which is worse; it maintains it “default” value, which in the case of a reference type is null, and that is potentially dangerous. [A primitive type as a field has a default value of “zero”.]

Now parameters behave a bit like local variables. In my previous Foo constructor, I had i j k as parameters, so they are available for use throughout the constructor and then vanish the way local variables do. But I had fields called i j k too. How do you get your hands on those fields when there are local variables with the same names?
Answer: with the keyword this. If I wrote this.i = i; you read that as meaning, “The field i becomes equal to the parameter i.” The field (with this.) goes on the left and the parameter (without this.) goes on the right. So:
this
then a dot
then name of field
then assignment operator =
then name of parameter
then semicolon ;
And that will work.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of having local variables or parameters with the same names as fields. The construct this.i = i; is so popular in constructors and setI methods, however, that most people consider it good style.

Now, get your 3‑arguments constructor working, and comment out all the bits about Pythagoras. You can consider un‑commenting code later when you have got the 3‑arguments constructor working. Also comment out your isRight method because I don't think that will work.
You know about commenting out? You write // at the very beginning of the line and it turns the line into a comment. If you need to comment out a whole method you have to write // at the beginning of every line in it. Don't use /*…*/ for commenting out; it may not work correctly.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . You should never . . .
What never??
Well, hardly ever.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!