Help with a newbies program

I have a program that that does calculation of afew didfferent areas.

The first three only take one user key imput but the third requires two user inputs for height and radius of a cylinder. I am unable to get the cal.

I have insert to prints to make sure the program is accepting the two key inputs, but cannot get them to work in the equation.import TerminalIO.*;

class FindAreaDemo{

publicstaticvoid main ( String[] args ){

KeyboardReader keyEntry =new KeyboardReader();

FindArea square =new FindArea();//made a new object from FindArea

square.setLength

(keyEntry.readDouble("\n\tEnter length of sides of the square: "));

System.out.println ("\t\tArea of the square is " + square.getAreaSquare());

FindArea cube =new FindArea();//made a new object from FindArea

cube.setCube

(keyEntry.readDouble("\n\tEnter length of sides of the Cube: "));

System.out.println ("\t\tArea of the cube is " + cube.getAreaCube());

SphereArea circle =new SphereArea();//made a new object from FindArea

circle.setCircle

(keyEntry.readDouble("\n\tEnter The radius for the cicle: "));

System.out.println ("\t\tArea of the circle is " + circle.getareaCircle());

//**************************************************************************

SphereArea height =new SphereArea();

height.setHeight1

(keyEntry.readDouble("\n\tEnter height of the cylinder: "));

System.out.println ("\t\t\t\t height is " + height.getHeight1());

SphereArea radius =new SphereArea();

radius.setRadius1

(keyEntry.readDouble("\n\tEnter radius of the cylinder: "));

System.out.println ("\t\t\t\t radius is " + radius.getRadius1());

SphereArea cylinder =new SphereArea();

System.out.println ("\n\t\tArea of a cylinder is " + cylinder.getareaCylinder());

}

}

the two class I madepublicclass SphereArea{// begin a new class

//public static double classSphereArea; //class variable

finalprivatedouble PI = (double)3.14;//Object variable Constant

finalprivatedouble SPCONSTANT2 = 2.0;//Object variable Constant

//corrected my Constant to so that it now is really a constant

privatedouble radius;//Object variable

privatedouble height;//Object variable

privatedouble areaCircle;//Object variable

privatedouble areaCylinder;//Object variable

privatedouble height1;

privatedouble radius1;

//corrected my two variables so that they are private now and not public areaCube & areaSquare

//*******************************************************

publicvoid setCircle(double radius){

areaCircle = (PI*(radius * radius));// surface area = 4pr2

}

publicdouble getareaCircle(){

return areaCircle;

}

//*******************************************************

publicvoid setHeight1 (double height){

height1 = height;

}

publicdouble getHeight1(){

return height1;

}

//*******************************************************

publicvoid setRadius1 (double radius){

radius1 = radius;

}

publicdouble getRadius1(){

return radius1;

}

//*******************************************************

//The entire formula for the surface area of a cylinder is 2pr2 + 2prh

publicvoid setareaCylinder (double areaCylinder){

areaCylinder = (SPCONSTANT2*PI*(radius1 * radius1) + (SPCONSTANT2 * PI * radius1 * height1));

}

publicdouble getareaCylinder(){

return areaCylinder;

}

}

ublicclass FindArea{// begin a ne class

//public static double classFindArea; //class variable

privatedouble length;//Object variable

finalpublicdouble CUBE6 = 6.0;//Object variable Constant

// corrected my Constant to so that it now is really a constant

privatedouble areaCube;//Object variable

privatedouble areaSquare;//Object variable

publicvoid setLength(double length){

areaSquare = length * length;

}

publicdouble getAreaSquare(){

return areaSquare;

}

publicvoid setCube(double length){

areaCube = length * length * CUBE6;

}

publicdouble getAreaCube(){

return areaCube;

}

}//end class

[8987 byte] By [Kithinga] at [2007-10-2 6:04:17]
# 1

This is just a stab in the dark, but you might need to do:

public void setareaCylinder (double radius1)

in your sphereArea class instead of:

public void setareaCylinder (double areaCylinder)

.

I haven't run the code, and I haven't done Java in a while so this may be wrong.

C

kateshortforboba at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 2

Please ignore my previous post - I am tired and therefore stupid. I had another look and I think part of the problem is that you are reading radius and height into different SphereArea objects and then creating another sphereArea object for cylinder. I think you need to create a single object (cylinder) send radius & height to it and then do the calculation.

kateshortforboba at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for the help, I had orginally had them set in the same object, I went ahead and put them separate because I thought that only one of the variables was being set. But it makes sense that there should only be one object and the two variables. So I went ahead and created one object and setting radius and height, It did not help not sure if I have the correct syntax. It it now does not calculate or print a value for radius or hight. The result is

Enter the height for the cylinder: 3

Enter The radius for the cicle: 7

Area of a cylinder is 0.0

height is 0.0

radius is 0.0

new code snippetSphereArea cylinder = new SphereArea();

cylinder.setRadius

(keyEntry.readDouble("\n\tEnter the height for the cylinder: "));

cylinder.setHeight

(keyEntry.readDouble("\n\tEnter The radius for the cicle: "));

System.out.println ("\n\t\tArea of a cylinder is " + cylinder.getCylinder());

System.out.println ("\t\t\t\t height is " + cylinder.getHeight());

System.out.println ("\t\t\t\t radius is " + cylinder.getRadius());

snippet for chasnges to the class

public void setHeight(double height){

height = height;

}

public double getHeight(height) {

return height;

}

public double getRadius(radius) {

return radius;

}

public void setRadius( double radius){

radius = radius;

}

//The entire formula for the surface area of a cylinder is 2 p r2 + 2 p r h

public void setCylinder (){

areaCylinder = (SPCONSTANT2 *(PI *(radius * radius) + (SPCONSTANT2 * PI * radius * height)));

}

public double getCylinder() {

return areaCylinder;

}

Kithinga at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 4

You need to call the method setCylinderArea. At the moment it isn't being executed, so there isn't anything being returned. It should go like this:

create new sphereArea object called cylinder

read in radius and height

set radius of cylinder

set height of cylinder

call setCylinder()

print out(getCylinder)

Hope this helps!

C

kateshortforboba at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 5

hmmm, I thought that was what I was doing with per above

Thanks

SphereArea cylinder = new SphereArea();

cylinder.setRadius

(keyEntry.readDouble("\n\tEnter the height for the cylinder: "));

System.out.println ("\t\t\t\t radius is " + cylinder.getRadius());

cylinder.setHeight

(keyEntry.readDouble("\n\tEnter The radius for the cicle: "));

System.out.println ("\t\t\t\t height is " + cylinder.getHeight());

System.out.println ("\n\t\tArea of a cylinder is " + cylinder.getCylinder());

Kithinga at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 6
All your logic to calculate "areaCylinder" is done in your "setCylinder" method, which you never invoke, so that variable's value remains at 0.Why do you have a setCylinder method anyway? getCylinder should perform and return the calculation.
warnerjaa at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 7

That make sense to call the getcylinder to make the calculation. The variables have already been set.

I am not sure the syntax to make that happen though?

this is what I put in the Spereclass

public double getCylinder(SPCONSTANT2 * (PI *(radius * radius)) + (SPCONSTANT2 * PI * radius * height)) {

return areaCylinder;

}

but I get a error for identifier> expected

public double getCylinder(SPCONSTANT2 * (PI *(radius * radius)) + (SPCONSTANT2 * PI * radius * height)) {

should I create a constructor in Spereclass?

Kithinga at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 8

You really need to cover the basics.

But for this simple method:

public double getCylinder() {

return SPCONSTANT2 *(PI *(radius * radius) +

(SPCONSTANT2 * PI * radius * height));

}

(That's one way, though I'd simplify it to not calculate the sub-expressions more than once)

warnerjaa at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 9
... by the way, then you can just simply remove the "areaCylinder" member -- it is not needed anymore as you can see.
warnerjaa at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 10

I am still getting 0 for the output

If I try to print either variable I still get 0 for the output of the variable.

I should be able to print the variable?

output

Enter the height for the cylinder: 2

Enter The radius for the cicle: 3

Area of a cylinder is 0.0

SphereArea cylinder = new SphereArea();

cylinder.setRadius

(keyEntry.readDouble("\n\tEnter the height for the cylinder: "));

cylinder.setHeight

(keyEntry.readDouble("\n\tEnter The radius for the cicle: "));

System.out.println ("\n\t\tArea of a cylinder is " + cylinder.getCylinder());

final private double PI = (double)3.14;//Object variable Constant

final private double SPCONSTANT2 = 2.0;//Object variable Constant

// corrected my Constant to so that it now is really a constant

private double radius;//Object variable

public double height;//Object variable

private double areaCircle;//Object variable

private double areaCylinder;//Object variable

//corrected my two variables so that they are private now and not public areaCube & areaSquare

public void setCircle( double radius){

areaCircle = (PI*(radius * radius));// surface area = pr2

}

public double getAreaCircle() {

return areaCircle;

}

public void setHeight(double height){

height = height;

}

public double getHeight() {

return height;

}

public void setRadius(double radius){

radius = radius;

}

public double getRadius() {

return radius;

}

public double getCylinder() {

return SPCONSTANT2 *(PI *(radius * radius) +

(SPCONSTANT2 * PI * radius * height));

}

Kithinga at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 11

You're getting 0 because you never actually set your height nor radius members. Look at the following line of code:

> height = height;

What do you expect that to do? I know that you meant to set the height member to the value that was passed to the setHeight method, but the compiler doesn't know that. It just sees the exact same variable name on both sides of the equation, and basically does nothing.

You meant:

this.height = height;

Obviously you need to do likewise for the setRadius method.

warnerjaa at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...
# 12
Thank you very much, yes that makes sense that I was resetting the variable back to the orginal value. Thank you again.
Kithinga at 2007-7-16 13:04:42 > top of Java-index,Java Essentials,Java Programming...