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();
}
}
18 5836
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
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
If you need more help on this same problem, post in this same thread instead of starting a new thread for the same problem
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
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.
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.
- import java.util.Scanner;
-
public class LargeInteger {
-
private int[] number;
-
private int max;
-
public LargeInteger() {
-
number = new int[40];
-
max = 0;
-
}
-
-
//If you've done exception handling, you should use it here
-
public LargeInteger(int max) {
-
if(max > 40) {
-
System.out.println("I can only store 40 digit numbers");
-
}
-
else {
-
number = new int[40];
-
this.max = max;
-
}
-
}
-
//****Be careful when writting this one!
-
//public int[] add(int[] num) {
-
-
//}
-
public void setDigit(int digit, int position) {
-
number[position] = digit;
-
}
-
public String toString() {
-
String num = "";
-
for(int i = 0;i < max; i++) {
-
num = num + number[i];
-
}
-
return num;
-
}
-
-
public static void main(String[] args) {
-
Scanner key= new Scanner(System.in);
-
System.out.print("Enter the number of digits in the number: ");
-
int size = key.nextInt();
-
LargeInteger large = new LargeInteger(size);
-
for(int i = 0; i < size; i++) {
-
large.setDigit(key.nextInt(), i);
-
}
-
-
System.out.println("Number stored is: "+large.toString());
-
-
}
-
}
THANKS r035198x,
will keep u posted as i progress with this problem.
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.
[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?
problem with adding of numbers
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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.
|
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?
|
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
| |
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...
|
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?
|
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...
|
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.
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
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
| | |