Loading [MathJax]/extensions/MathZoom.js

Saturday, 13 December 2014

Here again Java

Java, Java and Java again! Here is my last script in Java, which I wrote today.

The script is quite simple, it is composed of three classes and lets you handle some basic calculations with lines and parabolas on the real plane and calculate the area under the curve (definite integrals).

Optionally you can plot the data.

file

Here is the main class with a simple example:

public class Principal
{
public static void main(String[] args)
{
// Let's generate our line and parabola
Integral f1 = new Integral(1,2,3);
System.out.println(f1.description());
System.out.println("\nIntersections between line and parabola");
f1.findIntersections();
System.out.println("\nIntersections of line with x axis");
f1.findIntersectionsWithXaxis("line");
System.out.println("\nIntersections of parabola with x axis");
f1.findIntersectionsWithXaxis("parabola");
// Integral of the parabola
System.out.println("\nDefinite integral of the parabola from 0 to 10:");
System.out.println(f1.approxIntegral(0,10,100000,"parabola"));
// Plot of the function
f1.plotFunction("both",-10,10,20);
}
}

And the integral class which handles all the background operations

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));
}
}
}

Optionally, should you want to output something to a .txt file, here is the class that can help in doing that:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class GetOutData
{
private String content;
// if you need to print a string
public GetOutData(String output)
{
this.content = output;
}
public void writeString(String path)
{
try
{
File file = new File(path);
if(!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(this.content);
bw.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}

Bye until the next project! Occhiolino

No comments:

Post a Comment