Search code examples
javacompiler-errorsheappriority-queuecomparable

Priority Queue implemented Heap


Hi so i am suppose to be making a priority queue implemented Heap in descending order, largest to small, I want to this with exponents. So i fnished my heap and priority queue classes, but now when making my class I get an error saying

xponents.java:66: error: invalid method declaration; return type required
public Exponent(int num, int exponent, int x)

I'm not sure where im going wrong

  public class Exponents implements Comparable<Exponents>
{
   private int num;
   private int exponent;
   private int x;

   public Exponents(int num, int exponent, int x)
   {
      this.num = num;
      this.exponent = exponent;
      this.x = x;
   }
   public int Answer()
   {
      int x = Math.pow(num,exponent);
      return x;
   }

    public String toString()
   {
      String s = ("Exponent: " + exponent + "\n Number: "+ num + "\nYour x is: )" + x);
      return s;
   }
   public int compareTo(Answer e)
   {
      return this.x - o.x;
   }
}

Problem was fixed because of a typo and now im getting more errors, im not really sure what i wrote wrong:

Exponents.java:83: error: cannot find symbol
   public int compareTo(Answer e)
                    ^
  symbol:   class Answer
  location: class Exponents



Exponents.java:74: error: incompatible types: possible lossy conversion from double to int
      int x = Math.pow(num,exponent);


                  ^
Exponents.java:85: error: cannot find symbol
      return this.x - o.x;
                  ^
  symbol:   variable o
  location: class Exponents
3 errors

Solution

  • There's a typo in your constructor. Your class is named Exponents but in your constructor, it says Exponent. It becomes an invalid method declaration because it is looking for a return data type since the name of the 'constructor' you created is not the same as the name of the class.

    Edit:

    1. You do not have a class name Answer.
    2. You cannot implicitly cast double to int. You can use Math.pow(num,exponent).intValue()
    3. You do not have a variable named o within the scope of the method or the class.