So I'm trying to program a way to solve linear equations in java but when doing math like 24.4-11.6 it has an error where it outputs 12.79 instead of 12.8 here's the code I got, I tried to work around the issue by converting the number to an int by multiplying it by 10000 and the doing the math, after which i divide it by 10000 but in the RtChngX method it seems to be outputting 0 :
import java.util.Scanner;
public class x_y_intercept_with_2_coords {
public static void main(String[] args) {
System.out.println("yinter or xinter");
Scanner scanner = new Scanner(System.in);
String interCheck = scanner.nextLine();
if(interCheck.equals("yinter")) {
float[] a = new float[2];
float[] b = new float[2];
System.out.println("first x coord");
a[0] = scanner.nextFloat();
System.out.println("second x coord");
a[1] = scanner.nextFloat();
System.out.println("first y coord");
b[0] = scanner.nextFloat();
System.out.println("second y coord");
b[1] = scanner.nextFloat();
backgroundWrkr one = new backgroundWrkr();
System.out.println("yinter = "+one.bWrkrOne(a, b));
System.out.println("slope = "+one.getSlope());
one.rateOfChangeX(a);
one.rateOfChangeY(b);
} else {
float[] a = new float[2];
float[] b = new float[2];
System.out.println("first x coord");
a[0] = scanner.nextFloat();
System.out.println("second x coord");
a[1] = scanner.nextFloat();
System.out.println("first y coord");
b[0] = scanner.nextFloat();
System.out.println("second y coord");
b[1] = scanner.nextFloat();
backgroundWrkr one = new backgroundWrkr();
System.out.println("xinter = "+one.bWrkrTwo(a, b));
System.out.println("slope = "+one.getSlope());
}
}
public class backgroundWrkr {
float slope;
float rtChngX;
float rtChngY;
backgroundWrkr(){
}
public float bWrkrOne(float[] a, float[] b) {
float xslope = a[1]-a[0];
float yslope = b[1]-b[0];
slope = yslope/xslope;
float yinter = b[0]-slope*a[0];
return yinter;
}
public float bWrkrTwo(float a[], float b[]) {
float xslope = a[1]-a[0];
float yslope = b[1]-b[0];
slope = yslope/xslope;
float yinter = b[0]-slope*a[0];
float xinter = yinter*-1/slope;
return xinter;
}
public float getSlope() {
return slope;
}
public void rateOfChangeX(float a[]) {
float gaeA = a[1]*1000;
System.out.println(gaeA);
float gaeB = a[0]*1000;
System.out.println(gaeB);
int GaeA = (int)gaeA;
System.out.println(GaeA);
int GaeB = (int)gaeB;
System.out.println(GaeB);
float temp = GaeA-GaeB;
System.out.println(temp);
rtChngY = temp/1000;
System.out.println("rtChngX = "+rtChngX);
}
public void rateOfChangeY(float b[]) {
float gaeA = b[1]*10000;
float gaeB = b[0]*10000;
int GaeA = (int)gaeA;
int GaeB = (int)gaeB;
int temp = GaeA-GaeB;
rtChngY = temp/10000;
System.out.println("rtChngY = "+rtChngY);
}
}