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:
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.
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
DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...
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 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...
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=...
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...
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. 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 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...
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...
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...
Security is an important feature of information systems. Often, text is encrypted before being sent, and then decrypted upon receipt. We want to build a class (or several classes) encapsulating the concept of encryption. You will need to test that class with a client program where the main method is located. For this project, encrypting consists of translating each character into another character. For instance, if we consider the English alphabet, including characters a through z, each character is randomly...