This site requires JavaScript, please enable it in your browser!
Greenfoot back
DPD4AU
DPD4AU wrote ...

2013/1/30

Slope Program

DPD4AU DPD4AU

2013/1/30

#
I'm trying to create a program that receives user input for x2, x1, y2, y1 variables to calculate slope. Here's what I've got so far, can't figure out how to display undefined with error coming up...
import java.util.Scanner;

public class SlopeIntercept
{
   public static void main(String[] args)
   {
   
      int x1;
      int y1;
      int x2;
      int y2;
      double slope; 
      double undefined;
      double yIntercept;
      double xIntercept;
      
      Scanner userInput = new Scanner(System.in);
      
      System.out.print("Enter the X and Y coordinates of starting point:");
      System.out.print("\n\tx1" + " = ");
      x1 = userInput.nextInt();
      System.out.print("\ty1" + " = ");
      y1 = userInput.nextInt();
      
      
      System.out.print("Enter the X and Y coordinates of ending point:");
      System.out.print("\n\tx2" + " = ");
      x2 = userInput.nextInt();
      System.out.print("\ty2" + " = ");
      y2 = userInput.nextInt();
      
      System.out.print("Slope:" + slope);
      if (x2 == x1);
      {
         System.out.print("Slope: undefined");
      }
      
      
      yIntercept = y1 - (slope * x1);
      yIntercept = y2 - (slope * x2);
      System.out.print(yIntercept);
      
      xIntercept = -1 * (yIntercept / slope);
      System.out.print(xIntercept);
      
   }

}
danpost danpost

2013/1/30

#
Are you programming for Android or somthing like that? or, are you trying to use this code in Greenfoot?
DPD4AU DPD4AU

2013/1/30

#
Nah not in greenfoot, but in jGRASP, I figured you guys could help out with java though, i figured out the code:
import java.util.Scanner;
/**
 * User types an input into program and the program
 * calculates slope, y intercept and x intercept.
 *
 * @author Peyton Davis
 * published 1-28-2013
 */
public class SlopeIntercept
{
   /**
    * User types an input into program and the program
    * calculates slope, y intercept and x intercept.
    *
    * @param args standard output
    */

   public static void main(String[] args)
   {
   
      double x1;
      double y1;
      double x2;
      double y2;
      double slope; 
      double yIntercept;
      double xIntercept;
      
      Scanner userInput = new Scanner(System.in);
      
      System.out.print("Enter the X and Y coordinates of starting point:");
      System.out.print("\n\tx1" + " = ");
      x1 = userInput.nextInt();
      System.out.print("\ty1" + " = ");
      y1 = userInput.nextInt();
      
      
      System.out.print("Enter the X and Y coordinates of ending point:");
      System.out.print("\n\tx2" + " = ");
      x2 = userInput.nextInt();
      System.out.print("\ty2" + " = ");
      y2 = userInput.nextInt();
      
      slope = (y2 - y1) / (x2 - x1);
      
      if (x1 == x2) 
      {
         System.out.print("Slope: \"undefined\"");
      }
      else 
      {
         System.out.print("Slope: " + slope);
      }
      
      yIntercept = y1 - (slope * x1);
      yIntercept = y2 - (slope * x2);
      System.out.print("\nY intercept: " + yIntercept);
      
      xIntercept = -1 * (yIntercept / slope);
      if (slope == 0) 
      {
         System.out.print("\nX intercept: \"undefined\"");
      }
      else 
      {
         System.out.print("\nX intercept: " + xIntercept);
      }

      
   }

}
You need to login to post a reply.