Hi, i need to write a program for a computer science class. The directions my pr
ID: 3619966 • Letter: H
Question
Hi, i need to write a program for a computer science class. The directions my professor gave it very confusing.. he wants a program that can add, subtract, divide, multiply integers upto 500.. heres what he wrote on the board. First day of class he wrote this on the board:We have to write a program called "BigInt" basically big integer.
only thing he gave us is this:
Create the bigInt class
main class;
b1=b2.add(b3);
you will do both positive and negative
Constructor
BigInt b1 = new BigInt();
BigInt b2= new BigInt ();
BigInt b3 = new BigInt();
+
-
*
/ _____________________________________________________________________________ SECOND DAY OF CLASS: - he wrote this on the board
Class BigInt;
{
int [ ] big Num=newint[1000];
boolean pos; (true or false depends on the number if its positive or negative)
BigInt 1= new BigInt("17163666664563458998654656666666665645");
("0");
("+"); (if you get a plus sign than make it a zero)
("/");
("136ABC");
the integers can go upto 500 integers.
for addition
b3=b2.add(b1);
system. println("........................+b3);
this is all i know... please help me with this asap.. thank you so much
Hi, i need to write a program for a computer science class. The directions my professor gave it very confusing.. he wants a program that can add, subtract, divide, multiply integers upto 500.. heres what he wrote on the board. First day of class he wrote this on the board:
We have to write a program called "BigInt" basically big integer.
only thing he gave us is this:
Create the bigInt class
main class;
b1=b2.add(b3);
you will do both positive and negative
Constructor
BigInt b1 = new BigInt();
BigInt b2= new BigInt ();
BigInt b3 = new BigInt();
+
-
*
/ _____________________________________________________________________________ We have to write a program called "BigInt" basically big integer.
only thing he gave us is this:
Create the bigInt class
main class;
b1=b2.add(b3);
you will do both positive and negative
Constructor
BigInt b1 = new BigInt();
BigInt b2= new BigInt ();
BigInt b3 = new BigInt();
+
-
*
/ SECOND DAY OF CLASS: - he wrote this on the board
Class BigInt;
{
int [ ] big Num=newint[1000];
boolean pos; (true or false depends on the number if its positive or negative)
BigInt 1= new BigInt("17163666664563458998654656666666665645");
("0");
("+"); (if you get a plus sign than make it a zero)
("/");
("136ABC");
the integers can go upto 500 integers.
for addition
b3=b2.add(b1);
system. println("........................+b3);
this is all i know... please help me with this asap.. thank you so much
Class BigInt;
{
int [ ] big Num=newint[1000];
boolean pos; (true or false depends on the number if its positive or negative)
BigInt 1= new BigInt("17163666664563458998654656666666665645");
("0");
("+"); (if you get a plus sign than make it a zero)
("/");
("136ABC");
the integers can go upto 500 integers.
for addition
b3=b2.add(b1);
system. println("........................+b3);
this is all i know... please help me with this asap.. thank you so much
Explanation / Answer
please rate - thanks maybe this will be clearer for you import java.io.*;public class bigint
{
public static void main(String argv[]) throws IOException
{
BigInteger b1,b2,b3;
b1 = new BigInteger();
b2 = new BigInteger();
System.out.println("input the first BigInteger:");
b1.inputBigInteger();
System.out.println("input the second BigInteger:");
b2.inputBigInteger();
System.out.print("BigInt #1: "); b1.printBigInteger();
System.out.print("BigInt #2: "); b2.printBigInteger();
System.out.println(" =========================");
b3 = b1.add(b2);
System.out.print("SUM: "); b3.printBigInteger();
System.out.println();
System.out.print("BigInt #1: "); b1.printBigInteger();
System.out.print("BigInt #2: "); b2.printBigInteger();
System.out.println(" =========================");
b3 = b1.subtract(b2);
System.out.print("DIFFERENCE: "); b3.printBigInteger();
System.out.println();
System.out.print("BigInt #1: "); b1.printBigInteger();
System.out.print("BigInt #2: "); b2.printBigInteger();
System.out.println(" =========================");
b3 = b1.multiply(b2);
System.out.print("PRODUCT: "); b3.printBigInteger();
System.out.println();
}
}
class BigInteger
{
private final int INTSIZ=500;
private int intArray[] = new int[INTSIZ];
public BigInteger add(BigInteger bi)
{
BigInteger bsum = new BigInteger();
int carry = 0;
for (int i=INTSIZ-1; i>=0; i--)
{
int tmp = intArray[i] + bi.intArray[i] + carry;
carry = tmp/10;
bsum.intArray[i] = tmp%10;
}
return bsum;
}
public BigInteger subtract(BigInteger bi)
{
BigInteger bsubtract = new BigInteger();
int tmpIntArray[] = new int[INTSIZ];
for(int i=0;i!=INTSIZ;i++){tmpIntArray[i]=intArray[i];}
int borrow = 0;
for (int i=INTSIZ-1; i>=0; i--)
{
if(tmpIntArray[i]<bi.intArray[i]){tmpIntArray[i]=tmpIntArray[i]+10;tmpIntArray[i-1]=tmpIntArray[i-1]-1;}
int tmp = tmpIntArray[i] - bi.intArray[i] - borrow;
borrow = tmp/10;
bsubtract.intArray[i] = tmp%10;
}
return bsubtract;
}
public BigInteger multiply(BigInteger bi)
{ BigInteger bmultiply = new BigInteger();
for(int i=INTSIZ-1; i>=0; i--)
bmultiply.intArray[i] =0;
int k;
for (int i=INTSIZ-1; i>=1; i--)
{ k=i;
for (int j=INTSIZ-1; j>=1; j--)
{int tmp = (intArray[j]) * bi.intArray[i];
bmultiply.intArray[k] =bmultiply.intArray[k]+tmp;
bmultiply.intArray[k-1] =bmultiply.intArray[k-1]+ bmultiply.intArray[k]/10;
bmultiply.intArray[k] %=10;
k--;
if(k==0)
j=0;
}
}
return bmultiply;
}
public void printBigInteger()
{
for (int i=0; i<INTSIZ; i++) {
System.out.print(intArray[i]);
}
System.out.println();
}
public void inputBigInteger() throws IOException
{
BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("enter the BigInteger: (do not pad with zeros): ");
String str = input.readLine();
if (str.length() > INTSIZ) throw new ArithmeticException("OVERFLOW!");
for (int i=0; i<str.length(); i++)
{
intArray[INTSIZ-str.length()+i] =
Integer.parseInt(str.substring(i, i+1));
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.