im trying to make a code block to compute the volume of a cylinder but im unfamiliar with java so this is what i had so far. Is there anyway to re-purpose it so i can compute the volume?
package javaapplication231;
import java.util.Scanner;
public class JavaApplication231 {
public static void main(String[] args) {
int a,b,i,product;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of a and b: ");
a = keyboard.nextInt(); //Line 6
b = keyboard.nextInt();
i=0;
product=0;
while (i<a){
i=i+1;
product= product +b;
}
System.out.println("Product=" + product);
}
}
V =PIr^2h
where r is the radius and h is the height
user input: r and h
PI= 3.1416
To calculate the volume of Cylinder simply evaluate the expression, why do you need loop and do the addition. As you mentioned use the formula to calculate the
Volume=PI*radius2*height
int radius, height;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of radius and Height: ");
radius = keyboard.nextInt(); // Line 6
height = keyboard.nextInt();
double volume = 3.14 * radius * radius * height;
System.out.println("Volume=" + volume);
Related
When I run this program, I need the part where it says example " cans needed 3.0" I need it to only say the integer.
public class PaintEstimator {
public static void main(String[] args) {
// Create the Scanner object scnr
Scanner scnr = new Scanner(System.in);
// Declare the identifiers
double wallHeight;
double wallWidth;
double wallAera;
double gallonPaint = 350;
int paintNeeded;
// Prompt user for and input wall's height and width; and then display them
System.out.println("Enter wall height (feet) :");
wallHeight = scnr.nextInt();
System.out.println("Enter wall width (feet) :");
wallWidth = scnr.nextInt();
System.out.println("Wall height is: " + wallHeight + " and Wall width is: " + wallWidth);
// Calculate and output wall area
wallAera = wallHeight * wallWidth;
System.out.println("Wall area: " + wallAera + "square feet");
// Calculate and output the amount of paint in gallons needed to paint the wall
System.out.println("Paint needed: " + (wallAera / gallonPaint) + " gallons");
// Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
System.out.println("Cans needed: " + Math.ceil(wallAera / gallonPaint) + " can ( s )");
// Calculate and output the cost of painting the wall.
double costPaint = Math.ceil(wallAera / gallonPaint) * 45.0;
System.out.println("Cost to paint the wall: $" + costPaint);
}
}
You can round/ceil it up, as you are already doing, and then cast it to an int:
System.out.println("Cans needed: " + (int) Math.ceil(wallAera / gallonPaint) + " can ( s )");
There are 3 basic methods for change a double to an integer.
Where var-name is an integer and var-name1 is a double.
Method 1: Type-casting
Example:
int var-name = (int) var-name1;
Method 2: Rounding
Example:
int var-name = Math.round(var-name1);
Method 3: Double.intValue
Example:
int var-name = Double.intValue(var-name1);
A while ago I started to learn JAVA and everything was going well, until I got to this exercise:
CONDITIONS
Write a program that when complete will calculate the amount of paint needed to paint the walls and the ceiling of a room.
Your program should ask the length, width, and height of the room. Assume that the room has doors and windows the don't need painting. Also, the floor in the room is not painted.
Ask the user to enter the number of doors and number of windows in the room, and adjust the total square feet to be painted accordingly.
Assume that each door is 20 square feet and each window is 15 square feet.
Suppose the paint covers 350 square feet per gallon. enter code here
My chosen parameters
Width 3;
Length 10;
Height 2.0;
Doors 1;
Windows 2;
MY SOLUTION
int width = Integer.parseInt(JOptionPane.showInputDialog("Please, enter the width of a room"));
int length = Integer.parseInt(JOptionPane.showInputDialog("Please, enter the length of the room"));
double height = Double.parseDouble(JOptionPane.showInputDialog("Please, enter the height of the room"));
int doors = Integer.parseInt(JOptionPane.showInputDialog("If there are doors, please, enter the amount of them:"));
int windows = Integer.parseInt(JOptionPane.showInputDialog("If there are some windows, please, enter the amount of them:"));
double feet = 1.00/350.00;
double area = (width*length*height)-(windows*15+doors*20);
JOptionPane.showMessageDialog(null, "You need:"+feet*area+" gallons of paint.");
}
}
The output is 0.02857142
OTHER SOLUTIONby the author of the exercise
public static void main(String[] args) {
{
int length, width, numberOfDoors, numberOfWindows;
double height;
Scanner console = new Scanner(System.in);
System.out.print("Enter length: ");
length = console.nextInt();
System.out.print("Enter width: ");
width = console.nextInt();
System.out.print("Enter height: ");
height = console.nextDouble();
System.out.print("Enter number of doors: ");
numberOfDoors = console.nextInt();
System.out.print("Enter number of windows: ");
numberOfWindows = console.nextInt();
double totalSurfaceArea = 2 * (length * width + length
* height + width * height);
int areaOfFloor = length * width;
int areaOfDoors = 20 * numberOfDoors;
int areaOfWindows = 15 * numberOfWindows;
double totalPaintArea = totalSurfaceArea - areaOfFloor
- areaOfDoors - areaOfWindows;
double requiredPaint = totalPaintArea / 350;
System.out.println("Paint required "
+ requiredPaint + " gallons.");
}
}
}
The output is 0.09142857142857143;
So, two different solutions gave two different outputs.
Is there anyone who could help me to clarify, where I went wrong?
I'm working on a project and I have most of it done but I'm having trouble seeing how to get the coordinates to line up. I'm stuck and I'm not sure how to get a point to be at 3 o'clock and I'm stuck. I've tried finding examples but all I see is polygons that don't need to lineup with anything. Any help?
The instructions: Suppose an n-sided regular polygon is centered at (0, 0) with one point at the 3 o’clock position, as shown in Figure 5.4. Write a program that prompts the user to enter the number of the sides, the radius of the bounding circle of a polygon, and displays the coordinates of the corner points on the polygon.
import java.util.Scanner;
public class Polygon {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of sides: ");
int sides = input.nextInt();
System.out.print("Enter the radius of the bounding circle: ");
double radius = input.nextDouble();
input.close();
System.out.println("The coordinates of the points on the polygon are");
for (int i = 0; i < sides; i++) {
double x = radius * java.lang.Math.sin(2.0 * java.lang.Math.PI / sides * i);
double y = radius * java.lang.Math.cos(2.0 * java.lang.Math.PI / sides * i);
System.out.print("(");
System.out.printf("%.2f", x);
System.out.print(" ");
System.out.printf("%.2f",y);
System.out.print(")");
System.out.println();
}
}
}
You need to switch your sin and cos expressions. The the first point of your polygon will then always lie at (radius, 0), i.e. aligned with the 3-o'clock position.
double x = radius * java.lang.Math.cos(2.0 * java.lang.Math.PI / sides * i);
double y = radius * java.lang.Math.sin(2.0 * java.lang.Math.PI / sides * i);
import java.util.Scanner;
import java.text.DecimalFormat;
public class palomba2 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
//allows input to be read
// VOLUME OF A PRISM
int prLngth, prWdth, prHght, prVol;
System.out.print("Enter the length of the prism in feet: ");
prLngth = keyboard.nextInt();
System.out.print("Enter the width of the prism in feet: ");
prWdth = keyboard.nextInt();
System.out.print("Enter the height of the prism in feet: ");
prHght = keyboard.nextInt();
prVol = prLngth * prWdth * prHght;
System.out.print("The volume of the prism is " + prVol);
System.out.print(" feet.");
System.out.println("");
System.out.println("");
// AREA/CIRCUMFERENCE OF A CIRCLE
DecimalFormat format = new DecimalFormat("0.##");
double radius, circ, area;
System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
radius = keyboard.nextInt();
circ = 2 * radius * 3.14159265358979;
area = radius * radius * 3.14159265358979;
System.out.print("The circumference of the circle is " + format.format(circ));
System.out.print("units.");
System.out.print("The area of the circle is " + format.format(area));
System.out.print("units.");
}
}
Everything in my code works up until I input the radius of the circle. It comes up with the error messages after I input a radius.
Enter the radius of a circle rounded to the nearest hundreth: 2.12
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:860)
at java.base/java.util.Scanner.next(Scanner.java:1497)
at java.base/java.util.Scanner.nextInt(Scanner.java:2161)
at java.base/java.util.Scanner.nextInt(Scanner.java:2115)
at palomba2.main(palomba2.java:52)
You type a double variable 2.12 but trying to read it as an int : input mismatch -> InputMismatchException
Also, the variable radius is of type double so you cannot give to it an int value (also you, you need :
radius = keyboard.nextDouble();
But still better to use radius = Double.parseDouble(keyboard.nextLine()) to consume the return char (same for int : int val = Integer.parseInt(keyboard.nextLine())
You just have to change the:
radius = keyboard.nextInt();
to:
radius = keyboard.nextDouble();
because the variable radius is declared as double
it should look like this:
double radius, circ, area;
System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
radius = keyboard.nextDouble();
So I have three static, overloaded methods that are used in my AreaClient class that are taking input from the user and passing what those inputs are as parameters to the methods below. For some reason though I can't seem to get the last area method to take in my hieght variable as a parameter. I keep getting an error saying "cannot find symbol". These are supposed to be overloaded methods, just what the assignment says. Sorry if this is real simple but I am pretty new to programming. Here is the code that I wrote.
import java.util.Scanner; // Needed for the Scanner class
public class AreaClient {
public static void main(String[] args) {
double circleRadius; //input for radius of circle
int width, length; //input for rectangle width and length
double cylinderRadius, height; //input for radius of a cylinder and hieght
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// gathering input for radius of circle
System.out.println("Enter radius of circle");
circleRadius = keyboard.nextDouble();
// input for width and length of rectangle
System.out.println("Enter width of rectangle");
width = keyboard.nextInt();
System.out.println("Enter length of rectangle");
length = keyboard.nextInt();
// input for radius and hieght of cylinder
System.out.println("Enter radius of cylinder");
cylinderRadius = keyboard.nextDouble();
System.out.println("Enter hieght of cylinder");
height = keyboard.nextDouble();
//returning area methods results and storing them in new variables
double circleArea = area(circleRadius);
int rectangleArea = area(width, length);
double cylinderArea = area(cylinderRadius, height);
//displaying results of methods
System.out.println("The area of your circle is: " + circleArea);
System.out.println("The area of your rectangle is: " + rectangleArea);
System.out.println("The area of your cylinger is: " + cylinderArea);
}
//overloaded methods that take different inputs
public static double area(double r)
{
return 3.14159265359 * Math.pow(r, 2);
}
public static int area(int w, int l)
{
return w * l;
}
//actual method that doesn't recognize h inside
public static double area(double r, double h)
{
return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
}
}
And the error msg I am getting
AreaClient.java:54: error: cannot find symbol
return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
^
symbol: method h(double)
location: class AreaClient
1 error
Thanks guys. Any help is much appreciated.
Notice in the error message:
symbol: method h(double)
Why it is looking for a method called h() which accepts a double? Because you're telling it to:
h (2*3.14159265359*r)
h isn't a method, it's just a value. You need to use an operator to connect it to that other value. I think you meant to do this:
h * (2*3.14159265359*r)
I think you mean: h * (2*3.14159265359*r). Without the operator, Java thinks you're trying to call a method named h(double)
return 2*3.14159265359 * Math.pow(r,2) + h * (2*3.14159265359*r);