Gabri_RDiaz

Entero

Oct 25th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. package com.liceolapaz.des.grd;
  2.  
  3. public class Entero implements Numero {
  4.     private int valor;
  5.    
  6.     public Entero(int valor) {
  7.         super();
  8.         this.valor = valor;
  9.     }
  10.  
  11.     public int getValor() {
  12.         return valor;
  13.     }
  14.  
  15.     public void setValor(int valor) {
  16.         this.valor = valor;
  17.     }
  18.  
  19.     @Override
  20.     public Numero suma(Numero operando) {
  21.         if (operando instanceof Entero) {//Si es de tipo.
  22.             Entero op = (Entero)operando; //Cast a entero
  23.             //Cuando sea racional le pasaremos dos valores
  24.             //Y cogeremos el segundo como denominador.
  25.             int resultado = this.valor + op.getValor();
  26.             return new Entero(resultado);
  27.         }
  28.         return null;
  29.     }
  30.  
  31.     @Override
  32.     public Numero resta(Numero operando) {
  33.         if (operando instanceof Entero) {
  34.             Entero op = (Entero)operando;
  35.             int resultado = this.valor - op.getValor();
  36.             return new Entero(resultado);
  37.         }
  38.         return null;
  39.     }
  40.  
  41.     @Override
  42.     public Numero multiplicacion(Numero operando) {
  43.         if (operando instanceof Entero) {
  44.             Entero op = (Entero)operando;
  45.             int resultado = this.valor * op.getValor();
  46.             return new Entero(resultado);
  47.         }
  48.         return null;
  49.     }
  50.  
  51.     @Override
  52.     public Numero division(Numero operando) {
  53.         if (operando instanceof Entero) {
  54.             Entero op = (Entero)operando;
  55.             if(op.getValor()==0) {
  56.                 System.out.println("Error: División entre 0");
  57.                 return null;
  58.             }
  59.             int resultado = this.valor / op.getValor();
  60.             return new Entero(resultado);
  61.         }
  62.         return null;
  63.     }
  64.  
  65.     @Override
  66.     public Numero modulo(Numero operando) {
  67.         // TODO Auto-generated method stub
  68.         return null;
  69.     }
  70.  
  71.     @Override
  72.     public String mostrar() {
  73.         // TODO Auto-generated method stub
  74.         return "" + this.valor;
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment