This are other problems I ran into that is having the same issue. Can anyone point out my logic error in the following below:
Question: A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree... (Exercise 4.5)
This was my response:
import java.util.Scanner;
public class Exercise04_05 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of sides: ");
double n = input.nextDouble();
System.out.print("Enter the side: ");
double s = input.nextDouble();
double area = n * Math.pow(s, 2) / 4 * Math.tan(Math.PI / n );
System.out.println("The area of the polygon is " + area);
}
}
Please explain where the Logic error is. i commented out the code or else I'll keep getting errors preventing me from submitting the question.
Your formula is wrong and it contains an error.
First / 4 is an integer division which is rounded to an int, so you get mostly an error. You have to use / 4.0 so you get the right division.
The formula for calculation the area of a polygon is
A = 1 / 4 * n * s2 * cot(PI / n)
Note it is cotangens, not tangens. As java.math has no cotangens you have to calculate it by yourself, e.g. 1 / Math.tan(x).
So finally this should work for you:
double area = n / 4.0 * Math.pow(s, 2) / Math.tan(Math.PI / n );
Related
My task is to implement the cos(x) function withou using Math. library and with the taylor polynom, my code looks like this:
public class Cosinus {
public static void main(String[] args) {
/*if(args.length == 0){
System.out.println("ERROR: Geben Sie ein Argument für x ein!");
return;
}*/
double x = 5;
double summand1 = (x*x) / 2;
double summand2 = (x*x*x*x) / (2*3*4);
double summand3 = (x*x*x*x*x*x) / (2*3*4*5*6);
double summand4 = (x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8);
double summand5 = (x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10);
double summand6 = (x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12);
double summand7 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14);
//double summand8 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
//double summand9 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
//double summand10 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
double cosinusFunktion = (((((((1 - summand1) + summand2) - summand3) + summand4) - summand5) + summand6) - summand7);
System.out.println(cosinusFunktion);
}
}
For x = 1, 2, 3, and 4 Y is between 1 and -1
but with x = 5 it goes too -4 and if the x are even getting bigger this continues too 1287918274.
I cant solve this task but tthe task says it is enough to implement this funktion iwth the taylor polynom and the first 11 summand. I tried this too, but then even with x = 1 the bounds are broken. How can i solve this, so x = 42.5 is in bound of -1 and 1?
Tried more summands to make the result more excact, but the bounds get broken even more.
tried implement the periodicity of x-2*PI, but I dont know where to put it and results get messed up eeven more.
you are getting an integer overflow for the factorial in the summand7 line
as a simple fix you can change the line to:
double summand7 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x) / ((double) 2*3*4*5*6*7*8*9*10*11*12*13*14);
The Taylor expansion will always blow up for larger inputs. However, since:
sin(x) = sin(x + n*2*pi) // for any integer n
You can simply pre-process you input with a modulus function to prevent your output from blowing up.
I can't test compile right now, but if memory serves, you would add one of the following lines prior to computing your first summand:
x = x%(Math.PI*2)
Or, if you can't use Math
x = x%((double)3.14159265358979323846*2)
I declared a double variable as below:
double x=56.27d
And then all I tried to do is the following: (56.27*10*10)
System.out.println(xE2);
And this is not working.
There isn't an 'E' operator in java. This would conflict with variable names for one thing. double xE2=x*1e2; In this case, xE2 is a variable name, but I do use 1e2 as a java literal.
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
double d = sc.nextDouble(); // Get input from the user
System.out.println(d * 10 * 10);
// Displaying it using scientific notation
// Format the number rounded up to two places after decimal and into scientific notation
System.out.println(String.format("%.2e", d * 10 * 10));
// Examples of e or E with double literals
double x = 1e2; // 1 * 10 to the power of 2 = 100.0
System.out.println(x);
double y = 1E2; // 1 * 10 to the power of 2 = 100.0
System.out.println(y);
double z = 1e+02; // 1 * 10 to the power of 2 = 100.0
System.out.println(z);
System.out.println(d * 1e2);// i.e. d * 100.0
}
}
A sample run:
Enter a number: 56.27
5627.0
5.63e+03
100.0
100.0
100.0
5627.0
Notes:
Check this to learn more about Formatter.
E or e can be used only with double literals, not double variables.
Feel free to comment in case of any doubt/issue.
You shouldn't use the exponent notation since it is a part of double literal. Try multiplying.
System.out.println(x * 10 * 10)
I suggest you to read java.util.Math library documentation, it includes many scientific functions like Exponents: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
This is the value of the double "m" in my program (the mass of a planet after calculations, this value is specifically for the mass of Earth)
5.973405437304745E24
When printing using System.out.println(m);
The output is
5.973405437304745E24 (correct output)
When printing using System.out.println(Math.round(m));
The output is
9223372036854775807 (incorrect output)
How am I able to shorten the value of m so it fits within %6s?
Like this for example
5.97E24
This is my code below. The assignment requires us to output the final values in a formatted chart (as I've attempted to do below)
//Java imports
import java.util.*;
import java.lang.Math;
//Main class
class Main {
public static void main(String[] args) {
//Initializing scanner name userInput
Scanner userInput = new Scanner (System.in);
//Greeting statement, prompts user to input the circumference in km
System.out.println("\nWelcome to the Escape Velocity Application. To begin, please enter the following information below. \nEnter the circumference (km):");
//Stores circumference in double named "circum" and converts the value to its meter equivalent (unit conversion required)
double circum = userInput.nextDouble() * Math.pow(10, 3);
//Prompts user to input the acceleration in m/s^2
System.out.println("Enter the acceleration due to gravity (m/s^2):");
//Stored value in double named "f"
double f = userInput.nextDouble();
//Gravitational Constant
double G = 6.67408e-11;
//1 - Radius calculation using the circumference of a circle formula
double r = circum/(2*Math.PI);
//2 - Mass calculation using the gravity formula
double m = f*Math.pow(r, 2)/G;
//3 - Calculation escape velocity using the escape velocity formula
double e = (Math.sqrt((2.0*G*(m))/r));
//Final output statements
System.out.println("\nThe radius is: " + Math.round(r * Math.pow(10, -3)) + " kilometers.");
System.out.println("The mass is: " + m + " kg.");
System.out.println("The escape velocity is: " + Math.round(e) + " m/s.");
//Formatted output statements
System.out.format("\n%20s %6s %10s", "Radius:", Math.round(r * Math.pow(10, -3)), "km.");
System.out.format("\n%20s %6s %10s", "Mass:", Math.round(m), "kg.");
System.out.format("\n%20s %6s %10s", "Escape Velocity:", Math.round(e), "m/s.");
}
}
This is what the output looks like. The center of the second line is offset due to the long value of m.
The radius is: 6378 kilometers.
The mass is: 5.973405437304745E24 kg.
The escape velocity is: 11181 m/s.
Radius: 6378 km.
Mass: 9223372036854775807 kg.
Escape Velocity: 11181 m/s.
You could use the following code:
double x = 5.973405437304745e24;
System.out.printf("Mass: %.2e kg.%n", x);
Which outputs Mass: 5.97e+24 kg..
%.2e formats the number and %n just adds a newline character. The .2 specifies that two decimal places after the dot are desired. The e requests scientific notation from the formatter.
The problem with Math.round() is, that the result is stored in a long which cannot represent such a large number.
I'm frustrated with this error I'm getting on my assignment. The code I have is clearly correct and running, but I'm still getting this error that the Standard Output was not what was expected. This is the question for that particular assignment:
(Algebra: solve quadratic equations) The two roots of a quadratic equation
ax^2 + bx + c = 0 can be obtained using the following formula:
b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the
equation has two real roots. If it is zero, the equation has one root. If it is negative,
the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displays
the result based on the discriminant. If the discriminant is positive, display two
roots. If the discriminant is 0, display one root. Otherwise, display “The equation
has no real roots”.
Note that you can use Math.pow(x, 0.5) to compute 2x.
And this is my response:
import java.util.Scanner;
public class Exercise03_01 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double discriminant = Math.pow(b, 2) - 4 * a * c;
if (discriminant > 0) {
double r1 = -b + Math.pow(discriminant, 0.5) / (2 * a);
double r2 = -b - Math.pow(discriminant, 0.5) / (2 * a);
System.out.println("The equation has two roots" + r1 + " and " + r2);
}
else if (discriminant == 0) {
double r1 = -b + Math.pow(discriminant, 0.5) / (2 * a);
System.out.println("The equation has one root " + r1);
}
else {
System.out.println("The equation has no real roots ");
}
}
}
Can you explain what "standard output was not what was expected" means? I this resolved as soon as possible because this assignment is due this saturday.
Thanks everyone!
I two faced such issues while submitting my assignments online. Most basic cause for which your answer was not accepted by system could be one of these :
Check for whether you need to use print or println to satisfy the conditions.
Check for decimal precisions required to match the test cases.
Check for the correctly displayed sentence(Alphabetically) to match the output.
Let us know if you found it helpful.
Here's the exercise:
35. Write a Java program to compute the area of a polygon. Go to the editor
Area of a polygon = (n*s^2)/(4*tan(π/n))
where n is n-sided polygon and s is the length of a side
Input Data:
Input the number of sides on the polygon: 7
Input the length of one of the sides: 6
Expected Output
The area is: 130.82084798405722
My code returns 127.30573435631248 (length = 7, sides = 6)
The code on the website returns 130.82084798405722 (length = 7, sides = 6)
I'm having trouble seeing why mine is different from theirs...
Any ideas?
Here's my code:
public static void exercise35(){
int number1 = integerInput(); //set to 7, length
int sides = integerInput(); //set to 6, sides
double area = (sides * (number1 * number1)) / (4.0 * Math.tan((Math.PI / sides)));
System.out.println("The area of a polygon with " + sides + " sides of length " + number1 + " = " + area);
}
Here's the solution from http://www.w3resource.com/java-exercises/basic/index.php (exercise #35)
import java.util.Scanner;
public class Exercise35 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input the number of sides on the polygon: ");
int ns = input.nextInt();
System.out.print("Input the length of one of the sides: ");
double side = input.nextDouble();
System.out.print("The area is: " + polygonArea(ns, side)+"\n");
}
public static double polygonArea(int ns, double side) {
return (ns * (side * side)) / (4.0 * Math.tan((Math.PI / ns)));
}
}
You mixed sides and number in the formula.
That teaches an interesting lesson of programming: "Give sensible names to variables".
Change 6 and 7 and you'll get the answer you want : ) And in the future, please try debugging your code first to see what's going on.