Created
August 29, 2015 15:18
My first java programs. Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JFrame; | |
import org.math.plot.*; | |
public class Integral | |
{ | |
private double a; | |
private double b; | |
private double c; | |
// Constructor of the class | |
public Integral(double a, double b, double c) | |
{ | |
this.a = a; | |
this.b = b; | |
this.c = c; | |
} | |
// Returs a string with a simple description of what this class can do | |
public String description() | |
{ | |
String content = "This class lets you generate lines and parabolas" | |
+ "\nFurthermore you can:" | |
+ "\nCalculate definite integrals" | |
+ "\nEvaluate functions" | |
+ "\nPlot functions" | |
+ "\nOutput data (txt format)"; | |
return content; | |
} | |
// Lets you evaluate the line at x and optionally print the result (print=true) | |
public double line(double x,boolean print) | |
{ | |
double value = a*x + b; | |
if(print) | |
{ | |
System.out.println("Line: the value of y at x="+x+" is equal to: "+value); | |
} | |
return value; | |
} | |
// Same as above but for the parabola | |
public double parabola(double x, boolean print) | |
{ | |
double value = a*Math.pow(x,2) + b*x + c; | |
if(print) | |
{ | |
System.out.println("Parabola: the value of y at x="+x+" is equal to: "+value); | |
} | |
return value; | |
} | |
// Evaluate definite integral with a finite number of steps | |
public double approxIntegral(double initialp, double finalp, double steps, String function) | |
{ | |
double sum = 0; | |
double interval = (finalp-initialp)/steps; | |
if(function.equals("line")) | |
{ | |
while(initialp < finalp) | |
{ | |
sum = sum + interval*this.line(initialp,false); | |
initialp += interval; | |
} | |
return sum; | |
}else if(function.equals("parabola")) | |
{ | |
while(initialp < finalp) | |
{ | |
sum = sum + interval*this.parabola(initialp,false); | |
initialp += interval; | |
} | |
return sum; | |
}else | |
{ | |
String error = "Error, please insert either of the following:\nparabola\nline"; | |
System.out.println(error); | |
return 0; | |
} | |
} | |
// Plot the functions | |
public void plotFunction(String function,double from, double to,int npoints) | |
{ | |
if((from >= to) || (from==to) || (npoints==0)) | |
{ | |
System.out.println("Check input please! Something is wrong."); | |
return; | |
} | |
double step = Math.abs((to-from)/npoints); | |
Plot2DPanel plot = new Plot2DPanel(); | |
double x[] = new double[npoints]; | |
double y[] = new double[npoints]; | |
if(function.equals("line")) | |
{ | |
for(int i=0; i < npoints; i++ ) | |
{ | |
x[i] = from; | |
y[i] = this.line(from,false); | |
from += step; | |
} | |
plot.addLinePlot("Line 1",x,y); | |
}else if(function.equals("parabola")) | |
{ | |
for(int i=0; i < npoints; i++ ) | |
{ | |
x[i] = from; | |
y[i] = this.parabola(from,false); | |
from += step; | |
} | |
plot.addLinePlot("Parabola 1",x,y); | |
}else if(function.equals("both")) | |
{ | |
double y1[] = new double[npoints]; | |
for(int i=0; i < npoints; i++) | |
{ | |
x[i] = from; | |
y[i] = this.parabola(from, false); | |
y1[i] = this.line(from, false); | |
from += step; | |
} | |
plot.addLinePlot("Parabola 1",x,y); | |
plot.addLinePlot("Line1",x,y1); | |
}else | |
{ | |
System.out.println("Please check your input, function's name seems not ok."); | |
return; | |
} | |
// Plot name and frame settings | |
plot.setAxisLabel(0,"X"); | |
plot.setAxisLabel(1,"Y"); | |
JFrame frame = new JFrame("Graph"); | |
frame.setContentPane(plot); | |
frame.setVisible(true); | |
return; | |
} | |
// Find intersections between line and parabola | |
void findIntersections() | |
{ | |
double delta = Math.pow(this.b-this.a, 2)-4*this.a*(this.c-this.b); | |
if(delta < 0) | |
{ | |
System.out.println("Delta is negative, no solutions!"); | |
}else | |
{ | |
double x1 = (this.a - this.b + Math.sqrt(delta))/(2*this.a); | |
double x2 = (this.a - this.b - Math.sqrt(delta))/(2*this.a); | |
System.out.println("Intersections at:" | |
+ "\nx1: "+x1 + "y1: "+this.parabola(x1,false) | |
+ "\nx2: "+x2 + "y2: "+this.parabola(x2,false)); | |
} | |
} | |
// Self explanatory ;) | |
void findIntersectionsWithXaxis(String function) | |
{ | |
if(function.equals("parabola")) | |
{ | |
if(this.c==0) | |
{ | |
double x2 = -this.b/this.a; | |
System.out.println("x1: 0 y1: 0"); | |
System.out.println("x2: "+x2+" y2: "+this.parabola(x2,false)); | |
} | |
else | |
{ | |
double delta = Math.pow(this.b, 2)-4*this.a*this.c; | |
if(delta < 0) | |
{ | |
System.out.println("Negative delta, no solutions!"); | |
return; | |
}else | |
{ | |
double x1 = (-this.b + Math.sqrt(delta))/(2*this.a); | |
double x2 = (-this.b - Math.sqrt(delta))/(2*this.a); | |
System.out.println("x1: "+x2+"y1: "+this.parabola(x1,false)); | |
System.out.println("x2: "+x2+"y2: "+this.parabola(x2,false)); | |
} | |
} | |
}else if(function.equals("line")) | |
{ | |
double x1 = -this.b/this.a; | |
System.out.println("x1: "+x1+"y1: "+this.line(x1,false)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment