By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

How do I import code?

Blue1244
P: 39
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. package testingclasses;
  3.  
  4.  
  5. public class TestingClasses {
  6.  
  7.     public static void main(String[] args) {
  8.         //code needs to be imported here.
  9.     }
  10. }
  11.  
  12.  
This is the code im trying to import
Expand|Select|Wrap|Line Numbers
  1. package testingclasses;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class linearFunction {
  6.  
  7.     public static void main(String[] args) 
  8.     {
  9.      Scanner scan = new Scanner(System.in);
  10.      System.out.println("You have Chosen the Linear Function Solving!");
  11.      System.out.println("This part of the program is going to ask you for three things, first it is going to ask");
  12.      System.out.println("you for your coefficient, then it is going to ask you for your constant,");
  13.      System.out.println("then it is going to ask you for three numbers that are going to substitute for x.");
  14.      System.out.println();
  15.      System.out.println("Okay here we go!");
  16.      System.out.println("What is your coefficient?");
  17.      double coef = scan.nextDouble();
  18.      System.out.println("");
  19.      System.out.println("What is your constant?");
  20.      double constant = scan.nextDouble();
  21.      System.out.println("What are your three constants?");
  22.      double a = scan.nextDouble();
  23.      double b = scan.nextDouble();
  24.      double c = scan.nextDouble();
  25.      System.out.println("");
  26.      System.out.println("Your function should look like this, y=" + coef + "(x)" + "+" + constant);
  27.      System.out.println("**************************************************************************");
  28.      System.out.println("Your Functions with the x substitutes should look like this...");
  29.      String function1 = "f(x)=" + coef + "(" + a + ")" + "+" + constant;
  30.      String function2 = "f(x)=" + coef + "(" + b + ")" + "+" + constant;
  31.      String function3 = "f(x)=" + coef + "(" + c + ")" + "+" + constant;
  32.      System.out.println("Function 1 => " + function1);
  33.      System.out.println("Function 2 => " + function2);
  34.      System.out.println("Function 2 => " + function3);
  35.      System.out.println("**************************************************************************");
  36.      System.out.println("Your funstion's answers should look like this.");
  37.      System.out.println("**************************************************************************");
  38.      double one = coef * a + constant;
  39.      double two = coef * b + constant;
  40.      double three = coef * c + constant;
  41.      System.out.println("Answer Function 1 =>" + one );
  42.      System.out.println("Answer Function 2 =>" + two );
  43.      System.out.println("Answer Function 3 =>" + three );
  44.      System.out.println("**************************************************************************");
  45.      System.out.println("Your Funstion table should look like this,");
  46.      System.out.println("**************************************************************************");
  47.      System.out.println("X | Y");
  48.      System.out.println(a + "|" + one);
  49.      System.out.println(b + "|" + two);
  50.      System.out.println(c + "|" + three);
  51.      System.out.println("**************************************************************************");
  52.      System.out.println();
  53.      System.out.println("This is other important info");
  54.      System.out.println("**************************************************************************");
  55.      System.out.println("Slope = " + coef );
  56.      System.out.println("Y-intercept = " + constant );
  57.      System.out.println("**************************************************************************");
  58.     }
  59. }
  60.  
  61.  
Sorry there is so much code.
Sep 27 '13 #1

✓ answered by Nepomuk

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.driveForward()".

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:
Expand|Select|Wrap|Line Numbers
  1. package testingclasses;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LinearFunction {
  6.  
  7.     public void run() 
  8.     {
  9.      Scanner scan = new Scanner(System.in);
  10.      System.out.println("You have Chosen the Linear Function Solving!");
  11.      System.out.println("This part of the program is going to ask you for three things, first it is going to ask");
  12.      System.out.println("you for your coefficient, then it is going to ask you for your constant,");
  13.      System.out.println("then it is going to ask you for three numbers that are going to substitute for x.");
  14.      System.out.println();
  15.      System.out.println("Okay here we go!");
  16.      System.out.println("What is your coefficient?");
  17.      double coef = scan.nextDouble();
  18.      System.out.println("");
  19.      System.out.println("What is your constant?");
  20.      double constant = scan.nextDouble();
  21.      System.out.println("What are your three constants?");
  22.      double a = scan.nextDouble();
  23.      double b = scan.nextDouble();
  24.      double c = scan.nextDouble();
  25.      System.out.println("");
  26.      System.out.println("Your function should look like this, y=" + coef + "(x)" + "+" + constant);
  27.      System.out.println("**************************************************************************");
  28.      System.out.println("Your Functions with the x substitutes should look like this...");
  29.      String function1 = "f(x)=" + coef + "(" + a + ")" + "+" + constant;
  30.      String function2 = "f(x)=" + coef + "(" + b + ")" + "+" + constant;
  31.      String function3 = "f(x)=" + coef + "(" + c + ")" + "+" + constant;
  32.      System.out.println("Function 1 => " + function1);
  33.      System.out.println("Function 2 => " + function2);
  34.      System.out.println("Function 2 => " + function3);
  35.      System.out.println("**************************************************************************");
  36.      System.out.println("Your funstion's answers should look like this.");
  37.      System.out.println("**************************************************************************");
  38.      double one = coef * a + constant;
  39.      double two = coef * b + constant;
  40.      double three = coef * c + constant;
  41.      System.out.println("Answer Function 1 =>" + one );
  42.      System.out.println("Answer Function 2 =>" + two );
  43.      System.out.println("Answer Function 3 =>" + three );
  44.      System.out.println("**************************************************************************");
  45.      System.out.println("Your Funstion table should look like this,");
  46.      System.out.println("**************************************************************************");
  47.      System.out.println("X | Y");
  48.      System.out.println(a + "|" + one);
  49.      System.out.println(b + "|" + two);
  50.      System.out.println(c + "|" + three);
  51.      System.out.println("**************************************************************************");
  52.      System.out.println();
  53.      System.out.println("This is other important info");
  54.      System.out.println("**************************************************************************");
  55.      System.out.println("Slope = " + coef );
  56.      System.out.println("Y-intercept = " + constant );
  57.      System.out.println("**************************************************************************");
  58.     }
  59. }
and
Expand|Select|Wrap|Line Numbers
  1. package testingclasses;
  2.  
  3.  
  4. public class TestingClasses {
  5.  
  6.     public static void main(String[] args) {
  7.         LinearFunction linear = new LinearFunction(); // Create a new object of the type "LinearFunction" called "linear"
  8.         linear.run(); // Call the "run" function of this new object 
  9.     }
  10. }
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. :-)

Share this Question
Share on Google+
2 Replies


Blue1244
P: 39
Oh and if you have any tips on organizing code.. I would be much appreciative.
Sep 27 '13 #2

Nepomuk
Expert 2.5K+
P: 3,112
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.driveForward()".

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:
Expand|Select|Wrap|Line Numbers
  1. package testingclasses;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LinearFunction {
  6.  
  7.     public void run() 
  8.     {
  9.      Scanner scan = new Scanner(System.in);
  10.      System.out.println("You have Chosen the Linear Function Solving!");
  11.      System.out.println("This part of the program is going to ask you for three things, first it is going to ask");
  12.      System.out.println("you for your coefficient, then it is going to ask you for your constant,");
  13.      System.out.println("then it is going to ask you for three numbers that are going to substitute for x.");
  14.      System.out.println();
  15.      System.out.println("Okay here we go!");
  16.      System.out.println("What is your coefficient?");
  17.      double coef = scan.nextDouble();
  18.      System.out.println("");
  19.      System.out.println("What is your constant?");
  20.      double constant = scan.nextDouble();
  21.      System.out.println("What are your three constants?");
  22.      double a = scan.nextDouble();
  23.      double b = scan.nextDouble();
  24.      double c = scan.nextDouble();
  25.      System.out.println("");
  26.      System.out.println("Your function should look like this, y=" + coef + "(x)" + "+" + constant);
  27.      System.out.println("**************************************************************************");
  28.      System.out.println("Your Functions with the x substitutes should look like this...");
  29.      String function1 = "f(x)=" + coef + "(" + a + ")" + "+" + constant;
  30.      String function2 = "f(x)=" + coef + "(" + b + ")" + "+" + constant;
  31.      String function3 = "f(x)=" + coef + "(" + c + ")" + "+" + constant;
  32.      System.out.println("Function 1 => " + function1);
  33.      System.out.println("Function 2 => " + function2);
  34.      System.out.println("Function 2 => " + function3);
  35.      System.out.println("**************************************************************************");
  36.      System.out.println("Your funstion's answers should look like this.");
  37.      System.out.println("**************************************************************************");
  38.      double one = coef * a + constant;
  39.      double two = coef * b + constant;
  40.      double three = coef * c + constant;
  41.      System.out.println("Answer Function 1 =>" + one );
  42.      System.out.println("Answer Function 2 =>" + two );
  43.      System.out.println("Answer Function 3 =>" + three );
  44.      System.out.println("**************************************************************************");
  45.      System.out.println("Your Funstion table should look like this,");
  46.      System.out.println("**************************************************************************");
  47.      System.out.println("X | Y");
  48.      System.out.println(a + "|" + one);
  49.      System.out.println(b + "|" + two);
  50.      System.out.println(c + "|" + three);
  51.      System.out.println("**************************************************************************");
  52.      System.out.println();
  53.      System.out.println("This is other important info");
  54.      System.out.println("**************************************************************************");
  55.      System.out.println("Slope = " + coef );
  56.      System.out.println("Y-intercept = " + constant );
  57.      System.out.println("**************************************************************************");
  58.     }
  59. }
and
Expand|Select|Wrap|Line Numbers
  1. package testingclasses;
  2.  
  3.  
  4. public class TestingClasses {
  5.  
  6.     public static void main(String[] args) {
  7.         LinearFunction linear = new LinearFunction(); // Create a new object of the type "LinearFunction" called "linear"
  8.         linear.run(); // Call the "run" function of this new object 
  9.     }
  10. }
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. :-)
Sep 27 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.