News Ticker

Menu

Obligatory Objects In Java

Project Overview

Problem Sets

This project has 3 problems to complete. All of them involve creating new classes which do not exist in Java. The first two problems involve modeling simple linear equations of two variables using classes. This will introduce the idea of invariants: properties that should always be true about a class after one of its methods completes. The third problem revisits the queues of the previous project and implements them in a proper, Java-esque fashion as an object with associated methods. The honors problems build on this to improve the efficiency of the queue methods and makes it more flexible to allow for expansion.

Problem: Slope/Intercept Linear Equations

Good old Em ex plus Bee

A linear equation is in slope intercept form if it is written
where  and  are constants that do not change while  and  are variables that do change.

Define a class which models a linear equation in slope-intercept form. A template is provided for you in the file SlopeInterceptLE.java. You will need to add any fields that are required for the class to work properly and fill in definitions for the below methods.

Class Structure of SlopeInterceptLE

// A class to model linear equations of two variable (x and y) in
// slope intercept form.
public class SlopeInterceptLE {

  // Public constructor that accepts values for the slope m and
  // intercept b, defaults the value of x to 0.0 and sets y
  // appropriately.
  public SlopeInterceptLE(double m, double b);

 // Public constructor that accepts values for the slope m and
 // intercept b and initial value of x and sets y appropriately.
  public SlopeInterceptLE(double m, double b, double x);

  // Return the numeric value of the equation which is the numeric
  // quantity that should be on both left and right sides of the equal
  // sign.
  public double value();

  // Return the current value of x
  public double getX();

  // Return the current value of y
  public double getY();

  // Set the value of x and change y accordingly to preserve the
  // equation.
  public void setX(double x);

  // Set the value of $y$ and change $x$ accordingly to preserve the
  // equation.
  public void setY(double y);

  // Return a =String= version of the general form of the equation.
  // The pretty version's general format should be as follows.
  // 
  //   y = M.MM * x + B.BB
  //
  // M.MM is the slope with 2 digits of accuracy and B.BB is the
  // intercept with two digits of accuracy.  Look for a method of the
  // String class to assist with formatting.
  //
  // Examples:
  //    y = 1.23 * x + 122.41
  //    y = -1.23 * x + -122.41
  public String toString();

}

Sample Session

Welcome to DrJava.  Working directory is /...
> SlopeInterceptLE si = new SlopeInterceptLE(1.0,2.0)
> si
y = 1.00 * x + 2.00
> si.toString()
y = 1.00 * x + 2.00
> si.getX()
0.0
> si.getY()
2.0
> si.value()
2.0
> si.setX(4)
> si.value()
6.0
> si.getY()
6.0
> si
y = 1.00 * x + 2.00
> si.toString()
y = 1.00 * x + 2.00
> si = new SlopeInterceptLE(0.25, 10.25, 1.0);
> si.getX()
1.0
> si.getY()
10.5
> si
y = 0.25 * x + 10.25
> si.setY(4.5)
> si.getY()
4.5
> si.getX()
-23.0
> si
y = 0.25 * x + 10.25
> si.value()
4.5
> si = new SlopeInterceptLE(-0.25, -10.25, -1.0);
> si.toString()
y = -0.25 * x + -10.25
> si = new SlopeInterceptLE(0.67512, -1.999, -1.0);
> si.toString()
y = 0.68 * x + -2.00

Problem: General Form of Linear Equations

General LE

A linear equation is in general form if it is written
where  are constants that do not change while  and  are variables that do change.

Define a class which models a linear equation in general form. You will need to create the file GeneralLE.java and fill in the appropriate fields and methods.

Class Structure of GeneralLE

public class GeneralLE {

  // Public constructor that accepts values for the three constants a,
  // b, and c, as well as the variable x. This is the only public
  // constructor.
  public GeneralLE(double a, double b, double c, double x);

  // Return the numeric value of the equation which is the numeric
  // quantity that should be on both left and right sides of the equal
  // sign.
  public double value();

  // Return the current value of x
  public double getX();

  // Return the current value of y
  public double getY();

  // Set the value of x and update other values as appropriate to
  // preserve the equation.
  public void setX(double x);

  // Set the value of y and update other values as appropriate to
  // preserve the equation.
  public void setY(double y);

  // Return a String version of the general form of the equation.  The
  // pretty version's general format should be
  // 
  //   A.AA * x + B.BB * y = C.CC
  // 
  // A.AA, B.BB, and C.CC are the coefficients for the linear equation
  // with 2 decimal digits of accuracy. Examples:
  // 
  //   1.23 * x + 45.68 * y = 2.00
  //   -54.99 * x + -9.86 * y = 42.41
  public String toString();

  // Produce a version of this GeneralLE in slope intercept form.  The
  // values of x and y must be preserved but the coefficients should
  // be converted to slope intercept form. The resulting
  // SlopeInterceptLE is not connected to the generating GeneralLE in
  // any way: changes to one do not affect the other.
  public SlopeInterceptLE toSlopeInterceptLE();

}

Sample Session

Welcome to DrJava.  
> GeneralLE g = new GeneralLE(1, 10, 11, 1);
> g
1.00 * x + 10.00 * y = 11.00
> g.value()
11.0
> g.getX()
1.0
> g.getY()
1.0
> g.setX(5)
> g.getY()
0.6
> g.value()
11.0
> g.setY(5.5)
> g.getX()
-44.0
> g
1.00 * x + 10.00 * y = 11.00
> g.value()
11.0
> SlopeInterceptLE si = g.toSlopeInterceptLE();
> si
y = -0.10 * x + 1.10
> si.getX()
-44.0
> g.getX()
-44.0
> si.getY()
5.5
> g.getY()
5.5
> si.value()
5.5
> si
y = -0.10 * x + 1.10
> g.value()
11.0
> g
1.00 * x + 10.00 * y = 11.00
> g = new GeneralLE(1.999, -10.238, 11.2345, 1);
> g.toString()
2.00 * x + -10.24 * y = 11.23

Problem: Proper Integer Queues

A Better Queue than Last Time

We will revisit the notion of a queue that we introduced in our previous project. There will be two significant changes:

  • Queues this time will be built as a class with methods called ProperQueue instead of just using an array itself as the queue. There will still be an array inside of the objects of this class but it will be better encapsulated to hide details of the implementation.
  • We are storing Integer objects in the queue instead of primitive-type int values. This implies that null is a possible value in the array.

Sample Session

The following interactive session in DrJava demonstrates the core functionality of the ProperQueue.

Welcome to DrJava.
> ProperQueue q = new ProperQueue(5);
> q.toString()
""
> q

> q.getSize()
0
> q.getCapacity()
5
> q.isEmpty()
true
> q.isFull()
false

// add() returns true on a successful addition
> boolean result = q.add(20);
> result
true
> q.toString()
"20"
> q
20
> q.isEmpty()
false
> q.isFull()
false
> q.getSize()
1

// element() returns the first element without removing it
> q.element()
20
> q
20

// remove() returns the first element and removes it
> Integer first = q.remove();
> first
20
> q

> q.getSize()
0

// Attempting to remove() from an empty queue raises an exception
> first = q.remove();
java.lang.RuntimeException: Queue empty
        at ProperQueue.remove(ProperQueue.java:140)

// Attempting to poll() an empty queue returns null
> first = q.poll();
> first
null

// add() and offer() are identical when the queue has room
> q.add(15)
true
> q.offer(25)
true
> q.add(35)
true
> q.offer(45)
true
> q.add(55)
true

// q is now full (at capacity)
> q.getSize()
5
> q.getCapacity()
5
> q.isFull()
true

// add() on a full queue raises an exception without altering the queue
> q.add(65)
java.lang.RuntimeException: Queue full
        at ProperQueue.add(ProperQueue.java:91)

// offer() on a full queue returns false without altering the queue
> q.offer(65)
false
> q
15 25 35 45 55
> q.offer(65)
false
> q
15 25 35 45 55

// remove() and poll() are identical when the queue has elements
> first = q.remove()
15
> q
25 35 45 55
> first = q.poll()
25
> q
35 45 55

Order your solution now only for $10


Share This:

Hassnain

I'm Hassnain. A full time Java Developer. I enjoy to make modern projects. I love to create JavaFX, Java Swing GUI and write about Java Programming, OOP. Now I'm working with CS2IT. You can buy our projects from here.

No Comment to " Obligatory Objects In Java "

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM