I'm making a simple test in java, (i'm a begineer) and i was trying to make a program that calculates the discriminate of a second degree equation and displays the results, but when i change the sign of nom1 and nom2, the program doesn't displays any result, i've made some tests and i'm sure that the problem is there but i don't know how to resolve it.
Any help is welcome.
Here is my code:
public class test {
public static void main(String[] args) {
double a = 2;
double b = 5;
double c = 3;
double delta = b*b - 4 * a * c;
double den = 2 * a;
if(delta == 0){
double nom = b;
double pgcd = pgcd(nom, den);
double x0nom = nom/pgcd;
double x0den = den/pgcd;
System.out.println("x0 = "+String.valueOf(x0nom)+" / "+String.valueOf(x0den));
}else if(delta > 0){
// x1:
double nom1 = -b + Math.sqrt(delta);
double pgcd1 = pgcd(nom1, den);
int x1nom = (int) (nom1 / pgcd1);
int x1den = (int) (den / pgcd1);
// x2:
double nom2 = -b - Math.sqrt(delta);
double pgcd2 = pgcd(nom2, den);
int x2nom = (int) (nom2 / pgcd2);
int x2den = (int) (den / pgcd2);
System.out.println("pgcd = "+String.valueOf(pgcd1)+" "+String.valueOf(pgcd2));
System.out.println("x1 = "+String.valueOf(x1nom)+" / "+String.valueOf(x1den));
System.out.println("x2 = "+String.valueOf(x2nom)+" / "+String.valueOf(x2den));
}else if(delta < 0){
}
}
public static double pgcd(double a, double b) {
while (a != b) {
if (a < b){
b = b - a;
}else{
a = a - b;
}
}
return a;
}
}
Use BigInteger's gcd method
public static double pgcd(double a, double b) {
return (BigInteger.valueOf((long) a).gcd(BigInteger.valueOf((long) b)).intValue());
}
Related
I have a class definition for a class of rational numbers. My assignment is to be able to add, multiply and divide any fraction I put in my main function. My program can do all that, but I'm having trouble simplifying the fractions. I want to try and use only two methods to simplify, for example public void reduce(); and private static gcd();
public class Rational {
private int num;
private int denom;
public Rational() {
num = 0;
denom = 1;
}
public Rational(int n, int d) {
num = n;
denom = d;
reduce();
}
public Rational plus(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = (this.num * t.denom) + (this.denom * t.num);
tdenom = (t.denom * this.denom);
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational minus(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = (this.num * t.denom) - (this.denom * t.num);
tdenom = (t.denom * this.denom);
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational multiply(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = this.num * t.num;
tdenom = t.denom * this.denom;
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational divide(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = this.num / t.num;
tdenom = this.denom / t.denom;
Rational r = new Rational (tnum, tdenom);
return r;
}
private static int gcd(int n, int d) {
return gcd(d, n%d);
}
public void reduce() {
//call gcd
gcd(num, denom);
//divide num and denom by gcd by
num = num / gcd(num,denom);
denom = denom / gcd(num,denom);
}
public String toString() {
return String.format("%d/%d", num, denom);
}
}
public class RationalMain {
public static void main(String[] args) {
Rational x = new Rational();
Rational y = new Rational(1,4);
Rational z = new Rational(1,2);
//x = y - z;
x = y.plus(z);
System.out.printf("%s = %s + %s\n", x.toString(), y.toString(), z.toString());
x = z.minus(y);
System.out.printf("%s = %s - %s\n", x.toString(), z.toString(), y.toString());
x = z.multiply(y);
System.out.printf("%s = %s * %s\n", x.toString(), z.toString(), y.toString());
x = y.divide(z);
System.out.printf("%s = %s / %s\n", x.toString(), y.toString(), z.toString());
}
}
That's not how you might achieve the Greatest Common Divisor (GCD). Before you will be able to get your code to work properly you will need to at least fix your gcd() method since currently it will recurse until an ArithmeticException (/ by zero) is generated. You might achieve the task this way:
private static int gcd(int num, int den) {
num = Math.abs(num); // if numerator is signed convert to unsigned.
int gcd = Math.abs(den); // if denominator is signed convert to unsigned.
int temp = num % gcd;
while (temp > 0) {
num = gcd;
gcd = temp;
temp = num % gcd;
}
return gcd;
}
To convert your fractions too their Lowest Terms your reduce() method might look like this if it accepted a Fraction String as an argument (you modify the method parameters if you like):
/*
A Fraction String can be supplied as: "1/2", or "2 1/2", or
"2-1/2, or "-2 32/64", or "-2-32/64". The last 2 examples are
negative fraction values.
*/
private String reduce(String fractionString) {
// Fraction can be supplied as: "1/2", or "2 1/2", or "2-1/2".
// Make sure it's a Fraction String that was supplied as argument...
inputString = inputString.replaceAll("\\s+", " ").trim();
if (!inputString.matches("\\d+\\/\\d+|\\d+\\s+\\d+\\/\\d+|\\d+\\-\\d+\\/\\d+")) {
return null;
}
str2 = new StringBuilder();
String wholeNumber, actualFraction;
if (inputString.contains(" ")) {
wholeNumber = inputString.substring(0, inputString.indexOf(" "));
actualFraction = inputString.substring(inputString.indexOf(" ") + 1);
str2.append(wholeNumber);
str2.append(" ");
}
else if (inputString.contains("-")) {
wholeNumber = inputString.substring(0, inputString.indexOf("-"));
actualFraction = inputString.substring(inputString.indexOf("-") + 1);
str2.append(wholeNumber);
str2.append("-");
}
else {
actualFraction = inputString;
}
String[] tfltParts = actualFraction.split("\\/");
int tfltNumerator = Integer.parseInt(tfltParts[0]);
int tfltDenominator = Integer.parseInt(tfltParts[1]);
// find the larger of the numerator and denominator
int tfltN = tfltNumerator;
int tfltD = tfltDenominator;
int tfltLargest;
if (tfltNumerator < 0) {
tfltN = -tfltNumerator;
}
if (tfltN > tfltD) {
tfltLargest = tfltN;
}
else {
tfltLargest = tfltD;
}
// Find the largest number that divides the numerator and
// denominator evenly
int tfltGCD = 0;
for (int tlftI = tfltLargest; tlftI >= 2; tlftI--) {
if (tfltNumerator % tlftI == 0 && tfltDenominator % tlftI == 0) {
tfltGCD = tlftI;
break;
}
}
// Divide the largest common denominator out of numerator, denominator
if (tfltGCD != 0) {
tfltNumerator /= tfltGCD;
tfltDenominator /= tfltGCD;
}
str2.append(String.valueOf(tfltNumerator)).append("/").append(String.valueOf(tfltDenominator));
return str2.toString();
}
As you can see, a whole number can also be supplied with your fraction so, if you do a call to the above reduce() method like this:
System.out.println(reduce("12-32/64"));
System.out.println(reduce("12 32/64"));
The console window will display:
12-1/2
12 1/2
so I am trying to build this ADT for rational numbers in java but for some reason I keep getting this error that says cannot find symbol when trying to compile. What em I doing wrong? Is this error because of my syntax?
Author: Juan Suarez
// Class : CS1102 ~ java
// Date : 01/30/2018
// Topic : This porblem set focuse on the implemantation of an
// ADT for rational numbers.
public class RationalC implements Rational {
private int num;
private int den;
// ****************** CONSTRUCTOR **********************************
public RationalC (int numerator, int denominator) {
if (this.den == 0){
throw new ArithmeticException("*** WARNING! input non zero denominator");
}
int reduceFraction = gcd(numerator, denominator);
this.num = numerator / reduceFraction;
this.den = denominator / reduceFraction;
if (this.den < 0) {
this.den = this.den * -1;
this.num = this.num * -1;
}
}
//********************* GETTERS ************************************
public int getNumerator() { return this.num; }
public int getDenominator() { return this.den; }
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
// ******************* OPERATIONS **********************************
//return this + that
//
public RationalC plus(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 + num2;
return new RationalC (complete, commonDenominator);
}
//returns this - that
//
public RationalC subtract(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 - num2;
return new RationalC (complete, commonDenominator);
}
// return this * that
//
public Rational multiply(Rational b){
int top = this.getNumerator() * b.getNumerator();
int bottom = this.getDenominator() * b.getDenominator();
return new RationalC (top, bottom);
}
//return this / that
//
public Rational divide(Rational b){
int top = this.getNumerator() * b.getDenominator();
int bottom = this.getDenominator() * b.getNumerator();
return new RationalC (top, bottom);
}
//retuns value
//
public boolean equals(Rational b) {
if (num == b.getNumerator() && this.getDenominator() == b.getDenominator() )
return(true);
}
//********************* TOOLS **************************************
//returns the rational number to a string
//
public String toString() {
return "(" + this.num + "," + this.den + ")";
}
//returns -1 , 0, +1 if the value of the rational is <, >, or =
//
public int compareTo(Rational b) {
long leftHand = this.getNumerator() * b.getDenominator();
long rightHand = this.getDenominator() * b.getNumerator();
if (leftHand < rightHand) return -1;
if (leftHand > rightHand) return +1;
return 0;
}
private static int gcd(int m, int n) {
if(m < 0) m = -m;
if(n < 0) n = -n;
return m * (n / gcd(m,n));
}
public Rational reciprical(Rational b){
return new RationalC (this.getDenominator(), this.getNumerator() );
}
//******************* TEST UNIT ************************************
public static void main(String[] args) {
x = new Rational (1,2);
y = new Rational (1,3);
z = x.plus(y);
StdOut.println(z);
}
}
In the below piece of code, you didn't declare local variable v.
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
getNumerator and getDenominator are methods.
Call them as this.getNumerator() and this.getDenominator().
Also make sure Rational class is having getNumerator and getDenominator methods.
I'm trying to write own code for calculating approximation of rad angle. I works so far for only a specific range of numbers, but fails for large ones like 500 or so.
Additional subquestion: which is more efficient - calculating powers by Math.pow() or the current way of doing it - only multiplication operators.
private static double sin_range(double rad) {
double sin_rad = rad;
while (sin_rad > 2 * Math.PI) {
sin_rad -= 2 * Math.PI;
}
return sin_rad;
}
private static double approx(double rad, int err) {
double rad_in_range = sin_range(rad);
double sin = rad_in_range, r = rad_in_range;
int previous = 1, factorial = 1;
for (int i = 0; i < err; i++) {
factorial = (factorial * (previous + 1) * (previous + 2));
r *= rad * rad;
if ((i & 1) == 0) { //even
sin -= r / factorial;
} else { //odd
sin += r / factorial;
}
previous += 2;
}
return sin;
}
public static void main(String[] args) {
double approximated = approx(15, 5);
System.out.println(approximated + " = " + Math.sin(15));
}
I want to convert this method into one loop, any kind of loop is fine i find it hard to convert from recursion.
public class Problem5
{
public double getRoot(double a, double b)
{
double x = (a + b)/2;
if (b - a <= 0.00)
return x;
double y = getValue(x);
if(y < 0)
return getRoot(x, b);
else
return getRoot(a, x);
}
public double getRoot(double a, double b) {
while (true) {
double x = (a + b)/2;
if (b - a <= 0.00)
return x;
double y = getValue(x);
if(y < 0)
a = x;
else
b = x;
}
throw new AssertionError("should not happen");
}
I'm using Wallis' method to calculate pi, and I think I did it right. At least I thought I did anyway. I think the problem (output is 0)has to do with rounding and remainders, though I can't be sure. Here's the code:
import java.util.Scanner;
public class WallisPi {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = 2;
int b = 3;
int c = 1;
int pi = 0;
double acc = 0.0;
int n = scan.nextInt();
scan.close();
for (int i = 0; i <= n; i++) {
pi = (2 / 3) * c;
if (a > b) {
b += 2;
} else {
a += 2;
}
c = a / b;
}
pi *= 4;
System.out.println("The approximation of pi is " + pi + ".");
acc = Math.PI - pi;
System.out.println("It is " + acc + " off.");
}
}
Since posting this I've made some changes to the code, though it's still not quite functional. I get 2.666..., so there's something else at work here as well.
import java.util.Scanner;
public class WallisPi {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a = 2.0;
double b = 3.0;
double c = 1.0;
double pi = 0;
double acc = 0.0;
int n = scan.nextInt();
scan.close();
for (int i = 0; i <= n; i++) {
pi = (2.0 / 3.0) * c;
if (a > b) {
b += 2;
} else {
a += 2;
}
c = a / b;
}
pi *= 4;
System.out.println("The approximation of pi is " + pi + ".");
acc = Math.PI - pi;
System.out.println("It is " + acc + " off.");
}
}
int a=2;
int b=3;
double pi=2;
for(int i=0;i<=n;i++){
pi *= (double)a/(double)b;
if(a>b){
b+=2;
} else {
a+=2;
}
}
pi*=2;
Using n = 4000 yields 3.141200
Here's the whole program, fixed:
import java.util.Scanner;
public class WallisPi {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
double pi = 2;
int a = 2;
int b = 3;
for (int i = 0; i <= n; i++){
pi *= (double) a / (double) b;
if (a > b) {
b += 2;
} else {
a += 2;
}
}
pi *= 2;
double acc = Math.PI - pi;
System.out.println("The approximation of pi is " + pi + ".");
System.out.println("It is " + acc + " off.");
}
}
Since your varibles are ints, all your divisions are integer divisions, omitting the fraction (and preserving only the whole part of the result). For accurate results, you should define your variables as doubles:
double a=2;
double b=3;
double c=1;
double pi=0;