JAVA multiple class file

I just started learning JAVA a couple of days ago and the first program I wrote had two classes in one file. here is the program :

class fib_num {

public int value;

public boolean is_even;

}

class Fibonacci {

/** Print the Fibonacci sequence for values < MAX and mark even numbers with an asterick */

private static final int MAX = 50;

private static final String Title = "The Fibonacci sequence for values less than " + MAX + ":";

private static fib_num[] fib = new fib_num[MAX];//This is actually an array of object

//references to objects of the fib_num class

public static void main(String[] args) {

System.out.println(Title);

//We must initialize each element of the array also !!!!

for (int i = 0; i < fib.length; i += 1) {

fib = new fib_num();

}

int lo = 1, hi = 1;

fib[0].value = lo;

fib[0].is_even = false;

fib[1].value = hi;

fib[1].is_even = false;

for (int i = 2; i < fib.length; i += 1) {

//create the next Fibonacci number and then save the previous Fibonacci number

hi = lo + hi;

lo = hi - lo;

fib.value = hi;

//now indicate if the Fibonacci number is even/odd

if (fib.value % 2 == 0) {

fib.is_even = true;

}else {

fib.is_even = false;

}

}

print (fib);

}

//This method prints an array of Fibonacci numbers

public static void print(fib_num[] array) {

if (array == null || array.length == 0)

throw new IllegalArgumentException();

String mark;

for (int i = 0; array.value < MAX; i += 1) {

if (array.is_even) {

mark = "*";

}else {

mark = "";

}

System.out.println((i + 1) + ": " + array.value + mark);

}

}

}

I ran the program and everything went fine. But today I started to write another program with two classes. However the file will not compile and I get an error about interfacing or something. here is the program:

Note: it's not nearly complete.

class enumerate {

//print out all permutations of a list of integers

public static final int MAX = 4;

public static int[] initialize(int[] nums) {

for (int i = 0; i < nums.length; i++) {

nums = i + 1;

}

return nums;

}

public static void print(int[] nums) {

for (int i = 0; i < nums.length; i++) {

System.out.print(nums);

}

System.out.println("");

}

public static void swap (int[] nums, int i, int j) {

int temp = nums;

nums = nums[j];

nums[j] = temp;

}

public static void main (String[] args) {

int[] list = new int[MAX];

list = initialize(list);

PermutationGenerator x = new PermutationGenerator(5);

}

}

//--

// Systematically generate permutations.

//--

import java.math.BigInteger;

public class PermutationGenerator {

private int[] a;

private BigInteger numLeft;

private BigInteger total;

//--

// Constructor. WARNING: Don't make n too large.

// Recall that the number of permutations is n!

// which can be very large, even when n is as small as 20 --

// 20! = 2,432,902,008,176,640,000 and

// 21! is too big to fit into a Java long, which is

// why we use BigInteger instead.

//-

public PermutationGenerator (int n) {

if (n < 1) {

throw new IllegalArgumentException ("Min 1");

}

a = new int[n];

total = getFactorial (n);

reset ();

}

//

// Reset

//

public void reset () {

for (int i = 0; i < a.length; i++) {

a = i;

}

numLeft = new BigInteger (total.toString ());

}

//

// Return number of permutations not yet generated

//

public BigInteger getNumLeft () {

return numLeft;

}

//

// Return total number of permutations

//

public BigInteger getTotal () {

return total;

}

//--

// Are there more permutations?

//--

public boolean hasMore () {

return numLeft.compareTo (BigInteger.ZERO) == 1;

}

//

// Compute factorial

//

private static BigInteger getFactorial (int n) {

BigInteger fact = BigInteger.ONE;

for (int i = n; i > 1; i--) {

fact = fact.multiply (new BigInteger (Integer.toString (i)));

}

return fact;

}

//--

// Generate next permutation (algorithm from Rosen p. 284)

//--

public int[] getNext () {

if (numLeft.equals (total)) {

numLeft = numLeft.subtract (BigInteger.ONE);

return a;

}

int temp;

// Find largest index j with a[j] < a[j+1]

int j = a.length - 2;

while (a[j] > a[j+1]) {

j--;

}

// Find index k such that a[k] is smallest integer

// greater than a[j] to the right of a[j]

int k = a.length - 1;

while (a[j] > a[k]) {

k--;

}

// Interchange a[j] and a[k]

temp = a[k];

a[k] = a[j];

a[j] = temp;

// Put tail end of permutation after jth position in increasing order

int r = a.length - 1;

int s = j + 1;

while (r > s) {

temp = a[s];

a[s] = a[r];

a[r] = temp;

r--;

s++;

}

numLeft = numLeft.subtract (BigInteger.ONE);

return a;

}

}

I thought the error had somethin to do with only having one class per .java file since the compiler creates a .class file. But how come my first program had two classes and it was OK. Is it b/c the second class was merely a collection of fields, almost like a simple struct in C?

Any help would be appreciated. Thanks

[5856 byte] By [whasian85a] at [2007-9-29 1:50:54]
# 1

Hello whasian85.

You can have as many classes in a single file as you want as long as you take care of some little things:

- There may be only one public class per file

- The file must be named the same as the public class (plus .java)

Btw I don't believe, that any of your code compiled without problems, since you keep forgetting the indice of arrays like here:

//We must initialize each element of the array also !!!!

for (int i = 0; i < fib.length; i += 1) {

fib = new fib_num();

}

which has to be

//We must initialize each element of the array also !!!!

for (int i = 0; i < fib.length; i += 1) {

fib [i] = new fib_num();

}

Got that [ i ] in the loop?

There's plenty of errors like this in both your first program and your second one.

Anyways: Your first program may be in a file with any name, since there's no public class.

Your second program wants to be in a file named PermutationGenerator.java since the public class' name is PermutationGenerator.

Since you are only starting it may be the easiest, if you just skip the public keyword of the PermutationGenerator class. Later on you'll learn whether to call a class public or not.

tom

tomvollerthuna at 2007-7-13 18:03:57 > top of Java-index,Other Topics,Java Community Process (JCP) Program...