home study engineering computer science questions and an

home / study / engineering / computer science / questions and answers / write a program in java language that asks the ...

Question: Write a program in java language that asks the use...

Bookmark

Write a program in java language that asks the user for two (x, y) coordinates then calculates and displays the equation between the coordinates in slope-intercept form. You will then plot the two points on a “graph”.

Additional requirements

• Your output should look EXACTLY like the example above when it runs (depending on the values of the coordinates). Your program will repeat until the user chooses to quit.

• Your program will create a NEW graph for each and require the user to enter NEW points for each execution. You are simply repeating the display shown above without the user having to re-run the program each time.

Round both the slope and y-intercept to two decimal places and display.

• Use nested loops to plot the two points. You may need to add extra if-statements for displaying the x-axis and y-axis.

• If you have two points (x1, y1) and (x2, y2), the slope is: (y2-y1 ) / (x2-x1) Then, using the slope and one of the points, the y-intercept is: y1 – slope*x1

• Display the line in slope-intercept form as shown in the example: y = slope*x + y-intercept

Include the following Data validation: Display a unique (i.e. specific) error message and loop until user enters in valid data if the user 1) enters coordinates that are not between (0,0) and (9,9); or 2) if the slope between the two points is undefined or 3) or enters the same point twice. By unique, one generic error messages must not be used (such as “error occurred”) but must be specific to the error that occurred. (All 3 data validations required)

Please only use loops (for loops, do-while loops, and while loops) and condtionals besides the basics becasue that is all I have learned and I want to be able to understand what is happening when I read over the code. Remember to please use java programming language, I will be compiling in BlueJ. Also, be sure to use only ONE class with ONE main method.

Thanks

Here is a sample run of the program: Oours will not be side-by-side. Done here to save space) Enter x1: 7 Enter x1:99 Enter y1: 2 Enter 1:2 Enter 2: 00 Enter 2:8 Enter y2:0 Enter 2:1 All points must be between (0,0) and (9,9). y a 0.29x +0.00 Re-enter X1:9 Re-enter 1:2 9 I Re-enter 2:88 Re-enter 2:1 All points must be between (0,0) and (9,9). Re-enter x1:9 Re-enter yl: 2 Re-enter x2:9 Re-enter 2:1 Line between points must have a slope (ie. x1 and x2 must differ) 0 1 2 3 4 5 6789 Re-enter x1:9 Re-enter yl: 2 Run program again? (y/n) y Re-enter x2:8 Re-enter x1:8 Re-enter 2:1 Re-enter y1:2 y 1.00x+-7.00 Re-enter 2:8 Re-enter y2: 2 9 I Point values must be different. Re-enter X1: 8 Re-enter 1:2 Re-enter 2:8 Re-enter y2: 1 Line between points must have a slope (ie. x1 andx2 must differ) 0123456789 Run program again? (y/n) n

Solution

slope.java

import java.util.Scanner;

public class slope
{

public static void main(String[] args)
{
       String s = \"\";
       while(true)
       {
   double x1,x2,y1,y2;
   Scanner scanner = new Scanner(System.in);
   System.out.println(s+ \"Enter x1: \");
   x1 = scanner.nextInt();
       System.out.println(s+ \"Enter y1: \");
   y1 = scanner.nextInt();
   System.out.println(s+ \"Enter x2: \");
   x2 = scanner.nextInt();
       System.out.println(s+ \"Enter y2: \");
   y2 = scanner.nextInt();

   if((x1 > 9 || x1 < 0) || (x2 > 9 || x2 < 0) || (y1 > 9 || y1 < 0) || (y2 > 9 || y2 < 0))
   {
       System.out.println(\"All points must be between (0,0) and (9,9) \");
       s = \"Re\";    
   }
   else if(x1 == x2 && y1 == y2)
   {
       System.out.println(\"Point values must be different\");        
       s = \"Re\";    
   }
   else if(x1 == x2)
   {
       System.out.println(\"Line betweeen points must have a slope\");            
       s = \"Re\";    
   }
   else
   {

       double m = (y2 - y1)*1.0/((x2 - x1)*1.0);
       double c = y2 - m*x2 ;
       System.out.println(\"Y = \" + m + \" X + \" + c);
       for (int i = 9; i>=0 ;i--)
       {
           if(i != y1 && i != y2)
           {
System.out.println(i + \"|\");
}
if( i == y1)
{
   System.out.print(i + \"|\");
for (int x = 0; x< x1 - 1 ; x++) {
                                   System.out.print(\" \");                                     
}
System.out.print(\"*\ \");
}
if( i == y2)
{
System.out.print(i + \"|\");
for (int x = 0; x< x2 - 1 ; x++) {
                                   System.out.print(\" \");                                     
}
System.out.print(\"*\ \");
}
       }
       for (int j = 0; j<=20 ;j++ )
       {
            System.out.print(\"*\");           
       }
       System.out.print(\"\ \");
       System.out.print(\" \");
       for (int j = 0; j<=9 ;j++ )
       {
            System.out.print(j + \" \");           
       }        
       System.out.println(\"\ Do you want to exit the program (1/0)?\");
   int in;
   in = scanner.nextInt();
   if(in == 1)
   {
   continue;
   }   
   else
   {
       System.exit(0);
   }
   }
   }
}

}

Sample Output:

Enter x1:
7
Enter y1:
2
Enter x2:
0
Enter y2:
0
Y = 0.2857142857142857 X + 0.0
9|
8|
7|
6|
5|
4|
3|
2| *
1|
0|*
*********************
0 1 2 3 4 5 6 7 8 9
Do you want to exit the program (1/0)?
1
Enter x1:
8
Enter y1:
2
Enter x2:
8
Enter y2:
2
Point values must be different
ReEnter x1:
8
ReEnter y1:
2
ReEnter x2:
8
ReEnter y2:
1
Line betweeen points must have a slope
ReEnter x1:
99
ReEnter y1:
2
ReEnter x2:
8
ReEnter y2:
1
All points must be between (0,0) and (9,9)
ReEnter x1:
9
ReEnter y1:
2
ReEnter x2:
88
ReEnter y2:
1
All points must be between (0,0) and (9,9)
ReEnter x1:
9
ReEnter y1:
2
ReEnter x2:
9
ReEnter y2:
1
Line betweeen points must have a slope
ReEnter x1:
9
ReEnter y1:
2
ReEnter x2:
8
ReEnter y2:
1
Y = 1.0 X + -7.0
9|
8|
7|
6|
5|
4|
3|
2| *
1| *
0|
*********************
0 1 2 3 4 5 6 7 8 9
Do you want to exit the program (1/0)?
0

home / study / engineering / computer science / questions and answers / write a program in java language that asks the ... Question: Write a program in java lan
home / study / engineering / computer science / questions and answers / write a program in java language that asks the ... Question: Write a program in java lan
home / study / engineering / computer science / questions and answers / write a program in java language that asks the ... Question: Write a program in java lan
home / study / engineering / computer science / questions and answers / write a program in java language that asks the ... Question: Write a program in java lan

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site