Question:
JAVA HELP! PLEASEEEEEEEEEEEE?
Alex
2012-04-28 20:05:57 UTC
i need to write a java code with this info: The calculation for the volume of a box is length times width times height. The calculation for the volume of a cylinder is 3.14 times the radius times the radius again times the height.

Design a computer program to calculate the volume of either a box or cylinder using the formulas above. Display the name of the object (box or cylinder) and its volume. Design the program so that it loops for any number of geometric objects (boxes and/or cylinders), that is the program sequence needs to repeat for each set of input data to be processed. Do not be data specific.

For each iteration of your program you need to determine which geometric object to compute the volume for. A suggestion is to set up keys for each iteration such as 1 for box, 2 for cylinder, and 3 for no more data

Some sample data are as follows:
Geometric type (1 for box, 2 for cylinder, 3 for no more data)

THIS IS WHAT I GOT SO FAR BUT I KNOW THERE ARE A LOT OF ERRORS AND IM SO CONFUSED I WOULD REALLY APPRECIATE THE HELP. THANKS

import java.io.*;

import java.lang.*;

import java.text.*;

import java.util.*;

//Set up public class

public class calculation

{

//declare main() method

public static void main(String[] args) throws IOException

{

//Declare data types

float length,width,height,volume;

//set up input stream

Scanner readin = new Scanner(System.in);

//prompt for length

System.out.println("\n\n enter value for length");

//stream in length and convert to float

length = readin.nextFloat();

//prompt for width

System.out.println("\n\n enter value for width");

//stream in width and convert to float

width = readin.nextFloat();

//prompt for height

System.out.println("\n\n enter value for height");

//stream in height and convert to float

height = readin.nextFloat();

//Calculate results

volume of box= length*width*height;

//Declare data types

float length,radius,volume;

//set up input stream

//prompt for length

System.out.println("\n\n enter value for length");

//stream in length and convert to float

length = readin.nextFloat();

//prompt for radius

System.out.println("\n\n enter value for radius");

//stream in radius and convert to float

radius = readin.nextFloat();

//calculate results

volume of cylinder= 3.14(radius*radius*lenght)

//prepare data for out put display

System.out.print("\n\nThe results of the calcuations are\n\n\n");

System.out.print("the volume of the box is is:\t\t" + volume of box + "\n\n");
System.out.print("the volume ofthe cylinder is is:\t\t" + volume of cylinder + “\n\n”);

}//end main

}//end class
Three answers:
Thomas Dibona
2012-04-28 20:15:48 UTC
"volume of cylinder= 3.14(radius*radius*length)" use a more accurate number.............3.141592653589793238462643383279502884197169399375105820974944592307816406286 208998628034825342117067982148086513282306647093844609550582231725359408128481 117450284102701938521105559644622948954930381964428810975665933446128475648233 786783165271201909145648566923460348610454326648213393607260249141273724587006 606315588174881520920962829254091715364367892590360011330530548820466521384146 951941511609433057270365759591953092186117381932611793105118548074462379962749 567351885752724891227938183011949129833673362440656643086021394946395224737190 702179860943702770539217176293176752384674818467669405132000568127145263560827 785771342757789609173637178721468440901224953430146549585371050792279689258923 542019956112129021960864034418159813629774771309960518707211349999998372978049 951059731732816096318595024459455346908302642522308253344685035261931188171010 003137838752886587533208381420617177669147303598253490428755468731159562863882 353787593751957781857780532171226806613001927876611195909216420198938095257201 065485863278865936153381827968230301952035301852968995773622599413891249721775 283479131515574857242454150695950829533116861727855889075098381754637464939319 255060400927701671139009848824012858361603563707660104710181942955596198946767
James Bond
2012-04-29 03:42:47 UTC
public class calculation{

public static void main(String[] args) throws IOException

{

volume_of_box= length*width*height;

.......

volume_of_cylinder= 3.14*(radius*radius*lenght);



System.out.print("\n\nThe results of the calcuations are\n\n\n");



while(true)

{

System.out.println("1 for Box 2 For Cyllinder 3 for exit");

int N=readin.nextInt());

if (N==1)

System.out.print("the volume of the box is is:\t\t" + volume_of_box + "\n\n");

else if(N==2)

System.out.print("the volume ofthe cylinder is is:\t\t" + volume_of_cylinder + “\n\n”);

else break;

}



}//end main



}//end class
2012-04-29 03:32:29 UTC
Correct Program:

------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;



public class calculation



{



public static void main(String[] args)



{



float length,width,height,volume1;



Scanner readin = new Scanner(System.in);



System.out.println("Details for volume of Box");



System.out.println("\n\n enter value for length");



length = readin.nextFloat();



System.out.println("\n\n enter value for width");



width = readin.nextFloat();



System.out.println("\n\n enter value for height");



height = readin.nextFloat();



volume1 = length*width*height;



System.out.println("Details for volume of Cylinder");



float radius,volume2;



System.out.println("\n\n enter value for length");



length = readin.nextFloat();



System.out.println("\n\n enter value for radius");



radius = readin.nextFloat();



volume2 = (3.14f)*(radius*radius*length);



System.out.print("\n\nThe results of the calcuations are\n\n\n");



System.out.print("\n\nthe volume of the box is is:"+volume1);

System.out.print("\n\nthe volume ofthe cylinder is is:"+volume2);



}//end main



}//end class

------------------------------------------------------------------------------------------------------------------------------

Basic Rule for naming a Variable:

--------------------------------------------

Never use spaces in naming variables.If u want , use a _(Underscore) like volume_of_the_box;



Note:You have used the same variable volume for both so use 2 variables, volume1 for volume of



box and volume2 for volume of cylinder.



Declaring "volume" variable 2 times. is also an error.



What actually happens in your code is the value of volume is overwritten with the volume of the



cylinder.



First Problem:

--------------------

float volume1,volume2;

volume of box= length*width*height; => volume1 = length*width*height;

--------

Second problem

---------------------

volume of cylinder= 3.14(radius*radius*lenght) => volume2 = 3.14f*(radius*radius*lenght);



System.out.print("the volume of the box is is:\t\t" + volume1+ "\n\n");

System.out.print("the volume ofthe cylinder is is:\t\t" + volume2 + “\n\n”);



Note : Dont forget to put the f after 3.14.

It's called type cast.



Another Way

------------------

If you are so keen on using just one variable volume, shift the position of System.out.println("The volume of the box is"+volume);

before

float length,radius;



The Overall code is:


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...