• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help for a complete Java Newbie  RSS feed

 
Ted Lorenzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have started taking a Java evening course 3 weeks ago, and we have been given a task to complete. I am trying my best to get my head around it, but I just can't seem to get it.
The question asked was:
"Create a class - Cube - with one instance variable - length - containing methods to calculate the are of the base of the cube, the total surface area of the cube, the volume of the cube, the length of the body diagonal of the cube, and the angle the body diagonal makes with the base of the cube. The skeleton of the class is given below: -


I have been trying all weekend to get something in, just so i don't get zero marks, but I just don't seem to be getting anywhere, and am really struggling. I tried to do some reading, and asked the teacher, but he is not getting back to me.
Here's what i've tried so far. If anyone can have a look and point me as to where i am going horribly wrong, it would be really appreciated.
I've added the class code, and the test code.





Thanks,

Ted.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the instructions your class needs just 1 instance variable called length so delete all those other instance variables. Then when you are doing a calculation that needs a value provided by one of your methods, call that method rather than using one of the redundant instance variables (which as you have no doubt found out don't hold the values you thought they would.

Your constructor needs you assign the passed in value to 'length'.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify what Tony says.

The skeleton has all the methods and variables that you need declared in it already.
The only things you should add is code within the methods. i.e. replace the "..." in each case.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i only looked at your code for about ½″ but I think some if it would work. I did notice some things however. You have six fields which you never use. You only need length. You are re‑declaring local variables with the same names in each method, so you are not using those fields. You might as well delete them.
Don't import java.lang.Math, because it is unnecessary. It will work just as well without that import.
You have already been told that you set up the length in the constructor. You mustn't simply write length = length; however.
I am not sure you are using the right formulae for the diagonal methods, however. Hint: if you have a true cube, then the angle of the diagonal in the face is 45° and the length of the face diagonal is √2 × length. You can't get 45° from tan but you might get it from atan.
Don't put spaces around ( or ). The () in lines 22 28 and 33 are unnecessary and you can remove them without any change in results.
Avoid Math.pow(x, 2). You can usually get better performance from x * x.
Have you been told what formula to use for the body diagonal? It will be √(length² + (√2 × length)²) =
√(length² + 2 × length²) =
√(3 × length²) =
√3 × length.

That formula is much more complicated than most things seen in programming. The √2 and √3 bits only work for true cubes; they will not work if any sides are different.

And welcome again
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . the angle of the diagonal in the face is 45° . . .
I suspect that angle of 45° is a different angle from that you have been told to calculate.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I quite forgot: find the Math#hypot method, which will work out Pythagoras without your need to specify square roots.
 
Ted Lorenzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your answers... didn't expect so many responses.

Can I just clarify what I need to do, as I am still not great with the terminology?
So i need to delete lines 11-17 pretty much? (These are the instance variables?) When I have done this and try to compile it, it produces an error message:

Cube.java:13: cannot find symbol
symbol : variable lengthValue
location: class Cube
return (lengthValue * lengthValue);


I'm not sure what I am putting in the constructor.. what do i have to include?

Are the text in the methods all okay? do i need to delete 'base area' = xxxx, and just use return (length * length)

I know i'm asking loads of questions, but I am struggling even though i've been reading about it all day.

Many thanks again... it really is appreciated.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>I'm not sure what I am putting in the constructor.. what do i have to include?

The purpose of a constructor is to set up the attributes in your class ready for use.
This class only has one attribute: length. Assign it a value.

lengthValue is the name of the parameter to the constructor.
As you appear to have figured out, you should be using "length" rather than "lengthValue" in your methods.

>Are the text in the methods all okay? do i need to delete 'base area' = xxxx, and just use return (length * length)


Sounds good to me.

And then your other methods should call the baseArea() method any time they need that value.
 
Ted Lorenzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, thanks!!!

I now have:




Where do i go from here?
I am tearing my hair out! Haha!
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From here?
Start fixing up your compilation errors?

I've got 13 right now from this code example you posted.
Mainly along the lines of

XXXXX cannot be resolved to a variable - This is left over after deleting your instance variables.
Duplicate field Cube.length - you have it declared at the top and the bottom of your code...

The sample skeleton had it declared at the bottom. It IS very easy to miss there :-)

Edit: Because I'm feeling in a charitable mood, this is the next step.
Comment out all of your code right now, and start again :-)

This will get you something that compiles:


I've basically commented out all of your code with errors, and put in a simple stupid (and wrong) answer but one that will pass the compiler.

Start with baseArea(). Get that method working (there is still a mistake in it)
Then one by one with the remaining methods.



 
Ted Lorenzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Stefan.

I cannot see what I need to amend though.
Do I have to add something in baseArea(...here..)?
Or add something in the main method?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Does running the CubeTest application work?
What does it output?

How I'm trying to help you here is by simplifying the errors so you can deal with them and fix them one at a time.
Starting from my example code, you should be able to compile and run the program, and it should give you 0 for every answer.
Not a correct answer, but it is running.


Next - take the baseArea method
- remove the comment hiding your code
- delete my "return 0" line.

You are left with this:


The compiler complains about this saying: "lengthValue cannot be resolved to a variable"
How would you fix that problem?
Fix that problem. Run your code. Is it the right answer?
If so, move on to the next method.

 
Ted Lorenzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've used this:


and the test application outputs the correct value for baseArea.

Then if id do the same thing for surface area:




i get this error when compiling:

Cube.java:17: cannot find symbol
symbol : variable baseArea
location: class Cube
return 6 * baseArea;
^
1 error

Do i have to declare it somewhere?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you don't need to declare anything else.
You have everything that you need already. Including the answer earlier in this thread:

Tony Docherty wrote:... when you are doing a calculation that needs a value provided by one of your methods, call that method rather than using one of the redundant instance variables


Stefan Evans wrote:
And then your other methods should call the baseArea() method any time they need that value.


The compiler is complaining right now because it thinks that baseArea is a variable, and it can't find it.
How do you tell the compiler to look for and call a method rather than a variable?

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted Lorenzo wrote:. . .
Cube.java:17: cannot find symbol
symbol : variable baseArea
location: class Cube
return 6 * baseArea;
^
1 error

Do i have to declare it somewhere?
Because quite rightly you ahve got rid of the haseArea field. No, you should not declare it. You do however have a method with a similar name which calculates said area. Change the field which you haven't got to a method call by the simple technique of adding () to it.
 
Ted Lorenzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thank everyone for their patience. I know it seems like I'm being lazy, but I genuinely am spending a lot of time reading about the subject. It just isn't sinking in. I doubt I am going to pursue Java, because I'm really struggling at this basic stage, but I would at least like to pass this module.

So would it just be

}
public double totalSurfaceArea() {

return 6 * baseArea();
}
?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted Lorenzo wrote: . . .
public double totalSurfaceArea() {

return 6 * baseArea();
}
?
That will do nicely Try it and see. Make sure to get your indentation and spacing right; we have some suggestions of our own.
 
Ted Lorenzo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got it to work. Genuinely, thank everyone who helped for their patience.

My angle calculation is incorrect though. It should be about 35. I realise i have the return in radians, but when i convert them it's still wrong. One last bit of advice?

Again,

Thank you, thank you, thank you.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the formula you are using for the angles. And the code (which might be different!)
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I presume is:



I recommend you take another look at the API for java.lang.Math.
As already mentioned by Campbell in an earlier post, the method you are using does not do what you think it does...

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!