Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package HugeInteger;
- import java.lang.Math;
- /**
- *
- * @author Adam Guo
- */
- public class HugeInteger {
- double big;
- //constructors
- public HugeInteger(double huge){
- big = huge;
- }
- //constructor 1
- public HugeInteger(String val) throws Exception{
- try{
- big = Double.parseDouble(val);
- }
- catch(Exception e){
- System.out.println("Invalid number");
- }
- }
- //constructor 2
- public HugeInteger(int n) throws Exception{
- int digits[] = new int[n];
- String str = new String("");
- try{
- for(int i=0; i<n; i++){
- if(i != 0){
- digits[i] = (int)(Math.random()*10);
- }
- else{
- int randNum = (int)(Math.random()*10+1);
- if(randNum == 10){
- randNum -= 1;
- }
- digits[i] = randNum;
- }
- str += digits[i];
- }
- big = Double.parseDouble(str);
- }
- catch(Exception e){
- System.out.println("Enter a non-negative number");
- }
- }
- //methods
- //method 1
- public HugeInteger add(HugeInteger h){
- this.big += h.big;
- return new HugeInteger(this.big);
- }
- //method 2
- public HugeInteger subtract(HugeInteger h){
- this.big -= h.big;
- return new HugeInteger(this.big);
- }
- //method 3
- public HugeInteger multiply(HugeInteger h){
- this.big *= h.big;
- return new HugeInteger(this.big);
- }
- //method 4
- public int compareTo(HugeInteger h){
- if(this.big < h.big){
- return -1;
- }
- else if(this.big > h.big){
- return 1;
- }
- else{
- return 0;
- }
- }
- //method 5
- public String toString(){
- String str = new String("" + this.big);
- return str;
- }
- public static void main(String[] args) throws Exception{
- HugeInteger x = new HugeInteger(3);
- HugeInteger y = new HugeInteger("10");
- HugeInteger z = new HugeInteger("10000");
- HugeInteger temp = (y.add(z));
- System.out.println(temp.big);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement