CS 201 - Computer Science I - Fall 2004
Lab 1 - Variables, Assignments, and Expressions
Loyola College >
Department of Computer Science >
CS 201 >
Labs >
Lab 1
Due
Monday, September 20 at 11:59pm.
Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Write-up and print out due Wednesday at the beginning of class.
Objectives
- to perform computations using assignment statements and expressions
- to choose appropriate types for variables
- to apply Java's rules of type and precedence
- to use output statements to display results
Reading
Koffman and Wolz, sections 2.1 and 2.2
Examples from lecture
Assignment - Part 1
Enter and run the following Java program.
// file: LineCalculator
// by: Jim Glenn
// date: 8/26/2003
// version: 0.1
// Determines the slope-intercept equation of a line given by two
// points.
public class LineCalculator
{
// Outputs the equation of the line through the points
// (x1, y1) and (x2, y2). Change the assignments to x1, y1, x2, and y2,
// recompile, and rerun the program to use for different pairs of
// points.
//
// @param args ignored
public static void main(String[] args)
{
// the coordinates of the two points
int x1, y1, x2, y2;
x1 = 5;
y1 = 11;
x2 = 2;
y2 = 5;
// compute the slope and intercepts of the line through the points
double slope, intercept;
slope = y2 - y1 / x2 - x1;
intercept = y1 - x1 * slope;
// output the slope and intercept of the line
System.out.println(slope);
System.out.println(intercept);
}
}
The program is supposed to determine the equation of the line between
the points (5, 11) and (2, 5). After running the program, it should
be evident that there are errors in it. Find and fix those errors
by doing the following.
- Change the name of the class to LineCalculator<Last Name 1><Last Name 2>
- Determine the correct slope and intercept by performing the calculations
by hand.
- Determine what the Java program is actually doing by using the rules to evaluate expressions for the expressions in the second section of code in
main. Write a number over each operator to indicate the order.
- Change the code so that the expressions are computed correctly.
- Change the program so it starts with points (2, 1) and (4, 2). Run the
program and check whether the output is correct.
- If the output is not correct, determine the source of the error
by computing the expressions as a Java program would: start by examining order of operations and compute the result step by step (hint: be very careful
with what 1 / 2 is in Java).
- Fix the error you discovered in the previous step. There are at least
two ways to fix this error; one is better than the other. (Hint: what
restrictions have we put on the values of x1, y1,
x2, and y2? Are those restrictions appropriate?)
- Extra Credit: The program will still fail on some pairs of points. Find such a
pair of points. (Do not attempt to correct this error.)
Once you have fixed the first two errors,
make the output more informative. Using a single
System.out.println statement, make the output of the program
look like
The equation of the line is y = 2.0x + 1.0
Assignment - Part 2
Write a program called PerpendicularBisector<Last Name 1><Last Name 2>
that outputs the equation of the perpendicular bisector
of the line segment between two points. Your program should
- begin with the declaration and initialization of four variables for
the coordinates of the two points (try the points (0, 0) and (2, 2));
- compute the slope of the line between those two points;
- compute the coordinates of the midpoint of the line segment
between the two points by averaging the two x coordinates and the two y
coordinates;
- compute the slope of the perpendicular bisector by taking the
negative reciprocal of the slope of the line segment;
- compute the intercept of the perpendicular bisector (you now have
the slope m
of the bisector and a point (xmid, ymid)
on the bisector; the intercept is ymid - m xmid);
and
- output the formula of the perpendicular bisector.
Your program should be well formatted and commented. Your choice of variable
names should be appropriate and your output should be user friendly. (Hint: You may start with the first program and make modifications to it.)
Test your program to make sure it works on different pairs of points.
However, as for the program in part 1, there will be
some pairs of points for which you can't make your program work
(at least not until later in the semester). Think about
what points will cause your program to fail.
Individual Write-Up
In this write-up you will describe each line of code in the perpendicular bisector program in terms of syntax and semantics, excluding comments. To begin, write an introduction that describes the program. When describing your code, identify the class and method headers. You should explain your choice of types for your variables. Also, write out in normal mathematical formulation each of the formulas you use to calculate the Perpendicular Bisector. In the conclusion, include any comments or concerns you have pertaining to the lab.
This write up will be graded for quality of writing and use of technical terms.
Submissions
Submit the source code (.java file) for your corrected
LineCalculator<Last Name 1><Last Name 2> class and your PerpendicularBisector<Last Name 1><Last Name 2>
class using this webpage. Also, turn in a print out of your code as well as your write-ups by September 22 at the beginning of class.
Grading
- Attendance in lab (5 pts)
- Code compiles and runs (10 pts)
- Code produces the expected output (10 pts)
- Followed naming conventions (10 pts)
- Program is formatted correctly (5 pts)
- Program includes sufficient comments (10 pts)
- Write-up (50 pts)
Pairs for this lab
Section 1
Section 2