APLine free response answer 4

The APLine problem from the 2010 AP Computer Science Exam is unusually easy. The problem requires you to write a simple class in response to a prompt that contains client code.

Review the APLine free response problem with AP CS Tutor Brandon Horn.

APLine

public class APLine
{
 private int a, b, c;

 public APLine(int a, int b, int c)
 {
  this.a = a;
  this.b = b;
  this.c = c;
 }

 public double getSlope()
 {
  return -(a / ((double) b));
 }

 public boolean isOnLine(int x, int y)
 {
  return a * x + b * y + c == 0;
 }
}

Get AP CS Help

2010 AP CS Exam Free Response Solutions

Recommended Practice Problems

4 thoughts on “APLine free response answer

  1. Reply Soroush Antikchi Apr 15,2012 4:08 pm

    what is the this. for ?

    • Reply Brandon Horn Apr 15,2012 6:46 pm

      The instance fields have the same names as the parameters to the constructor.

      this.a refers to the instance field.

      a refers to the parameter.

      this.a = a assigns the value of the parameter to the instance field.

  2. Reply Pushkin Abbott Apr 30,2014 9:51 pm

    Another one . When you casted (double) to the slope , why did you just cast it to b ?
    Thanks in advance .

    • Reply Brandon Horn May 1,2014 9:39 am

      When both operands are integers the / operator performs integer division. When either operand is a double the operator performs double division.

Leave a Reply