Results 1 to 7 of 7
  1. #1
    sinreaver is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default help creating two classes that function together

    Hi i need help making this code into two different classes, the first class is supposed to receive what info the user wants (slope, midpoint, or length) and the info they have (the two x coordinates and the two y coordinates) and send this message to the second class who finds the results and outputs the message. The problem is i don't know how to separate my one code (which functions correctly) into two codes to get two classes. how would i get one class to recieve the info of another? Also my professor mentioned something about making some variables private, but i really don't grasp what making a variable private means. here is my code if it helps at all:

    import java.util.*;
    class line
    {

    public static void main ( String [] args )
    {

    double x0, x1, y0, y1, L, m, x, y, mp;

    Scanner scanner;
    scanner = new Scanner (System.in);

    //User inputs X0
    System.out.print("What is the value of X0?");
    x0 = scanner.nextInt ();

    //user inputs x1
    System.out.print("What is the value of X1?");
    x1 = scanner.nextInt ();

    //user inputs y0
    System.out.print("What is the value of y0?");
    y0 = scanner.nextInt ();

    //user inputs y1
    System.out.print("What is the value of y1?");
    y1 = scanner.nextInt ();

    //finding L
    L = (Math.pow((x1-x0),2)) + (Math.pow((y1-y0),2));

    //Finding m
    m = ((y1 - y0)) / ((x1 - x0));

    //finding x midpoint
    x = x0 + (.5 * (x1 - x0));

    //finding y midpoint
    y = y0 + (.5 * (y1-y0));


    //display length results
    System.out.print ("The length of the line is: " + L + "\nThe slope of the line is: " + m + "\nThe midpoint of the line is: " + x + "," + y);


    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Homework? Why not show what you've done first?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Well, what of it?

  4. #4
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    Try something like this
    Class 1
    Java Code:
    import java.util.Scanner;
    
    public class SeperateClasses {
    
    	public static final Scanner scanner = new Scanner(System.in);
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Double inputVariable = null;
    		Double outputVariable = null;
    
    		while (!scanner.hasNextDouble()) {
    			System.out.println("That's not a double please try again");
    		}
    		inputVariable = scanner.nextDouble();
    		outputVariable = TheOtherClass.doSomething(inputVariable);
    		System.out.println("Your output is " + outputVariable);
    	}
    }
    Class 2
    Java Code:
    public class TheOtherClass {
    
    	public static Double doSomething(Double inputVariable) {
    		return inputVariable + 1;
    	}
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    thanks for doing this lazy dolt's homework for him.

    Note that he's cross-posted this in several forums, in none of the posts has he shown a lick of work, and has been just waiting for a sucker to do this for him. He will go far in programming.

  6. #6
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    eh, i showed a pattern. the assignment is far from done

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by racerxadam View Post
    eh, i showed a pattern. the assignment is far from done
    You are right. I'm sorry for not reading it carefully. Your suggestions were right on the money.

Similar Threads

  1. [SOLVED] Best practice for creating test classes
    By mwildam in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 05:59 PM
  2. Creating inner classes
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:07 AM
  3. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-08-2008, 12:33 AM
  4. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •