Answered! I have problems for my add and substract methods. what I am returning in my add and substract method is null, but…

This is the program that I want to do.

View post on imgur.com

I have problems for my add and substract methods. what I am returning in my add and substract method is null, but I want to return the answer (String variable display) and it said incompatible type: String cannot be converted to BigInt

How I supposed to return the String display or to make my System.out.println(b3); able to output the String display.

Also,

if (pos == other.pos){

statement}

else{

here I want to return to substract method to calculate the answer how I suppose to write this statement?

}

here is my java program

package bigint;
import java.util.ArrayList;

public class BigInt {
private ArrayList<Integer>list = new ArrayList<Integer>();
private ArrayList<Integer>answer = new ArrayList<Integer>();
private boolean pos=true;
private String number;
private Integer number1;
private String sign;
private Integer numberWithSign;
private String signAnswer;
BigInt(String num)
{
number = num;
numberWithSign = Integer.parseInt(num);
}

public boolean TestStringAndRemoveSign (){

if (number.length() >= 1) {
if (number.charAt(0) == ‘-‘){
pos = false;
number = number.substring(1);
if (number.length() == 0){
return false;
}
else {
return true;
}
}
if (number.charAt(0) == ‘+’){
number = number.substring(1);
pos = true;
if (number.length() == 0){
return false;
}
else {
return true;
}
}
if (number.length() == 0){
return false;}
else {
return true;
}
}
else {
return false;
}
}
public void Store(){
if(TestStringAndRemoveSign()){
for (int i = (number.length() – 1); i >= 0 ; i–){
if (Character.isDigit(number.charAt(i))){
number1 = Character.getNumericValue(number.charAt(i));
list.add(number1);
}
else{
throw new BigIntOutOfBoundsException (“Wrong format”);
}
}
}
else{
throw new BigIntOutOfBoundsException (“Wrong format”);
}
}
public String ToString(){
String display = “”;
if (pos == true){
sign = “”;
}
if (pos == false){
sign = “-“;
}
display = display + sign;
for (int i = number.length()-1; i>=0; i–){
display = display + list.get(i);
}

return display;

}
public void FixLength(BigInt other){
if (list.size()>other.list.size()){
for (int b=other.list.size(); b<list.size();b++){
other.list.add(0);
}
}
if (list.size()<other.list.size()){
for (int b=list.size(); b<other.list.size();b++){
other.list.add(0);
}
}
}
public BigInt add(BigInt other){

FixLength(other);
if (pos ==other.pos){
for (int a =0; a<=list.size() – 1; a++){
answer.add(list.get(a) + other.list.get(a));
}
for(int k=0; k<=answer.size() – 1; k++){
if(answer.get(k)>= 10){
answer.set(k,answer.get(k)-10);
answer.set(k+1,answer.get(k+1)+1);
}
}
Integer finalAnswer;
finalAnswer = numberWithSign + other.numberWithSign;
if (finalAnswer.toString().charAt(0) == ‘-‘){
signAnswer = “-“;
}
else{
signAnswer = “”;
}
String display = “”;
display = display + signAnswer;
for(int c=answer.size()-1; c>=0; c–){
display = display + answer.get(c);
}

return null;

}
else{
//return substract
}
return null;
}
public BigInt substract(BigInt other){
FixLength(other);
Integer IntNumber;
Integer otherIntNumber;
IntNumber = Integer.parseInt(number);
otherIntNumber = Integer.parseInt(other.number);
if (IntNumber > otherIntNumber || IntNumber==otherIntNumber){
for (int a =0; a<=list.size() – 1; a++){
if (list.get(a)<other.list.get(a)){
list.set(a,list.get(a)+10);
list.set(a+1,list.get(a+1)-1);
}
answer.add(list.get(a) – other.list.get(a));
}
}
else{
for (int a =0; a<=list.size() – 1; a++){
if(list.get(a)>other.list.get(a)){
other.list.set(a,other.list.get(a)+10);
other.list.set(a+1,other.list.get(a+1)-1);
}
answer.add(other.list.get(a)-list.get(a));
}
}
return null;
}

}

my java class that test the program

package bigint;

public class BigIntDemo {
public static void main(String[] args) {

BigInt b1 = new BigInt(“652234”);
BigInt b2 = new BigInt(“4567”);
BigInt b3;
b1.Store();
b2.Store();

b3 = b1.add(b2);
System.out.println(b3);
System.out.println(“b1 is ” + b1.ToString());

}
}

Expert Answer

 // i modified your code. and added code for subtraction method.

BigInt.java

import java.util.ArrayList;
class BigInt {
private ArrayList<Integer>list = new ArrayList<Integer>();
private ArrayList<Integer>answer = new ArrayList<Integer>();
private boolean pos=true;
private String number;
public Integer number1;
private String sign;
private Integer numberWithSign;
private String signAnswer;
BigInt(String num)
{
number = num;
numberWithSign = Integer.parseInt(num);

}

public boolean TestStringAndRemoveSign (){

if (number.length() >= 1) {
if (number.charAt(0) == ‘-‘){
pos = false;
number = number.substring(1);
if (number.length() == 0){
return false;
}
else {
return true;
}
}
if (number.charAt(0) == ‘+’){
number = number.substring(1);
pos = true;
if (number.length() == 0){
return false;
}
else {
return true;
}
}
if (number.length() == 0){
return false;}
else {
return true;
}
}
else {
return false;
}
}
public void Store(){
if(TestStringAndRemoveSign()){
for (int i = (number.length() – 1); i >= 0 ; i–){
if (Character.isDigit(number.charAt(i))){
number1 = Character.getNumericValue(number.charAt(i));
list.add(number1);
}
else{
System.out.println( “throw new BigIntOutOfBoundsException Wrong format”);
}
}
}
else{
System.out.println(“throw new BigIntOutOfBoundsException Wrong format”);
}
}
public String ToString(){
String display = “”;
if (pos == true){
sign = “”;
}
if (pos == false){
sign = “-“;
}
display = display + sign;
for (int i = number.length()-1; i>=0; i–){
display = display + list.get(i);
}

return display;

}
public void FixLength(BigInt other){
if (list.size()>other.list.size()){
for (int b=other.list.size(); b<list.size();b++){
other.list.add(0);
}
}
if (list.size()<other.list.size()){
for (int b=list.size(); b<other.list.size();b++){
other.list.add(0);
}
}
}
public BigInt add(BigInt other){

FixLength(other);

if (pos ==other.pos){
for (int a =0; a<=list.size() – 1; a++){
answer.add(list.get(a) + other.list.get(a));
}
for(int k=0; k<=answer.size() – 1; k++){
if(answer.get(k)>= 10){
answer.set(k,answer.get(k)-10);
answer.set(k+1,answer.get(k+1)+1);
}
}
Integer finalAnswer;
finalAnswer = numberWithSign + other.numberWithSign;
if (finalAnswer.toString().charAt(0) == ‘-‘){
signAnswer = “-“;
}
else{
signAnswer = “”;
}
String display = “”;
display = display + signAnswer ;
for(int c=answer.size()-1; c>=0; c–){
display = display + answer.get(c);
}
//addition result is stored in display.

BigInt sum= new BigInt(String.valueOf(display)); //new BigInt is created for added value
System.out.println(“addition : ” +sum.numberWithSign); //display the added results
return sum;

}
//if sign is different for both integers. then call the substraction methods.
else{
BigInt s=new BigInt(String.valueOf(numberWithSign));
s.Store();
BigInt b =s.substract(other);
}
return null;
}
public BigInt substract(BigInt other){
FixLength(other);
Integer sub;
Integer IntNumber;
Integer otherIntNumber;
IntNumber = Integer.parseInt(number);
otherIntNumber = Integer.parseInt(other.number);
if (IntNumber > otherIntNumber || IntNumber==otherIntNumber){
for (int a =0; a<=list.size() – 1; a++){
if (list.get(a)<other.list.get(a)){
list.set(a,list.get(a)+10);
list.set(a+1,list.get(a+1)-1);
}
answer.add(list.get(a) – other.list.get(a));
}
}
else{
for (int a =0; a<=list.size() – 1; a++){
if(list.get(a)>other.list.get(a)){
other.list.set(a,other.list.get(a)+10);
other.list.set(a+1,other.list.get(a+1)-1);
}
answer.add(other.list.get(a)-list.get(a));
}
}
Integer finalAnswer;
finalAnswer = numberWithSign – other.numberWithSign;
if (finalAnswer.toString().charAt(0) == ‘-‘){
signAnswer = “-“;
}
else{
signAnswer = “”;
}
String display = “”;
display = display + signAnswer ;
for(int c=answer.size()-1; c>=0; c–){
display = display + answer.get(c);
}
//substracted results is stored in subvalue
BigInt subvalue= new BigInt(String.valueOf(display));
System.out.println(“substraction : ” +subvalue.numberWithSign); //results is displayed
return subvalue;

}
}

//my java class that test the program

//package bigint;

public class BigIntDemo {
public static void main(String[] args) {

BigInt b1 = new BigInt(“-652234”);
//System.out.println(b1);
BigInt b2 = new BigInt(“4567”);
BigInt b3;
b1.Store();
b2.Store();

System.out.println(“b1 is ” + b1.ToString());
System.out.println(“b2 is ” + b2.ToString());
b3 = b1.add(b2);
;

}
}

output1:

output2:

//try with your testcases.. it works fine…for any clarification or modification tell me in comment section.. dont forget to upvote..

Still stressed from student homework?
Get quality assistance from academic writers!