• 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

Triangle exercise error and I don't know what it is.

 
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm on Page 65 of the Heads First Java book.  
I'm doing the pool puzzle.
I forgot my high school geometry formulas and really couldn't even guess at how to do this exercise.   I typed in the code in the book for the answer and I get an error.
There is a good chance that I made a typo.  
I looked at the code on the answers online at O'reily.   I could cut and paste their code to see if it works but if it does it still won't teach me what I typed wrong.

I'm also confused that the Triangle class is calling itself.  I have seen this done before but I don't understand it.  Is this something like a constructor?

I attached the error messages.



Triangle-error.png
[Thumbnail for Triangle-error.png]
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"setArea()" is not "static" therefore it is a method that is a member of the Triangle class therefore you need a Triangle object when you call the method. Hint: you have an array of 4 Triangle objects.

[edit] Also setArea() does not return anything.
 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 7, 10 and 19 would be better if replaced by a for loop.
for loops are often used to iterate through an array's contents

The setArea method should take the length and height as parameters and return the area.
If changed as above a better name for the method would be computeArea

The Triangle class should have a constructor that takes the length and height and computes the area using the above method.
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:better name for the method would be computeArea

 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey and Norm,

The code for the answer does not seem correct in the book or the online answers located here:

https://resources.oreilly.com/examples/0636920034452/-/commit/5342a0995b6ce893e16a125846a242ef6dd2da5e

Maybe the book answer is wrong and then the online answer fixed it but I don't know how to use the answer on line.  It looks like something that shows a history change but I don't know which is the most recent version.  Also all the versions don't look right to me.

I'd rather use a for loop but I'm trying to use what the book taught me so far and it didn't mention the for loop yet.

I see the height and length declared as int at the top of the Triangle class.   Then I don't see these values changing in the main method.   I agree that setArea is not a meaningful method name.  

I see area set as a double at the top of the Triangle class.   Then I see it inside the setArea method.  If the code was working should the main class be able to see the value of area even though the return value of the method is void?

The writing in the book and diagrams are fun but I don't feel comfortable when the author is not giving an answer that is correct.   Maybe they are correct and I don't know better?

Kevin



 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:should the main class be able to see the value of (member variable) area (?)


Yes.

even though the return value of the method is void?


main() cannot see any return value from the method because there is none.
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:Carey and Norm,

The code for the answer does not seem correct in the book or the online answers located here:

https://resources.oreilly.com/examples/0636920034452/-/commit/5342a0995b6ce893e16a125846a242ef6dd2da5e

Maybe the book answer is wrong and then the online answer fixed it but I don't know how to use the answer on line.  It looks like something that shows a history change but I don't know which is the most recent version.  Also all the versions don't look right to me.


I don't know how you got to that link. I gave you a link in one of your other threads and all you needed to do was follow: src -> main -> java -> Ch3

Which takes you to https://resources.oreilly.com/examples/0636920034452/-/tree/main/src/main/java/ch3
Which DOES NOT have the history of the Triangle.java file.
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is the code from the link.  I ran it and it works.  I can't see why my code does not work and I don't have beyond compare.  There was a way to compare code in eclipse but I don't know how to do it in Intellij yet.

If you see my error please explain it to me.



THis is my code that does not work.  I will compare line by line for a few minutes.




 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are two completely separate exercises.

Yours is Triangle.java which you can find at the link above.

[edit] Pausing while OP edits his old <sigh> post.
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

Sorry.

I noticed that I pasted two different exercises after pressing enter.  So I made a comment that I was in the process of fixing my post. Once I made the error I don't know what else I could have done.

I got the link to the code from an email from O'Reily.  

I need to go back through the threads to find your link which was better.  

Kevin
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I found your link:


https://resources.oreilly.com/examples/0636920034452

Is there a way to save favorite posts in the forum?  I thought I saw an option for this.

Thanks,

Kevin
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might try bookmarking the thread.
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I forgot about the bookmark feature in CodeRanch.  

 I just tried it again and it will be useful.  

Kevin

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ta[x] = setArea();

ta[x].setArea();
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic