3 Replies - 855 Views - Last Post: 26 January 2009 - 08:08 PM Rate Topic: -----

#1 csgwms   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-August 08

Turning static method into class method

Post icon  Posted 26 January 2009 - 07:13 PM

Hi i was wondering how i would turn a static method such as this one
//****************************************************************
// ReverseArray.java
//
//
// Program that prompts the user for an integer, enter many value
// prints out the array, reverses the array then outputs result
//****************************************************************
import java.util.Scanner;

public class ReverseArray {

public static void main(String[] args) {
int size, temp;

Scanner scan = new Scanner(System.in);

System.out.print("Enter the size of your array: ");
size = scan.nextInt();


int[] myArray = new int[size];
// input array of ints

for(int i = 0; i< myArray.length; i++){
System.out.print("Enter number " + i+": ");
myArray[i] = scan.nextInt();
}
// print original array
printArray(myArray);

//reverse elements in array using swap technique
for(int j = 0; j<(myArray.length/2); j++){
temp = myArray[j];
myArray[j] = myArray[myArray.length-1-j];
myArray[myArray.length-1-j] = temp;
}
// print reversed array
printArray(myArray);
}


// printing an int array

public static void printArray(int[] a){

for(int i =0; i<a.length;i++)
System.out.print(a[i]+"\t");
System.out.println();
}
} 


into a class method, meaning i would liek to split the printArray static method into a separate class file so there fore i will have a client and class file. I have tried splitting it into the class but got confused as to what to do with the constructor, how i would call the method and stuff.. any help would be appreciated. :) by the way the program is supposed to turn an array backwards.

Is This A Good Question/Topic? 0
  • +

Replies To: Turning static method into class method

#2 csgwms   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-August 08

Re: Turning static method into class method

Posted 26 January 2009 - 07:49 PM

does nobody know the answer to this problem? :( :(
Was This Post Helpful? 0
  • +
  • -

#3 markhazlett9   User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Turning static method into class method

Posted 26 January 2009 - 08:03 PM

Just go like this...

public class className
{
public void methodName(int[] array)
{
//the code you want here
}
}




Then to call it you will have to treat it as an object

className object = new className();

object.methodName(array[]);



And you're done!
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Turning static method into class method

Posted 26 January 2009 - 08:08 PM

View Postcsgwms, on 26 Jan, 2009 - 06:49 PM, said:

does nobody know the answer to this problem? :( :(

There is no problem but depends what you want to do and you didn't mention it
You just hate the word "static"
Actually kind of useless but if you want it ..
Does the class hold the array or should it just provide the method to sort/print ?
Something like that:

import java.util.Scanner;

public class ReverseArray {

	int[] myArray;
	
	ReverseArray(Scanner scan, int size) {
		myArray = new int[size];
		for(int i = 0; i< size; i++){
			System.out.print("Enter number " + i+": ");
			myArray[i] = scan.nextInt();
		}
	}
	
	//	reverse elements in array using swap technique
	void reverseArray() {

		for(int j = 0; j<(myArray.length/2); j++){
			int temp = myArray[j];
			myArray[j] = myArray[myArray.length-1-j];
			myArray[myArray.length-1-j] = temp;
		}		
	}

	void printArray(){

		for(int i =0; i<myArray.length;i++)
			System.out.print(myArray[i]+"\t");
		System.out.println();
	}

	public static void main(String[] args) {
		int size;

		Scanner scan = new Scanner(System.in);

		System.out.print("Enter the size of your array: ");
		size = scan.nextInt();
		ReverseArray ra = new ReverseArray(scan, size);

//		print original array
		ra.printArray();

//		reverse elements in array using swap technique
		ra.reverseArray();
//		print reversed array
		ra.printArray();
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1