CS 630 - Computing Fundamentals I - Fall 2005
Homework 5


Loyola College > Department of Computer Science > CS 630 > Homework > Homework 5

Due

Monday, October 17th at the beginning of class

Problems

Exercise (p. 195)
4.1
2. Consider the following instantiable class.

class QuestionTwo {
   public final int A = 345;
   public int b;
   private double c;

   public QuestionTwo() {
      b = 0;
      c = 0;
   }

   private void methodOne(int a) {
      b = a;
   }

   public double methodTwo() {
      return 23;
   }
}
Identify invalid statements in the following main class. For each invalid statement, state why it is invalid. Although, the compiler can easily tell you the problem, the purpose of this (and all written assignments) is for you to determine the problems with the code to ensure that you understand the rules. Hint: There are at least 5 problems.
class Q2Main {
   public static void main(String [] args) {
      QuestionTwo = q2;
      q2 = new QuestionTwo();

      q2.A = 12;
      q2.b = 12;
      q2.c = 12;

      q2.methodOne(12);
      q2.methodOne();
      System.out.println(q2.methodTwo(12));
      q2.c = q2.methodTwo();
   }
}

Programming Projects


4.1 (pg. 195)

2-Dimensional Line

(myCodeMate DL0148)

Objectives

Introduction

A Java class definition contains the declarations of the data fields, which record the essential information about an object, the definitions of the constructors, which initialize the data fields, and the defintions of methods, which manipulate the data fields.

For a line class, the fields must record enough information to uniquely identify a line. There are different ways to do this: two points on a line are enough to distinguish it from other lines; a point and the slope will distinguish a line; and the slope and y-intercept will distinguish a line.

The class definition allows us to hide our choice of data fields from the users of our class. However, through the use of different versions of the constructor, users may be able to specify a line either by giving a slope and intercept or by giving two points on the line. The constructors would convert the information given as constructor arguments to the information stored in the data fields. For example, if we choose to implement our line class with slope and intercept fields, we can still allow users to specify lines by giving two points.

The methods must be written to reflect our choice of data fields. For example, writing a method that returns the slope of a line is as simple as returning the value in the appropriate data field if we choose slope and intercept as our fields. On the other hand, if we choose the coordinates of two points on the line as our data fields, then we would have to perform the "rise over run" calculation in order to compute the slope. Either way, though, the behavior is the same from the user's point of view. No matter how we implement our line class, the code

Line l = new Line(0, 0, 6, 3); // make a line through (0, 0) and (6, 3)
System.out.println(l.getSlope());
should produce the same results.

Recall that the equation of a line is y = mx + b where m is the slope and b is the intercept. Given the slope and a point (x,y), you can solve for the intercept. In addition you can calculate the slope from two points by substracting one y-value from another and dividing be the difference of the two x-values.

Assignment

Write a class called Line2D that can be used to represent non-vertical lines in the plane. Your class should contain

Your completed class should work with the following test driver. Download the driver and save it in the same directory as your Line2D class. When you have completed the Line2D class you can check for syntax errors by compiling that file. However, since Line2D does not have a main method, you cannot run it. Instead, open LineTest.java and select Compile > Compile and then Run > Run from the menu in its window.

Solution