Results 1 to 8 of 8
  1. #1
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default using a driver with a class

    I am using Eclipse and trying to incorporate a driver with a class. How would I tie the two together?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vendetta View Post
    I am using Eclipse and trying to incorporate a driver with a class. How would I tie the two together?
    A driver for what? Does your driver vendor give you any documentation? You have to supply more details because as it is now your question can't be answered.

    kind regards,

    Jos

  3. #3
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    this is my first week in OOP, so I'm not really sure what I'm talking about.
    From what I understand this is a class:

    Java Code:
    Public static int [] stutter[ int [] a ]
    
    {
    
    int [] result = new int [ a.length*2]
    
    for (int i=0; i<a.length; i++)
    {
    result [ 2*i] = a[i];
    result [2*i] = a [i];
    }
    
    return result;
    }
    And that this was a driver for the class:

    Java Code:
    Public static void main
    
    int [] a = {4,7,-2,15};
    int [] a2 = stutter[a];
    
    System.out.println(arrays.tostring(a2)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Except for a couple of syntax errors you're doing fine. Read Sun's tutorials to make sure you've got the syntax right; also try to compile what you have now and try to study the compiler's error diagnostic messages, they're very accurate.

    kind regards,

    Jos

  5. #5
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    alrighty, Thank you

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    'Driver' is a loose term in CS. We generally think of it as software that 'drives' hardware, but that isn't its only meaning.

    We sometimes use the term Driver in OOP to mean the part of the program which integrates components together and provides the general control and flow logic of your program. Its a vague term that really just represents the glue that holds all the parts together.

    I'm sure the OP is referring to this definition.

    That being said, your syntax is a mess. I'll make a couple changes for you, please read your book about class structure, method structure, etc...

    Java Code:
    public static int[] stutter(int[] a){
        int[] result = new int[a.length*2];
    
        for (int i=0; i<a.length; i++){
            result [ 2*i] = a[i];
            result [2*i] = a [i];
        }
    
        return result;
    }
    
    //And that this was a driver for the class:
    
    public static void main(String[] args){
        int [] a = {4,7,-2,15};
        int [] a2 = stutter[a];
    
        System.out.println(arrays.toString(a2));
    }

  7. #7
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    I tried using the code that quad64bit fixed for me and I can't get it to work. I don't think the code is the problem, but how I am using it with the IDE. I tried to put it into the IDE and it isn't even close to being able to compile. every character of the code is an error. Earlier today I loaded some some files that used a class in .java file and everything was ok. This file above doesn't even have a class, but I thought everything needed a class.

    I'm not sure what "static" is all about, I researched it and it says something to the effect that you don't need an object prior to using...something...maybe "stutter" in this case?

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I didn't actually try to compile your code, and so I missed a couple errors. This compiles but I have not tried to run it:
    Java Code:
    import java.util.Arrays;
    
    public class MyClass {
    
        public static int[] stutter(int[] a) {
            int[] result = new int[a.length * 2];
    
            for (int i = 0; i < a.length; i++) {
                result[ 2 * i] = a[i];
                result[2 * i] = a[i];
            }
    
            return result;
        }
    
    //And that this was a driver for the class:
        public static void main(String[] args) {
            int[] a = {4, 7, -2, 15};
            int[] a2 = stutter(a);
    
            System.out.println(Arrays.toString(a2));
        }
    }
    As far as the definition of static, it can be somewhat broad for a beginner but perhaps this will help you:
    Generally speaking, if a method/function is static, then it can be called without making a new instance of the class containing said method. For example:
    double number = Math.random(); //works!
    You can do the above statement without creating an instance of the Math class because the random() method in Math is static. This is useful for functions that perform a simple task and return a result.

    Generally speaking, you should not use static in day to day programming unless making utility classes or methods where static makes sense.

    Look at the java API for the Math class, and you'll see many great examples of methods that use static appropriately. Note how just about every other class in java requires you create an instance of that class to use its methods -- this is generally what you want to do.

    The main() method in any java program must always be static, and because of the way you are writing your program, your stutter method needs to be static as well. To avoid using static, you could write your code like this:
    Java Code:
    import java.util.Arrays;
    
    public class MyClass {
        public static void main(String[] args) {
            new MyClass();
        }
    
        public MyClass(){
            int[] a = {4, 7, -2, 15};
            int[] a2 = stutter(a);
    
            System.out.println(Arrays.toString(a2));
        }
    
        public int[] stutter(int[] a) {
            int[] result = new int[a.length * 2];
    
            for (int i = 0; i < a.length; i++) {
                result[ 2 * i] = a[i];
                result[2 * i] = a[i];
            }
    
            return result;
        }
    }
    I added a constructor, and then created a new instance of MyClass in the main method. I moved your 'driver' code into the constructor which in the end has the same result as what you had before, but without static methods. It will depend on what your teach wants as far as how you should do it, but most of the time code is written the way I just showed you.

Similar Threads

  1. Driver, Class, and Connection String to UltraLite 11
    By Harrie_KalaChakra in forum Eclipse
    Replies: 0
    Last Post: 01-06-2010, 04:33 AM
  2. Compile error in Driver class, help plz
    By Battlefeldt in forum New To Java
    Replies: 3
    Last Post: 11-25-2009, 11:30 PM
  3. Driver for Java-SQL
    By Pr0n in forum New To Java
    Replies: 7
    Last Post: 07-23-2009, 11:30 PM
  4. Replies: 1
    Last Post: 03-14-2008, 02:51 PM
  5. Help with JDBC driver
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 07-03-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •