Merge branch 'master' of gitea.it:gicorada/java-scuola
This commit is contained in:
commit
61e53ba3e2
|
@ -0,0 +1,3 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue