Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Binary algorithm assignment.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Binary algorithm assignment.

    Hello Community,

    I'm working on an assignment for my data structure & algorithm class. My whole summer of not programming in java made me a extremely rusty. I need help creating an array of objects via a class I created. Never mind what I comment out I will work on that later. Here is my code below.

    /**
     * 
     *  
     *  CSC 385 
     *  Module 5
     *  
     */
     
     
    import java.util.*;
     
    public class Module5
    {
     
       public static void main(String[] args)
       {
     
         Scanner scan = new Scanner(System.in);      
         Comparable test = new Comparable(10);  
     
     
         for(int i=0 ; i < 10; i++)
         {
            // test[i] = new Comparable();
             System.out.print(" Input value:" + i + " " );
             //test[i] = scan.nextInt();
     
         }
     
     
       }
     
     
      /** public int binarySearch(Comparable[] objArray, Comparable searchObj)
       {
     
          int low = 0;
          int high = objArray.length - 1;
          int mid = 0;  
          int temp = 0;
     
     
          while (low <= high){
     
           mid = (low + high) / 2;
     
           if (arrayIndex.compareTo(searchObj) < 0)
           {
            low = mid + 1;
            temp = low;
           }
           else if (objArray[mid].compareTo(searchObj) > 0)
           {   
            high = mid - 1;
            temp = high;
           }
           else
           {
             return mid;
           }
     
       }
         return temp; 
     
          if( temp < 0)
          {
           temp+=1;
          }
     
     
       } */
     
     
     
     
    }



     
    public class Comparable
    {
     
        Object[] objArray; 
     
     
        public Comparable( Object objArray, int x)  
         {
     
            objArray = this.objArray;
            objArray = new Comparable[x];
     
        }
     
        public Comparable(Object[] o)
        {
            objArray = o;
     
        }
     
        public Comparable(int x)
        {
            //objArray = this.objArray;
            objArray = new Comparable[x];
     
        }
     
        public void setArray(int x){
            objArray = new Comparable[x];
        }
     
    }


    For some reason when I try to create an object from my class and start to add int values to that array object I receive this error message.

    ********************************
    Array required, but Comparable found

    You are using syntax here that suggestions that you are trying access to as array element. The variable you trying is not an array, though.
    ********************************

    It seems my syntax is wrong of creating this array object. I have the right idea of want to store objects in the array, but I don't know what I'm doing wrong. Any suggestions as well as documentation I can review?

    Much Appreciated!
    Rain_Maker


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    Please copy the full text of the error message that shows the source statement causing the problem and paste it here.

    One confusing bit in the code is its using the name of a Java SE class: Comparable. It will be less confusing if you use your own unique name.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    errormessage.JPG


    I'm using BlueJ as my IDE. So I hope this error message is able for you to answer my question.

  4. #4
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.


  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    Please copy the text and paste it here, no images. Text for responses can't be copied from images.

    Be sure to rename your class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    Quote Originally Posted by Norm View Post
    Please copy the text and paste it here, no images. Text for responses can't be copied from images.

    Be sure to rename your class.
    Norm,

    BlueJ will not allow me to copy the error message as text. I can only write out the error message or provide a screen shot. Is there anyway else to help. FYI I was able to rename my class FYI.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    I don't know BlueJ. I use the javac command for compiling my code. It has good error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    Let me try that....one moment please

    --- Update ---

    I just rqn my code via the command prompt.(Learned something new )


    C:\Users\......>javac Module5
    error: Class names, 'Module5', are only accepted if annotation processing is ex
    licitly requested
    1 error


    Weird error message. I don't know what this mean?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    The javac command takes the filename (ie with the .java extension)
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    Microsoft Windows [Version 6.0.6001]
    Copyright (c) 2006 Microsoft Corporation. All rights reserved.

    C:\Users\.....>cd Documents

    C:\Users\......\Documents>dir
    Volume in drive C has no label.
    Volume Serial Number is 367B-6966

    Directory of C:\Users\......\Documents

    09/27/2014 07:15 PM <DIR> .
    09/27/2014 07:15 PM <DIR> ..
    04/25/2014 06:00 AM 10,521 1.docx
    09/04/2014 08:39 PM <DIR> Cisco Fall Semster
    09/01/2014 03:13 PM 139,798 CompTIA_A_220-801_Objectives.pdf
    09/01/2014 03:13 PM 140,209 CompTIA_A_220-802_Objectives.pdf
    09/02/2014 07:48 PM 7,852 Counter (1).html
    09/02/2014 07:47 PM 2,574 Counter (1).java
    09/02/2014 06:59 PM 7,852 Counter.html
    09/02/2014 06:59 PM 2,574 Counter.java
    09/02/2014 07:47 PM 315 CounterInitializationException (1).java
    09/02/2014 06:59 PM 315 CounterInitializationException.java
    09/02/2014 07:47 PM 10,847 CounterTest (1).java
    09/02/2014 06:59 PM 10,847 CounterTest.java
    09/24/2014 01:46 PM <DIR> Data Structure & Alogrithms
    06/23/2014 07:57 PM 22,081
    09/08/2014 08:57 PM 22,522
    09/09/2014 12:34 PM 20,332
    03/05/2014 11:34 PM <DIR> LoopingTesting
    09/14/2014 09:42 PM <DIR> Module2
    09/20/2014 06:06 PM <DIR> Module3
    09/20/2014 07:49 PM <DIR> Module4
    09/28/2014 09:46 AM <DIR> Module4hwk
    09/28/2014 10:16 PM <DIR> Module5
    09/02/2014 07:47 PM 508,603 Module_2 _hw (1).pdf
    09/02/2014 06:59 PM 508,603 Module_2 _hw.pdf
    03/31/2014 11:25 PM <DIR> New Unity Project
    07/02/2014 09:34 PM <DIR> PracticeTest
    04/21/2014 08:14 PM <DIR> Quiz 2 Prepartion
    09/20/2014 06:05 PM 6,646 RecursionDemo.zip
    06/23/2014 02:19 PM 12,174 signature.docx
    07/02/2014 07:59 PM <DIR> testClass
    05/04/2014 10:43 PM 16,001 The Speakers Engage Series for my Engaged
    Citizenship Common Experience requirement has been an engaging and intriguing e
    xperienc1.docx
    19 File(s) 1,450,666 bytes
    14 Dir(s) 50,228,674,560 bytes free

    C:\Users\.......\Documents>javac Module5
    error: Class names, 'Module5', are only accepted if annotation processing is exp
    licitly requested
    1 error

    C:\Users\......\Documents>javac Module5.java
    javac: file not found: Module5.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    C:\Users\....\Documents>

    I'm receiving this error message now. I do have my java files in a package. Could that be the problem on why I have this error message now?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    javac: file not found: Module5.java
    Where is the source file: Module5.java? The javac program could not find it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    Quote Originally Posted by Norm View Post
    Where is the source file: Module5.java? The javac program could not find it.
    It located here....Don't know what else to really do..Should I move it?javaFile.JPG

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    The source file needs to be where the javac program can find it. The easiest place is in the same directory where the javac command is entered. If you don't see the file in the dir command's output, you are in the wrong directory.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    Norm,

    I was able to find my error in my syntax but I have another problem. I'm trying to use the compareTo method. I'm comparing an integer and another integer from an object array. If I try to compile my code I receive this error message:Cannot find Symbol method retrieveArray()


     
    /**
     * 
     *  Gregory B Shavers Jr
     *  CSC 385 
     *  Module 5
     *  
     */
     
     
    import java.util.*;
     
    public class Module5
    {
     
       public static void main(String[] args)
       {
     
         SearchIndex test = new SearchIndex();  
         test.addArray(1,0); 
         test.addArray(2,1); 
         test.addArray(3,2); 
         test.addArray(4,3); 
         test.addArray(5,4); 
         test.addArray(6,5);
         test.addArray(7,6); 
         test.addArray(8,7); 
         test.addArray(9,8); 
         test.addArray(10,9);       
     
       }
     
     
       public int binarySearch(Object[] objArray, int searchObj)
       {
     
          int low = 0;
          int high = objArray.length - 1;
          int mid = 0;  
          int temp = 0;
     
     
          while (low <= high){
     
           mid = (low + high) / 2;
     
           int i = objArray[mid].rretrieveArray();   
     
           if (mid.compareTo(searchObj) < 0)
           {
            low = mid + 1;
            temp = low;
           }
           else if (objArray[mid].compareTo(searchObj) > 0)
           {   
            high = mid - 1;
            temp = high;
           }
           else
           {
             return mid;
           }
     
       }
       return temp; 
     
          if( temp < 0)
          {
           temp++;
          }
     
     
       }
     
     
     
     
    }


     
     
     public class SearchIndex
    {
     
        Object[] objArray; 
     
        public SearchIndex(){
        }
     
        public SearchIndex( Object objArray, int x)  
        {
     
            objArray = this.objArray;
            objArray = new SearchIndex[x];
     
        }
     
        public SearchIndex(Object[] o)
        {
            objArray = o;
     
        }
     
        public SearchIndex(int x)
        {
            //objArray = this.objArray;
            objArray = new Comparable[x];
     
        }
     
        public void addArray(Object a , int i)
        {
                objArray[i] = a;        
        }
     
        public Object[] retrieveArray()
        {
                return objArray;        
        }
    }

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    That array is defined as holding Object items. The Object class does not have that method.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    Quote Originally Posted by Norm View Post
    That array is defined as holding Object items. The Object class does not have that method.

    Could you elaborate on that? If I created my array objects shouldn't my data be recoverable from my objects? I'm not quite sure what I need to code.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    Could you elaborate on that?
    The class: Object does not have a method: retrieveArray()

    Why are you using Object class objects instead of the class that has the retrieveArray() method?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Binary algorithm assignment.

    Why are you using Object class objects instead of the class that has the retrieveArray() method?[/QUOTE]

    My teachers wants us to use object Arrays for this assignment and compare them to a int value to see if its in the array object list or not.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Binary algorithm assignment.

    use object Arrays for this assignment and compare them to a int value
    You can not compare an object to an int. One is an instance of a class, the other is a primitive.

    If you have a class that contains an int field, that class could have a method that returns the value of the int field which could then be compared to another int value.
    (refToClass.getIntValue() == 123) // compares value in class with the int: 123
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Binary Search Algorithm
    By omgitztmarie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2014, 06:22 AM
  2. Programming, Algorithm and Data Structures Assignment
    By rd33java in forum Object Oriented Programming
    Replies: 1
    Last Post: January 5th, 2014, 09:15 AM
  3. Replies: 7
    Last Post: January 23rd, 2013, 09:04 PM
  4. For Loops in a Route Assignment Algorithm
    By Aaron140 in forum Loops & Control Statements
    Replies: 5
    Last Post: April 3rd, 2012, 12:23 PM
  5. Binary datafile and object creation according to binary tag ?
    By loicus in forum Object Oriented Programming
    Replies: 4
    Last Post: October 14th, 2011, 01:00 PM