Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package HugeInteger;
  8.  
  9. import java.lang.Math;
  10.  
  11. /**
  12. *
  13. * @author Adam Guo
  14. */
  15. public class HugeInteger {
  16. double big;
  17. //constructors
  18.  
  19. public HugeInteger(double huge){
  20. big = huge;
  21. }
  22.  
  23. //constructor 1
  24. public HugeInteger(String val) throws Exception{
  25. try{
  26. big = Double.parseDouble(val);
  27. }
  28. catch(Exception e){
  29. System.out.println("Invalid number");
  30. }
  31. }
  32.  
  33. //constructor 2
  34. public HugeInteger(int n) throws Exception{
  35. int digits[] = new int[n];
  36. String str = new String("");
  37.  
  38. try{
  39. for(int i=0; i<n; i++){
  40. if(i != 0){
  41. digits[i] = (int)(Math.random()*10);
  42. }
  43. else{
  44. int randNum = (int)(Math.random()*10+1);
  45. if(randNum == 10){
  46. randNum -= 1;
  47. }
  48. digits[i] = randNum;
  49. }
  50. str += digits[i];
  51. }
  52. big = Double.parseDouble(str);
  53. }
  54. catch(Exception e){
  55. System.out.println("Enter a non-negative number");
  56. }
  57. }
  58.  
  59. //methods
  60. //method 1
  61. public HugeInteger add(HugeInteger h){
  62. this.big += h.big;
  63. return new HugeInteger(this.big);
  64. }
  65.  
  66. //method 2
  67. public HugeInteger subtract(HugeInteger h){
  68. this.big -= h.big;
  69. return new HugeInteger(this.big);
  70. }
  71.  
  72. //method 3
  73. public HugeInteger multiply(HugeInteger h){
  74. this.big *= h.big;
  75. return new HugeInteger(this.big);
  76. }
  77.  
  78. //method 4
  79. public int compareTo(HugeInteger h){
  80. if(this.big < h.big){
  81. return -1;
  82. }
  83. else if(this.big > h.big){
  84. return 1;
  85. }
  86. else{
  87. return 0;
  88. }
  89. }
  90.  
  91. //method 5
  92. public String toString(){
  93. String str = new String("" + this.big);
  94. return str;
  95. }
  96.  
  97. public static void main(String[] args) throws Exception{
  98. HugeInteger x = new HugeInteger(3);
  99. HugeInteger y = new HugeInteger("10");
  100. HugeInteger z = new HugeInteger("10000");
  101. HugeInteger temp = (y.add(z));
  102. System.out.println(temp.big);
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement