How do I import code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blue1244
    New Member
    • Aug 2013
    • 39

    How do I import code?

    I have two files, one holds the code i want to send to another file how do i do this?
    This is the code I have for the main file:
    Code:
    package testingclasses;
    
    
    public class TestingClasses {
    
        public static void main(String[] args) {
            //code needs to be imported here.
        }
    }
    This is the code im trying to import
    Code:
    package testingclasses;
    
    import java.util.Scanner;
    
    public class linearFunction {
    
        public static void main(String[] args) 
        {
         Scanner scan = new Scanner(System.in);
         System.out.println("You have Chosen the Linear Function Solving!");
         System.out.println("This part of the program is going to ask you for three things, first it is going to ask");
         System.out.println("you for your coefficient, then it is going to ask you for your constant,");
         System.out.println("then it is going to ask you for three numbers that are going to substitute for x.");
         System.out.println();
         System.out.println("Okay here we go!");
         System.out.println("What is your coefficient?");
         double coef = scan.nextDouble();
         System.out.println("");
         System.out.println("What is your constant?");
         double constant = scan.nextDouble();
         System.out.println("What are your three constants?");
         double a = scan.nextDouble();
         double b = scan.nextDouble();
         double c = scan.nextDouble();
         System.out.println("");
         System.out.println("Your function should look like this, y=" + coef + "(x)" + "+" + constant);
         System.out.println("**************************************************************************");
         System.out.println("Your Functions with the x substitutes should look like this...");
         String function1 = "f(x)=" + coef + "(" + a + ")" + "+" + constant;
         String function2 = "f(x)=" + coef + "(" + b + ")" + "+" + constant;
         String function3 = "f(x)=" + coef + "(" + c + ")" + "+" + constant;
         System.out.println("Function 1 => " + function1);
         System.out.println("Function 2 => " + function2);
         System.out.println("Function 2 => " + function3);
         System.out.println("**************************************************************************");
         System.out.println("Your funstion's answers should look like this.");
         System.out.println("**************************************************************************");
         double one = coef * a + constant;
         double two = coef * b + constant;
         double three = coef * c + constant;
         System.out.println("Answer Function 1 =>" + one );
         System.out.println("Answer Function 2 =>" + two );
         System.out.println("Answer Function 3 =>" + three );
         System.out.println("**************************************************************************");
         System.out.println("Your Funstion table should look like this,");
         System.out.println("**************************************************************************");
         System.out.println("X | Y");
         System.out.println(a + "|" + one);
         System.out.println(b + "|" + two);
         System.out.println(c + "|" + three);
         System.out.println("**************************************************************************");
         System.out.println();
         System.out.println("This is other important info");
         System.out.println("**************************************************************************");
         System.out.println("Slope = " + coef );
         System.out.println("Y-intercept = " + constant );
         System.out.println("**************************************************************************");
        }
    }
    Sorry there is so much code.
  • Blue1244
    New Member
    • Aug 2013
    • 39

    #2
    Oh and if you have any tips on organizing code.. I would be much appreciative.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      OK, I'm guessing you're not familiar with the concept of functions and methods? No problem, let me explain:

      In any object oriented language (and Java is one of their number), you can write classes (which are a bit like blueprints) and create objects (which are built from these blueprints). These objects can have methods. Projected to the real world a class may be the blueprint of a car (i.e. a Car.java file), an object would be a car built according to the blueprints (Car myCar = new Car();) and a function may be "myCar.driveFor ward()".

      In Java, every function must belong to an object or class. Most functions you will use belong to an object, so every time you create a new object it will have its own set of functions. Sometimes however it is necessary for functions to be available without an object; main is a classic example for this. To make a function available without an object, you make it static (that's why it's a public static void); so you can now access Car.main() rather than myCar.main() (though the latter is possible in theory).

      The main function is used for the jvm to know where it should start; so main is the function which controls the flow of the program. You will rarely if ever need more than one main function in a program.

      So, how do you define new functions? Very easily: do something like this:
      Code:
      package testingclasses;
       
      import java.util.Scanner;
       
      public class LinearFunction {
       
          public void run() 
          {
           Scanner scan = new Scanner(System.in);
           System.out.println("You have Chosen the Linear Function Solving!");
           System.out.println("This part of the program is going to ask you for three things, first it is going to ask");
           System.out.println("you for your coefficient, then it is going to ask you for your constant,");
           System.out.println("then it is going to ask you for three numbers that are going to substitute for x.");
           System.out.println();
           System.out.println("Okay here we go!");
           System.out.println("What is your coefficient?");
           double coef = scan.nextDouble();
           System.out.println("");
           System.out.println("What is your constant?");
           double constant = scan.nextDouble();
           System.out.println("What are your three constants?");
           double a = scan.nextDouble();
           double b = scan.nextDouble();
           double c = scan.nextDouble();
           System.out.println("");
           System.out.println("Your function should look like this, y=" + coef + "(x)" + "+" + constant);
           System.out.println("**************************************************************************");
           System.out.println("Your Functions with the x substitutes should look like this...");
           String function1 = "f(x)=" + coef + "(" + a + ")" + "+" + constant;
           String function2 = "f(x)=" + coef + "(" + b + ")" + "+" + constant;
           String function3 = "f(x)=" + coef + "(" + c + ")" + "+" + constant;
           System.out.println("Function 1 => " + function1);
           System.out.println("Function 2 => " + function2);
           System.out.println("Function 2 => " + function3);
           System.out.println("**************************************************************************");
           System.out.println("Your funstion's answers should look like this.");
           System.out.println("**************************************************************************");
           double one = coef * a + constant;
           double two = coef * b + constant;
           double three = coef * c + constant;
           System.out.println("Answer Function 1 =>" + one );
           System.out.println("Answer Function 2 =>" + two );
           System.out.println("Answer Function 3 =>" + three );
           System.out.println("**************************************************************************");
           System.out.println("Your Funstion table should look like this,");
           System.out.println("**************************************************************************");
           System.out.println("X | Y");
           System.out.println(a + "|" + one);
           System.out.println(b + "|" + two);
           System.out.println(c + "|" + three);
           System.out.println("**************************************************************************");
           System.out.println();
           System.out.println("This is other important info");
           System.out.println("**************************************************************************");
           System.out.println("Slope = " + coef );
           System.out.println("Y-intercept = " + constant );
           System.out.println("**************************************************************************");
          }
      }
      and
      Code:
      package testingclasses;
       
       
      public class TestingClasses {
       
          public static void main(String[] args) {
              LinearFunction linear = new LinearFunction(); // Create a new object of the type "LinearFunction" called "linear"
              linear.run(); // Call the "run" function of this new object 
          }
      }
      Note that I changed two things about the first class: I renamed it from "linearFunction " to "LinearFunction " because the convention is to give classes names that start with capital letters and I changed the public static void main(String[] args) to public void run().
      Also I added two lines to the main function in the TestingClasses class to use the new run function.

      OK, that should be enough to keep you busy for a bit; when you need more help with your code or in case you have difficulties understanding something I wrote, feel free to ask. :-)

      Comment

      Working...