Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package niit.summer.complex;
- import java.text.DecimalFormat;
- public class ComplexNumber {
- public static final ComplexNumber ZERO = new ComplexNumber(0,0);
- private double real;
- private double imaginary;
- DecimalFormat decimalFormat = new DecimalFormat("0.###");
- public ComplexNumber(double real, double imaginary) {
- this.real = real;
- this.imaginary = imaginary;
- }
- public double GetReal () {return real; }
- public double GetImaginary () {return imaginary; }
- public ComplexNumber add(ComplexNumber z2) {
- return new ComplexNumber(real + z2.real, imaginary + z2.imaginary);
- }
- public ComplexNumber multiply(ComplexNumber z2) {
- return new ComplexNumber(real * z2.real - imaginary * z2.imaginary,imaginary * z2.real + real * z2.imaginary);
- }
- /* (non-Javadoc)
- * @see java.lang.Object#equals(java.lang.Object)
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (!(obj instanceof ComplexNumber)) {
- return false;
- }
- ComplexNumber other = (ComplexNumber) obj;
- if (Double.doubleToLongBits(imaginary) != Double.doubleToLongBits(other.imaginary)) {
- return false;
- }
- if (Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real)) {
- return false;
- }
- return true;
- }
- /* (non-Javadoc)
- * @see java.lang.Object#hashCode()
- */
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- long temp;
- temp = Double.doubleToLongBits(imaginary);
- result = prime * result + (int) (temp ^ (temp >>> 32));
- temp = Double.doubleToLongBits(real);
- result = prime * result + (int) (temp ^ (temp >>> 32));
- return result;
- }
- public double abs() {
- return Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2));
- }
- public double arg() {
- double argRes = 0;
- if(real < 0 && imaginary >= 0){
- argRes = Math.PI + Math.atan(imaginary/real);
- }
- else if(real < 0 && imaginary < 0){
- argRes = (-Math.PI) + Math.atan(imaginary/real);
- }
- else if(real == 0 && imaginary > 0){
- argRes = Math.PI / 2;
- }
- else if(real == 0 && imaginary < 0){
- argRes = (-Math.PI) / 2;
- }
- else if(real > 0){
- argRes = Math.atan(imaginary/real);
- }
- else{
- argRes = Double.NaN;
- }
- return argRes;
- }
- public class AlgebraicRepresentation {
- public double aReal, aImaginary;
- public double getReal() {return aReal; }
- public double getImaginary() {return aImaginary; }
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- String formatNumber = null;
- String formatReal = "", formatImag = "";
- DecimalFormat decimalFormat = new DecimalFormat("0.###");
- if(aReal == 0 && aImaginary == 0){
- formatNumber = decimalFormat.format(0);
- }
- else if(aReal == 0){
- formatNumber = decimalFormat.format(aImaginary);
- formatNumber = String.format("%s" + "i", formatNumber);
- }
- else if(aImaginary == 0){
- formatNumber = decimalFormat.format(aReal);
- formatNumber = String.format("%s", formatNumber);
- }
- else if (aReal != 0 && aImaginary != 0){
- formatReal = decimalFormat.format(aReal);
- formatImag = decimalFormat.format(aImaginary);
- formatNumber = String.format("%s" + " + " + "%s" + "i", formatReal, formatImag);
- }
- return formatNumber;
- }
- }
- public AlgebraicRepresentation asAlgebraic() {
- AlgebraicRepresentation algVers = new AlgebraicRepresentation();
- algVers.aReal = ComplexNumber.this.GetReal();
- algVers.aImaginary = ComplexNumber.this.GetImaginary();
- return algVers;
- }
- public class TrigonometricRepresentation {
- private double tReal, tImaginary;
- public double gettReal(){ return tReal;}
- public double gettImaginary(){ return tImaginary;}
- public double getAbsoluteValue() {
- double result = ComplexNumber.this.abs();
- return result;
- }
- public double getArgument() {
- double arg = ComplexNumber.this.arg();
- return arg;
- }
- double r = TrigonometricRepresentation.this.getAbsoluteValue();
- double fi = TrigonometricRepresentation.this.getArgument();
- double fiDegree = (fi*180) / Math.PI;
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- String formatNumber = null;
- String formatAbs = "", formatArg = "";
- DecimalFormat decimalFormat = new DecimalFormat("0.###");
- if(tReal == 0 && tImaginary == 0){
- formatNumber = decimalFormat.format(0);
- }
- else if(tReal == 0){
- formatArg = decimalFormat.format(fiDegree);
- formatNumber = String.format("i" + "*" + "sin" + "%s" + "°", formatArg);
- System.out.println(formatNumber);
- }
- else if(tImaginary == 0){
- formatNumber = decimalFormat.format(tReal);
- formatNumber = String.format("%s", formatNumber);
- System.out.println(formatNumber);
- }
- else if (tReal != 0 && tImaginary != 0){
- formatAbs = decimalFormat.format(r);
- formatArg = decimalFormat.format(fiDegree);
- formatNumber = String.format("%s" + "(" + "cos" + "%s" + "°" + "+" + "i" + "*" + "sin" + "%s" + "°" +")", formatAbs, formatArg, formatArg);
- System.out.println(formatNumber);
- }
- return formatNumber;
- }
- }
- public TrigonometricRepresentation asTrigonometric() {
- TrigonometricRepresentation trigVers = new TrigonometricRepresentation();
- trigVers.tReal = ComplexNumber.this.GetReal();
- trigVers.tImaginary = ComplexNumber.this.GetImaginary();
- return trigVers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement