473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

more info on my question, ADDING LARGE INTEGERS, in java arrays

I need help in writing the addition , subtraction etc. method.

eample the add method must add large intergers,
say 2500 + 69587562. each number is entered in the subscript individually.


this is what i have done so far.I created the class,constructors


public class Example
{
private int[] values;
private int min;
private int max;


Scanner Key= new Scanner(System.in);

public Example()
{
values=20;
min=0;
max=20;
}

public int ReadSize( int size)
{

System.out.println("Enter the size of the array.")
size=key.nextInt();

for(int min=0; min<size.length;min ++)
{
values[min]=key.nextInt();
}
}
Oct 19 '06 #1
18 5796
r035198x
13,262 8TB
I need help in writing the addition , subtraction etc. method.

eample the add method must add large intergers,
say 2500 + 69587562. each number is entered in the subscript individually.


this is what i have done so far.I created the class,constructors


public class Example
{
private int[] values;
private int min;
private int max;


Scanner Key= new Scanner(System.in);

public Example()
{
values=20;
min=0;
max=20;
}

public int ReadSize( int size)
{

System.out.println("Enter the size of the array.")
size=key.nextInt();

for(int min=0; min<size.length;min ++)
{
values[min]=key.nextInt();
}
}
You will want to use code tags next time when posting code. Your constructor does not correctly initialize the array. If you want the array to store 20 integers you do
Expand|Select|Wrap|Line Numbers
  1. values = new int[20];
Now about that LARGE aspect. Either your array stores integers (type int), Integers (the Integer class) or long for larger integers or type Long. All these store integers deepending on the size of the integers required.
Look up their bounds and decide which one best suits your numbers
Oct 19 '06 #2
r035198x
13,262 8TB
If you need more help on this same problem, post in this same thread instead of starting a new thread for the same problem
Oct 19 '06 #3
You will want to use code tags next time when posting code. Your constructor does not correctly initialize the array. If you want the array to store 20 integers you do
Expand|Select|Wrap|Line Numbers
  1. values = new int[20];
Now about that LARGE aspect. Either your array stores integers (type int), Integers (the Integer class) or long for larger integers or type Long. All these store integers deepending on the size of the integers required.
Look up their bounds and decide which one best suits your numbers

I NEED UR HELP IN Writing at least one method, thanks



here is the full question;

create a class called large integer that uses an int array to store very large integers and perform arithmetic and logic operations.
given default constructot 40element array to hold large integer and sets the current length of array to 0 and max to 40.

provide methods, to perform addition, subtraction, output a large integer,compares two integers for equality.
Oct 20 '06 #4
r035198x
13,262 8TB
I NEED UR HELP IN Writing at least one method, thanks



here is the full question;

create a class called large integer that uses an int array to store very large integers and perform arithmetic and logic operations.
given default constructot 40element array to hold large integer and sets the current length of array to 0 and max to 40.

provide methods, to perform addition, subtraction, output a large integer,compares two integers for equality.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class LargeInteger {
  3.     private int[] number;
  4.     private int max;
  5.     public LargeInteger() {
  6.         number = new int[40];
  7.         max = 0;
  8.     }
  9.  
  10.     //If you've done exception handling, you should use it here
  11.     public LargeInteger(int max) {
  12.         if(max > 40) {
  13.             System.out.println("I can only store 40 digit numbers");
  14.         }
  15.         else {
  16.             number = new int[40];
  17.             this.max = max;
  18.         }
  19.     }
  20.     //****Be careful when writting this one!
  21.     //public int[] add(int[] num) {
  22.  
  23.     //}
  24.     public void setDigit(int digit, int position) {
  25.         number[position] = digit;
  26.     }
  27.     public String toString() {
  28.         String num = "";
  29.         for(int i = 0;i < max; i++) {
  30.             num = num + number[i];
  31.         }
  32.         return num;
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         Scanner key= new Scanner(System.in);
  37.         System.out.print("Enter the number of digits in the number: ");
  38.         int size = key.nextInt();
  39.         LargeInteger large = new LargeInteger(size);
  40.         for(int i = 0; i < size; i++) {
  41.             large.setDigit(key.nextInt(), i);
  42.         }
  43.  
  44.         System.out.println("Number stored is: "+large.toString());
  45.  
  46.     }
  47. }
Oct 21 '06 #5
THANKS r035198x,

will keep u posted as i progress with this problem.
Oct 21 '06 #6
the addition method i need help with must do for example 125895+3567904.


the read statement looks some thing like this
[quote]for ( int i=array.length -1; i >=0; i - -)
array[i]=key.nextInt();
/QUOTE]

Note this is a 1dim array.
Oct 24 '06 #7
r035198x
13,262 8TB
[quote=johnathan]the addition method i need help with must do for example 125895+3567904.


the read statement looks some thing like this
for ( int i=array.length -1; i >=0; i - -)
array[i]=key.nextInt();
/QUOTE]

Note this is a 1dim array.
Are you having problems with reading in the numbers or adding the numbers?
Oct 24 '06 #8
problem with adding of numbers
Oct 24 '06 #9
r035198x
13,262 8TB
problem with adding of numbers
The easiest way is to cheat it as follows:
declare an empty string s = "";
get each character in the array and append it to the string so that if the array had {4,6,7,8} as its values, the string s would now have "4678".
convert the string to a Long value and do the addition on Long values. Convert the Long value back to a string and read character by character into an int[] to get the number back to int[] format.

A more interesting "fair" approach would be to try to loop through the arrays from right to left, adding the corresponding values considering carrys. This is not too difficult actually as for each addition you check if the number exceeds 10. If not, fine just set that value. If it exceeds the unit value simply result - 10 and the carry is always 1
Oct 24 '06 #10
could u wirte a method for me to see what it would look like, doing it the "FAIR method. thanks
Oct 24 '06 #11
r035198x
13,262 8TB
could u wirte a method for me to see what it would look like, doing it the "FAIR method. thanks
should look something like this. I have to go for now but you should make the neccessary changes to your code to make this compile and then try to fine tune it to get the correct results. I 'll be in tommorrow morning about 8 hrs from now but others may be able to help you before then
Expand|Select|Wrap|Line Numbers
  1. public LargeInteger add(LargeInteger num) {
  2.         int size = num.getMax() > getMax() ? num.getMax(): getMax();
  3.         LargeInteger result = new LargeInteger(size + 1);
  4.  
  5.         for(int i = 0; i < size; i++) {
  6.             int resultPos = size;
  7.             int carry = 0;
  8.             int numPos = num.getMax();
  9.             int thisPos = number.getMax();
  10.             int unit = 0;
  11.             int sum = carry + num.getDigit(numPos) + number.getDigit(thisPos);
  12.             if(sum > 10) {
  13.                 unit = sum - 10;
  14.                 carry = 1;
  15.             }
  16.             else {
  17.                 carry = 0;
  18.                 unit = sum;
  19.             }
  20.             result.setDigit(unit, resultPos);
  21.             resultPos = resultPos - 1;
  22.             numPos = numPos - 1;
  23.             thisPos = thisPos - 1;
  24.         }
  25.         return result;
  26.  
  27.  
  28.  
  29.     }
Oct 24 '06 #12
this is what the program look like.

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

example test= new example (5);
example test2= new example(6);

example test3= new example(7);

test.readinfo();
test.printnum();

test2.readinfo();
test2.printnum();

test3.add(test,test2);



this is the class that i have so far. would u kindly help with the addition
subtraction methods. sorry am going to copy and paste this here.




public class example
{
private int [] values;


public example ()
{
values= new int[40];

}

public example(int size)
{
values=new int [size];
}

public void readinfo()
{
int ctr=0;
String asknum=JOptionPane.showInputDialog("Enter the numbers.");

for( int i=asknum.length() -1; i >=0; i--)
{
values [ctr]=asknum.charAt(i)-48;
ctr ++;
}
}

public void printnum()
{
for(int i=0; i<values.length; i ++)
{
JOptionPane.showMessageDialog(null,"Display the numbers." + values[i]);

}
}

public void add( )
{
Oct 26 '06 #13
r035198x
13,262 8TB
this is what the program look like.

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

example test= new example (5);
example test2= new example(6);

example test3= new example(7);

test.readinfo();
test.printnum();

test2.readinfo();
test2.printnum();

test3.add(test,test2);



this is the class that i have so far. would u kindly help with the addition
subtraction methods. sorry am going to copy and paste this here.




public class example
{
private int [] values;


public example ()
{
values= new int[40];

}

public example(int size)
{
values=new int [size];
}

public void readinfo()
{
int ctr=0;
String asknum=JOptionPane.showInputDialog("Enter the numbers.");

for( int i=asknum.length() -1; i >=0; i--)
{
values [ctr]=asknum.charAt(i)-48;
ctr ++;
}
}

public void printnum()
{
for(int i=0; i<values.length; i ++)
{
JOptionPane.showMessageDialog(null,"Display the numbers." + values[i]);

}
}

public void add( )
{
Are we still going the right direction here?
Oct 26 '06 #14
am sorry, but i am.
that is what i came up with for the read statement, now following that i need help in the addition method, note please take a look at the program also.
Oct 26 '06 #15
i need hepl with the addition and subtraction method.above you will see what i did so far.

also how do you declare a class within a class method. example.
the program would be:

example test=new example()
example test2=new example()
example test3=new example()

test.readinfo();
test2.readinfo()
test3(test,test2) HOW IS THIS REPRESENTED INT THE CLASS METHOD

public void add( WHAT GOES IN HERE)



THANKS FOR HELPING.
Oct 27 '06 #16
Is This Correct.



public void add( examplexx a, examplexx b)
{
int carry=0;
int sum=0;

for(int i=0; i<values.length; i ++)
{
sum=carry + values.length + values.length;


if( sum >=10)
{
sum= sum-10;
carry =1;
}
else
{
carry =0;
}

}
JOptionPane.showMessageDialog(null, "The final sum is . " + sum);
}
Oct 28 '06 #17
r035198x
13,262 8TB
Is This Correct.



public void add( examplexx a, examplexx b)
{
int carry=0;
int sum=0;

for(int i=0; i<values.length; i ++)
{
sum=carry + values.length + values.length;


if( sum >=10)
{
sum= sum-10;
carry =1;
}
else
{
carry =0;
}

}
JOptionPane.showMessageDialog(null, "The final sum is . " + sum);
}
All I can say is this depends on where the method is. If the method is the number class (which in this case is examplexx?) then the method signature should be
Expand|Select|Wrap|Line Numbers
  1. public examplexx add(examplexx a)
. This is how I think you should be doing it as you could tell for my draft method for add that I did above(If you looked at it).

Now your method is taking in two objects a and b and doing absolutely nothing with them?
Oct 30 '06 #18
r035198x
13,262 8TB
Needless for me to point out that values is not defined in your method and so the method will not compile anyway.
Oct 30 '06 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
6
by: Markus Dehmann | last post by:
I have n sets of elements. I want to find elements that occur more than once in more than one set. Maybe the following example shows what I mean: S1 = {1,2,3,2,4} S2 = {2,2,4,5,4} S2 =...
5
by: akameswaran | last post by:
Disclaimer - I recognize this is not a practical exercise. There are many implementations around that would do the job better, more efficiently (Meaning in C) or whatever. I caught some thread...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
168
by: broeisi | last post by:
Hello, Is there a way in C to get information at runtime if a processor is 32 or 64 bit? Cheers, Broeisi
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
3
by: hamishd | last post by:
What is the best way to store large arrays of numbers (eg, 4-byte integers)? Say I want to store an array of 1billion 4-byte integers.. If my computer has 4gB memory, then is this possible? ...
1
by: JinFTW | last post by:
Ok so I had to develop a class in which I needed to count the number of times someone enters in a number from a selected group of numbers from 1 to 100. In this case we're talking about groups of 10....
151
by: istillshine | last post by:
There are many languages around: C++, JAVA, PASCAL, and so on. I tried to learn C++ and JAVA, but ended up criticizing them. Is it because C was my first programming language? I like C...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.