Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By Tolls

Thread: how to invoke an array method from another class

  1. #1
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default how to invoke an array method from another class

    this is the problem
    Write a Java project with two different classes. First class named Lab11main should contain only the main() method and the second class named Lab11 should contain a method named int[] reverseArray(int array)which will receive an array from main() method in Lab11main class and then reverse the whole array and should return the reversed array to the main() to print out.
    this is what I did
    my main class
    Java Code:
    package java4;
    import java.util.Scanner;
    
    public class Lab11main {
    	
    	
    	public static void main(String[] args){
    		
    		int i=0;
    		int [] b;
    		
    			Scanner input= new Scanner(System.in);
    			int [] a= new int  [10];
    			a[i]=input.nextInt();
    			
    			
    			Lab11 ob= new Lab11();
    			b[i]=ob.reversArray(a);
    			
    			
    			
    			
    		}
    	
    	
    	
    	
    	
    	
    
    }




    Java Code:
    package java4;
    
    public class Lab11 {
    	
    	public int[] reverseArray(int[] a){
    		int right;
    		for (int left=0; right=a.length, left<right;i++)
    		{
    		  int temp= a[left];
    		  a[left]= a[right];
    		  a [right]=temp;
    		   return [] a;
    		  
    		}
    	}
    	
    	
    	
    
    }

  2. #2
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: how to invoke an array method from another class

    it says that the method reverseArray () is undefined in Lab 11

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: how to invoke an array method from another class

    First, do you have a question? Second, did you realize that you can put both classes in the same file? You just can't make the second one public.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: how to invoke an array method from another class

    Quote Originally Posted by skj View Post
    it says that the method reverseArray () is undefined in Lab 11
    No, it probably says reversArray() is undefined in Lab11.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: how to invoke an array method from another class

    Quote Originally Posted by jim829 View Post
    No, it probably says reversArray() is undefined in Lab11.

    Regards,
    Jim
    int [] reverseArray() is a method right?
    how can I call that method to the Lab11main Class ? I want to print the reversed array from Lab 11 in Lab11main!
    Thank you

  6. #6
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: how to invoke an array method from another class

    For the classes . yes I know I can put the two classes in the same file ,but the instructor said we have to put it in different files . I don't know why !

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: how to invoke an array method from another class

    Ok, I understand about the two files. However, you misspelled reverseArray(). You left off the third e. That's what my comment meant.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: how to invoke an array method from another class

    Quote Originally Posted by skj View Post
    this is the problem
    Write a Java project with two different classes. First class named Lab11main should contain only the main() method and the second class named Lab11 should contain a method named int[] reverseArray(int array)which will receive an array from main() method in Lab11main class and then reverse the whole array and should return the reversed array to the main() to print out.
    this is what I did
    my main class
    Java Code:
    package java4;
    import java.util.Scanner;
    
    public class Lab11main {
    	
    	
    	public static void main(String[] args){
    		
    		int i=0;
    		int [] b;
    		
    			Scanner input= new Scanner(System.in);
    			int [] a= new int  [10];
    			a[i]=input.nextInt();
    			
    			
    			Lab11 ob= new Lab11();
    			b[i]=ob.reversArray(a);
    			
    			
    			
    			
    		}
    	
    	
    	
    	
    	
    	
    
    }




    Java Code:
    package java4;
    
    public class Lab11 {
    	
    	public int[] reverseArray(int[] a){
    		int right;
    		for (int left=0; right=a.length, left<right;i++)
    		{
    		  int temp= a[left];
    		  a[left]= a[right];
    		  a [right]=temp;
    		   return [] a;
    		  
    		}
    	}
    	
    	
    	
    
    }
    please can someone help me with this problem

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: how to invoke an array method from another class

    You're trying to call a method 'reversArray', but the method is defined as 'reverseArray'; put your glasses on ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to invoke an array method from another class

    Poor Jim. So incredibly ignored its criminal.
    jim829 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: how to invoke an array method from another class

    Quote Originally Posted by gimbal2 View Post
    Poor Jim. So incredibly ignored its criminal.
    The OP most likely wasn't wearing his glasses; that'd explain it all ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: how to invoke an array method from another class

    Quote Originally Posted by gimbal2 View Post
    Poor Jim. So incredibly ignored its criminal.
    Jim who?
    jim829 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. invoke object in another class
    By storm88 in forum New To Java
    Replies: 1
    Last Post: 10-05-2011, 11:03 PM
  2. How to invoke method present in abstract class?
    By java_no1 in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 07:13 AM
  3. reflection invoke method
    By pprl in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 05:43 PM
  4. Loop through an Array and invoke SQL command
    By Robert_85 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-25-2010, 12:14 PM
  5. Replies: 10
    Last Post: 06-12-2009, 06:41 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
  •