Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Java "JELPPPPPPPPPPPPPPP!"


  • Please log in to reply

#1
sigfrid

sigfrid

    Member

  • Member
  • PipPip
  • 42 posts
I need to make a class with lots o methods in java to handle complex numbers, the little i know about java (trust me its almos null my java knowledge) allowed me to do this :
import java.io.*;
public class Complejo {
boolean rect;
double real,imag;
public Complejo(){
real=0.0;
imag=0.0;
rectangular= true;
}
//constructor del objeto Complejo
public Complejo(double r,double i, boolean t){
real=r;
imag=i;
rectangular=t;
}
//metodo de captura
public void asignarCoordenadas()throws IOException{
r=Obtenerdoble.ObtenerDoble("¿Cual es el valor de la parte real?");
i=Obtenerdoble.ObtenerDoble("¿Cual es el valor de la parte imaginaria?");
t=true || false;
return r,i,t;
}
//metodo para la impresion
public void imprime() {
if(imag >= 0) System.out.println(real + " + " + imag"i");
else System.out.println(real + " " + (-imag"i"));
ConvierteAPolar();
System.out.printl(real+"cis"+imag);


}

//metodo para la suma
public static Complejo suma(){
Complejo c=new Complejo
c.real=a.real+b.real;
c.imag=a.imag+b.imag;

return c;
}
//metodo para la resta
public static Complejo resta(){
Complejo c =new Complejo;
c.real=a.real-b.real;
c.imag=a.imag-b.imag;
return c;
}
public static Complejo restab(){
Complejo c =new Complejo;
c.real=b.real-a.real;
c.imag=b.imag-a.imag;
return c;
}
//metodo para el producto
public static Complejo producto(){
Complejo c=new Complejo;
c.real=a.real*b.real-a.imag*b.imag;
c.imag=a.real*b.imag+a.imag*b.real;
return c;
}
//metodo para la division de a/b
public static Complejo cociente(){
double div,tab,tba;
Complejo c=new Complejo;
div=b.real*b.real+(-(b.imag*b.imag));
tab=a.real*b.real+(-(a.imag*b.imag));
tba=a.imag*b.real-b.imag*a.real;
c.real=tab/div;
c.imag=tba/div;
return c;
}

//Para copiar el complejo c en el a
public static Complejo copia(){
Complejo a= new Complejo;
a.real=c.real;
a.imag=c.imag;
return a;
}
//metodo para copiar el complejo c en el b
public static Complejo copia(){
Complejo b=new Complejo;
b.real=c.real;
b.imag=c.imag;
return a;
}
//metodo para convertir de rectangular a polar
public void ConvierteAPolar() {
double r, i;
r = real;
i = imag;

real = Math.sqrt(r*r + i*i);
imag = Math.atan2(i,r);
rectangular = false;
}
//metodo para convertir de polar a rectangular
public void ConvierteARectangular(){
double mod,ang;
mod = real;
ang = imag;

real = mag * Math.cos(ang);
imag = mag * Math.sin(ang);
rectangular = true;
}
}
some dude at a web page turned that into this
import java.io.*;
public class Complejo {

boolean rectangular;
double real,imag; // coordenadas rectangulares
double mod,angle; // coordenadas polares

public Complejo(){
real=0.0;
imag=0.0;
rectangular= true;
}
//constructor del objeto Complejo
public Complejo(double r,double i, boolean t){
real=r;
imag=i;
rectangular=t;
}

//metodo de captura
public Complejo asignarCoordenadas()throws IOException{
real=ObtenerDoble.Obtenerdoble("¿Cual es el valor de la parte real?");
imag=ObtenerDoble.Obtenerdoble("¿Cual es el valor de la parte imaginaria?");
rectangular=true;
return new Complejo(real,imag,reactangular);
}

//metodo para la impresion
public void imprime() {
if(rectangular){
if(imag >= 0) System.out.println(real + " + " + imag+"i");
else System.out.println(real + " " + (-imag+"i"));
}
else
System.out.println(real+"cis"+imag);
}

//metodo para la suma
public Complejo suma(Complejo b){
if(!rectangular)
ConvierteARectangular();
if(!b.rectangular)
b.ConvierteARectangular();
Complejo c=new Complejo();
c.real=real+b.real;
c.imag=imag+b.imag;

return c;
}
//metodo para la resta
public Complejo resta(Complejo b){
if(!rectangular)
ConvierteARectangular();
if(!b.rectangular)
b.ConvierteARectangular();
Complejo c =new Complejo();
c.real=real-b.real;
c.imag=imag-b.imag;
return c;
}
public Complejo restab(Complejo b, Complejo a){
if(!rectangular)
ConvierteARectangular();
if(!b.rectangular)
b.ConvierteARectangular();
Complejo c =new Complejo();
c.real=b.real-real;
c.imag=b.imag-imag;
return c;
}
//metodo para el producto
public Complejo producto(Complejo b,Complejo a){
if(!rectangular)
ConvierteARectangular();
if(!b.rectangular)
b.ConvierteARectangular();
Complejo c=new Complejo();
c.real=real*b.real-imag*b.imag;
c.imag=real*b.imag+imag*b.real;
return c;
}
//metodo para la division de a/b
public Complejo cociente(Complejo b){
double div,tab,tba;
if(!rectangular)
ConvierteARectangular();
if(!b.rectangular)
b.ConvierteARectangular();
Complejo c=new Complejo();
div=b.real*b.real+b.imag*b.imag;
tab=real*b.real+imag*b.imag;
tba=imag*b.real-b.imag*real;
c.real=tab/div;
c.imag=tba/div;
return c;
}

//Para copiar el complejo c en el a
public static Complejo copiaa(Complejo c){
Complejo a= new Complejo();
if(!c.rectangular)
c.ConvierteARectangular();
a.real=c.real;
a.imag=c.imag;
return a;
}
//metodo para copiar el complejo c en el b
public static Complejo copiab(Complejo c){
Complejo b=new Complejo();
if(!c.rectangular)
c.ConvierteARectangular();
b.real=c.real;
b.imag=c.imag;
return b;
}

//metodo para convertir de rectangular a polar
public void ConvierteAPolar() {

mod = Math.sqrt(a.real*a.real + a.imag*a.imag);
angle = Math.atan2(a.imag,a.real);
rectangular = false;
}
//metodo para convertir de polar a rectangular
public void ConvierteARectangular(){

real = mod * Math.cos(angle);
imag = mod * Math.sin(angle);
rectangular = true;
}
}

i also need to make a class to call al those methods like this
class OperacionesConComplejos{
public static void main(String[]args)throws IOException{
InputStreamReader entrada=new InputStreamReader(System.in);
BufferedReader teclado=new BufferedReader (entrada);
String texto;
int opcion;
Complejo a=new Complejo(0,0, rectangular);
Complejo b=new Complejo(0,0, rectangular);
Complejo c=new Complejo(0,0, rectangular);
while (opcion>=1&&opcion<=14){
System.out.println("___________________________________________________");
System.out.println("|(1)Especifica el numero complejo a |");
System.out.println("|(2)Especifica el numero complejo b |");
System.out.println("|_________________________________________________|");
System.out.println("|(3)Imprimir el complejo a |");
System.out.println("|(4)Imprimir el complejo b |");
System.out.println("|(5)Imprimir el complejo c |");
System.out.println("|(6)Imprimir los tres complejos a,b,y c |");
System.out.println("|_________________________________________________|");
System.out.println("|(7)Sumar el complejo a al complejo b |");
System.out.println("|_________________________________________________|");
System.out.println("|(8)Restar el complejo a del complejo b |");
System.out.println("|(9)Restar el complejo b del complejo a |");
System.out.println("|_________________________________________________|");
System.out.println("|(10)Multiplicar el complejo a por el complejo b |");
System.out.println("|(11)Dividir el complejo a entre el complejo c |");
System.out.println("|(12)Dividir el complejo b entre el complejo a |");
System.out.println("|_________________________________________________|");
System.out.println("|(13)Copiar el complejo c al complejo a |");
System.out.println("|(14)Copiar el complejo c al complejo b |");
System.out.println("|_________________________________________________|");
System.out.println("|(15)Salir |");
System.out.println("|_________________________________________________|");
texto=teclado.readLine();
desicion=(Integer.valueOf(texto)).intValue();
switch (opcion){
case 1:
Complejo.asignarCoordenadas(Complejo a);
break;
case 2:
Complejo.asignarCoordenadas(Complejo b);
break;
case 3:
Complejo.imprime(Complejo a);
break;
case 4:
Complejo.imprime(Complejo b);
break;
case 5:
Complejo.imprime(Complejo c);
break;
case 6:
Complejo.imprime(Complejo a);
Complejo.imprime(Complejo b);
Complejo.imprime(Complejo c);
break;
case 7:
Complejo c=Complejo.suma(a,b);
imprime("Suma "+c);
break;
case 8:
Complejo c=Complejo.resta(a,b);
imprime("Suma"+c);
break;
case 9:
Complejo c=Complejo.resta(b,a);
imprime("Resta"+c);
break;
case 10:
Complejo c=Complejo.producto(a,b);
imprime("Producto"+c);
}

}}} if some one can help me to fix the first methos, i think i can handle the rest, thanks in advance, see ya
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
hi sigfrid,

the following steps will get suma() working. the rest of the methods will be left as an exercize for you.

part I:
referencing your post, copy 131 lines to a file starting after:
'some dude at a web page turned that into this'

name the file Complejo.java

this will not compile, yet

note: line numbers are approximate

comment these lines 22,23
//real=ObtenerDoble.Obtenerdoble("¿Cual es el valor de la parte real?");
//imag=ObtenerDoble.Obtenerdoble("¿Cual es el valor de la parte imaginaria?");

line 26
reactangular is the wrong spelling, change it to the correct spelling

modify line 119 to:
public void ConvierteAPolar(Complejo a) {

modify suma() starting at line 40:
public Complejo suma(Complejo a, Complejo b){
Complejo d=new Complejo();
d.real=a.real+b.real;
d.imag=a.imag+b.imag;
return d;
}


compile Complejo.java

..........................................................
part II:
referencing your post, copy all lines to a file starting after:
'i also need to make a class to call al those methods like this'

name the file OperacionesConComplejos.java

add the following line at line 1
import java.io.*;

modify line 9
int opcion = 1; // initialize opcion

add line 10
boolean rectangular = true;

comment cases 1-6 and 8-10 inside the switch() starting at line 44

replace desicion with opcion at line 37
opcion=(Integer.valueOf(texto)).intValue();

modify case 7 starting at line 66
case 7:
//Complejo c=Complejo.suma(a,b);
c=c.suma(a,b);
//imprime("Suma "+c);
c.imprime();
break;

there should be 4 } at the end of the file

modify lines 9, 10
Complejo a=new Complejo(1,2, rectangular);
Complejo b=new Complejo(3,4, rectangular);

compile OperacionesConComplejos.java

..........................................................
part III:
run OperacionesConComplejos
enter 7(for case 7)
verify the answer 4.0 + 6.0i
..........................................................
  • 0

#3
sigfrid

sigfrid

    Member

  • Topic Starter
  • Member
  • PipPip
  • 42 posts
Jeje, many thanks dude, many thanks, but i got it working, so i don't need assistance anymore (at least inf this) u wanna se my final code? is very diferent:
//clase compleja, operaciones con numeros complejos
// creada por Omar Nuñez saucedo
import java.io.*;
public class Complejo {
boolean rectangular;
double real,imag;
public Complejo(){
real=0.0;
imag=0.0;
rectangular= true;
}
//constructor del objeto Complejo
public Complejo(double r,double i, boolean t){
real=r;
imag=i;
rectangular=t;
}
//metodo de captura
public void asignarCoordenadas()throws IOException{
real=ObtenerDoble.Obtenerdoble("¿Cual es el valor de la parte real?");
imag=ObtenerDoble.Obtenerdoble("¿Cual es el valor de la parte imaginaria?");

}
//metodo para la impresion
public void imprime() {
if(imag >= 0) System.out.println(real + " + " + imag+"i");
else System.out.println(real + " " + (-imag+"i"));
ConvierteAPolar();
System.out.println(real+"cis"+imag);


}

//metodo para la suma
public void suma(Complejo a,Complejo b){
real=a.real+b.real;
imag=a.imag+b.imag;
}
//metodo para la resta
public void resta(Complejo a,Complejo b){
real=a.real-b.real;
imag=a.imag-b.imag;

}

//metodo para el producto
public void producto(Complejo a,Complejo b){
real=a.real*b.real-a.imag*b.imag;
imag=a.real*b.imag+a.imag*b.real;

}
//metodo para la division de a/b
public void cociente(Complejo a,Complejo b){
double div,tab,tba;
div=b.real*b.real+(b.imag*b.imag);
tab=a.real*b.real+(a.imag*b.imag);
tba=a.imag*b.real-b.imag*a.real;
real=tab/div;
imag=tba/div;
}

//Para copiar el complejo
public void copia(Complejo c){
real=c.real;
imag=c.imag;

}

//metodo para convertir de rectangular a polar
public boolean ConvierteAPolar(){
if (!rectangular) return true;
double r, i;
r = real;
i = imag;

real = Math.sqrt(r*r + i*i);
imag = Math.atan2(i,r);
rectangular = false;
return false;
}
//metodo para convertir de polar a rectangular
public boolean ConvierteARectangular(){
if (rectangular) return false;
double mod,ang;
mod = real;
ang = imag;

real = mod * Math.cos(ang);
imag = mod * Math.sin(ang);
rectangular = true;
return true;
}
}
and for the other:
//clase creada por Omar Núñez Saucedo
//clase para implementar los metodos de la clase Complejo
import java.io.*;
class OperacionesConComplejosApli{
public static void main(String[]args)throws IOException{
int opcion=1;
Complejo a=new Complejo(0,0,true);
Complejo b=new Complejo(0,0,true);
Complejo c=new Complejo(0,0,true);
while (opcion>=1&&opcion<=14){
System.out.println("___________________________________________________");
System.out.println("|(1)Especifica el numero complejo a |");
System.out.println("|(2)Especifica el numero complejo b |");
System.out.println("|_________________________________________________|");
System.out.println("|(3)Imprimir el complejo a |");
System.out.println("|(4)Imprimir el complejo b |");
System.out.println("|(5)Imprimir el complejo c |");
System.out.println("|(6)Imprimir los tres complejos a,b,y c |");
System.out.println("|_________________________________________________|");
System.out.println("|(7)Sumar el complejo a al complejo b |");
System.out.println("|_________________________________________________|");
System.out.println("|(8)Restar el complejo a del complejo b |");
System.out.println("|(9)Restar el complejo b del complejo a |");
System.out.println("|_________________________________________________|");
System.out.println("|(10)Multiplicar el complejo a por el complejo b |");
System.out.println("|(11)Dividir el complejo a entre el complejo c |");
System.out.println("|(12)Dividir el complejo b entre el complejo a |");
System.out.println("|_________________________________________________|");
System.out.println("|(13)Copiar el complejo c al complejo a |");
System.out.println("|(14)Copiar el complejo c al complejo b |");
System.out.println("|_________________________________________________|");
System.out.println("|(15)Salir |");
System.out.println("|_________________________________________________|");
opcion=ObtenerDoble.Obtenerentero("Elige una opcion");
switch (opcion){
case 1:
a.asignarCoordenadas();
break;
case 2:
b.asignarCoordenadas();
break;
case 3:
a.imprime();
break;
case 4:
b.imprime();
break;
case 5:
c.imprime();
break;
case 6:
a.imprime();
b.imprime();
c.imprime();
break;
case 7:
c.suma(a,b);
break;
case 8:
c.resta(a,b);
break;
case 9:
c.resta(b,a);
break;
case 10:
c.producto(a,b);
break;
case 11:
c.cociente(a,b);
break;
case 12:
c.cociente(b,a);
break;
case 13:
a.copia©;
break;
case 14:
b.copia©;
break;



}
For me, java is a lot harder than C
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
thanks for letting us know you got it working. at a quick glance, it looks like you did a thorough job of cleaning up the code - congrats.
  • 0

#5
sigfrid

sigfrid

    Member

  • Topic Starter
  • Member
  • PipPip
  • 42 posts
Indeed, in fact, it is almost not the same code (i had to review the "constructor" to create the objects, my constructor was fine, my methods were screwi)
  • 0

#6
sigfrid

sigfrid

    Member

  • Topic Starter
  • Member
  • PipPip
  • 42 posts
BTW , i hope u can help me in the future, i mean, this was a "pretty simple" thing to do (even though was [bleep] hard for me) as i hope i can help too , well see ta in da next post
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP