Question

DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

DIRECTIONS FOR THE WHOLE PROJECT BELOW

BigInt class

The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to store the string in an ArrayList. A public add method does not add. It calls helper methods to do the work. A public multiply method does not multiply. It calls two private methods. One is a multiply method that calls other methods to do the actual multiplication. A public divide method does not divide. It calls many private helper methods that do the work.

This is a challenging project that requires information hiding to be successful.

There are two main programs that you must use. On my website is a demo for the constructor and toString() methods. This demo must be run when you are working on the constructor and toString() method. There is a second BigInt demo for add, subtract, multiply and divide. This demo must be used when you test addition, subtraction, multiplication, division, and modulus.

(10 points) January 23, Wednesday: BigInt constructors and toString() method. Consider a String argument, an integer argument, and a BigInt argument to the constructor. Create a RuntimeException class that throws an exception when the input string does not hold a properly formed integer.

(20 points) February 4 Monday: BigInt addition and subtraction.
(10 points) February 11, Monday: BigInt multiplication.

(20 points) February 18, Monday: BigInt division and modulus.

When you solve this division problem use appropriate SOP() lines to demonstrate debugging your program.

BigInt Constructor and toString Considerations

Constructors initialize the private data at the point of declaration. The constructor has a String parameter. Java code must analyze this string and if it correctly represents a BigInt convert the characters to integers and store them in an ArrayList or an array. Example strings your constructor must correctly deal with are: “100”, “-5”, “-0”, “+0”, “+”, “-“, “56DDD8”, “   456”   The last four examples do not represent an integer and must be rejected.     A BigInt consists of a boolean sign and the array or ArrayList holding the absolute value of the number. If an array is used a third variable holding the size of the BigInt must also be included. There should be no other instance variables. Instance variables can be seen throughout the class. You will need other String, int and double variables that will be declared in methods and will be local to that method. These local variables can be passed as arguments to other methods. The algorithm could be:

  1. Read a String
  2. If the string has a – or + as the first character remove it and set the sign. . Using a very descriptive name the method might be called setSignAndRemoveItIfItIsThere(num). If the sign is the only character in the string end the program.
  3. Use a for loop to work through the string from the end of the string to the front. Store the string characters as integers. The Character wrapper class method isDigit() can make this conversion, or the character can be cast as an integer remembering to subtract 48 from the ASCII value. If a character other than a digit is encountered end the program.
  4. Think of the indexes of the ArrayList values going right to left starting with 0. With the string “549” the 9 can be placed int location 0, the 4 in location 1 and the 5 in location 2. With an ArrayList this happens automatically with the add method. Example: bigNum.add(value). (See ArrayList documentation. I will put some on my website.)
  5. The toString() method creates and returns a String with the sign if it is negative and then adding the ArrayList integers starting from the back of the list to the front.
  6. You must be familiar with the String methods to turn a string into integers that can be stored in an array. You then need to take the integers stored in an array and turn them back into a string in the toString() method.
  7.   important reminders:

String num = “-1234”; // creates a string of 5 characters

int i = num.length(); // strings know their length. i has a value of 5

the string method charAt returns the character value at the specified location

if(num.charAt(0) == '+' || num.charAt(0) == '-')

if(num.charAt(i) >= ‘0’ && num.charAt(i) <= ‘9’) it is a digit.

substring(start) returns a new string having the same characters as the substring that begins at index startof this string through to the end of the string. Index numbers begin at 0. Example:

num = num.substring(1) will assign the string “1234” to num

given int value = 5;

given num is “1234”

num = num + value;// num now is the string “12345”

Other String operations may be useful.

You can also test if a character is a digit with Character.isDigit()

Do not put a main method in the BigInt class. On my website are two main classes. One is to test the constructor and toString method and one is to test the operations add, subtract, multiply, divide, and modulus of the class. The main classes are in the same folder as the BigInt class.

Do not use an instance of the Scanner class for keyboard input. The constructor provides the strings for input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

ANSWER:

CODE:

import java.util.ArrayList;

public class BigInt {
   // variable that keep tracks of the positive/negative number
   private Boolean positive = true;
   // list containing the absolute value of the number
   private ArrayList<Integer> nums = new ArrayList<>();

   // constructor that takes the string as input and add values (if correct) to
   // arrayList
   public BigInt(String string) {
       String s = setSignAndRemoveItIfItIsThere(string);

       for (int i = s.length() - 1; i >= 0; i--) {
           try {
               Character c = s.charAt(i);
               Boolean val = Character.isDigit(c);

               if (val) {
                   nums.add(Integer.parseInt(s.substring(i, i + 1)));
               } else {
                   throw new InvalidIntegerException("not a correct Big integer");
               }

           } catch (InvalidIntegerException ex) {

           }

       }
   }

   // It receives the string that was sent to the constructor and sets a boolean
   // variable positive to true or false and then returns a string without the sign
   // back to constructor
   private String setSignAndRemoveItIfItIsThere(String string) {

       Character c = string.charAt(0);
       try {
           if (c.equals('+') || c.equals('-')) {

               // if only sign is there or empty string
               if (string.length() == 1 || c.equals(' ')) {
                   throw new InvalidIntegerException("not a correct Big integer");
               }
               if (c.equals('-')) {// negative number
                   positive = false;
                   return string.substring(1, string.length());
               } else if (c.equals('+')) {// positive number
                   positive = true;
                   return string.substring(1, string.length());
               }

           } else if (c.equals(' ')) {// case when the string starts with space
               throw new InvalidIntegerException("not a correct Big integer");
           }
       } catch (InvalidIntegerException ex) {
           return "";
       }
       return string;
   }

   @Override
   public String toString() {

       String result = "";

       for (int i = nums.size() - 1; i >= 0; i--) {
           result = result + nums.get(i);
       }

       if (!positive) {
           result = "-" + result;

       } else if (nums.size() > 0) {
           result = "+" + result;
       }

       return result;

   }

}

/////////////////////////////////////////////////////ENd BigInt.java///////////////////////////////////////

///////////////////////////////////////////////////InvalidIntegerException.java/////////////////////////

//Custom Exception class that is used when the Big Integer is not correct
public class InvalidIntegerException extends RuntimeException {

   public InvalidIntegerException(String msg) {
       super(msg);
   }
}
//////////////////////////////////ENd InvalidIntegerException.java/////////////////////////////////////

////////////////////////////////////BigIntTester.java////////////////////////////////////////////////

//The testing program

public class BigIntTester {

   /**
   * @param args
   */
   public static void main(String[] args) {
       BigInt b1 = new BigInt("1000000");
       BigInt b2 = new BigInt("+0");
       BigInt b3 = new BigInt("-0");
       BigInt b4 = new BigInt("5stt7");// error
       BigInt b5 = new BigInt("+");// error
       BigInt b6 = new BigInt("-");// error
       BigInt b7 = new BigInt(" 500");// error
       BigInt b8 = new BigInt("44444444445555555555666666666677777777770000000000");
       System.out.println("b1 is " + b1);
       System.out.println("b2 is " + b2);
       System.out.println("b3 is " + b3);
       System.out.println("b4 is " + b4);
       System.out.println("b5 is " + b5);
       System.out.println("b6 is " + b6);
       System.out.println("b7 is " + b7);
       System.out.println("b8 is " + b8);

   }

}

/////////////////////////////////////////ENd BigIntTester.java/////////////////////////////////

RATE THUMBSUP PLEASE

THANKS

Add a comment
Know the answer?
Add Answer to:
DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will...

    Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will have a default constructor, explicit constructor, and the following methods: read O public Complex add(Complex), public Complex subtract(Complex), public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(complex) and a toString) method. Include get and set methods as well. On my website is an explanation of complex numbers and examples with expected answers along with a main demo program. Study the explanation and use your...

  • This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones...

    This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones in which one letter is substituted for another. Although their output looks impossible to read, they are easy to break because the relative frequencies of English letters are known. The Vigenere cipher improves upon this. They require a key word and the plain text to be encrypted. Create a Java application that uses: decision constructs looping constructs basic operations on an ArrayList of objects...

  • package bigIntegerPackage; import java.util.Scanner; public class BigIntMathTesterOne {    private static Scanner keyboard = new Scanner(System.in);...

    package bigIntegerPackage; import java.util.Scanner; public class BigIntMathTesterOne {    private static Scanner keyboard = new Scanner(System.in);    /* *    * @param args    */    public static void main(String[] args)    { /** * Sample valid input and resulting output *    "44444444445555555555666666666677777777770000000000" * stores ["4","4","4","4","4","4","4","4","4","4","5","5","5","5","5","5","5","5","5","5","6","6","6","6","6","6","6'<'6","6","6","7","7","7","7","7","7","7","7","7","7","0","0","0","0","0","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "44444444445555555555666666666677777777770000000000" * "100000" stores ["1","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "100000" *"+0" stores ["0"] in ArrayList and sets...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

  • DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is...

    DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is only from-263 to 263-1. What if we need to manipulate integer values beyond this range? In this assignment you will write a HugeInteger class which is able to represent arbitrar- ily large integer numbers. This class must implement arithmetic operations on integers such as addition, subtraction, multiplication, division and comparison. You have to implement this class without using Java predefined classes, unless specified...

  • please answer question (5 points) Below is the skeleton for a simplified StringBuilder class as done...

    please answer question (5 points) Below is the skeleton for a simplified StringBuilder class as done in the Closed Lab this semester. As in the lab, the class has an ArrayList of Character to hold the individual characters of the StringBuilder object. Write the instance method indexOf as given below that returns the index of the first occurrence of the character c in the SimplestringBuilder. If there is no index for that character (i.e. the character is not in the...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • Given the following class: public class Vehicle { protected String manufacturer; // vehicle manufacturer protected int...

    Given the following class: public class Vehicle { protected String manufacturer; // vehicle manufacturer protected int year; // model year protected char fuelType; // G = gas, E = electric, D = diesel, W = wind, U = unknown protected String VIN; // an String to hold the 17 characters of the vehicle's VIN number. ... // no-arg constructor defined, as are all get/set methods. } Write me: An overloaded constructor (takes the four data members as parameters): assume that...

  • Programming language: Java Design an application that has three classes. The first one, named Book describes...

    Programming language: Java Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...

  • in java please Purpose: To practice designing a class that can be used in many applications....

    in java please Purpose: To practice designing a class that can be used in many applications. To write and implement a thorough test plan. You are NOT to use any of the Java API date classes (Gregorian calendar, Date…) except for the constructor method to set the date fields to the current data. Write a class to hold data and perform operations on dates. Ie: January 2, 2019   or 3/15/2018 You must include a toString( ), an equals( ) and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT