Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
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.

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!
 

MacNeXT

macrumors 6502
Jun 21, 2004
258
0
The error message indicates that you are calling the method increment() on a String. You should show us the TestDriver.java code.
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
essentially the calls I am supposed to make are something to the effect of:

This is an example test file he gave us to go from. He said this one would work but not get full credit as we are supposed to have an "interface" to ours.

Code:
public class TestBigNaturalSimple {

    public static void main (String[] args) {
	BigNatural b1 = new BigNatural();    //default constructor
	BigNatural b2 = new BigNatural(23);  //one-argument constructor
	BigNatural b3 = new BigNatural(b2);  //copy constructor
	
	b2.increment();
	b3.decrement();

	System.out.println(b1.toString());  //should print out O
    }
}
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
essentially the calls I am supposed to make are something to the effect of:

This is an example test file he gave us to go from. He said this one would work but not get full credit as we are supposed to have an "interface" to ours.

Code:
public class TestBigNaturalSimple {

    public static void main (String[] args) {
	BigNatural b1 = new BigNatural();    //default constructor
	BigNatural b2 = new BigNatural(23);  //one-argument constructor
	BigNatural b3 = new BigNatural(b2);  //copy constructor
	
	b2.increment();
	b3.decrement();

	System.out.println(b1.toString());  //should print out O
    }
}

I think it would be easier to understand if you posted both of your classes if they were separate....unless you already did and are trying to use them in one project in which case.....

BigNatural class doesn't have a constructor in your first post, it is just a bunch of static methods. So it couldn't work like your professor models in that sample code. Also, generally the toString() method is not static or have any parameters. Every class inherits that method from Object and when you implement yours you just override the existing method. Though technically I think you can do what you have done there and it would compile it is just not "proper". Also your increment() method takes parameters and in the sampel driver program it doesn't. You will need to have a method without parameters. Which would make more sense anyway. Your constructor is where you initalize the number, then your increment() / decrement() methods just do +1 and -1, no need for parameters.


Also, in java "interface" is a keyword. It is for a special type of class that just describes methods. When you create a new class that "implements" (another keyword) that interface it must have those methods.

For example:

Code:
public interface Vehicle {
 public int numberOfWheels();
 public String color();
}

public class RedMoto implements Vehicle {
 public int numberOfWheels() { return 2; }
 public String color() { return "red"; }
}

public class BlueCar implements Vehicle {
 public int numberOfWheels() { return 4; }
 public String color() { return "blue"; }
 public int numberOfDoors() { return 4; }
}

It's a stupid example but it shows two classes (RedMoto and BlueCar) that implement an interface (Vehicle). The interface only says what methods are needed but does not implement them, that is what the class does. Further more the class BlueCar has an extra method that is not part of the interface (just showing that you can do this and you are not limited to what is defined in the interface).

I hope this extra stuff did not confuse you but since you mentioned twice "interface" I suspect that maybe your professor wants you to use a similar method but I am not sure about that.
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
Thanks for the input so far

OK, i will read back through the above post again but in the mean time, this is a completed (in the sense that this is how it is supposed to work) project. This is the way I want to do it but the teacher wants it differently.

Anyway, below is the code:

Code:
import java.util.Scanner;

public class TestDriver 
{
	public static void main(String[] args) 
	{
		char keepgoing = 'y', answer;
		Scanner keyboard = new Scanner(System.in);
		String selection, naturalNumber=null;
		String[] nNAA = new String[0];
		
		while(keepgoing != 'q')
		{
			System.out.println("Please select from the following options: ");
			System.out.println("                enter natural number [e]: ");
			System.out.println("            increment natural number [+]: ");
			System.out.println("            decrement natural number [-]: ");
			System.out.println("    convert natural number to string [s]: ");
			System.out.print("                                quit [q]: ");
			
		selection = keyboard.nextLine();	
		answer = selection.charAt(0);
		
		System.out.println();
		
		if(answer == 'e')
		{
			System.out.print("Please enter your natural number: ");
			
			naturalNumber = keyboard.nextLine();
			System.out.println();
			naturalNumber = naturalNumber.replaceAll(",", "");
			nNAA = nnta(naturalNumber);
		}
		else if(answer == '+')
		{
			nNAA = increment(nNAA);
		}
		else if(answer == '-')
		{
			nNAA = decrement(nNAA);
		}
		else if(answer == 's')
		{
			naturalNumber = toString(nNAA);
			naturalNumber =  removeLeadingZeros(naturalNumber);
			
			System.out.println("Your natural number is: " + naturalNumber + "\n");
		}
		else
		{
			keepgoing = 'q';
			
			System.out.println("Quitting Program");
		}
		}
	}
	
	public static String[] nnta(String naturalNumber)
	{
		String[] nNAA = new String[naturalNumber.length()];
		String number = "";
		int count = 0;
		
		while(count < naturalNumber.length() && number != ".")
		{
			number = naturalNumber.substring(count, count+1);
			nNAA[count] = number;
			
			count++;
		}
		
		return nNAA; 
	}
	
	private static String[] increment(String[] nNAA)
	{
		String numberToIncrement = nNAA[nNAA.length-1];
		int  myInt = Integer.parseInt(numberToIncrement);
		
		if(myInt == 9)
		{
			nNAA = handlenine(nNAA, myInt);
		}
		else
		{
			myInt++;
			numberToIncrement = Integer.toString(myInt);
			nNAA[nNAA.length-1] = numberToIncrement;
		}
		
		return nNAA;
	}
	
	private static String[] decrement(String[] nNAA)
	{
		String numberToDecrement = nNAA[nNAA.length-1];	
		int  myInt = Integer.parseInt(numberToDecrement);
		
		if(myInt == 0 && nNAA.length == 1)
		{
			System.out.println("The natural number you entered was zero and a natural number cannot go below zero\n");
		}
		else if(myInt == 0)
		{
			nNAA = handlezero(nNAA, myInt);
		}
		else
		{
			myInt--;
			numberToDecrement = Integer.toString(myInt);
			nNAA[nNAA.length-1] = numberToDecrement;
		}
		
		return nNAA;
	}
	
	private static String toString(String[] nNAA)
	{
		String stringNaturalNumber = "";
		
		for(int index = 0; index < nNAA.length; index++)
		{
			stringNaturalNumber = stringNaturalNumber + (nNAA[index]);
		}
		
		return stringNaturalNumber;
	}
	
	private static String[] handlenine(String[] nNAA, int myInt)
	{
		int count = 1;
		String numberToIncrement = nNAA[nNAA.length-count];
		
		while(myInt == 9)
		{
			nNAA[nNAA.length-count] = "0";
			count++;
			numberToIncrement = nNAA[nNAA.length-count];
			myInt = Integer.parseInt(numberToIncrement);
		}

		if(nNAA[nNAA.length-count] != "9")
		{
			numberToIncrement = nNAA[nNAA.length-count];
			myInt = Integer.parseInt(numberToIncrement);
			myInt++;
			numberToIncrement = Integer.toString(myInt);
			nNAA[nNAA.length-count] = numberToIncrement;
		}
		
		return nNAA;
	}
	
	private static String[] handlezero(String[] nNAA, int myInt)
	{
		int count = 1;
		String numberToDecrement = nNAA[nNAA.length-count];
		
		while(myInt == 0)
		{
			nNAA[nNAA.length-count] = "9";
			count++;
			numberToDecrement = nNAA[nNAA.length-count];
			myInt = Integer.parseInt(numberToDecrement);
		}

		if(nNAA[nNAA.length-count] != "0")
		{
			numberToDecrement = nNAA[nNAA.length-count];
			myInt = Integer.parseInt(numberToDecrement);
			myInt--;
			numberToDecrement = Integer.toString(myInt);
			nNAA[nNAA.length-count] = numberToDecrement;
		}
		
		return nNAA;
	}
	
	public static String removeLeadingZeros(String str)
	{
		if (str == null)
		{
			return null;
		}
		char[] chars = str.toCharArray();
		int index = 0;
		
		for (; index < str.length();index++)
		{
			if (chars[index] != '0')
			{
				break;
			}
		}
		return (index == 0) ? str :
			str.substring(index);
	}
}

I need to create a TestDriver.java that will handle the "public static void main(String[] args)" part and then the BigNatural.java part to handle all the rest. Right now everything is in one file as you can see and it just needs to be separated and chaged a little to make it work.

Thanks
 

macsmurf

macrumors 65816
Aug 3, 2007
1,200
948
I need to create a TestDriver.java that will handle the "public static void main(String[] args)" part and then the BigNatural.java part to handle all the rest. Right now everything is in one file as you can see and it just needs to be separated and chaged a little to make it work.

Thanks

It's difficult to know exactly what your professor means by "interface". I'm going to assume he means command line interface and not java interfaces, which is an altogether different kind of animal. I'm also going to assume that the course is some kind of introductory course in object oriented programming.

You need to separate your testing code and your "production" code (the BigNatural class). This is where OOP comes in. The below is a very simple implementation of the BigNatural class, but of course I'm cheating and using the BigInteger class internally.

Code:
import java.math.BigInteger;

public class BigNatural {

    private BigInteger value;

    public BigNatural() {
        value = BigInteger.ZERO;
    }

    public BigNatural(long value) {
        this.value = BigInteger.valueOf(value);
    }

    public BigNatural(BigNatural b2) {
        value = b2.value;
    }

    public void increment() {
        value = value.add(BigInteger.ONE);
    }

    public void decrement() {
        value = value.subtract(BigInteger.ONE);
    }

    public String toString() {
        return value.toString();
    }
}

This class basically wraps BigInteger so what you get is something that works like BigInteger, but with fewer features. What you probably need to do is to rewrite the methods with another internal representation instead of BigInteger (a String would be the obvious choice). Also, you need to handle numbers less than zero. Natural numbers can't be negative (strictly speaking, they can't be zero either, but that is a matter of religion), so the nice way of handling this would be to use exceptions, but I don't know if you've learned that yet. If not, it would seem reasonable to put a check in one of the constructors and in the decrement() method and just use zero as the value for invalid construction or invalid decrementation (is that a word?).

One more thing. I think you might be missing what your professor is trying to teach you. He is not trying to teach you how to make a program that can increment and decrement large numbers. He is trying to get you used to the object oriented paradigm, which can be difficult if you have only been subjected to non object-oriented languages (which I suspect you have - I'm guessing C). That is not a minor technical point. No offence :)
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
Your professor is right. You should remove all those private methods that manipulate the data, make them public, put them in a different file and just do method calls to them. I just did some splitting of your code and I already cut it by about 25% between the way you did your curly braces and wasting lines calling for more input from the keyboard.

For example, why does the user have to say N three times just to get back to the main loop to input another natural number? He/she just wants to get back to the start!
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
It's difficult to know exactly what your professor means by "interface". I'm going to assume he means command line interface and not java interfaces, which is an altogether different kind of animal. I'm also going to assume that the course is some kind of introductory course in object oriented programming.

You need to separate your testing code and your "production" code (the BigNatural class). This is where OOP comes in. The below is a very simple implementation of the BigNatural class, but of course I'm cheating and using the BigInteger class internally.

Code:
import java.math.BigInteger;

public class BigNatural {

    private BigInteger value;

    public BigNatural() {
        value = BigInteger.ZERO;
    }

    public BigNatural(long value) {
        this.value = BigInteger.valueOf(value);
    }

    public BigNatural(BigNatural b2) {
        value = b2.value;
    }

    public void increment() {
        value = value.add(BigInteger.ONE);
    }

    public void decrement() {
        value = value.subtract(BigInteger.ONE);
    }

    public String toString() {
        return value.toString();
    }
}

This class basically wraps BigInteger so what you get is something that works like BigInteger, but with fewer features. What you probably need to do is to rewrite the methods with another internal representation instead of BigInteger (a String would be the obvious choice). Also, you need to handle numbers less than zero. Natural numbers can't be negative (strictly speaking, they can't be zero either, but that is a matter of religion), so the nice way of handling this would be to use exceptions, but I don't know if you've learned that yet. If not, it would seem reasonable to put a check in one of the constructors and in the decrement() method and just use zero as the value for invalid construction or invalid decrementation (is that a word?).

One more thing. I think you might be missing what your professor is trying to teach you. He is not trying to teach you how to make a program that can increment and decrement large numbers. He is trying to get you used to the object oriented paradigm, which can be difficult if you have only been subjected to non object-oriented languages (which I suspect you have - I'm guessing C). That is not a minor technical point. No offence :)

You are correct. I have done some java programming and a language called RESOLVE (which is based of C++). The problem with those two classes was that we never really had to "split" things up. Even though they are OOP we were never really forced to separate our stuff.

Anyway, i sat down with the teacher today and he explained the stuff a little better and i think i have got the hang of it now.

Thanks for the input form everyone.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.