Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions