public static int compareTo( String digitSequenceO, String digitSequencel) First
ID: 3529971 • Letter: P
Question
public static int compareTo( String digitSequenceO, String digitSequencel) First, if either digitSequenceO or digitSequencel is a bogus digitSequence, complain and die. (Of course I expect you'll call your digitSequence method, not repeat that logic here.) compareTo tests whether the integer represented by the first arg is less than, equal to, or greater than the integer represented by the second arg. Don't convert the Strings to ints, the String might be a million digits long and won't fit in an int. Instead, examine the digits to see which number is greater, e.g., // 123 123,so compareTo returns a positive int (it doesn't matter which // positive int) compareTo( " 124", "123") // 123 == 123, so compareTo returns 0 compareTo( "123", "00000000000000000000000000000000123" ) // 000123 -124, so compareTo returns a positive int // (Be smart about negative numbers) compareTo( "-123", "-124" ) // 0 == -00, so compareTo returns 0 // (Be smart about negative 0)compareTo( "0","-00" )Explanation / Answer
I have written similar to this earlier named BigInt class.The below program will add,multiply and compare two bigInt(i.e sequence of integer).
please rate
--------------------------------------------------
public class BigInt {
private int digit[];
// represent the integer as an array of digits
private static int size;
// number of digits in the integer
private final int max = 50;
//maximum number of digits in the integer
public BigInt() {
super();
}
public BigInt(String num)
{
size = num.length();
digit = new int[max];
for (int ct = size - 1; ct >= 0; ct --) {
digit[ct] = Integer.parseInt(num.substring(size - ct - 1, size - ct));
}
}
public BigInt(BigInt num) { // copy constructor
}
public String toString() { // override Object
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.