0% found this document useful (0 votes)
51 views6 pages

My Code

Code to work on

Uploaded by

Kajetan Francis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

My Code

Code to work on

Uploaded by

Kajetan Francis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*

* 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 lab_3;
import java.util.Random;
// if i want convert MAXINT&&MININT to a int number and
/**
*
* @author Kajetan
*/
public class HugeInteger {
private int[] Numbers = new int[1];
private int size;
public HugeInteger(){
size=1;
Numbers[0]=1;
}
public HugeInteger(int number){
if(number>=1){
Random random = new Random();
String HugeInt = String.valueOf(random.nextInt(number)+1); //string'
d to determine size of list!
size = HugeInt.length();
Numbers = new int[size];
for(int i=0;i<size-1;i++){
Numbers[i]=Integer.parseInt(""+HugeInt.charAt(i));}
}
else{throw new IllegalArgumentException("Number must be greater then 0")
;}
}
public HugeInteger(String val){
try{
String[] important = val.split("E");
double Value = Double.parseDouble(important[0]);//just to check boun
ds and positivity
int E = Integer.parseInt(important[1]);
//error handling section
if(E>Integer.MAX_VALUE-5){
throw new IllegalArgumentException("Number must be format X.YEZ
where Z<2147483643");}
if((important[0].length()-1>E+2)||((important[0].length()-1>E+1)&&(i
mportant[0].charAt(0)!="-".charAt(0)))){
throw new IllegalArgumentException("Number must be a whole numbe
r!");}
if((E>0&&E+3<0)||(E<0&&E-3>0)){
throw new IllegalArgumentException("Number must be format X.YEZ
where Z<2147483643");}
if(String.valueOf(E).compareTo(important[1])!=0){
throw new IllegalArgumentException("E must be fixed point intege
r");}
if(Value<1&&Value>1){//is a decimal
throw new IllegalArgumentException("Number must be fixed point i
nteger (i.e >1 or <-1)");}
if(important[0].compareTo("")==0)
throw new IllegalArgumentException("String must contain numbers

in format 'X.YEZ'");
try{important[3]="1";
throw new IllegalArgumentException("Error, too many 'E' in strin
g");
}
catch(IndexOutOfBoundsException e){}
if(Value>=10){
throw new IllegalArgumentException ("String must contain numbers
in format 'X.YEZ' where -10>X>10");}
if(important[1].compareTo(String.valueOf(E))!=0){
throw new IllegalArgumentException ("String must contain numbers
in format 'X.YEZ where Z is a whole number'");}
size = E+1;
//since E is 10^X there must be X+1 terms
int negShift;
int Val;
if(Value>1){//if positive dont need to do anything
Val= Integer.parseInt(""+important[0].charAt(0));//can only pars
eInt strings
negShift=0;
}
else{//add the negative to the initial digit
Val= Integer.parseInt(""+important[0].charAt(0)+important[0].cha
rAt(1));
negShift=1;
}
Numbers = new int[important[0].length()-1-negShift];
Numbers[0]=Val;
for(int i=1;i<important[0].length()-1-negShift;i++){
Numbers[i]= Character.getNumericValue(important[0].charAt(i+1+ne
gShift)); }

}
catch(NumberFormatException|IndexOutOfBoundsException e){
throw new IllegalArgumentException ("String must contain numbers in
format 'X.YEZ' and limit of 2147483643");
}
}

public HugeInteger add(HugeInteger h){


HugeInteger newInt = new HugeInteger();
int newSize;
int newSizeCancel;
int[] newArray;
int[] shorts;
int[] longs;
int range = abs(this.size-h.size);//difference in terms
int signThis;
int signThat;
int signShort;
int signLong;
int shorter;
int front=0;
int end=0;
int longer;

if(this.Numbers[0]>0){signThis=1;}
else{signThis=-1;}
if(h.Numbers[0]>0){signThat=1;}
else{signThat=-1;}

if(this.size==h.size){//determining size of final list


if(this.Numbers.length>h.Numbers.length){
shorter = h.Numbers.length;
longer = this.Numbers.length;
shorts = h.Numbers;
longs = this.Numbers;
signShort = signThat;
signLong = signThis;
}
else{//even if same length doesnt matter
shorter=this.Numbers.length;
longer=h.Numbers.length;
shorts = this.Numbers;
longs = h.Numbers;
signShort = signThis;
signLong = signThat;
}
range =0;
if(signThis!=signThat){
for(int i=0;i<shorter;i++){
if(this.Numbers[i]!=h.Numbers[i]){break;}
front++;//front values that zero out
}
}
newSize=longer;
}
else{
if(this.size>h.size){
shorter = h.Numbers.length;
shorts = h.Numbers;
signShort = signThat;
signLong = signThis;
longs = this.Numbers;
longer = this.Numbers.length;
}
else{
shorter = this.Numbers.length;
longer = h.Numbers.length;
shorts = this.Numbers;
longs = h.Numbers;
signShort = signThis;
signLong = signThat;
}
if(shorter+range>longer){//check if shorter is shorter list (not ju
st number)
newSize = shorter+range;
}
else{newSize=longer;}
}
if(shorter+range==longer&&signThis!=signThat){//if this is true then if
statement above did not activate!

for(int i=newSize-1;i>=0;i++){
if(shorts[i-range]!=longs[i]){break;}
end++;//end values that zero out
}
}
// shorter use finished
newSizeCancel = front + end;
/*if(newSize>Integer.MAX_VALUE-5){ //making sure size is within arra
y limits
throw new IllegalArgumentException("Size of new list is too big!
");//exceeded max list*/
newSize-=newSizeCancel;
newArray = new int[newSize];
for(int i=newSize-1;i>0;i++){
if(shorter+range>i&&longer>i){newArray[i]=abs(shorts[i+front]*si
gnShort+longs[i+front-range]*signLong);}
else{
if(longer>i){
newArray[i]=longs[i+front];}
else if(shorter+range>i){
newArray[i]=longs[i+front];}
}
}

/*for(int i=newSize-1+front;i>front;i--){//asigning values


if(i>h.Numbers.length){
newArray[j]=this.Numbers[i];}
else if(i>this.Numbers.length){
newArray[j]=h.Numbers[i];}
newArray[j]+=abs(this.Numbers[i]*signThis+h.Numbers[i]*signThat);
}
too ambitious trying to do in one forloop*/
for(int j=newSize-1;j>0;j++){
if(newArray[j]>=10){//if added term exceeded single digit(i.e carryo
ver)
newArray[j-1]+=1;
newArray[j]-=10;}
if(newArray[j]<0){
newArray[j-1]-=1;
newArray[j]+=10;
}
}
if(abs(newArray[0])>10){//if list is to small(addiion made size+1)
int[] tempArray = new int[newSize+1];
tempArray[0]=1*signThis;//same sign if adding
newArray[0]-=10;
for(int i=1;i<newSize+1;i++){
tempArray[i]=newArray[i-1];}
newArray = tempArray;
}
newInt.Numbers = newArray;
newInt.size = newSize+1;
return newInt;
}
public HugeInteger subtract(HugeInteger h){//adding the negative version of

h
HugeInteger newInteger = h;
newInteger.Numbers[0]*=(-1);
return add(newInteger);
}
public int compareTo(HugeInteger h){//returns which integer is bigger
if(this.size>h.size){return 1;}
else if (this.size<h.size){return -1;}
else{
int i=0;
int maxSize;
if(this.size>h.size){maxSize=this.size;}
else{maxSize=h.size;}
while(i<maxSize){
if(this.Numbers[i]!=h.Numbers[i]){break;}
}
return compareInt(this.Numbers[i],h.Numbers[i]);
}
}
public int compareInt(int n, int m){
if(n>m){return 1;}
else if (n<m){return -1;}
else{return 0;}
}
public int abs(int n){//returns absolute value
if(n<0){
return n*(-1);
}
else{
return n;
}
}
public HugeInteger multiply(HugeInteger h){
String[] multiThis = this.toString().split("E");//gets individual decima
l and power
String[] multiThat = h.toString().split("E");
int totalSize = Integer.parseInt(multiThis[1])+Integer.parseInt(multiTha
t[1]);
if(totalSize<0){
throw new IllegalArgumentException("Number is to large for array to
handle!");
}
double Decimal = Double.parseDouble(multiThis[0])*Double.parseDouble(mul
tiThat[0]);
if(Decimal>10){//will only happen once as the max can be (9.9999...)*(9.
9999...) = 99.9999...
Decimal/=10;
totalSize+=1;
}
String newValue =String.valueOf(Decimal)+"E"+String.valueOf(totalSize);/
/creates string in correct foemat
return new HugeInteger(newValue);
}
@Override
public String toString(){

String Decimal = String.valueOf(Numbers[0])+".";


if(size==1){
Decimal+="E0";
}
else{
for(int i=1;i<size;i++){
Decimal+=Numbers[i];
}
Decimal += "E" + (size-1);
}
return Decimal;
}
}

You might also like