Calling methods from a driver

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 926 Views - Last Post: 06 December 2011 - 04:17 PM Rate Topic: -----

#1 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Calling methods from a driver

Posted 05 December 2011 - 10:41 PM

/*arraysterDriver.java*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class arraysterDriver{

	public static void main(String []args){
	//To do so, create an array of integers in the driver class and pass it 
	//to the methods of an Arrayster object you create in your driver class. 
		int nums [];
		Arrayster method = new Arrayster();
		method.arrayTotal();
		method.arrayMin();
		method.arrayMax();
		method.setToZero();
		method.fillArray();
		method.display();
		method.toString();
	}
}


I am trying to call methods from Arrayster.java

Do I have to extend the Arrayster class in order to call the other methods. Or do i have to use keyword this?
Any help would be greatly appreciated

The Arrayster class takes an array from this driver.

Is This A Good Question/Topic? 0
  • +

Replies To: Calling methods from a driver

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Calling methods from a driver

Posted 05 December 2011 - 10:46 PM

in the same package? if no, then no need to import.
But another two questions:
- Are you getting any error by doing what you have done?
- What those methods definition looks like?
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Calling methods from a driver

Posted 05 December 2011 - 10:46 PM

Where is you Arrayster class ? which should have

void arrayTotal() {
...
}
void arrayMin() {
...
}
Was This Post Helpful? 1
  • +
  • -

#4 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Re: Calling methods from a driver

Posted 05 December 2011 - 10:48 PM

/*Arrayster.java
Lab that works with a one dimensional array.
*/
import java.util.Random;

public class Arrayster{
	int nums[];

	public void arrayTotal(int nums[]){
		int sum = 0;
		for(int i = 0; i < nums.length; i++)
		{
			sum += nums[i];
			System.out.println("The total is" + sum);
		}
	}

	
	public void arrayMin(int nums[]){
	int min = nums[0];
		for(int i = 0; i < nums.length; i++){
			if(min > nums[i]);
			min = nums[i];
		System.out.println("Minimum is " + min);
		}
	}

	
	public void arrayMax(int nums[]){
		int max = nums[0];
		for(int i = 0; i < nums.length; i++){
			if(max < nums[i]); 
			max = nums[i];
		System.out.println("Maximum is " + max);
		}
	}

	
	public void setToZero(int nums[]){
		int first = 0;
		for(int i = 0; i < nums.length; i++){ 
			nums[i] = first;
			}
	}

	
	public void fillArray(int nums[]){
		Random generator = new Random();
		
		int first = 0;
		for(int i = 0; i < nums.length; i++){ 
			int setRandom = generator.nextInt(25) + 25;
			nums[i] = setRandom;
			}

	}

	
	public void display(int nums[]){
		int first = 0;
		System.out.println ( "All the values in the array are: " );
		for(int i = 0; i < nums.length; i++){ 
			System.out.println (nums[i]);
				
			}

	}


	public String toString(){
	String firstPart = "I am ";
	String name = " Your Name ";
	String lastPart = " in the evening class at the HEC, this is Lab-8-Arrayster!!";
	String message = firstPart + name + lastPart;
	return firstPart + name + lastPart;

	}
}



The error message: "arraysterDriver.java:12: arrayTotal(int[]) in Arrayster cannot be applied to ()"

I think they are in the same package. I created both classes.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Calling methods from a driver

Posted 05 December 2011 - 10:51 PM

your actual methods expect a int[] as parameter
you call them without parameter
Was This Post Helpful? 1
  • +
  • -

#6 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Re: Calling methods from a driver

Posted 05 December 2011 - 11:55 PM


                method.arrayTotal(int[]);
		method.arrayMin(int[]);
		method.arrayMax(int[]);
		method.setToZero(int[]);
		method.fillArray(int[]);
		method.display(int[]);
		method.toString(int[]);




Would i pass nums[] ??

It gives an error: '.class' expected and points to (int[])
Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,019
  • Joined: 12-October 11

Re: Calling methods from a driver

Posted 06 December 2011 - 12:09 AM

Yes you would pass nums, but you have to initialize it first.


/*arraysterDriver.java*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class arraysterDriver{

	public static void main(String []args){
	//To do so, create an array of integers in the driver class and pass it 
	//to the methods of an Arrayster object you create in your driver class. 
		int[] nums = new int[20]; // Initialize here
		Arrayster method = new Arrayster();
		method.arrayTotal(nums);
		method.arrayMin(nums);
		method.arrayMax(nums);
		method.setToZero(nums);
		method.fillArray(nums);
		method.display(nums);
		method.toString();
	}
}


Was This Post Helpful? 1
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Calling methods from a driver

Posted 06 December 2011 - 12:29 AM

Another question to think is, why those methods should pass an array as parameter? I dont see the logic there, regarding that the class has an instance variable called nums but never used, instead we deal with the passed parameter only!
Was This Post Helpful? 1
  • +
  • -

#9 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Re: Calling methods from a driver

Posted 06 December 2011 - 12:57 AM

public void display(int nums[]){
		int first = 0;
		System.out.println ( "All the values in the array are: " );
		for(int i = 0; i < nums.length; i++){ 
			System.out.println (nums[i]);
				
			}

	}



In this portion I am tryingto print out the contents of the array. I want to format to look like this:
int int int int int
int int int int int
int int int int int

instead it comes out :
int
int
int
int
int
int

do I have to use println(nums[1], nums[2], nums[3], nums[4]);

I dont want to speciy a specific size though? any suggestions?

I see that I dont need the nums[] in the arrayster class. I have to accept any size of array of ints. I createdthe driver to test it? Im not sure what you mean.
Was This Post Helpful? 0
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,019
  • Joined: 12-October 11

Re: Calling methods from a driver

Posted 06 December 2011 - 01:01 AM

for(int i = 0; i < nums.length; i++) {
    System.out.print(nums[i]);
    if (i%5==0) {
        System.out.println();
    } else {
        System.out.print(" ");
    }
}


Something like that?

This post has been edited by Sheph: 06 December 2011 - 01:02 AM

Was This Post Helpful? 1
  • +
  • -

#11 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Re: Calling methods from a driver

Posted 06 December 2011 - 01:27 AM


public void display(int nums[]){
		
		System.out.println ( "All the values in the array are: " );
		for(int i = 0; i < nums.length; i++){ 
			
			if(i%5==0){
				System.out.print(nums[i]);
			}
			else{
				System.out.print (" ");
			}
			}

	}

This is what I have, and it is printing every 5th element. I think. Do I need to incorporate a counter inorder to track the amount of elements it passes. maybe divide it by 5?
Was This Post Helpful? 0
  • +
  • -

#12 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Re: Calling methods from a driver

Posted 06 December 2011 - 02:21 AM


	public void display(int nums[]){
		
		System.out.println ( "All the values in the array are: " );
		for(int i = 0; i < nums.length; i++){ 
			System.out.print(nums[i] );
			System.out.print (" ");
			if(i%5==0){
				System.out.print(nums[i]);
				System.out.print (" ");
			}
			else{
				System.out.print (" ");
			}
			}

	}





How do you call for a new line in the println?
Was This Post Helpful? 0
  • +
  • -

#13 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Re: Calling methods from a driver

Posted 06 December 2011 - 02:44 AM

public void display(int nums[]){
		int count = 0;
		System.out.println ( "All the values in the array are: ");
		for(int i = 0; i < nums.length; i++){ 
			System.out.print(nums[i]+ " ");
			count++;
			if(count%5==0){
				System.out.println(nums[i] + " ");
			}
		}

	}

this is the way I got it to work. Is this an efficient way of doing it?

This post has been edited by smohd: 06 December 2011 - 03:59 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#14 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Calling methods from a driver

Posted 06 December 2011 - 03:31 AM

Quote

public void display(int nums[]){
int count = 0;
System.out.println ( "All the values in the array are: ");
for(int i = 0; i < nums.length; i++){
System.out.print(nums[i]+ " ");
count++;
if(count%5==0){
System.out.println(nums[i] + " ");
}
}



You don't need to use a count variable, that is what 'i' is for. You can replace if (count % 5) back to i. Shephs suggestion was fine, except for one modification to make sure if(i%5==0 && i != 0)

To call for a printline just put System.out.println() // empty parameters

This post has been edited by Mylo: 06 December 2011 - 03:31 AM

Was This Post Helpful? 1
  • +
  • -

#15 bhawk29er  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 29
  • Joined: 01-February 11

Re: Calling methods from a driver

Posted 06 December 2011 - 01:27 PM

public void arrayMax(int nums[]){
		int max =0;
		for(int i = 0; i < nums.length; i++){
			if(max < nums[i]); 
			max = nums[i];
		
		}
		System.out.println("The largest is: " + max);
	}




I am currently debugging this, and I need it to return the maximum value from the array. when I walk the array the array it saves the values as the max even though they are smaller then the max the step before. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2