Skip to content

Instantly share code, notes, and snippets.

@danicunhac
Created March 25, 2019 20:27
Show Gist options
  • Save danicunhac/9ccd47232b6432487f08721f95c44b19 to your computer and use it in GitHub Desktop.
Save danicunhac/9ccd47232b6432487f08721f95c44b19 to your computer and use it in GitHub Desktop.
Fração
public class Aplicacao {
public static void main(String[] args) {
Fracao f1 = new Fracao();
f1.definirValores(7, 3);
Fracao f2 = new Fracao();
f2.definirValores(3, 2);
Fracao f3 = f1.multiplicar(f2);
System.out.println(f3.obterFracao());
System.out.println(f3.calcularValor());
}
}
public class Fracao {
double numerador;
double denominador;
void definirValores(double numerador, double denominador) {
this.numerador = numerador;
this.denominador = denominador;
}
double calcularValor() {
if(denominador == 0) {
return 0;
}
return numerador/denominador;
}
Fracao multiplicar(Fracao f) {
Fracao result = new Fracao();
result.numerador = this.numerador * f.numerador;
result.denominador = this.denominador * f.denominador;
return result;
}
String obterFracao() {
return numerador + "/" + denominador;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment