Having issue with my java program? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
EDIT 1:
program should output this:
java CircleBugs 1.2 0.6 r = 1.2, t = 0.6 c = 7.5398223686155035 a = 4.523893421169302 x = 0.9904027378916139, y = 0.6775709680740424
My error:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0
at debug.Debug.main(Debug.java:24)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
I am having an issue debugging this program:
public static void main(String[] args) {
double t = Double.parseDouble(args[0]);
int r = Integer.parseInt(args[]);
System.out.println("r = " + r + ", t = " + t); //Added the ";" to close the line of code
double c = 2 * Math.PI * r;
double A = Math.PI * r * r;
double x = r * Math.cos(Math.toRadians(t)); //fixed
double y = r * Math.sin(Math.toRadians(t)); //fixed
System.out.println("c = " + c );
System.out.println("A = " + A );
System.out.println("x = " + x + ", " + "y = " + y );
}

When using Double.parseDouble(args[0]) it assumes that you are passing values in command line for this arg values otherwise the array is empty(so you get indexoutofboundexception while calling the first element from an empty array). So just change code like this
double t = Double.parseDouble(args[0]);
int r = Integer.parseInt(args[1]);
And in command line execute this.
java test 5 4 //5 in place of t and 4 in place of r
I saved my file as test.java so replace test with your file name and run.

The args array is empty, and is causing this error.
The problem is that you don't give your program any inputs, so you try to parseDouble(args[0]) on an empty array.
Use something like this:
Scanner sc = new Scanner(System.in);
double t = sc.nextDouble();
int r = sc.nextInt();
Also you should use the methods like:
Math.cos(Math.toRadians(t))
Math.sin(Math.toRadians(t))

You have to use Math.cos() and Math.sin() methods.
double x = r * Math.cos(t);
double y = r * Math.sin(t);

Related

How to solve imaginary quadratic roots in java?

I've been trying a lot but it only shows NaN. I'm not sure if I'm doing the right thing.
class Imaginary{
double a = 2;
double b = 3;
double c = 5;
double result = b * b - 4 * a * c;
if(result < 0.0){
double im1 = -2 + (Math.sqrt((result))/ 10);
double im2 = -2 - (Math.sqrt((result))/ 10);
System.out.println("x = " + imaginary1 + " or x = " + imaginary2);
}
}
You need to take negate result to make it positive before taking the square root (taking square roots of negative numbers always results in NaN) and append "i" before printing.
double real = -b / (2*a);
double img = Math.sqrt(-result) / (2*a);
System.out.println("x = " + real + " + " + img +"i or x = " + real + " - " + img + "i");
You shouldn't use sqrt(result) since it will always result in you taking the square root of a negative number (that is your condition for result). Instead try to use a formula (eg completing the square).
Hope it answers your question :)
Since you have a complex root, you need to work with complex numbers to solve the equation. Java lacks builtin support for complex numbers, but you can e.g. use Apache Commons:
if (result < 0.0) {
final Complex cb = new Complex(-b, 0.0);
final Complex root = new Complex(result, 0.0).sqrt();
final Complex r1 = cb.add(root).divide(2 * a);
final Complex r2 = cb.subtract(root).divide(2 * a);
}

Java Population Growth Loop and Method; Improper Output

My task is to create a program that models population growth using the formula x_new = r * x_previous * (1-x_previous), where r is the fecundity parameter and x is the population. I have a working program and method. However, my math isn't checking out. I should be able to test for an initial population of .01, a fecundity parameter of 1.1, 1000 time steps and get an answer around .09).
Here's my program:
import java.util.Scanner;
public class Lab6Question3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("The population, p: ");
double p = input.nextDouble();
System.out.println("The fecundity rate, r: ");
double r = input.nextDouble();
System.out.println("Number of Time Steps, n: ");
int n = input.nextInt();
int i; //iterations
System.out.println("-------------------------------------");
System.out.println("Simulation with starting population " + p);
System.out.println("Running " + n + " steps");
System.out.println("Varying fecundicity 1, 1.1, ..., 4.9, 5");
for (i = 0; i < n; i++) {
System.out.println("r = " + r + " final population = " + p);
r = r + 0.1;
p = populationGrowth(p, r);
}
}
public static double populationGrowth(double population, double fecundicity)
{
double p;
return (fecundicity * population * (1 - population));
}
}
And my output is correctly formatted with the iterations, but the population results is wrong:
Simulation with starting population 0.01
Running 10000 steps
Varying fecundicity 1, 1.1, ..., 4.9, 5
r = 1.0 final population = 0.01
r = 1.1 final population = 0.01089
r = 1.2000000000000002 final population = 0.012925689480000004
r = 1.3000000000000003 final population = 0.01658620084090661
.
.
.
Any ideas what could be the cause of the pain? Thanks in advance!
When r reach 5.2 about iteration 43 your growth function goes to negative numbers, because x_previous is > 1 ... so 1-x_previous < 0, are you sure that model is right...it seems a conceptual problem. try to model it in an excel.
Another thing, you increment you r before calculate next p value. Are you sure is that right?

Variables and Assignment statements [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im having a hard time finishing the problem. I don't know what to do with the int and double values for the calculations. How would i go through with solving them. I am using Jgrasp for my class.
Calculations:
Standard: (20.0 + 40.0 + 5.0 + 60.0 + 30.0 ) / 5
Weighted: 20.0 * 0.15 + 40.0 * 0.1 + 4.0 * 0.2 + 60.0 * 0.25 + 30.0 * 0.3
standard carpet cost: $620.0
weighted carpet cost: $640.0
The Code that I have is
import javax.swing.JOptionPane;
public class Project2A
public static void main(String[] args)
{
int sqftbrm = 20;
int sqftdrm = 40;
int sqfthal = 5;
int sqftgrm = 60;
int sqftstr = 30;
double bf = .15;
double df = .10;
double hf = .20;
double gf = 0.25;
double sf = .30;
char stndCst[] = {"(sqftbrm + sqftdrm +sqfthal + sqftgrm + sqftstr)"/5};
String stdcst = new String(stndCst);
System.out.print(stdcst);
char wtAvg[] = {"sqftbrm*bf" + "sqftdrm*df" + "sqfthal*hf" + "sqftgrm*gf" + "sqftstr*sf");
String wtAverage = new String(wtAvg);
System.out.print(wtAverage);
}
Outside of the fact that you should probably be using an IDE to easily identify your mistakes.... Semi colon on main method, missing brackets, invalid variable types, etc.
All you have is two arrays with one string value each.
char stndCst[] = {"(sqftbrm + sqftdrm +sqfthal + sqftgrm + sqftstr)"/5};
char wtAvg[] = {"sqftbrm*bf" + "sqftdrm*df" + "sqfthal*hf" + "sqftgrm*gf" + "sqftstr*sf"} ;
If you want to do math with some variables, then remove the quotes.
For example,
double stndCst = (sqftbrm + sqftdrm +sqfthal + sqftgrm + sqftstr) / 5.0;
double wtAvg = sqftbrm*bf + sqftdrm*df + sqfthal*hf + sqftgrm*gf + sqftstr*sf ;

Is there a way to show the step by step computation of a calculation in Java?

I am currently building an Android app that has a calculator that not only shows the result, but also shows how it reached that result?
Is there any library or any way that I could show a step by step computation for the result of the code below?
int a = 5;
int b = 6
int c = 7;
int d = 8;
int result = a + (b * c) / d;
edit: By the way, it's a calculator for physics so I have lots of formulas. I'm using exp4j to parse a string formula as an expression. Here's a sample
//For formula velocity = (finalVelocity - initialVelocity) / time
String formula1 = "(finalVelocity - initialVelocity) / time";
Double result1 = new ExpressionBuilder(formula)
.variables("finalVelocity", "initialVelocity", "time")
.build()
.setVariable("finalVelocity", 4);
.setVariable("initialVelocity", 2);
.setVariable("time", 2)
.evaluate();
//Sample Output
//velocity = (4 - 2) / 2
//velocity = 2 /2
//velocity = 1
//For formula finalVelocity = (velocity * time) + initialVelocity
String formula2 = "(velocity * time) + initialVelocity";
Double result12 = new ExpressionBuilder(formula)
.variables("velocity", "time" "initialVelocity")
.build()
.setVariable("velocity", 4);
.setVariable("time", 2)
.setVariable("initialVelocity", 0)
.evaluate();
//Sample Output
//finalVelocity = (4 * 2) + 0
//finalVelocity = 8 + 0
//finalVelocity = 8
With many formulas, I'm trying to eliminate printing each step per formula. I'm trying to find a way to have a function that would print the steps for any formula.
Considering you will be using BODMAS to solve that problem, you could consider simply printing each step:
int a = 5;
int b = 6;
int c = 7;
int d = 8;
int ans = (b * c);
System.out.println(ans)
ans /= d;
System.out.println(ans)
ans += a;
System.out.println(ans)
To do this, you can build a function which searches for brackets first and solves the equations in the according to BODMAS(Brackets first, then Division, then Multiplication, then Addition, and finally Subtraction).
Considering you take the equation as a string, you first search for (), then solve / and print answers followed by *, + and -.

Approximating Pi in java using Gauss-Legendre algorithm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Just started picking up java and tried to write a simple piece of code to display an approximation of Pi based on the Gauss-Legendre algorithm; the result I get in the command line from the code below is -33.343229... which seems very odd to me. I am aware that my code is not complete as I do not have a restriction on the number of digits after the decimal point, which I was going to try and get from using BigDecimal. I don't quite understand what I am supposed to do with it though after reading the documentation!
Does anyone have any idea if there are any current mistakes I can fix and also how to implement a restriction on the number of digits after the decimal point? Thank you!
class calculatePi {
public static void main(String[] args) {
calculatePi x = new calculatePi();
double y = x.approxPi(3); //3 iterations
System.out.print(y);
}
double approxPi(int i) {
double a = 1; //The initial conditions for the iteration
double b = 1 / Math.sqrt(2);
double t = 1/4;
double p = 1;
double a1 = 0; //The internal n+1 terms for the iteration
double b1 = 0;
double t1 = 0;
double p1 = 0;
while (i > 0) {
a1 = (a + b) / 2;
b1 = Math.sqrt(a*b);
t1 = t - p*(a - a1)*(a - a1);
p1 = 2*p;
a = a1;
b = b1;
t = t1;
p = p1;
i = i - 1;
}
double applepie = ((a + b)*(a + b))/(4*t);
return applepie;
}
}
double t = 1/4; does an integer division for 1/4, which results in 0. Try 1/4.0.

Categories