Question

please help me I need two methods that add and subtract two very large numbers in...

please help me

I need two methods that add and subtract two very large numbers in java created in a class called number and it runs on the demo below class below.

Please don't use parseInt or BigIntger, they are not allowed.

Thank you so much

public class demoClass {

public static void main(String[] args) {

number num1; //number is class that we need to create to the add and subtract methods

num1 = new number("11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");

number num2; //number is class that we need to create to the add and subtract methods

num2 = new number("22222222222222222222222222222222222222222222222222222222222222222222222222222222");

number sum = num1.add(num2);

number minus = num1.subtract(num2);

}

}

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

public class number {

        private int[] n;

        public number() {
                n = new int[1];
        }

        public String trimZeroes(String s) {
                return s;
        }

        public number(String s) {
                while (s.startsWith("0")) {
                        s = s.substring(1);
                }
                n = new int[s.length()];

                for (int i = 0; i < n.length; ++i) {
                        n[n.length - i - 1] = s.charAt(i) - '0';
                }
        }

        public number(long n) {
                this.n = new int[String.valueOf(n).length()];
                int count = 0;

                while (n > 0) {
                        this.n[count++] = (int) (n % 10);
                        n = n / 10;
                }
        }

        public number add(number o) {
                int carry = 0;
                int max = n.length > o.n.length ? n.length : o.n.length;
                int[] result = new int[max + 1];

                for (int i = 0; i <= max; ++i) {

                        int top = i < n.length ? n[i] : 0;
                        int bot = i < o.n.length ? o.n[i] : 0;

                        result[i] = (top + bot + carry) % 10;
                        carry = (top + bot + carry) / 10;
                }

                return new number(join(result));
        }

        // Assuming Calling BigInteger is greater in value
        public number subtract(number o) {
                int carry = 0;
                int max = n.length > o.n.length ? n.length : o.n.length;
                int[] result = new int[max + 1];

                for (int i = 0; i <= max; ++i) {

                        int top = i < n.length ? n[i] : 0;

                        // remove carry
                        top = top - carry;
                        // reset carry
                        carry = 0;

                        int bot = i < o.n.length ? o.n[i] : 0;

                        // check if we need to take a carry
                        if (top < bot) {
                                top = top + 10;
                                carry = 1;
                        }

                        result[i] = (top - bot);
                }

                return new number(join(result));
        }

        private String join(int[] nums) {
                String result = "";
                for (int i = nums.length - 1; i >= 0; --i) {
                        result += nums[i];
                }
                return result;
        }

        public String toString() {
                String s = "";
                for (int i : n) {
                        s = i + s;
                }
                return s;
        }

        public static void main(String[] args) {
                number num1; // number is class that we need to create to the add and subtract methods

                num1 = new number(
                                "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");

                number num2; // number is class that we need to create to the add and subtract methods

                num2 = new number("22222222222222222222222222222222222222222222222222222222222222222222222222222222");

                number sum = num1.add(num2);
                number minus = num1.subtract(num2);
                
                System.out.println(sum);
                System.out.println(minus);
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
please help me I need two methods that add and subtract two very large numbers in...
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
  • 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=...

  • Hello, I need help writing the two methods to print a triangle shape in java. An...

    Hello, I need help writing the two methods to print a triangle shape in java. An example of the shape is as follows: 9 8 7 6 5 4 3 8 7 6 5 4 3 7 6 5 4 3 6 5 4 3 5 4 3 4 3 3 One method should be defined as "public static void printPattern(int num1, int num2, Boolean ascending)". This method will print a upper-triangle matrix-like layout filled will a sequence of integers...

  • Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input...

    Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input and create an object of the ScientificCalculator Class with those two numbers. 2. Then call the 8 operations one by one sequentially and display the result. Code must be written in Java. (Sample input/output: Enter number 1: 2 Enter number 2: 3 Add() result is : 5 Sub() result is: -1 ……. Power() result is: 8 …… sqrtNum1 result is:) The Problem: Given the...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • Need help with this Java question in 1 hour please JAVA Using the files from the...

    Need help with this Java question in 1 hour please JAVA Using the files from the in-class assignment add two additional animals of your choice. For each animal add two additional methods appropriate to your particular animal. As the majority of the code for this program exists you will not need an algorithm. Use the 3 java classes below. Thank you //Animal.java public class Animal {    public Animal() {    System.out.println("A new animal has been created!");    }   ...

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • 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....

  • Create a simple calculator. Your interface should contain two input boxes in which the user can...

    Create a simple calculator. Your interface should contain two input boxes in which the user can put two numbers. There should be four buttons: add, subtract, multiply, and divide. When the user inputs two number and clicks on an operation button, the result should be displayed in the label. Can some please help my code that i came up with just displays a pop box with no words or nothing! I'm not the best at coding. Any tips or advice...

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