Skip to content

Instantly share code, notes, and snippets.

@3c7
Created April 25, 2015 15:00
Show Gist options
  • Save 3c7/0cdaa1b4c805d64947b5 to your computer and use it in GitHub Desktop.
Save 3c7/0cdaa1b4c805d64947b5 to your computer and use it in GitHub Desktop.
Datenstrukturen und Algorithmen - Übungsblatt 3 - Aufgabe 2
/**
* Created by Nils on 21.04.2015.
*/
public class AbgeschlosseneAuswertung {
public static Double auswertung(Funktion f, double x) {
Double result;
try {
result = new Double(f.wert(x));
} catch (ValueOutOfRangeException e){
return null;
}
return result;
}
}
/**
* Created by Nils on 14.04.2015.
*/
public class Abs implements Funktion {
public boolean istDefiniertFuer(double x) {
return true;
}
public double wert(double x) {
if (!this.istDefiniertFuer(x)) return 0.0;
return Math.abs(x);
}
public String toString() {
return "|x|";
}
}
/**
* Created by Nils on 14.04.2015.
*/
public interface Funktion {
boolean istDefiniertFuer(double x);
double wert(double x) throws ValueOutOfRangeException;
}
public class Kettenfunktion implements Funktion {
private Funktion f;
private Funktion g;
public Kettenfunktion(Funktion f, Funktion g) {
this.f = f;
this.g = g;
}
public boolean istDefiniertFuer(double x) {
try {
return g.istDefiniertFuer(x) && f.istDefiniertFuer(g.wert(x));
} catch (ValueOutOfRangeException e) {
return false;
}
}
public double wert(double x) throws ValueOutOfRangeException {
return f.wert(g.wert(x));
}
public String toString() {
return "f(g(x)) mit f(x) = " + f + " und g(x) = " + g;
}
}
/**
* Created by Nils on 14.04.2015.
*/
public class Polynom implements Funktion {
private double[] werte;
public Polynom(double[] werte) {
this.werte = werte;
}
public boolean istDefiniertFuer(double x) {
return true;
}
public double wert(double x) {
double erg = 0.0;
for (int i=0; i<this.werte.length; i++) {
erg += this.werte[i] * Math.pow(x,i);
}
return erg;
}
public String toString() {
String fkt = "";
for (int i=0; i<this.werte.length; i++) {
fkt = this.werte[i] + "*x^" + i + fkt;
if (i < this.werte.length - 1) fkt = " + " + fkt;
}
return fkt;
}
}
/**
* Created by Nils on 14.04.2015.
*/
public class Reziprok implements Funktion {
public boolean istDefiniertFuer(double x) {
if (x != 0) return true;
return false;
}
public double wert(double x) throws ValueOutOfRangeException {
if (!this.istDefiniertFuer(x)) throw new ValueOutOfRangeException();
return 1.0 / x;
}
public String toString() {
return "1/x";
}
}
/**
* Created by Nils on 14.04.2015.
*/
public class RezWurzel implements Funktion {
Kettenfunktion kfkt = new Kettenfunktion(new Wurzel(), new Abs());
Kettenfunktion RezWurzel = new Kettenfunktion(new Reziprok(), kfkt);
public boolean istDefiniertFuer(double x) {
if (RezWurzel.istDefiniertFuer(x)) return true;
return false;
}
public double wert(double x) throws ValueOutOfRangeException {
if (!this.istDefiniertFuer(x)) throw new ValueOutOfRangeException();
return RezWurzel.wert(x);
}
public String toString() {
return RezWurzel.toString();
}
}
/**
* Created by Nils on 21.04.2015.
*/
public class ValueOutOfRangeException extends Exception {
public ValueOutOfRangeException () {
super();
}
public ValueOutOfRangeException(String msg) {
super(msg);
}
}
/**
* Created by Nils on 14.04.2015.
*/
public class Wurzel implements Funktion {
public boolean istDefiniertFuer(double x) {
if (x >= 0) return true;
return false;
}
public double wert(double x) throws ValueOutOfRangeException {
if (!this.istDefiniertFuer(x)) throw new ValueOutOfRangeException();
return Math.sqrt(x);
}
public String toString() {
return "sqrt(x)";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment