run from a separate class
So i need to run this class from a second class. How do I do that? Here's the code I want to run from a separate class:
import java.util.*;
public class Numbers {
int min(int a, int b, int c, int d, int e) {
int min1 = Math.min(a, b);
int min2 = Math.min(c, d);
int min3 = Math.min(min1, min2);
int min4 = Math.min(min3, e);
return min4;
}
int max(int a, int b, int c, int d, int e) {
int max1 = Math.max(a, b);
int max2 = Math.max(c, d);
int max3 = Math.max(max1, max2);
int max4 = Math.max(max3, e);
return max4;
}
double avg(int a, int b, int c, int d, int e) {
double average = (a+b+c+d+e)/5;
return average;
}
double stddev(int a, int b, int c, int d, int e) {
double average = (a+b+c+d+e)/5;
double a1 = Math.pow((a-average),2);
double b1 = Math.pow((b-average),2);
double c1 = Math.pow((c-average),2开发者_如何学Python);
double d1 = Math.pow((d-average),2);
double e1 = Math.pow((e-average),2);
double average2 = (a1+b1+c1+d1+e1)/5;
double x;
x = Math.sqrt(average2);
return x;
}
}
Somewhere in another class, you instantiate this class and call appropriate methods:
Numbers instance = new Numbers();
int maxValue = instance.max(1, 2, 3, 4, 5);
You mean call the methods in the Numbers class? You can do this as follows:
public Main{
public static void main( String[] args ){
Numbers numbers = new Numbers(); // Instance
int minimum = numbers.min( 1, 2, 3, 4, 5 );
System.out.println( "minimum:" + minimum );
int maximum = numbers.max( 1, 2, 3, 4, 5);
System.out.println("maximum:" + maximum );
double average = numbers.avg( 1, 2, 3, 4, 5 );
System.out.println("average:" + average );
double standardDev = numbers.devstd( 1, 2, 3, 4, 5 );
System.out.println("standard deviation:" + standardDev );
}
}
Better yet, make those methods static since they don't depend on any attributes:
public static int min(int a, int b, int c, int d, int e) {
int min1 = Math.min(a, b);
int min2 = Math.min(c, d);
int min3 = Math.min(min1, min2);
int min4 = Math.min(min3, e);
return min4;
}
public Main{
public static void main( String args[] ){
int minimum = Numbers.min( 1, 2, 3, 4, 5);
System.out.println( "minimum:" + minimum );
}
}
Added bonus with a more general-purpose method:
public static int min(int[] intArray ) {
// If empty/null array throws Exception. Caller/supplier should handle this.
int min = intArray[0];
for( int i = 1; i < intArray.length; i++ ){
min = Math.min(min, intArray[i]);
}
return min;
}
Create an object of this class in the other class ( Note : in the same package).
Like this,
Numbers n = new Numbers();
int minimum = n.min(10,20,30,40,50);
System.out.println(minimum); // Prints 10
int maximum = n.max(10,20,30,40,50);
System.out.println(maximum); // Prints 50
double average = n.avg(10,20,30,40,50);
System.out.println(average); // Prints 30
double dev = n.devstd(10,20,30,40,50);
If your class is in the same package (if you are using the default setup in Eclipse for example, then it will be), then in your new class you can call these functions just like you did with Math.pow
double l_stddev = Numbers.stddev(1,2,3,4,5);
for example.
Edit: Oops - missed that these were not static methods. You could make them static in your Numbers class, then you would not need to make an object to call them (just like the Math class in fact)
So as the other answer says, with you class as it is, you need to make a Number object.
In the another class , in main() method create an object of Numbers class and call the desired methods.
Numbers obj=new Numbers();
obj.max( a, b, c,d ,e);
I don't know what you mean by run from a separate class, but if you want to call these functions from another class you should make them static:
public static int min(...
Then you call it:
int m = Numbers.min(...
from wherever you want.
精彩评论