-
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!");
}
}
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
|