I can't seem to figure out why my coneVolume method is returning zero when all of my other methods are working properly.
import java.util.Scanner;
public class P56old{
public static double sphereVolume(double r){
double sphereVolume = (4/3)*(Math.PI)*(Math.pow(r, 3));
return sphereVolume;
}
public static double sphereSurface(double r){
double sphereSurface = 4 * (Math.PI) * Math.pow(r, 2);
return sphereSurface;
}
public static double cylinderVolume(double r, double h){
double cylinderVolume = (Math.PI) * (Math.pow(r, 2)) * h;
return cylinderVolume;
}
public static double cylinderSurface(double r, double h){
double cylinderSurface = 2 * (Math.PI) * (Math.pow(r, 2)) + 2 * Math.PI * r * h;
return cylinderSurface;
}
public static double coneVolume(double r, double h){
double coneVolume = (1/3) * Math.PI * (Math.pow(r,2)) * h;
return coneVolume;
}
public static double coneSurface(double r, double h){
double s = Math.sqrt(Math.pow(r,2) + Math.pow(h, 2));
double coneSurface = Math.PI * Math.pow(r,2) + Math.PI * r * s;
return coneSurface;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Please give the radius: ");
double r = in.nextDouble();
System.out.print("Please give the height: ");
double h = in.nextDouble();
double coneVolume = coneVolume(r,h);
double sphereVolume = sphereVolume(r);
double sphereSurface = sphereSurface(r);
double cylinderVolume = cylinderVolume(r,h);
double cylinderSurface = cylinderSurface(r,h);
double coneSurface = coneSurface(r,h);
System.out.println("The Sphere Volume is " + sphereVolume);
System.out.println("The Sphere Surface is " + sphereSurface);
System.out.println("The Cylinder volume is " + cylinderVolume);
System.out.println("The Cylinder Surface is " + cylinderSurface);
System.out.println("The Cone Volume is " + coneVolume);
System.out.println("The Cone Surface is " + coneSurface);
}
}
I'd appreciate any insight on the matter, and any critique is appreciated. I think it may have to do with all the public classes and maybe another method is affecting the coneVolume method but I just don't know enough about methods at the moment to fix the issue at hand.
When you do 1/3, it does integer division, resulting in 0 (the remainder is 1). Multiplying by 0 gives 0. Do 1.0/3.0 instead, and it will correctly compute an approximation to one third.
Related
I'm trying to find the area of a Polygon using the following formula:
Area = r^2 n sin( 2 π / n) / 2
where n is the number of sides and r is the radius. I do not think my code is producing the correct result. If n= 6 and r = 4, i'm getting an area of 24. My code is as follows:
import java.math.*;
public class RegularPolygon {
private int n; // number of vertices
private double r; //radius of the polygon
/**
* This is the full constructor
* #param n is the number of vertices
* #param r is the radius
*/
public RegularPolygon(int n, double r) {
super();
this.n = n;
this.r = r;
}
/**
* default constructor. Sets number of sides and radius to zero
*/
public RegularPolygon() {
super();
this.n = 0;
this.r = 0;
}
//getters and setters for Number of vertices "n" and radius "r".
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
#Override
public String toString() {
return "RegularPolygon [n=" + n + ", r=" + r + "]";
}
public double area(){
float area;
//this method returns the area of the polygon
//Use Math.PI and Math.sin as needed
return area = (float) (Math.pow(r, 2)* n * ( Math.sin(Math.PI / n)/2));
It is unclear to me where my order of operations is messed up.
You're not translating the formula correctly. You need return Math.pow(r, 2) * n * Math.sin(2 * Math.PI / n) / 2; As forpas pointed out in the comments, you're missing a 2 and saying Math.sin(Math.PI / n) This has nothing to do with order of operations, since it's all just multiplication and division.
You should not type cast to float as you have declared the return type of the method as double
return Math.pow(r,2) * n * Math.sin(2 * Math.PI/n) / 2;
I think the above code will satisfy your need.
How to write this theorem correctly as is written in the formula?
package com.company;
public class Exercise8 {
public static void main(String[] args) {
double AB = 6;
double AC = 16;
double Angle = 60;
double CosOfAngle = 0.5;
// Почему-то значение косинуса 60 градусов вместо 0.5, пишет
// -0.9524129804151563 ? ? ? (Do not pay attention)
// Formula is BC^2 = AB^2 + AC^2 - 2AB*AC * cos A
double bc = (2 * (Math.pow(AB, 2) + Math.pow(AC, 2) - ((AB * AC))) * CosOfAngle);
double BC = Math.sqrt(bc);
double P = AB + BC + AC;
double p = 0.5 * P; // Где p - полупериметр
double S0 = (p * ((p - AB) * (p - BC) * (p - AC)));
double S1 = Math.sqrt(S0);
double S = Math.round(S1);
System.out.println("Perimeter of triangle is : " + P + " cm ");
System.out.println("Area of triangle is : " + S + " cm^2 ");
}
}
The mistake is in this line:
double bc = (2 * (Math.pow(AB, 2) + Math.pow(AC, 2) - ((AB * AC))) * CosOfAngle);
which should be:
double bc = Math.pow(AB, 2) + Math.pow(AC, 2) - 2 * AB * AC * CosOfAngle;
You were multiplying the whole formula by 2, whereas only the cosine part needs to be multiplied by two. There were too many confusing parenthesis. Removing them made it a lot clearer.
This seems simple to me:
// https://www.mathsisfun.com/algebra/trig-cosine-law.html
public double lawOfCosines(double a, double b, double angleInRadians) {
return Math.sqrt(a*a + b*b - 2.0*a*b*Math.cos(angleInRadians));
}
I am trying to make a program that calculates the area of a triangle given the values for all three sides. When I run my program with my runner file my area comes out to " 0.0"
Here is the classes full code (also I know I did the same code for setSides and triangle but my instructor gave us this shell and I didn't know what to put in there)
import java.util.Scanner;
import java.lang.Math.*;
public class Triangle
{
private int sideA;
private int sideB;
private int sideC;
private double theArea;
private double s;
private double perimeter;
public Triangle()
{
sideA = 1;
sideB = 1;
sideC = 1;
}
public Triangle(int a, int b, int c)
{
sideA = (int) a;
sideB = (int) b;
sideC = (int) c;
}
public void setSides(int a, int b, int c)
{
sideA = (int) a;
sideB = (int) b;
sideC = (int) c;
}
private double calcPerimeter()
{
double perimeter = sideA + sideB + sideC;
return perimeter;
}
private double calcArea()
{
double s = calcPerimeter() / 2;
double theArea = (Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)));
return theArea;
}
public void print()
{
System.out.println("Area == " + theArea);
}
}
I did an system out on your statement. What you are using is not an subtract operator. Please fix that. It will work. Also define the permiter.
System.out.println((int)'–');
output:
8211
Is it possible that you copied some of your source code from a document that isn't a simple text document?
If so, then your problem is that your minus signs are not the proper character. Try re-typing them in a text editor.
ED: Also, a minus sign should be surrounded by spaces. When I first looked at this, it threw me.
In your revised code, you can be square rooting a negative number, which would through you in a really bad state.
Remove:
private double perimeter;
And change:
private double calcArea()
{
double s = perimeter / 2;
double theArea = (Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)));
return theArea;
}
To:
private double calcArea()
{
double s = calcPerimeter() / 2;
double theArea = (Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)));
return theArea;
}
Here is what is happening. You declare a private double called perimeter. Later in the code you once again declare a double called perimeter. When you do this, things get really confusing! Make sure you only declare variables with the same name ONCE, and reference them thereafter.
so i have a math equation that i need to use in java but for some reason my code is giving me small errors :(
the math equation is describe on this web page in the section extra credit
my current code outpouts 4000 and the answere is 4005 what am i duing wrong ?
my test class lookes like this
public class MainActivity {
public static void main(String[] args) throws Exception{
double baseMaterial =556;
int me =5;
int ml = 10;
int extraMaterial = 3444;
System.out.println(""+calculateMiniralTotal(baseMaterial,me,ml,extraMaterial));
}
public static double calculateMiniralTotal(double perfekt,int me,int ml,int extraMaterial) {
double s = (perfekt + (perfekt * (10 / (ml + 1)) / 100));
s = Math.round(s);
double r = s + (perfekt * (0.25 - (0.05 * me)));
r = Math.round(r);
double q = extraMaterial + (extraMaterial * (0.25 - (0.05 * me)));
q = Math.round(q);
//double r=q;
r = r + q;
return Math.round(r);
}
}
You are performing integer division with (10 / (ml + 1)) / 100, which in Java must result in another int. Your ml is 10, and in Java, 10 / 11 is 0, not 0.909..., and nothing is added to s.
Use a double literal or cast to double to force floating-point computations.
double s = (perfekt + (perfekt * (10.0 / (ml + 1)) / 100));
or
double s = (perfekt + (perfekt * ( (double) 10 / (ml + 1)) / 100));
Making either change makes the output:
4005.0
When you multiply a double by an int you get an int back.
public class Main
{
public static void main(String[] args)
throws Exception
{
double baseMaterial = 556;
int me = 5;
int ml = 10;
int extraMaterial = 3444;
System.out.println("" + calculateMiniralTotal(baseMaterial, me, ml, extraMaterial));
}
public static double calculateMiniralTotal(double perfekt, int me, int ml, int extraMaterial)
{
double s = (perfekt + (perfekt * (10.0 / (ml + 1)) / 100.0)); // <-- changed from 10 to 10.0 and 100 to 100.0. This way they are doubles too
s = Math.round(s);
double r = s + (perfekt * (0.25 - (0.05 * me)));
r = Math.round(r);
double q = extraMaterial + (extraMaterial * (0.25 - (0.05 * me)));
q = Math.round(q);
// double r=q;
r = r + q;
return Math.round(r);
}
}
In my Java Course, there's an exercise for making a class called Area with 4 overloaded constructors to calculate the area of a circle, a triangle, a rectangle or a cillinder.
After solving 6 errors, I still have 3 left.
This is the code I used:
import java.lang.Math;
class Area {
public double pi = Math.PI;
private int b, l, w, area;
private double r, h;
public Area(){
}
public Area(double radio){
r = radio;
area = pi * r * r;
}
public Area(int base, double alt){
b = base;
h = alt;
double o5 = 0.5;
double db = (double) b;
area = o5 * db * h;
}
public Area(int lar, int anc){
l = lar;
w = anc;
area = l * w;
}
public Area(double radio, double alt){
r = radio;
h = alt;
area = pi * r * r * h;
}
}
public class JavaCLab2P144 {
/**
* #param args
*/
public static void main(String[] args){
Area circ = new Area(4.0);
Area tria = new Area(6,3.0);
Area rect = new Area(2,4);
Area cili = new Area(4.0,10.0);
System.out.println("Area de un circulo:\t" + circ);
System.out.println("Area de un triangulo:\t" + tria);
System.out.println("Area de un rectangulo:\t" + rect);
System.out.println("Area de un cilindro:\t" + cili);
}
}
This is the error I get:
java/JavaCLab2P144/JavaCLab2P144.java:14: error: possible loss of precision
area = pi * r * r;
^
required: int
found: double
java/JavaCLab2P144/JavaCLab2P144.java:22: error: possible loss of precision
area = o5 * db * h;
^
required: int
found: double
java/JavaCLab2P144/JavaCLab2P144.java:34: error: possible loss of precision
area = pi * r * r * h;
^
required: int
found: double
3 errors
You cannot implicitly convert from double to it since you will possibly lose precision as errors indicate. You need an explicit cast to it using cast operator : (int) after assignment operator and becaude of order of ops you should put parens around multiplications like so
Area= (int)(pi * r * r);
There are many other issues to consider in your class such as choice of it for area instead of double since result is a double. As other poster stated you should use double instead for area field. You also don't need to declare variable for pi since it'd already a static cost on math or at least make it private static final
Problem is that double can have values like 1.5 which in case you want to change into int could be 1 or 2 (or is just you're mistake in coding). So java tells you that you have to do something about it.
To solve it you can cast double to int, which cuts to full number
double x = 1.2;
double y = 1.7;
int a = (int) x; // a == 1
int b = (int) y; // b == 1
Other option (in you're case better) is using Math#round(double) function
double x = 1.2;
double y = 1.7;
// Math.round(double) return long, so you also have to cast it into int
int a = (int) Math.round(x); // a == 1
int b = (int) Math.round(y); // b == 2