Taking a java programming class this quarter and am having trouble getting the program to work the way the prof wants. I can do it the way i want to but then technically it would not be 100% right.
Anyway, for the assignment we need to create and interface file (just command line type output no fancy interface) and another file in this case call BigNatural. It is basically an implementation of BigInteger that we are doing ourselves. The problem I am having is getting the two java files to play nice together... i think.
Below is the code the way i want to do it with it being in just one file. This is only a rough so don't bash it to hard. The way he wants it done is in a file called BigNatural.java and one called TestDriver.java. When i do it that way i get error messages telling me that "The method increment() is undefined for the type String" for resource TestDriver.java.
When i separated the files i put BigNatural.java in a folder called myBigNatural and imported (in TestDriver.java) myBigNatural.BigNatural.
I am using eclipse 3.3 on 10.4.
Any help would be great!
Anyway, for the assignment we need to create and interface file (just command line type output no fancy interface) and another file in this case call BigNatural. It is basically an implementation of BigInteger that we are doing ourselves. The problem I am having is getting the two java files to play nice together... i think.
Below is the code the way i want to do it with it being in just one file. This is only a rough so don't bash it to hard. The way he wants it done is in a file called BigNatural.java and one called TestDriver.java. When i do it that way i get error messages telling me that "The method increment() is undefined for the type String" for resource TestDriver.java.
When i separated the files i put BigNatural.java in a folder called myBigNatural and imported (in TestDriver.java) myBigNatural.BigNatural.
Code:
import java.util.Scanner;
public class BigNatural
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String input;
boolean keepgoing = true;
while(keepgoing)
{
String naturalNumber = "0", number;
int count = 0;
System.out.print("Please enter your natural number: ");
naturalNumber = keyboard.nextLine();
naturalNumber = naturalNumber.replaceAll(",", "");
double conversionInt = Double.parseDouble(naturalNumber);
int intConversionInt = (int)conversionInt;
naturalNumber = Integer.toString(intConversionInt);
System.out.println(naturalNumber);
String[] naturalNumberAsArray = new String[naturalNumber.length()];
while(count < naturalNumberAsArray.length)
{
number = naturalNumber.substring(count, count+1);
naturalNumberAsArray[count] = number;
count++;
}
System.out.print("Would you like to increment your natural number(y/n): ");
input = keyboard.nextLine();
char ans = input.charAt(0);
if(ans == 'y')
{
System.out.println("IN INCREMENT");
naturalNumberAsArray = increment(naturalNumberAsArray);
}
System.out.print("Would you like to decrement your natural number(y/n): ");
input = keyboard.nextLine();
ans = input.charAt(0);
if(ans == 'y')
{
System.out.println("IN DECREMENT");
naturalNumberAsArray = decrement(naturalNumberAsArray);
}
System.out.print("Would you like to see your natural number(y/n): ");
input = keyboard.nextLine();
ans = input.charAt(0);
if(ans == 'y')
{
String myString = toString(naturalNumberAsArray);
System.out.println(myString);
}
System.out.print("Would you like to enter a new natural number(y/n): ");
input = keyboard.nextLine();
ans = input.charAt(0);
if(ans != 'y')
{
keepgoing = false;
}
}
}
private static String[] increment(String[] naturalNumberAsArray)
{
String numberToIncrement = naturalNumberAsArray[naturalNumberAsArray.length-1];
int myInt = Integer.parseInt(numberToIncrement);
if(myInt == 9)
{
naturalNumberAsArray = handlenine(naturalNumberAsArray);
}
else
{
myInt++;
numberToIncrement = Integer.toString(myInt);
naturalNumberAsArray[naturalNumberAsArray.length-1] = numberToIncrement;
}
return naturalNumberAsArray;
}
private static String[] decrement(String[] naturalNumberAsArray)
{
String numberToDecrement = naturalNumberAsArray[naturalNumberAsArray.length-1];
int myInt = Integer.parseInt(numberToDecrement);
if(myInt == 0 && naturalNumberAsArray.length == 1)
{
System.out.println("The natural number you entered was zero and a natural number cannot go below zero");
}
else if(myInt == 0)
{
naturalNumberAsArray = handlezero(naturalNumberAsArray, myInt);
}
else
{
myInt--;
numberToDecrement = Integer.toString(myInt);
naturalNumberAsArray[naturalNumberAsArray.length-1] = numberToDecrement;
}
return naturalNumberAsArray;
}
private static String toString(String[] naturalNumberAsArray)
{
String stringNaturalNumber = null;
for(int index = 0; index < naturalNumberAsArray.length; index++)
{
stringNaturalNumber = naturalNumberAsArray[index];
}
return stringNaturalNumber;
}
private static String[] handlenine(String[] naturalNumberAsArray)
{
int count = 1;
while(naturalNumberAsArray[naturalNumberAsArray.length-count] == "9")
{
naturalNumberAsArray[naturalNumberAsArray.length-count] = "0";
count++;
}
if(naturalNumberAsArray[naturalNumberAsArray.length-count] != "9")
{
String numberToIncrement = naturalNumberAsArray[naturalNumberAsArray.length-count];
int myInt = Integer.parseInt(numberToIncrement);
myInt++;
numberToIncrement = Integer.toString(myInt);
naturalNumberAsArray[naturalNumberAsArray.length-count] = numberToIncrement;
}
return naturalNumberAsArray;
}
private static String[] handlezero(String[] naturalNumberAsArray, int myInt)
{
int count = 1;
String numberToDecrement = "9";
naturalNumberAsArray[naturalNumberAsArray.length-count] = numberToDecrement;
count++;
if(naturalNumberAsArray[naturalNumberAsArray.length-count] == "9")
{
numberToDecrement = naturalNumberAsArray[naturalNumberAsArray.length-count];
myInt = Integer.parseInt(numberToDecrement);
numberToDecrement = Integer.toString(myInt);
naturalNumberAsArray[naturalNumberAsArray.length-count] = numberToDecrement;
}
else
{
numberToDecrement = naturalNumberAsArray[naturalNumberAsArray.length-count];
myInt = Integer.parseInt(numberToDecrement);
myInt--;
numberToDecrement = Integer.toString(myInt);
naturalNumberAsArray[naturalNumberAsArray.length-count] = numberToDecrement;
}
return naturalNumberAsArray;
}
}
I am using eclipse 3.3 on 10.4.
Any help would be great!