// Komplex.java MM 2010 /** * Komplexe Zahlen */ public class Komplex { /* ------------------------------------------------- */ // Attribute /** * Realteil einer komplexen Zahl. */ private double re; /** * Imaginaerteil einer komplexen Zahl. */ private double im; /* ------------------------------------------------- */ // set-Methode /** * Setzt Real- und Imaginaerteil einer komplexen Zahl. * @param real Realteil der komplexen Zahl * @param imaginaer Imaginaerteil der komplexen Zahl */ public void setKomplex ( double real, double imaginaer) { re = real; im = imaginaer; } /* ------------------------------------------------- */ // get-Methode /** * Liest Realteil. * @return Realteil der komplexen Zahl */ public double getRe() { return re; } /** * Liest Imaginaerteil. * @return Imaginaerteil der komplexen Zahl */ public double getIm() { return im; } /* ------------------------------------------------- */ // toString /** * Darstellen einer komplexen Zahl. * @return komplexen Zahl in linearer Schreibweise */ public String toString() { return "" + re + " + " + im + " i"; } /* ------------------------------------------------- */ // Operationen /** * Addition. * @param a komplexe Zahl * @return komplexe Zahl */ public Komplex add( Komplex a) { double x, y; x = re + a.getRe(); y = im + a.getIm(); Komplex b = new Komplex(); b.setKomplex( x, y); return b; } /** * Subtraktion. * @param a komplexe Zahl * @return komplexe Zahl */ public Komplex sub( Komplex a) { double x, y; x = re - a.getRe(); y = im - a.getIm(); Komplex b = new Komplex(); b.setKomplex( x, y); return b; } /** * Multiplikation. * @param a komplexe Zahl * @return komplexe Zahl */ public Komplex mult( Komplex a) { double x, y; x = re * a.getRe() - im * a.getIm(); y = re * a.getIm() + im * a.getRe(); Komplex b = new Komplex(); b.setKomplex( x, y); return b; } /** * Division. * @param a komplexe Zahl * @return komplexe Zahl */ public Komplex div( Komplex a) { double x, y; x = ( re * a.getRe() + im * a.getIm()) / ( a.getRe() * a.getRe() + a.getIm() * a.getIm()); y = ( im * a.getRe() - re * a.getIm()) / ( a.getRe() * a.getRe() + a.getIm() * a.getIm()); Komplex b = new Komplex(); b.setKomplex( x, y); return b; } /* ------------------------------------------------- */ /** * Testprogramm zu komplexe Zahlen. */ public static void main( String[] args) { // Komplexe Zahlen Komplex a = new Komplex(); Komplex b = new Komplex(); Komplex c = new Komplex(); Komplex d = new Komplex(); a.setKomplex( 4, 2); b.setKomplex( 3, 4); // Testbeispiel 0 // c = a.add( b); // Testbeispiel 1 c = a.add( b); c = c.mult( a); // Testbeispiel 2 d = a.sub( b); d = d.mult( d); System.out.println( "c = " + c); // c = 16.0 + 38.0 i System.out.println( "d = " + d); // d = -3.0 + -4.0 i } }