17 Guests viewing this page
Hidden 7 yrs ago 7 yrs ago Post by GalaxyRaider
Raw
Avatar of GalaxyRaider

GalaxyRaider Isaac

Member Seen 4 mos ago

Time for Please Help Me Find Out What's Wrong With My Code Because It Is Killing Me 2!
If you don't mind it.
I'm mainly concerned with Shape and Rectangle right now. If I get those, I'll be fine. I get no errors when running this, but my values for the Area and Perimeter are always 0.0 no matter what I do.



Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

Time for Please Help Me Find Out What's Wrong With My Code Because It Is Killing Me 2!
If you don't mind it.
I'm mainly concerned with Shape and Rectangle right now. If I get those, I'll be fine. I get no errors when running this, but my values for the Area and Perimeter are always 0.0 no matter what I do.





I'll get you answers as soon as I'm out of class.
Hidden 7 yrs ago Post by Heroic
Raw
Avatar of Heroic

Heroic Zoey

Member Seen 11 days ago

<Snipped quote by GalaxyRaider>

I'll get you answers as soon as I'm out of class.


It'll be much appreciated.
Hidden 7 yrs ago Post by Meta
Raw
Avatar of Meta

Meta Lily

Member Seen 19 hrs ago

<Snipped quote by Legend>

It'll be much appreciated.


Unless you need them right away, of course.
Hidden 7 yrs ago Post by GalaxyRaider
Raw
Avatar of GalaxyRaider

GalaxyRaider Isaac

Member Seen 4 mos ago

<Snipped quote by Heroic>

Unless you need them right away, of course.


Nah. It's due in 2 and a half hours, but once I see where the mistake is, I should be able to fix it in a snap.
Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

Time for Please Help Me Find Out What's Wrong With My Code Because It Is Killing Me 2!
If you don't mind it.
I'm mainly concerned with Shape and Rectangle right now. If I get those, I'll be fine. I get no errors when running this, but my values for the Area and Perimeter are always 0.0 no matter what I do.





Did you not import or initialize the scanner in Circle or Square?
Hidden 7 yrs ago Post by DarkwolfX37
Raw
GM
Avatar of DarkwolfX37

DarkwolfX37 Absolute L User

Member Seen 14 days ago

Time for Please Help Me Find Out What's Wrong With My Code Because It Is Killing Me 2!
If you don't mind it.
I'm mainly concerned with Shape and Rectangle right now. If I get those, I'll be fine. I get no errors when running this, but my values for the Area and Perimeter are always 0.0 no matter what I do.





Well for one, you might be better off getting the length and width in the same place.
Secondly, this might be a different thing with java than I learned, but personally I'd put "width * 2" and "length * 2" instead of "2*width" and "2*length". Not really a problem per say unless java has a hard time with the way you put it.
Third, you're defining length and width as 0. Instead of "templength" and "tempwidth" I'd suggest creating new variables and pulling those instead of trying to apply them back to length and width.
Fourth, it could be an issue of order. Having Rectangle(double length, double width) after defining them as zero could be the problem. Again, I suggest new variables entirely than ones you've already used.
But since I'm not much for Java I bet none of this is applicable.

Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

Your static methods can't access the members of a class; they can only access static variables. You need to make your methods public but not static so they can be used by instances of the class. Also, the keyword this is not required, but feel free to use it if you want.
Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

<Snipped quote by GalaxyRaider>

Well for one, you might be better off getting the length and width in the same place.
Secondly, this might be a different thing with java than I learned, but personally I'd put "width * 2" and "length * 2" instead of "2*width" and "2*length". Not really a problem per say unless java has a hard time with the way you put it.
Third, you're defining length and width as 0. Instead of "templength" and "tempwidth" I'd suggest creating new variables and pulling those instead of trying to apply them back to length and width.
Fourth, it could be an issue of order. Having Rectangle(double length, double width) after defining them as zero could be the problem. Again, I suggest new variables entirely than ones you've already used.
But since I'm not much for Java I bet none of this is applicable.


It's because static methods. They have to be instance methods because otherwise, the class looks at static variables length and width. I mean, you could technically pass this into the static method as a parameter, but that's super convoluted. Otherwise, the static method has absolutely no way to see instances of your class.
Hidden 7 yrs ago Post by DarkwolfX37
Raw
GM
Avatar of DarkwolfX37

DarkwolfX37 Absolute L User

Member Seen 14 days ago

<Snipped quote by DarkwolfX37>

It's because static methods. They have to be instance methods because otherwise, the class looks at static variables length and width. I mean, you could technically pass this into the static method as a parameter, but that's super convoluted. Otherwise, the static method has absolutely no way to see instances of your class.


Ah, that does make more sense. I figured making new variables would work better, but I totally missed that they were static in the first place.
Geez I always feel like I'm back in class when you come up with a much simpler solution. Bad memories of making a long bit of code for something there was a function in the language for. :/
Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

<Snipped quote by Legend>

Ah, that does make more sense. I figured making new variables would work better, but I totally missed that they were static in the first place.
Geez I always feel like I'm back in class when you come up with a much simpler solution. Bad memories of making a long bit of code for something there was a function in the language for. :/


Yeah, but also analyzing other peoples' code is difficult. The problem is easy to miss when you aren't familiar with the internals.
Hidden 7 yrs ago Post by DarkwolfX37
Raw
GM
Avatar of DarkwolfX37

DarkwolfX37 Absolute L User

Member Seen 14 days ago

<Snipped quote by DarkwolfX37>

Yeah, but also analyzing other peoples' code is difficult. The problem is easy to miss when you aren't familiar with the internals.


True, but I prided myself on helping the people next to me when they had trouble so it hurts double that I can't with this.
Hidden 7 yrs ago 7 yrs ago Post by GalaxyRaider
Raw
Avatar of GalaxyRaider

GalaxyRaider Isaac

Member Seen 4 mos ago

<Snipped quote by GalaxyRaider>

Did you not import or initialize the scanner in Circle or Square?


Your static methods can't access the members of a class; they can only access static variables. You need to make your methods public but not static so they can be used by instances of the class. Also, the keyword this is not required, but feel free to use it if you want.


Not yet. I'm trying to get Rectangle to work so that I can base my code in Square and Circle off it.

Also, I now have edited my code to where none of them are static anymore except for the main method, but results haven't changed.
Hidden 7 yrs ago Post by GalaxyRaider
Raw
Avatar of GalaxyRaider

GalaxyRaider Isaac

Member Seen 4 mos ago

<Snipped quote by GalaxyRaider>

Well for one, you might be better off getting the length and width in the same place.
Secondly, this might be a different thing with java than I learned, but personally I'd put "width * 2" and "length * 2" instead of "2*width" and "2*length". Not really a problem per say unless java has a hard time with the way you put it.
Third, you're defining length and width as 0. Instead of "templength" and "tempwidth" I'd suggest creating new variables and pulling those instead of trying to apply them back to length and width.
Fourth, it could be an issue of order. Having Rectangle(double length, double width) after defining them as zero could be the problem. Again, I suggest new variables entirely than ones you've already used.
But since I'm not much for Java I bet none of this is applicable.


Well, whether it's right or not, some of this might be more efficient or appealing to coding etiquette so thank you.
Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

Okay Sven, since that solution kind of opened up a can of worms, I went ahead and repaired all static issues and implemented Perimeter and Area methods in circle. This has been tested with all three shapes and produces correct results.











The only downside is that I don't know what coding practices your class is supposed to follow, but I tried modeling it after what you already had. There are some constructors/methods that do nothing, but those were there before. I tried leaving everything as intact as possible, unless you want me to go in and pull out some of the extraneous stuff.
Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

<Snipped quote by Legend>

True, but I prided myself on helping the people next to me when they had trouble so it hurts double that I can't with this.


At least it doesn't hurt int.



But you don't know Java, so it's extra not a big deal.
Hidden 7 yrs ago Post by GalaxyRaider
Raw
Avatar of GalaxyRaider

GalaxyRaider Isaac

Member Seen 4 mos ago

@Legend@DarkwolfX37

Thanks for the help. I got done just in time.
Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

@Legend@DarkwolfX37

Thanks for the help. I got done just in time.


Yeah, no problem. What did you have to do beyond area/perimeter?
Hidden 7 yrs ago Post by GalaxyRaider
Raw
Avatar of GalaxyRaider

GalaxyRaider Isaac

Member Seen 4 mos ago

<Snipped quote by GalaxyRaider>

Yeah, no problem. What did you have to do beyond area/perimeter?


Not much. But I did have to make some changes once I got the code to work so that it fit the specifications of the assignment.
Hidden 7 yrs ago Post by Legend
Raw
Avatar of Legend

Legend

Member Seen 7 days ago

<Snipped quote by Legend>

Not much. But I did have to make some changes once I got the code to work so that it fit the specifications of the assignment.


Coolio. Classes and objects are really hard.
↑ Top
17 Guests viewing this page
© 2007-2024
BBCode Cheatsheet