Question: You will write a program that asks a user for two (x, y) coordinates. Your program will calculate and print the equation between the coordinates

You will write a program that asks a user for two (x, y) coordinates. Your program will calculate and print the equation between the coordinates in slope-intercept form. You will then plot the two points on a graph. Finally, you will ask the user if they want to repeat the process with a different set of points. You should allow the user to enter as many sets of points as they want until they enter that they don't want to go again. In Java.

I have written this code so far and i have some error with printing the correct graphing , So would you look at it and let me know where is the problem .!

import java.util.*;

import java.text.*;

/*

* The problems are the grame are not accurate and in the case when x1 = x2 and when y2 = y1 it is not printing very well.

*/

public class sep19 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

DecimalFormat df = new DecimalFormat("#0.00");

char y = 'y';

while (y == 'y') {

System.out.print("Enter the bound for the grid (1-15): ");

int bound = Integer.parseInt(s.nextLine());

while (bound < 1 || bound > 15) {

System.out.print("Reenter the bound for the grid (1-15): ");

bound = Integer.parseInt(s.nextLine());

}

System.out.println();

System.out.print("Enter x1: ");

int x1 = Integer.parseInt(s.nextLine()); // X1

while (x1 < 0 || x1 > bound) {

System.out.println("Error: coordinates must be between 0 and " + bound);

System.out.print("Reenter x1: ");

x1 = Integer.parseInt(s.nextLine());

}

System.out.print("Enter y1: "); // Y1

int y1 = Integer.parseInt(s.nextLine());

while (y1 < 0 || y1 > bound) {

System.out.println("Error: coordinates must be between 0 and " + bound);

System.out.print("Enter y1: ");

y1 = Integer.parseInt(s.nextLine());

}

System.out.print("Emter x2: ");// X2

int x2 = Integer.parseInt(s.nextLine());

while (x2 < 0 || x2 > bound) {

System.out.println("Error: coordinates must be between 0 and " + bound);

System.out.print("Enter x2: ");

x2 = Integer.parseInt(s.nextLine());

}

System.out.print("Enter y2: "); // Y2

int y2 = Integer.parseInt(s.nextLine());

while (y2 < 0 || y2 > bound) {

System.out.println("Error: coordinates must be between 0 and " + bound);

System.out.print("Enter y2: ");

y2 = Integer.parseInt(s.nextLine());

}

System.out.println();

double slope;

for (int i = bound; i >= 1; i--) { // rows

if ((y1 == i && x1 == 0) || (y2 == i && x2 == 0)) {

if (i < 10) {

System.out.print(" ");

System.out.print(i + " *");

} else if (i >= 10) {

System.out.print("");

System.out.print(i + " *");

}

} else {

if (i < 10) {

System.out.print(" ");

System.out.print(i + " |");

} else if (i >= 10) {

System.out.print("");

System.out.print(i + " |");

}

}

int count = 1;

for (int j = 2; j <= 2 * bound; j += 2) {

if ((i == y1 && count == x1) || (i == y2 && count == x2)) {

System.out.print(" *");//

} else {

System.out.print(" "); //

}

count++;

}

System.out.println();

}

System.out.print(" 0 ");

for (int i = 0; i <= bound; i++) {

if ((y1 == 0 && x1 == i) || (y2 == 0 && x2 == i)) {

System.out.print("*");

} else {

if (i < 10) {

System.out.print("---");

} else if (i >= 10) {

System.out.print("---");

}

}

}

System.out.println();

System.out.print(" ");

for (int i = 0; i <= bound; i++) {

if (i < 10) {

System.out.print(" " + i);

} else if (i >= 10) {

System.out.print(" " + i);

}

}

System.out.println();

if (x1 == x2) {

System.out.println(" Slope is undefined ");

} else {

slope = ((y2 - y1) / (x2 - x1));

System.out.println(" y = " + (df.format(slope)) + "x + " + (df.format(y1 - slope * x1)));

}

System.out.print(" Do you want to play another round? (y/n) ");

y = (s.nextLine()).charAt(0);

System.out.println();

} // while end

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!