Aggiunto Polinomio

This commit is contained in:
Giacomo R. 2023-02-20 16:26:28 +01:00
parent c4b5cdb2e9
commit efa134deec
7 changed files with 91 additions and 0 deletions

3
polinomio/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

6
polinomio/.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/polinomio.iml" filepath="$PROJECT_DIR$/polinomio.iml" />
</modules>
</component>
</project>

6
polinomio/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

11
polinomio/polinomio.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

16
polinomio/src/Main.java Normal file
View File

@ -0,0 +1,16 @@
public class Main {
public static void main(String[] args) {
double[] first = {1, 1, 1};
double[] second = {-7, 0, 2};
Polinomio p = new Polinomio(first);
Polinomio p2 = new Polinomio(second);
System.out.println(p);
System.out.println(p2);
System.out.println(new Polinomio(p2.add(p)));
System.out.println(p.y(2));
}
}

View File

@ -0,0 +1,41 @@
public class Polinomio {
private double[] coefficienti;
public Polinomio(double[] coefficienti) {
this.coefficienti = coefficienti;
}
public double y(double x) {
double y = 0;
for (int i = coefficienti.length - 1; i >= 0; i--) {
y += coefficienti[i] * Math.pow(x, i);
}
return y;
}
public double[] add(Polinomio other) {
if(other.coefficienti.length != coefficienti.length) throw new IllegalArgumentException();
double[] sum = new double[coefficienti.length];
for (int i = 0; i < coefficienti.length; i++) {
sum[i] = coefficienti[i] + other.coefficienti[i];
}
return sum;
}
@Override
public String toString() {
String testo = "";
for (int i = coefficienti.length - 1; i >= 0; i--) {
if (coefficienti[i] >= 0) {
testo += "+";
}
testo += coefficienti[i] + "x^" + i + " ";
}
return testo;
}
}