IanG
IanG

Reputation: 1

Array List won't use the methods I've created

I'm supposed to create methods a-j, and then use them on an array list. I've created all them methods, and they should be working correctly, but whenever I try too use them, it gives me a cannot find symbol. I'm sure it's something obvious, but I'm just not seeing it.

Any help would be greatly appreciated, thanks

Here's the code:

//Ian G
//AList-Asg2: ArrayLists Bk Program

import java.util.ArrayList;

class Bell
{
   private int studentId;

   public Bell( int id )
   {studentId = id;}

   public int id()
   {return studentId;}

   public void setId(int newId)
   {studentId = newId;}

   public int compareTo( Bell otherBell )
   {return this.studentId - otherBell.studentId;}

   public String toString()
   { return ""+studentId;}
}


class BellTester_Granger
{

   public ArrayList<Bell> rayList;

   public static void main(String[] args)
   {

      ArrayList<Bell> rayList = new ArrayList<Bell>();
      Bell student1 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student2 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student3 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student4 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student5 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student6 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student7 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student8 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student9 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student10 = new Bell((int)(Math.random() * 1000) + 216000);
      rayList.add(student1);
      rayList.add(student2);
      rayList.add(student3);
      rayList.add(student4);
      rayList.add(student5);
      rayList.add(student6);
      rayList.add(student7);
      rayList.add(student8);
      rayList.add(student9);
      rayList.add(student10);


      /**
      Here i'm trying to use one of the method's that i created, but it's giving me the error
      */

      rayList.swapFL();   

   }

   //a 
   public void swapFL()
   {   
      int f= (rayList.get(0)).id();
      int l= (rayList.get(rayList.size()-1)).id();
      (rayList.get(0)).setId(l);
      (rayList.get(rayList.size()-1)).setId(l);
   }

   //b
   public void shiftR()
   {   
      rayList.add(0, rayList.get(rayList.size()-1));
      rayList.remove(rayList.size()-1);
   }

   //c
   public void replaceEven()
   {   
      for( Bell b: rayList)
      {
         if (b.id()%2 == 0)
         {
            b.setId(216222);
         }
      }
   }

   //d
   public void setToLarger()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if((rayList.get(c-1)).id() > rayList.get(c+1).id())
         {
            (rayList.get(c)).setId((rayList.get(c-1)).id());
         }
         else
         {
            (rayList.get(c)).setId((rayList.get(c+1)).id());
         }
      }
   }

   //e
   public void removeMiddle()
   {
      rayList.remove(5);
      rayList.remove(6);
   }

   //f
   public void evenToFront()
   {   
      for(int c=0; c<rayList.size(); c++)
      {
         if (rayList.get(c).id()%2 == 0)
         {
            rayList.set(0,rayList.get(c));
            rayList.remove(c);
         }
      }
   }

   //g
   public void secondLargest()
   {   
      int largest = 0;
      int secondLargest = -1;
      for( Bell b: rayList)
      {
         if (b.id() > largest)
         {
            largest = b.id();
         }
      }

      for( Bell b: rayList)
      {
         if (b.id() > secondLargest && b.id() < largest)
         {
            secondLargest = b.id();
         }
      }            
   }

   //h
   public boolean increasingOrder()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if (!(rayList.get(c).id()>rayList.get(c-1).id() && rayList.get(c).id()>rayList.get(c+1).id()))
         {
            return false;
         }
      }
      return true;
   }

   //i
   public boolean adjacentDupiclates()
   {   
      for(int c=0; c<rayList.size()-1; c++)
      {
         if (rayList.get(c).id()==rayList.get(c+1).id())
         {
            return true;
         }
      }
      return false;
   }

   //j
   public boolean duplicates()
   {
      int c=0;
      while(c< rayList.size())
      {
         for( Bell b: rayList)
         {
            if (b.id() == rayList.get(c).id())
            {
               return true;
            }
         }
         c++;
      }
      return false;
   }
}

Upvotes: 0

Views: 131

Answers (4)

JLRishe
JLRishe

Reputation: 101778

Defining methods in your BellTest_Granger class doesn't add them to ArrayTest objects. You should either make a class that inherits from ArrayList and add the methods to that class, or define your methods as static and pass the ArrayList to them as arguments:

public static void swapFL(ArrayList<Bell> list)
{   
   int f= (list.get(0)).id();
   int l= (list.get(list.size()-1)).id();
   (list.get(0)).setId(l);
   (list.get(rayList.size()-1)).setId(l);
}

then you can use

swapFL(rayList);

However, in either approach, it's generally better to use generics and not have functionality that's hardcoded to only work with ArrayList<Bell> objects:

public static <T> void shiftR(ArrayList<T> list)
{   
   list.add(0, list.get(list.size()-1));
   list.remove(list.size()-1);
} 

Again, you can call this with:

shiftR(rayList);

Upvotes: 0

michaeltang
michaeltang

Reputation: 2898

there is more than one problem in your code , the function swapFL is not in the right way,I update your code , and test it with simple case for SwapFL,it works,hope it will help,

import java.util.ArrayList;

class Bell
{
   private int studentId;

   public Bell( int id )
   {studentId = id;}

   public int id()
   {return studentId;}

   public void setId(int newId)
   {studentId = newId;}

   public int compareTo( Bell otherBell )
   {return this.studentId - otherBell.studentId;}

   public String toString()
   { return ""+studentId;}
}


class BellTester_Granger 
{

   public static ArrayList<Bell> rayList;

   public static void main(String[] args)
   {

      rayList = new ArrayList<Bell>();
      Bell student1 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student2 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student3 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student4 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student5 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student6 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student7 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student8 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student9 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student10 = new Bell((int)(Math.random() * 1000) + 216000);
      rayList.add(student1);
      rayList.add(student2);
      rayList.add(student3);
      rayList.add(student4);
      rayList.add(student5);
      rayList.add(student6);
      rayList.add(student7);
      rayList.add(student8);
      rayList.add(student9);
      rayList.add(student10);


      /**
      Here i'm trying to use one of the method's that i created, but it's giving me the error
      */
      for(int i =0; i< rayList.size(); i++)
      {
          System.out.println(rayList.get(i));
      }
      swapFL();   
      System.out.println("after swapFL");
      for(int i =0; i< rayList.size(); i++)
      {
          System.out.println(rayList.get(i));
      }
   }

   //a 
   public static void swapFL()
   {   
      int f= (rayList.get(0)).id();
      int l= (rayList.get(rayList.size()-1)).id();
      (rayList.get(0)).setId(l);
      (rayList.get(rayList.size()-1)).setId(f);
   }

   //b
   public static void shiftR()
   {   
      rayList.add(0, rayList.get(rayList.size()-1));
      rayList.remove(rayList.size()-1);
   }

   //c
   public static void replaceEven()
   {   
      for( Bell b: rayList)
      {
         if (b.id()%2 == 0)
         {
            b.setId(216222);
         }
      }
   }

   //d
   public static void setToLarger()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if((rayList.get(c-1)).id() > rayList.get(c+1).id())
         {
            (rayList.get(c)).setId((rayList.get(c-1)).id());
         }
         else
         {
            (rayList.get(c)).setId((rayList.get(c+1)).id());
         }
      }
   }

   //e
   public static void removeMiddle()
   {
      rayList.remove(5);
      rayList.remove(6);
   }

   //f
   public static void evenToFront()
   {   
      for(int c=0; c<rayList.size(); c++)
      {
         if (rayList.get(c).id()%2 == 0)
         {
            rayList.set(0,rayList.get(c));
            rayList.remove(c);
         }
      }
   }

   //g
   public static void secondLargest()
   {   
      int largest = 0;
      int secondLargest = -1;
      for( Bell b: rayList)
      {
         if (b.id() > largest)
         {
            largest = b.id();
         }
      }

      for( Bell b: rayList)
      {
         if (b.id() > secondLargest && b.id() < largest)
         {
            secondLargest = b.id();
         }
      }            
   }

   //h
   public static boolean increasingOrder()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if (!(rayList.get(c).id()>rayList.get(c-1).id() && rayList.get(c).id()>rayList.get(c+1).id()))
         {
            return false;
         }
      }
      return true;
   }

   //i
   public static boolean adjacentDupiclates()
   {   
      for(int c=0; c<rayList.size()-1; c++)
      {
         if (rayList.get(c).id()==rayList.get(c+1).id())
         {
            return true;
         }
      }
      return false;
   }

   //j
   public static boolean duplicates()
   {
      int c=0;
      while(c< rayList.size())
      {
         for( Bell b: rayList)
         {
            if (b.id() == rayList.get(c).id())
            {
               return true;
            }
         }
         c++;
      }
      return false;
   }
}

Upvotes: 0

Reimeus
Reimeus

Reputation: 159874

swapFL is an instance method of BellTester_Granger rather than the built-in class java.util.ArrayList

BellTester_Granger granger = new BellTester_Granger();
// rayList.swapFL();  oh oh
granger.swapFL();

Side note: Follow Java naming conventions when naming classes, e.g BellTesterGranger

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68725

Two problems which i saw when i glanced your code.

First is that you are shadowing your class lever rayList with the rayList you are defining in your main method. Just remove the re-definition

 public static void main(String[] args)
 {

      ArrayList<Bell> rayList = new ArrayList<Bell>();  
      // rest of your main method

  }

Just intialized instead of redefining:

    rayList = new ArrayList<Bell>(); 

Second problem is that you don't need to call the swapFl method using rayList because ArrayList class does not have any such method. Its your BellTester_Granger class that has this method, so call it using its isntance as mentioned here:

BellTester_Granger grangerInstance = new BellTester_Granger();
grangerInstance .swapFL();

Upvotes: 0

Related Questions