Todd Chambers
Todd Chambers

Reputation: 3

array of objects as they relate Interfaces and abstract classes

So I have been pulling my hair out trying to understand how to call methods on objects in an array. The gist of this homework is to create a comparable interface from scratch, an abstract class called "Weapon", an interface called "Drawable", a couple classes that extend Weapon, and a "Spaceship" class that creates a spaceship with an array of weapon objects.

The following is the code for the "Spaceship" class. The error "not a statement" occures at line 40. The intended purpose of the fireFastestWeapon method is to sort the object array based on each objects fire time, and then activate the fire method for the first n weapons.

    public class Spaceship implements Drawable
 {
    private Weapon [] mountedWeapons = new weapon[4];
    private int curWeapon = 0;
   public void draw()
   {
       System.out.print("Ship will be drawn here");
    }
   public void addWeapon(Weapon w)
   {
       if (curWeapon<mountedWeapons.length)
       {
           mountedWeapons [curWeapon] = w;
           curWeapon++;
        }
        else System.out.print("The weapons bay is full Commander");
    }
   public void fireFastestWeapon(int n)
   {
       int count=mountedWeapons.length;
       int k;
        for (int m = count; m>=0;m--)
        {
            for(int i = 0; i<count-1;i++)
            {
                k=i+1;
                if (mountedWeapons[i].compareTo(mountedWeapons[k]) == 1){
                    Weapon temp;
                    temp = mountedWeapons[i];
                    mountedWeapons[i] = mountedWeapons[k];
                    mountedWeapons[k] = temp;
                }
            }
       }

       if(n>mountedWeapons.length)
       {
           n=mountedWeapons.length;
        }
       for (f=0;f<n-1;n++)
       {
           mountedWeapon[f].fire;
       }
   }
}

Thank you for looking!

Upvotes: 0

Views: 53

Answers (1)

Mark Peters
Mark Peters

Reputation: 81124

The parentheses on a method call are not optional in Java:

mountedWeapon[f].fire();

Upvotes: 1

Related Questions