473,796 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

12 New Member
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,construct ors


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.prin tln("Enter the size of the array.")
size=key.nextIn t();

for(int min=0; min<size.length ;min ++)
{
values[min]=key.nextInt();
}
}
Oct 19 '06 #1
18 5836
r035198x
13,262 MVP
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,construct ors


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.prin tln("Enter the size of the array.")
size=key.nextIn t();

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 MVP
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
johnathan
12 New Member
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,compare s two integers for equality.
Oct 20 '06 #4
r035198x
13,262 MVP
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,compare s 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
johnathan
12 New Member
THANKS r035198x,

will keep u posted as i progress with this problem.
Oct 21 '06 #6
johnathan
12 New Member
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 MVP
[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
johnathan
12 New Member
problem with adding of numbers
Oct 24 '06 #9
r035198x
13,262 MVP
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

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

Similar topics

4
5975
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 been reading this ng all semester, and found it very helpful. I just have a stupid problem going on right now. Here's the project: We have to make a little frame with a text field, where the user inputs a number, and a text area where a...
6
1707
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 = {2,5,2} The algorithm should find that the "2" occurs more than once in S1, S2, and
5
2215
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 about sorting and started thinking about shell sort.... and as part of my trying to pythonise my mind and break my java mindset So I decided to tackle this old school problem with the python mindset.
7
6444
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 now thown out of the array released properly by the CLI?
168
7305
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
1777
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, but I purposely had this array filled unsequentially because the key for the array is meant to act as an ID which has a contextual meaning in my script. The problem, of course, is that for...in also returns my method 'contains' as one of the...
3
2570
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? int Large_Array; This will cause a "stack overflow" when i try and execute. How can I do this?
1
1266
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. Since I'm not very good with arrays (it was advised I use an array of counters, but since my professor has yet to teach us do so, that wouldn't really work anyway). So I decided to get creative and do it this way (not finished yet, only worked up to...
151
8135
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 because, comparatively, it is small, efficient, and able to handle large and complex tasks. I could not understand why people are using and talking about other programming languages.
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.