-
November 7th, 2011, 12:17 AM
#1
Arrays and Methods Help in Java
Hello everyone,
I'm new to java and having difficulty getting the correct syntax. I am supposed to create a HugeInteger(), inputHugeInteger(), outputHugeInteger(), addHugeIntgers(), isEqualTo(), and isOdd() methods. I only need help fixing addHugeIntgers() [adds two separate char arrays together and returns result to be saved in third array], isEqualTo() [detemines if the two arrays are equal to each other] and isOdd() [determines if the array is odd] methods. My syntax is the problem. Any help would be greatly appreciated.
Here is my code so far:
//Start of HugeInteger class
import java.util.*;
public class HugeInteger
{
private char[] digits;
private int MAX = 25;
public HugeInteger()
{
digits = new char[MAX];
for(int i = 0; i < MAX; i++)
digits[i] = '0';
}
public void inputHugeInteger()
{
Scanner scan = new Scanner(System.in);
String value = scan.nextLine();
for(int i = 0; i < value.length(); i++)
digits[value.length() - 1 - i] = value.charAt(i);
}// End of method inputHugeInteger()
public void outputHugeInteger()
{
for(int i = 25; i >= 0; i--)
System.out.println(digits[i]);
}//End of method outputHugeInteger()
public HugeInteger addHugeInteger(HugeInteger X, HugeInteger Y)
{
HugeInteger result = new.HugeInteger();
for(int i = 0; i <= 25; i++)
result[i]= HugeInteger[i] + HugeInteger[i];
return result;
}//End of method addHugeInteger()
public boolean isEqualTo(HugeInteger num2[]) //Method returns true or false as to whether two arrays equal eachother
{
boolean isEqual= false;
for(int i = 0; i < value.length(); i++)
if(digits[i] == num2.digits[i])
isEqual = true;
return isEqual;
}// End of method isEqualTo()
public boolean isOdd(HugeInteger X) //Method returns true or false as to whether it is odd
{
boolean isOddnum = false;
if(X[0] %2 != 0) //Num is Odd
isOddnum = true;
return isOddnum;
}// End of method isOdd()
}//End of class HugeInteger
//Start of the main class
//Purpose of program is to call all the HugeInteger methods
public class mainHugeInt
{
public static void main(String[] args)
{
HugeInteger num1 = new HugeInteger();
num1.inputHugeInteger();
HugeInteger num2 = new HugeInteger();
num2.inputHugeInteger();
HugeInteger result = new HugeInteger();
result =num1.addHugeInteger(num2);
//Print statements of whether even or odd integer
if(num1.isOdd())//Result was true
System.out.println("Odd integer.");
else
System.out.println("Even integer.");
if(num2.isOdd())//Result was true
System.out.println("Odd integer.");
else
System.out.println("Even integer.");
if(num1.isEqualTo()) //Result was true
System.out.println("Both integers are equal to eachother!");
else
System.out.println("The integers are not equal to eachother!");
}
}
-
November 7th, 2011, 09:20 AM
#2
Re: Arrays and Methods Help in Java
Please post code in code tags.
What problems are you actually having? If it doesn't compile/run post the compiler/runtime error messages. If it doesn't do what you expect then tell us what the method is supposed to do and what it is actually doing.
Some pointers:
- It would be easier to implement the methods if you stored each of the digits as an int rather than a char. You can't add 2 chars together as if they are numeric values.
- Remember each array element must be empty or a single digit so if the result of a digit addition is greater than 9 you need to subtract 10 and carry 1 to the next digit addition. Remember also that some array elements may be null eg if the number has less than 25 digits.
- When iterating over an array use the array's length property to get the size of the array rather than using a literal value ie use digits.length rather than 25.
- When checking if 2 arrays are equal to each other you can use a fail fast approach ie as soon as one set of elements are not equal to each other there is no need to check the rest of the elements so you can break out the loop and return false.
-
November 7th, 2011, 12:16 PM
#3
Re: Arrays and Methods Help in Java
Sorry, I'll try this again...
I updated my code for HugeInteger so that the isEqualTo method is an if statement inside of a while loop inside of a for loop. If there is a better way to code this than please explain it to me.
My main issues are with the syntax errors and getting the correct arguments for the methods to work. Other than that, I believe the functionality is fine and it will work properly when it is able to compile. My mainHugeInteger class is supposed to call all the methods from the HugeInteger class to show that it actually works properly semantically.
Thanks for your help and I hope you will help me again.
Code:
import java.util.*;
public class HugeInteger
{
private char[] digits;
private int MAX = 25;
public HugeInteger()
{
digits = new char[MAX];
for(int i = 0; i < MAX; i++)
digits[i] = '0';
}
public void inputHugeInteger()
{
Scanner scan = new Scanner(System.in);
String value = scan.nextLine();
for(int i = 0; i < value.length(); i++)
digits[value.length() - 1 - i] = value.charAt(i);
}// End of method inputHugeInteger()
public void outputHugeInteger()
{
for(int i = 25; i >= 0; i--)
System.out.println(digits[i]);
}//End of method outputHugeInteger()
public HugeInteger addHugeInteger(HugeInteger X, HugeInteger Y)
{
HugeInteger result = new.HugeInteger();
for(int i = 0; i <= 25; i++)
result[i]= HugeInteger[i] + HugeInteger[i];
return result;
}//End of method addHugeInteger()
public boolean isEqualTo(HugeInteger num2[]) //Method returns true or false as to whether two arrays equal eachother
{
boolean isEqual= true;
for(int i = 0; i <= MAX; i++)
{
while(isEqual == true)
{
if(digits[i] != num2.digits[i])
isEqual = false;
}
}
return isEqual;
}// End of method isEqualTo()
public boolean isOdd(HugeInteger X) //Method returns true or false as to whether it is odd
{
boolean isOddnum = false;
if(X[0] %2 != 0) //Num is Odd
isOddnum = true;
return isOddnum;
}// End of method isOdd()
}//End of class HugeInteger
Here are the compiler errors for the code above
Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addHugeInteger(HugeInteger, HugeInteger) in the type HugeInteger is not applicable for the arguments (HugeInteger)
The method isOdd(HugeInteger) in the type HugeInteger is not applicable for the arguments ()
The method isOdd(HugeInteger) in the type HugeInteger is not applicable for the arguments ()
The method isEqualTo(HugeInteger[]) in the type HugeInteger is not applicable for the arguments ()
at mainHugeInt.main(mainHugeInt.java:14)
Code:
//Program created by Michelle Jones 11/3/11
//Purpose of program is to call all the HugeInteger methods
public class mainHugeInt
{
public static void main(String[] args)
{
HugeInteger num1 = new HugeInteger();
num1.inputHugeInteger();
HugeInteger num2 = new HugeInteger();
num2.inputHugeInteger();
HugeInteger result = new HugeInteger();
result =num1.addHugeInteger(num2);
//Print statements of whether even or odd integer
if(num1.isOdd())//Result was true
System.out.println("Odd integer.");
else
System.out.println("Even integer.");
if(num2.isOdd())//Result was true
System.out.println("Odd integer.");
else
System.out.println("Even integer.");
if(num1.isEqualTo()) //Result was true
System.out.println("Both integers are equal to eachother!");
else
System.out.println("The integers are not equal to eachother!");
}
}
And here are the compiler errors for the main method
Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addHugeInteger(HugeInteger, HugeInteger) in the type HugeInteger is not applicable for the arguments (HugeInteger)
The method isOdd(HugeInteger) in the type HugeInteger is not applicable for the arguments ()
The method isOdd(HugeInteger) in the type HugeInteger is not applicable for the arguments ()
The method isEqualTo(HugeInteger[]) in the type HugeInteger is not applicable for the arguments ()
at mainHugeInt.main(mainHugeInt.java:14)
-
November 7th, 2011, 12:39 PM
#4
Re: Arrays and Methods Help in Java
My main issues are with the syntax errors and getting the correct arguments for the methods to work. Other than that, I believe the functionality is fine and it will work properly when it is able to compile.
I wouldn't be so confident if I was you. Especially as I've already pointed out something that won't work and you haven't fixed.
If there is a better way to code this than please explain it to me.
Your while loop inside the for loop will never exit if the elements contain the same char, which means your program will go into an infinite loop.
All you need to do is test if the values are not the same in which case set isEqual to false and then break out of the loop ie:
Code:
//Method returns true or false as to whether two arrays equal each other
public boolean isEqualTo(HugeInteger num2[])
{
boolean isEqual = true;
for ( int i = 0; i <= digits.length; i++ )
{
if ( digits[i] != num2.digits[i] )
{
isEqual = false;
break;
}
}
return isEqual;
}// End of method isEqualTo()
Here are the compiler errors for the code above
You are attempting to call methods that don't exist ie you do not have any no argument methods with those names. If, for example, you want to call the isOdd method you have to pass in a HugeInteger object as you have declared the isOdd method as having a parameter of type HugeInteger.
Whilst we are on the subject of the isOdd method, it's a little strange that it takes any argument at all, surely isOdd should be checking this objects state and not another objects state.
Last edited by keang; November 7th, 2011 at 04:05 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|