Ok, so I misunderstood something in class, and I don\'t have time to correct eve
ID: 3557710 • Letter: O
Question
Ok, so I misunderstood something in class, and I don't have time to correct everything in time, so I need help. I already have alot of the information, just need someone to put it together, correct it if its wrong, and add a couple of methods.
I need a class, called BigInt, and in that class It will have a constructor and toString method, and the following methods: Add, subtract, multiplication, and division. I've already have the toString, constructor, addition, and maybe the subtraction done. But I have them in their own classes by misunderstanding. So they need to be methods, and not all of them are correctly done but just need to be tweaked a bit.
If the division can't b done at the moment thats alright.
I want the class to accept input from the keyboard like +35, or -34, as well as just the numbers, and when it generates the answer I want the answer to have the positive or negative sign (ex: -45). Also I want all of the methods to deal with array lists.
Here is what I have:
//Actually look at the bottom for the correct toString and constructor (under the class name string) which this BigInt class had, but it gave the wrong output:
import java.util.Scanner;
public class BigInt {
String value, updatedvalue = "";
public BigInt(String b1) {
value = b1;
if (b1.charAt(0) == '+' || b1.charAt(0) == '-') {
if (b1.length() > 1) {
value = b1.substring(1);
} else {
System.out.println("this contains only sign: " + b1);
return;
}
}
int arr[] = new int[value.length()];
for (int i = 0; i < value.length(); i++) {
if (Character.isDigit(value.charAt(i))) {
arr[i] = Character.digit(value.charAt(i), 10);
} else {
System.out.println("this contains non numeric values: " + b1);
return;
}
}
if (arr.length > 0) {
}
for (int i = arr.length - 1; i > -1; i--) {
updatedvalue += arr[i];
}
if (updatedvalue != null)
System.out.println("this prints: " + updatedvalue);
}
}
//This one is addition
import java.util.Scanner;
public class Addition {
static Scanner in = new Scanner(System.in);
int num;
public static void main(String[] args) {
Addition b1=new Addition();
Addition b3=new Addition();
Addition b2=new Addition();
System.out.println("Enter the first number");
int num1=Integer.parseInt(in.nextLine());
b2.num=num1;
System.out.println("Enter the second number");
int num2=Integer.parseInt(in.nextLine());
b3.num=num2;
b1=b2.add(b3);
if(b1.num>0)
System.out.println("Sum : +"+b1.num);
else
System.out.println("Sum : "+b1.num);
}
public Addition add(Addition a)
{
Addition temp=new Addition();
temp.num=this.num+a.num;
return temp;
}
}
// And I found some templates of the subtraction, but its not quite right. It needs to do the above inputs and stuff. If you can't do the division right now it is fine.
//Subtraction:
minusArray.addAll(array1);
for(int 1=0; i< minusArray.size();i++){
for(int j = 0; j< array2.size(); j++){
if(minusArray.get(1).equals(array2.get(j))){
minusArray.remove(i);
if(i ==0){
;
}
else if (j == 0){
;
}
else{
i = 0;
j = 0;
}
}
else{}
}
}
return minusArray;
If the above toString and constructor doesn't work use this one, because this is perfect pretty much. It does have a slight problem though, it just needs the positive int to have a + sign. Otherwise its great, but you be the judge of that.
import java.util.*;
public class string {
public static String toString(int[] array, boolean is_neg)
{
StringBuilder st = new StringBuilder();
for(int i=0; i<array.length; i++)
{
st.append(array[i]);
}
if(is_neg) st.insert(0,'-');
return st.toString();
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
boolean is_negative = false;
String str;
System.out.println("Enter a string :");
str = in.next();
if(str.charAt(0) == '+' || str.charAt(0) == '-')
{
if(str.charAt(0) == '-') is_negative = true;
str = str.substring(1);
}
int[] array = new int[str.length()];
for(int i=str.length()-1; i>=0; i--)
{
if(Character.isDigit(str.charAt(i)))
array[i] = Character.valueOf(str.charAt(i))-'0';
else
System.exit(0);
}
System.out.println("Result is " + toString(array,is_negative));
}
}
Explanation / Answer
import java.util.Scanner;
public class BigInt {
private boolean is_neg; // negative
private int[] array;
public BigInt(String b1) {
String value = b1;
is_neg = false;
if (b1.charAt(0) == '+' || b1.charAt(0) == '-') {
if (b1.length() > 1) {
value = b1.substring(1);
} else {
System.out.println("this contains only sign: " + b1);
return;
}
if (b1.charAt(0) == '-')
is_neg = true;
}
array = new int[value.length()];
for (int i = 0; i < value.length(); i++) {
if (Character.isDigit(value.charAt(i))) {
array[array.length - 1 - i] = Character.digit(value.charAt(i), 10);
} else {
System.out.println("this contains non numeric values: " + b1);
return;
}
}
}
public String toString()
{
StringBuilder st = new StringBuilder();
if (is_neg)
st.append('-');
else
st.append('+');
for(int i=0; i<array.length; i++)
{
st.append(array[array.length - 1 - i]);
}
return st.toString();
}
private int[] removeLeadingZero(int[] a)
{
int i = a.length - 1;
while (i > 0 && a[i] == 0)
i--;
int[] c = new int[i+1];
for (int j = 0; j < c.length; j++)
c[j] = a[j];
return c;
}
// return 0 if same, <0 if a<b, > 0 if a > b
private int compareArr(int[] a, int[] b)
{
if (a.length != b.length)
return a.length - b.length;
for (int i = a.length - 1; i >= 0; i--)
{
if (a[i] != b[i])
return a[i] - b[i];
}
return 0;
}
private int[] addArr(int[] a, int[] b)
{
int size = Math.max(a.length, b.length) + 1;
int[] c = new int[size];
int carry = 0;
for (int i = 0; i < a.length || i < b.length || carry > 0; i++)
{
int sum = carry;
if (i < a.length)
sum += a[i];
if (i < b.length)
sum += b[i];
carry = sum / 10;
sum = sum % 10;
c[i] = sum;
}
return removeLeadingZero(c);
}
// assume a >= b
private int[] subArr(int[] a, int[] b)
{
int[] c = new int[a.length];
int carry = 0;
for (int i = 0; i < a.length || i < b.length || carry != 0; i++)
{
int r = carry;
if (i < a.length)
r += a[i];
if (i < b.length)
r -= b[i];
carry = (r < 0 ? -1 : 0);
if (r < 0)
r += 10;
c[i] = r;
}
return removeLeadingZero(c);
}
public BigInt add(BigInt b)
{
BigInt r = new BigInt("0");
if (is_neg && b.is_neg)
{
r.is_neg = true;
r.array = addArr(array, b.array);
}
else if (!is_neg && !b.is_neg)
{
r.is_neg = false;
r.array = addArr(array, b.array);
}
else if (!is_neg) // positive + negative
{
if (compareArr(array, b.array) >= 0)
{
r.is_neg = false;
r.array = subArr(array, b.array);
}
else
{
r.is_neg = true;
r.array = subArr(b.array, array);
}
}
else // negative + positive
{
if (compareArr(array, b.array) <= 0)
{
r.is_neg = false;
r.array = subArr(b.array, array);
}
else
{
r.is_neg = true;
r.array = subArr(array, b.array);
}
}
return r;
}
public BigInt subtract(BigInt b)
{
BigInt r = new BigInt("0");
if (!is_neg && b.is_neg) // positive - negative
{
r.is_neg = false;
r.array = addArr(array, b.array);
}
else if (is_neg && !b.is_neg) // negative - positive
{
r.is_neg = true;
r.array = addArr(array, b.array);
}
else if (!is_neg && !b.is_neg) // positive - positive
{
if (compareArr(array, b.array) >= 0)
{
r.is_neg = false;
r.array = subArr(array, b.array);
}
else
{
r.is_neg = true;
r.array = subArr(b.array, array);
}
}
else // negative - negative
{
if (compareArr(array, b.array) <= 0)
{
r.is_neg = false;
r.array = subArr(b.array, array);
}
else
{
r.is_neg = true;
r.array = subArr(array, b.array);
}
}
return r;
}
public BigInt multiple(BigInt b)
{
BigInt r = new BigInt("+0");
int p = 1;
for (int i = 0; i < array.length; i++)
{
int times = array[i] * p;
for (int j = 0; j < times; j++)
r = r.add(b);
p *= 10;
}
if (is_neg)
r.is_neg = !r.is_neg;
return r;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number : ");
BigInt ba = new BigInt(in.next());
System.out.print("Enter another number : ");
BigInt bb = new BigInt(in.next());
System.out.println(ba.toString() + " + " + bb.toString() + " = " +
ba.add(bb).toString());
System.out.println(ba.toString() + " - " + bb.toString() + " = " +
ba.subtract(bb).toString());
System.out.println(ba.toString() + " * " + bb.toString() + " = " +
ba.multiple(bb).toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.