-2

Here is my RandomSequence class code: import java.util.*;

public class RandomSequence implements Sequence
{
   private ArrayList<Double> random;

   public RandomSequence()
   {
      random = new ArrayList<Double>(); 
   }

   public List<Double> firstTenTerms()
   {
      if(random.size()!=0)
      {
         while(random.size()>0)
         {
            random.remove(random.size()-1);
         }
      }

      for(int k=0; k<10; k++)
      {
         random.add((int)(Math.random()*10)+1.0);
      }
      return random;
   }
}

And this is my driver class: import java.io.; import java.util.;

public class SequenceMain
{
   public static void main(String[] args) throws Exception
   {
      File f = new File("sequence1.txt");
      Scanner scan = new Scanner(f);
      System.out.println("I'm going to generate a random \"sequence\" and sort it. Here're the first 10 terms of sequence.");
      System.out.println(RandomSequence.firstTenTerms());
    }
    public void ascendingSort(List<Double> alist)
    {
      Collections.sort(alist);
    }

}

When I run the driver class, I keep on getting these error messages:

SequenceMain.java:13: error: non-static method firstTenTerms() cannot be referenced from a static context
      System.out.println(RandomSequence.firstTenTerms());
SequenceMain.java:18: error: non-static method ascendingSort(List<Double>) cannot be referenced from a static context
  ascendingSort(alist);

There already seems to be quite a few questions related to the non-static methods and static context etc, and I've gone through some of those posts, but frankly, I'm now more confused than before.

Update: Thank you for those who gave me feedback on line 13, but how can I fix 18? I feel that it's a bit different.

space
  • 129
  • 5
  • 2
    `new RandomSequence().firstTenTerms()` – luk2302 Jun 03 '19 at 13:10
  • You need to create an Object of `RandomSequence` using `new` then you can call `.firstTenTerms()` using that. `static` basically means you do not need to create an object from the class to use the method. – Nexevis Jun 03 '19 at 13:11
  • Hint: almost any "newbie" problem you can dream of asking about ... has been asked before. So please do some research prior posting a question. Usually writing up a new question takes you more time than it would take you to find an existing solution to the problem on the internet. Simply put the error message into google, and there you go. – GhostCat Jun 03 '19 at 13:21

1 Answers1

0

You declared firstTenTerms() method as an instance method so you must create RandomSequence instance like this:

new RandomSequence().firstTenTerms();
marchew
  • 21
  • 4