Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* ******************************************************************** * * Proj

ID: 3620133 • Letter: #

Question

/* ********************************************************************
*
* Project #23 Data Structures
*
* Objective: Implement a LongNumber class to hold long positive integer.
* Note: Taken from text book,
Data Strictures and Abstraction with Java 2nd Edition, by Frank Carrano, Prentice Hall 2007
page 35, Chapter 1, Project 6
* our teacher have slightly modified the statement from text book.
*
* The outline of LongNumber class is given, You should not modify any
* methods and should not add any new fields or public methods
* You need to implement all methods with "need to implement!" msg.
*
* LongNumber objects is set to hold upto 50 (MAX_SIZE) digits in
* an int array digit[]
*
* When there is an error, print error msg and set proper digit[] to null
* teacher's Note: A better way is to throw exception if there is an error.
* but he Will cover exception handling later.
*
* please give me a link of your source file project1.java
or submit it here as txt output

our instructor also gave a test file so we can test the accordingly.


*
* *************************************************************************
*

Project statement from text book:

The largest positive integer of type int is 2,147,483,647.
Another integer type, long, represents integers up to
9,223,372,036,854,775,807. Imagine that you want to represent even
larger integers.

Design and implement a class Huge of very large nonnegative integers.
The largest integer should contain at least 30 digits.
Provide operations for the class that
- Set the value of a nonnegative integer (provide both set methods
and constructors)
- Return the value of a nonnegative integer as a string
- Read a large nonnegative integer (skip leading zeros, but remember
that zero is a valid number)
- Write a large nonnegative integer (do not write leading zeros,
but if the integer is zero, write a single zero)
- Add two nonnegative integers to produce the sum as a third integer
- Multiply two nonnegative integers to produce the product as a
third integer

You should handle overflow when reading, adding, or multiplying
integers. An integer is too large if it exceeds MAX_SIZE digits,
where MAX_SIZE is a named constant that you define.

Write a test program that demonstrates each method.


*************************************************************************


*/

class LongNumber
{

// private field digit[] array to hold
// upto MAX_SIZE digits
private int digit[];
private final int MAX_SIZE = 50;


// default constructor
public LongNumber()
{
digit = new int [MAX_SIZE];
resetArray();
} // end default constructor


// constructor:
// copy input number (in array) into digit[] array
// check for error: too long or invalid digit
public LongNumber(int longDigitArray[])
{
// need to implement!
} // end constructor


// constructor:
// copy input number (in string ) into digit[] array
// check for error: too long or invalid char
public LongNumber(String longNumStr)
{
// need to implement!
}


// replace digit[] array by input number (in array)
// check for error: too long or invalid digit
public void setLongNumber(int longDigitArray[])
{
// need to implement!
} // end setLongNumber


// replace digit[] array by input number (in string)
// check for error: too long or invalid char
public void setLongNumber(String longNumStr)
{
// need to implement!
}


// static method:
// create a LongNumber object from input number (in array)
// return the object
//
// check for error: too long or invalid char
public static LongNumber createLongNumber(String longNumString)
{
// need to implement!
// should return proper LongNumber object
return null;
} // end createLongNumber


// static method:
// create a LongNumber object from input number (in string)
// return the object
//
// check for error: too long or invalid digit
public static LongNumber createLongNumber(int longDigitArray[])
{
// need to implement!
// should return proper LongNumber object
return null;
} // end createLongNumber


// add this LongNumber with rhs LongNumber
// return a result LongNumber object
//
// check for error: overflow
// return null digit[] in result if there is an error
public LongNumber addLongNumber(LongNumber rhs)
{
// need to implement!
// should return proper LongNumber object
return null;
}


// multiply this LongNumber with rhs LongNumber
// return a result LongNumber object
//
// check for error: overflow
// return null digit[] in result if there is an error
public LongNumber multiplyLongNumber(LongNumber rhs)
{
// need to implement!
// should return proper LongNumber object
return null;
} // end multiplyLongNumber


// convert digit[] number into string and return it
public String toString()
{
String tmp = new String("");

// special case when there was an error, digit[] is null
if (digit == null)
tmp="null";
else {
int size = digit.length;
int i=0;

// skip front 0 digits
while((i < size ) && (digit[i]==0))
i++;

// special case, all 0 in digit[]
if (i == size ) tmp += "0";
else {
for (int j = i; j < size; j++)
tmp += digit[j];
}
}

// return result string
return tmp;
} // end toString


//============================================================
// add more private methods here if you like to .....

private void resetArray()
{
for (int i = 0; i < MAX_SIZE; i++)
digit[i] = 0;
}


//============================================================
// print msgs for main method

private static void printMsg(String msg)
{
System.out.println(msg);
System.out.println("==============================");
}

// use main method to create test cases
// these are test cases that i will use to test your program
public static void main(String[] args)
{
int num1[] = {0,0,0,0,0,0,0,0,0,1};
int num2[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9};
int num3[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
int num4[] =

{9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9};

String str1 = "1234567890123456789012345678901234567890";
String str2 = "0000123456789012345678901234567890123456";
String str3 = "98765432109876543210987654321098765432109876543210";
String str4 = "989";
String str5 = "3211";

int badNum[] = {2,2,2,2,2,20,2,2,2,2,2,2,2,2,2,2,2,2};
int longNum[] =

{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};

String badString = "12345678901234567890x12345678901234567890";
String longString = "987654321098765432109876543210987654321098765432108888";


// check constructors
LongNumber longNum0 = new LongNumber();
printMsg("Test#1: longNum="+longNum0);

LongNumber longNum1 = new LongNumber(num1);
printMsg("Test#2: longNum="+longNum1);

LongNumber longNum2 = new LongNumber(num2);
printMsg("Test#3: longNum="+longNum2);

LongNumber longNum3 = new LongNumber(longNum);
printMsg("Test#4: longNum="+longNum3);

LongNumber longNum4 = new LongNumber(badNum);
printMsg("Test#5: longNum="+longNum4);

LongNumber longNum5 = new LongNumber(str1);
printMsg("Test#6: longNum="+longNum5);

LongNumber longNum6 = new LongNumber(str2);
printMsg("Test#7: longNum="+longNum6);

LongNumber longNum7 = new LongNumber(longString);
printMsg("Test#8: longNum="+longNum7);

LongNumber longNum8 = new LongNumber(badString);
printMsg("Test#9: longNum="+longNum8);

longNum1.setLongNumber(str2);
printMsg("Test#10: longNum="+longNum1);

longNum1.setLongNumber(longString);
printMsg("Test#11: longNum="+longNum1);

// need to create new storage
longNum1=new LongNumber();
longNum1.setLongNumber(badString);
printMsg("Test#12: longNum="+longNum1);

longNum1=new LongNumber();
longNum1.setLongNumber(num2);
printMsg("Test#13: longNum="+longNum1);

longNum1.setLongNumber(longNum);
printMsg("Test#14: longNum="+longNum1);

longNum1=new LongNumber();
longNum1.setLongNumber(badNum);
printMsg("Test#15: longNum="+longNum1);

LongNumber longNum9 = LongNumber.createLongNumber(num3);
printMsg("Test#16: longNum="+longNum9);

LongNumber longNum10 = LongNumber.createLongNumber(badString);
printMsg("Test#17: longNum="+longNum10);

LongNumber longNum11 = LongNumber.createLongNumber(str2);
printMsg("Test#18: longNum="+longNum11);

LongNumber longNum12 = LongNumber.createLongNumber(longNum);
printMsg("Test#18: longNum="+longNum12);


longNum2= new LongNumber(num2);
longNum3= new LongNumber(num3);
longNum5= new LongNumber(num4);

longNum4 = longNum2.addLongNumber(longNum3);
printMsg("Test#19:"+longNum2 + " + " + longNum3 + " = " + longNum4);

longNum4 = longNum2.addLongNumber(longNum5);
printMsg("Test#20:"+longNum2 + " + " + longNum5 + " = " + longNum4);

longNum4 = longNum2.multiplyLongNumber(longNum3);
printMsg("Test#21:"+longNum2 + " * " + longNum3 + " = " + longNum4);


longNum4 = longNum2.multiplyLongNumber(longNum5);
printMsg("Test#22:"+longNum2 + " * " + longNum5 + " = " + longNum4);

longNum2.setLongNumber(str4);
longNum3.setLongNumber(str5);
longNum4 = longNum2.addLongNumber(longNum3);
printMsg("Test#23:"+longNum2 + " + " + longNum3 + " = " + longNum4);
longNum4 = longNum2.multiplyLongNumber(longNum3);
printMsg("Test#24:"+longNum2 + " + " + longNum3 + " = " + longNum4);
} // end main

}















*************************************************************************
we need to test this





class TestLongNumber
{
// use main method to create test cases
private static void printMsg(String msg)
{
System.out.println(msg);
System.out.println("==============================");
}

public static void main(String[] args)
{
int num1[] = {0,0,0,0,0,0,0,0,0,1};
int num2[] = {1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9};
int num3[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
int num4[] =

{9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9};

String str1 = "1234567890123456789012345678901234567890";
String str2 = "0000123456789012345678901234567890123456";
String str3 = "98765432109876543210987654321098765432109876543210";
String str4 = "989";
String str5 = "3211";

int badNum[] = {2,2,2,2,2,20,2,2,2,2,2,2,2,2,2,2,2,2};
int longNum[] =

{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};

String badString = "12345678901234567890x12345678901234567890";
String longString = "987654321098765432109876543210987654321098765432108888";


// check constructors
LongNumber longNum0 = new LongNumber();
printMsg("Test#1: longNum="+longNum0);

LongNumber longNum1 = new LongNumber(num1);
printMsg("Test#2: longNum="+longNum1);

LongNumber longNum2 = new LongNumber(num2);
printMsg("Test#3: longNum="+longNum2);

LongNumber longNum3 = new LongNumber(longNum);
printMsg("Test#4: longNum="+longNum3);

LongNumber longNum4 = new LongNumber(badNum);
printMsg("Test#5: longNum="+longNum4);

LongNumber longNum5 = new LongNumber(str1);
printMsg("Test#6: longNum="+longNum5);

LongNumber longNum6 = new LongNumber(str2);
printMsg("Test#7: longNum="+longNum6);

LongNumber longNum7 = new LongNumber(longString);
printMsg("Test#8: longNum="+longNum7);

LongNumber longNum8 = new LongNumber(badString);
printMsg("Test#9: longNum="+longNum8);

longNum1.setLongNumber(str2);
printMsg("Test#10: longNum="+longNum1);

longNum1.setLongNumber(longString);
printMsg("Test#11: longNum="+longNum1);

// need to create new storage
longNum1=new LongNumber();
longNum1.setLongNumber(badString);
printMsg("Test#12: longNum="+longNum1);

longNum1=new LongNumber();
longNum1.setLongNumber(num2);
printMsg("Test#13: longNum="+longNum1);

longNum1.setLongNumber(longNum);
printMsg("Test#14: longNum="+longNum1);

longNum1=new LongNumber();
longNum1.setLongNumber(badNum);
printMsg("Test#15: longNum="+longNum1);

LongNumber longNum9 = LongNumber.createLongNumber(num3);
printMsg("Test#16: longNum="+longNum9);

LongNumber longNum10 = LongNumber.createLongNumber(badString);
printMsg("Test#17: longNum="+longNum10);

LongNumber longNum11 = LongNumber.createLongNumber(str2);
printMsg("Test#18: longNum="+longNum11);

LongNumber longNum12 = LongNumber.createLongNumber(longNum);
printMsg("Test#18: longNum="+longNum12);


longNum2= new LongNumber(num2);
longNum3= new LongNumber(num3);
longNum5= new LongNumber(num4);

longNum4 = longNum2.addLongNumber(longNum3);
printMsg("Test#19:"+longNum2 + " + " + longNum3 + " = " + longNum4);

longNum4 = longNum2.addLongNumber(longNum5);
printMsg("Test#20:"+longNum2 + " + " + longNum5 + " = " + longNum4);

longNum4 = longNum2.multiplyLongNumber(longNum3);
printMsg("Test#21:"+longNum2 + " * " + longNum3 + " = " + longNum4);


longNum4 = longNum2.multiplyLongNumber(longNum5);
printMsg("Test#22:"+longNum2 + " * " + longNum5 + " = " + longNum4);

longNum2.setLongNumber(str4);
longNum3.setLongNumber(str5);
longNum4 = longNum2.addLongNumber(longNum3);
printMsg("Test#23:"+longNum2 + " + " + longNum3 + " = " + longNum4);
longNum4 = longNum2.multiplyLongNumber(longNum3);
printMsg("Test#24:"+longNum2 + " + " + longNum3 + " = " + longNum4);
} // end main

}





*************************************************************************



and output supposed to be like this


TestOutput
Test#1: longNum=0
==============================
Test#2: longNum=1
==============================
Test#3: longNum=123456789123456789
==============================
Error: input array too long
Test#4: longNum=null
==============================
Error: invalid digit
Test#5: longNum=null
==============================
Test#6: longNum=1234567890123456789012345678901234567890
==============================
Test#7: longNum=123456789012345678901234567890123456
==============================
Error: input string too long
Test#8: longNum=null
==============================
Error: invalid char
Test#9: longNum=null
==============================
Test#10: longNum=123456789012345678901234567890123456
==============================
Error: input string too long
Test#11: longNum=null
==============================
Error: invalid char
Test#12: longNum=null
==============================
Test#13: longNum=123456789123456789
==============================
Error: input array too long
Test#14: longNum=null
==============================
Error: invalid digit
Test#15: longNum=null
==============================
Test#16: longNum=222222222222222222
==============================
Error: invalid char
Test#17: longNum=null
==============================
Test#18: longNum=123456789012345678901234567890123456
==============================
Error: input array too long
Test#18: longNum=null
==============================
Test#19:123456789123456789 + 222222222222222222 = 345679011345679011
==============================
Error: + overflow
Test#20:123456789123456789 + 99999999999999999999999999999999999999999999999999 =
null
==============================
Test#21:123456789123456789 * 222222222222222222 =
27434842027434841972565157972565158
==============================
Error: * overflow
Test#22:123456789123456789 * 99999999999999999999999999999999999999999999999999 =
null
==============================
Test#23:989 + 3211 = 4200
==============================

TestOutput
Test#24:989 + 3211 = 3175679
==============================





*************************************************************************







888 888 888
888 888 888
888 888 888
888888 88888b. 8888b. 88888b. 888 888 .d8888b
888 888 "88b "88b 888 "88b 888 .88P 88K
888 888 888 .d888888 888 888 888888K "Y8888b.
Y88b. 888 888 888 888 888 888 888 "88b X88
"Y888 888 888 "Y888888 888 888 888 888 88888P'




888 888 888
888 888 888
888 888 888
88888b. 888 888 .d88888 .d88888 888 888
888 "88b 888 888 d88" 888 d88" 888 888 888
888 888 888 888 888 888 888 888 888 888
888 d88P Y88b 888 Y88b 888 Y88b 888 Y88b 888
88888P" "Y88888 "Y88888 "Y88888 "Y88888
888





Explanation / Answer

class LongNumber

{

    // private field digit[] array to hold

     // upto MAX_SIZE digits

     private int digit[];

     private final int MAX_SIZE = 50;

      // default constructor

     public LongNumber()

     {

          digit = new int [MAX_SIZE];

          resetArray();

     } // end default constructor

     // constructor:

     // copy input number (in array) into digit[] array

     // check for error: too long or invalid digit

     public LongNumber(int longDigitArray[])

     {

          for(int i=0;i<longDigitArray.length;i++)

              digit[i]=longDigitArray[i];

     } // end constructor

     // constructor:

     // copy input number (in string ) into digit[] array

     // check for error: too long or invalid char

     public LongNumber(String longNumStr)

     {

          int number=Integer.parseInt(longNumStr);

          // need to implement!

          for(int i=longNumStr.length()-1;i>=0;i--)

          { digit[i]=number%10;number=number/10;}

     }

     // replace digit[] array by input number (in array)

     // check for error: too long or invalid digit

     public void setLongNumber(int longDigitArray[])

     {

          for(int i=0;i<longDigitArray.length;i++)

          digit[i]=longDigitArray[i];

     } // end setLongNumber

     // replace digit[] array by input number (in string)

     // check for error: too long or invalid char

     public void setLongNumber(String longNumStr)

     {

          int number=Integer.parseInt(longNumStr);

          // need to implement!

          for(int i=longNumStr.length()-1;i>=0;i--)

          { digit[i]=number%10;number=number/10;}

     }

     // static method:

     // create a LongNumber object from input number

      // (in array) return the object

     // check for error: too long or invalid char

     public static LongNumber createLongNumber

                                 (String longNumString)

     {

          // need to implement!

          LongNumber lg=new LongNumber(longNumString);

          // should return proper LongNumber object

          return lg;

     } // end createLongNumber

     // static method:

     // create a LongNumber object from input number

      //(in string) return the object

     // check for error: too long or invalid digit

     public static LongNumber createLongNumber

                                   (int longDigitArray[])

     {

          // need to implement!

          LongNumber lg=new LongNumber(longDigitArray);

          // should return proper LongNumber object

          return lg;

     } // end createLongNumber

     // add this LongNumber with rhs LongNumber

     // return a result LongNumber object

     //

     // check for error: overflow

     // return null digit[] in result if there is an error

     public LongNumber addLongNumber(LongNumber rhs)

     {

          int sum[];

          sum=new int[MAX_SIZE];

          // need to implement!

         for(int i=0;i<digit.length;i++)

              sum[i]=digit[i]+rhs.digit[i];

        LongNumber lg=new LongNumber(sum);

          // should return proper LongNumber object

          return lg;

     }

     // multiply this LongNumber with rhs LongNumber

     // return a result LongNumber object

     //

     // check for error: overflow

     // return null digit[] in result if there is an error

     public LongNumber multiplyLongNumber(LongNumber rhs)

     {

          int mul[];

          mul=new int[MAX_SIZE];

          // need to implement!

          for(int i=0;i<digit.length;i++)

              mul[i]=digit[i]+rhs.digit[i];

          LongNumber lg=new LongNumber(mul);

          // should return proper LongNumber object

          return lg;

     } // end multiplyLongNumber

     // convert digit[] number into string and return it

     public String toString()

     {

          String tmp = new String("");

       // special case when there was an error,digit[]

        // is null

          if (digit == null)

              tmp="null";

          else

          {

              int size = digit.length;

              int i=0;

              // skip front 0 digits

              while((i < size ) && (digit[i]==0))

                   i++;

              // special case, all 0 in digit[]

              if (i == size ) tmp += "0";

              else

              {

                   for (int j = i; j < size; j++)

                        tmp += digit[j];

              }

          }

          // return result string

          return tmp;

     } // end toString

     //=========================================================

// add more private methods here if you like to .....

     private void resetArray()

     {

          for (int i = 0; i < MAX_SIZE; i++)

              digit[i] = 0;

     }

     //=========================================================

     // print msgs for main method

     private static void printMsg(String msg)

     {

          System.out.println(msg);

     }

}

Hope this will help you..