d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Import User Created Class
Add Reply New Topic New Poll
Member
Posts: 13,862
Joined: Jan 19 2008
Gold: 366.01
Jun 26 2012 02:26pm
I've never had experience with a program that has taken methods from another class. But I have created a public class that has methods that im suppose to import to another java program to test. I dont know how to do this.

This is my code with the testing in the main method and the other methods i should have imported.
Code
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;

public class Static_Methods
{

   public static void main(String[] args) throws IOException
   {
    File input = new File("Scores.txt");
    Scanner keyboard = new Scanner(input);
    int[] scores = new int[36];
    int total = 0;
    int average;
    int highest;
    int lowest;
    int median;
    int[] deviations = new int[36];
    PrintWriter output = new PrintWriter("deviations.txt");
    double stdDev;
    int counter = 0;
    float comparison = 0;

 System.out.println("Scores:");
    for(int i=0;i<scores.length;i++)
    {
     scores[i] = keyboard.nextInt();
     System.out.print(scores[i] + " ");
     if((i+1)%4 ==0)
     {
      System.out.print("\n");
     }
    }

 System.out.println();
    total = getTotal(scores);
    average = getAverage(total, scores);
    System.out.println("Average: " + average);

    Arrays.sort(scores);

    highest = getHighest(scores);
    System.out.println("Highest Score: " + highest);

    lowest = getLowest(scores);
    System.out.println("Lowest Score: " + lowest);

    median = getMedian(scores);
    System.out.println("Median: " + median + "\n");

    getPosition(scores);

 getDeviations(scores, deviations, average);
 for(int i=0;i<deviations.length;i++)
 {
  output.print(deviations[i] + " ");
 }

 stdDev = getStdDev(deviations);
 System.out.println("Standard Deviation: " + stdDev);

 for(int i=0;i<scores.length;i++)
 {
  if((average-stdDev)<= scores[i] && scores[i]<=(average+stdDev))
  {
   counter++;
  }
 }

 comparison = ((float)counter / (float)(scores.length)) * 100;

 System.out.println("Scores within one standard deviation: " + counter);

 if(60 <= comparison && comparison <=70)
 {
  System.out.println("The percent of scores within one deviation is " + comparison + "% and is similar to the Normal Distribution.");
 }
 else if(comparison<60)
 {
  System.out.println("The percent of scores within one deviation is " + comparison + "% and is lower to the Normal Distribution.");
 }
 else if(comparison>60)
 {
  System.out.println("The percent of scores within one deviation is " + comparison + "% and is higher to the Normal Distribution.");
 }

 output.close();


   }

   /*Method named getTotal to get the total of all the
   scores to calculate the average*/
   public static int getTotal(int[] scores) throws IOException
   {
    int total = 0;
    for(int i=0;i<scores.length;i++)
    {
     total += scores[i];
    }

    return total;
   }

   //Method using the total from getTotal to calculate the average
   public static int getAverage(int total, int[]scores) throws IOException
   {
    int average;
    average = total/(scores.length);
    return average;
   }

   //Method to get the highest value in the array
   public static int getHighest(int[] scores) throws IOException
   {
    int highest;
    highest = scores[scores.length-1];
    return highest;
   }

   //Method to get the lowest value in the array
   public static int getLowest(int[] scores) throws IOException
   {
    int lowest;
    lowest = scores[0];
    return lowest;
   }

   //Method to get the median value in the array
   public static int getMedian(int[] scores) throws IOException
   {
    int median;
    int lowerMid;
    int higherMid;

    lowerMid = scores[(int)(scores.length/2)];
    higherMid = scores[(int)((scores.length/2)+1)];
    if(higherMid == lowerMid)
    {
     median = lowerMid;
    }
    else
    {
     median = (lowerMid+higherMid)/2;
    }

    return median;
   }

   //Method to find the position in the original array for values 68,77,99
   public static void getPosition(int[] scores) throws IOException
   {
    boolean found68 = false;
    boolean found77 = false;
    boolean found99 = false;
    int position68 = 0;
    int position77 = 0;
    int position99 = 0;

    for(int i=0;i<scores.length;i++)
    {
     if(scores[i] == 68)
     {
      found68 = true;
      position68 = i+1;
     }
     else if(scores[i] == 77)
     {
      found77 = true;
      position77 = i+1;
     }
     else if(scores[i] == 99)
     {
      found99 = true;
      position99 = i+1;
     }
    }

    if(found68)
    {
     System.out.println("68 is in position " + position68 + " in the array.");
    }
    else
    {
     System.out.println("68 was not found.");
    }

     if(found77)
    {
     System.out.println("77 is in position " + position77 + " in the array.");
    }
    else
    {
     System.out.println("77 was not found.");
    }

     if(found99)
    {
     System.out.println("99 is in position " + position99 + " in the array.\n");
    }
    else
    {
     System.out.println("99 was not found.\n");
    }
   }

//Method to get the deviation of each number from the mean
   public static void getDeviations(int[] scores, int[] deviations, int average)
   {
    for(int i=0;i<scores.length;i++)
    {
     deviations[i] = scores [i] - average;
    }
   }

   //Method to get the Standard Deviation of the array
   public static double getStdDev(int[] deviations)
   {
    double stdDevTotal=0;
    double stdDev;
    for(int i=0;i<deviations.length;i++)
    {
     stdDevTotal += Math.pow(deviations[i], 2);
    }

    stdDev = Math.sqrt((stdDevTotal/(deviations.length)));

    return stdDev;
   }

}
Member
Posts: 31
Joined: May 18 2012
Gold: 1,450.00
Jun 26 2012 11:56pm
Just create another file, put it in the same directory as that one, and from the other file, call the methods like this:
Code
Static_Methods.getLowest(scores)
or
Static_Methods.getAverage(scores)

Basically you should be able to just cut and paste the main method you already have into that new file, and add "Static_Methods." before each method call.
Member
Posts: 3,640
Joined: Dec 5 2005
Gold: 8,518.83
Jun 27 2012 12:35am
Well since it is a java.class and not a library you just have to drag and drop it into the package of your new project.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll