COS401a Compiler Theory

advertisement
Page 1
INF120 Basics in JAVA Programming
Homework 01
Elementary Programming
Don’t forget: Paper record to be presented before the end of next class
Write a program to solve the problem specified below:
 Problem. (geometry) Given two points with 2-dimensional coordinates (x1,y1) and
(x2,y2). Write a program to compute two result values as follows:
 The distance between the points. Formula for computing the distance is
2
√(𝑥2 − 𝑥1 ) 2 + (𝑦2 − 𝑦1 )2
Note: you can use sqrt(val) or pow(val, 0.5) to compute square root of val.
 The slope of the line that connects the points. Formula to compute the slope is
defined as the following expression: (y2-y1)/(x2-x1)
Instructions for program implementation: All the solution to be capsulated as
contents of the main() method.
The program to be implemented in two variants:
1. Write a Java console application that prompts the user to enter 2D coordinates of two
points (x1,y1) and (x2,y2) and displays two result values: the distance between the
points and the slope of the line that connects the points. Use instance of the
Scanner class for input. Use methods System.out.print()/println()
for output. Comment lines to be inserted.
2. Write a Java dialog box application that prompts the user to enter 2D coordinates of
two points (x1,y1) and (x2,y2) and displays two result values: the distance between
the points and the slope of the line that connects the points. Use JOptionPane
class
method
showInputDialog()
for
input
and
method
showMessageDialog() for output. Comment lines to be inserted.
Prepare, please the paper record using the following template for the header page:
INF120 Basics in JAVA Programming
Student: Ivan Petrov
Homework No 01
Task1: text of the task copied/pasted
Download