Assignment 1 - Thomy-G/BIU_OOP_2025B GitHub Wiki

Objects, Geometry, and Abstract Art

In this assignment, you will start working with Objects. You will define Classes and instantiate objects of those classes. The code in this assignment will be used in later assignments.

First, please read about working with multiple classes: MultFiles. (You can ignore the mentioned biuoop-1.4.jar for now, we will explain about it in Part 2.).

We supply this build.xml for the assignment. Recommendation: Read the entire assignment before you start writing it.

Important Note

In this course, your plan should simply work. Just like real programmers, you need to think about odd edge cases that might occur - nothing should cause your program to crash. It is your responsibility to deal with these edge cases.

Open your mind - in this assignment (and the following assignments), we provide you with some classes and methods which are obligatory in your implementation; but it is very likely that you need to implement other classes and methods to complete the assignment. Good design is key to your success in these assignments.

Signatures of methods must not be changed!

Don't forget JAVADOC

Part 1: Geometry

The goal of this part is to develop a program to reason about points, lines, rectangles, and their intersection.

You will define two classes: Point and Line.

We suggest that you work incrementally -- whenever you complete a method, see that everything compiles and that the method works as expected.

1.1 Point

A point has an x and a y value, and can measure the distance to other points, and if it is equal to another point.

public class Point {
   // constructor
   public Point(double x, double y) { }
 
   // distance -- return the distance of this point to the other point
   public double distance(Point other) { }
   
   // equals -- return true is the points are equal, false otherwise
   public boolean equals(Point other) { }

   // Return the x and y values of this point
   public double getX() { }
   public double getY() { }
}

Remember that the distance between two points (x1,y1) and (x2,y2) is the square root of: ((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)). You can use the Math.sqrt method to get the square root of a number in Java:

double root_of_13 = Math.sqrt(13);

1.2 Line

A line (actually a line-segment) connects two points -- a start point and an end point. Lines have lengths, and may intersect with other lines. It can also tell if it is the same as another line segment.

public class Line {
   // constructors
   public Line(Point start, Point end) { }
   public Line(double x1, double y1, double x2, double y2) { }
   
   // Return the length of the line
   public double length() { }

   // Returns the middle point of the line
   public Point middle() { }
   
   // Returns the start point of the line
   public Point start() { }
   
   // Returns the end point of the line
   public Point end() { }

   // Returns true if the lines intersect, false otherwise
   public boolean isIntersecting(Line other) { }

   // Returns true if this 2 lines intersect with this line, false otherwise
   public boolean isIntersecting(Line other1, Line other2) { }
  
   // Returns the intersection point if the lines intersect,
   // and null otherwise.
   public Point intersectionWith(Line other) { }

   // equals -- return true if the lines are equal, false otherwise
   public boolean equals(Line other) { }

} 

The intersectionWith(Line other) method is a bit complicated.

There are 2 main ways to go about it, you could use vector math or linear equations.

You can find some hints about how to calculate the intersection between two line segments using linear equations here.

Although you can not use the Line2D class included in the java standard library you can still look at how they implement their line intersection logic in the OpenJDK, this code is production code and isn't intended for everyone to understand at a glance, but it might be helpful for understanding how production java code looks.

Testing your code

You can perform some sanity-checks for your code using our provided GeometryTester class.

What to submit?

For this part, you need to submit the code for the Point and Line classes (and any additional classes you may write).

Note that no run target in the build.xml correspond to this task.

Notes & Edge cases

  • If there are infinite intersection points (including cases of inclusion): 'intersectionWith' function will return null 'isIntersecting' function will return true

  • Lines are defined as equal if they are the same visually - that is, even if the starting point of one line is the end of another line & vice versa, they are considered equal.

  • As you learned in Introduction To Computer Science, computers' representation of numbers are finite and thus not completely accurate. Therefore, it is common to compare doubles using a threshold. You can read more about it in google, and see the usage of it in the GeometryTester class.

Part 2: GUI and Abstract Art

In this part, we provide you with some code (the GUI class) that handles drawing graphics to a window (The name GUI stands for Graphical User Interface). You do not need to understand how the GUI class works, but you do need to understand how to use it.

Here is a simple program that uses the GUI class:

import biuoop.GUI; 
import biuoop.DrawSurface;

import java.util.Random;
import java.awt.Color;

public class SimpleGuiExample {

  public void drawRandomCircles() {
    Random rand = new Random(); // create a random-number generator
    // Create a window with the title "Random Circles Example"
    // which is 400 pixels wide and 300 pixels high. 
    GUI gui = new GUI("Random Circles Example", 400, 300);
    DrawSurface d = gui.getDrawSurface();
    for (int i = 0; i < 10; ++i) {
       int x = rand.nextInt(400) + 1; // get integer in range 1-400
       int y = rand.nextInt(300) + 1; // get integer in range 1-300
       int r = 5*(rand.nextInt(4) + 1); // get integer in 5,10,15,20
       d.setColor(Color.RED);
       d.fillCircle(x,y,r);
    }   
    gui.show(d);
  }

  public static void main(String[] args) {
     SimpleGuiExample example = new SimpleGuiExample();
     example.drawRandomCircles();
  }
}

This code is using packages. In particular, it is using the biuoop package which we provide, as well as classes from the java.util and java.awt packages which are part of the java runtime environment (and are always available).

The GUI, DrawSurface and Sleeper classes belong to the biuoop package, which are defined in the biuoop-1.4.jar file. In order to use this code, Java needs to know where to look for it. After you download the biuoop-1.4.jar file and put it in the directory of the assignment (not in the src), Compile and run the code using:

> javac -cp biuoop-1.4.jar:src src/SimpleGuiExample.java -d bin
> java -cp biuoop-1.4.jar:bin SimpleGuiExample

See MultFiles for an explanation.

Alternatively, you can run the following ant commands with the provided build.xml.

> ant compile
> ant run-gui-example 

Make sure you are able to compile and run this code before you proceed.

Explaining the code example

The java.awt.Color class is a part of the Java JDK, and is used to represent colors. In the example, we used a predefined color (RED), which is defined in a static field of the java.awt.Color class. There are also constructors for creating colors based on proportions of red, green and blue components. See the java.awt.Color documentation for further details.

The biuoop.GUI and biuoop.DrawSurface classes are part of the biuoop package that we supply. Creating a new GUI object creates a screen, with a title and dimensions. In order to draw on the screen, you ask the GUI object for a DrawSurface. The DrawSurface has several methods that you can use to draw lines, circles and so on.
Once you are done drawing, you call the show(DrawingSurface d) method of the GUI object with your DrawingSurface, and the drawing is displayed on the screen.

Sleeper is also a part of the biuoop package. This object allows us to halt the program execution for a specified number of milliseconds.

For further details, see the documentation for the biuoop package.

Note: This documentation is generated using JavaDoc.

Your Task

Your goal is to use the GUI class to generate random pictures like the following:

lines intersection

In this image, we have 10 lines, drawn in black. The middle point in each line is indicated in blue, while the intersection points between the lines are indicated in red (the points are filled circles with a radius of 3), and the parts of the line that create the triangles between the intersection point drawn in green.

Modify the example code above to generate pictures such as this one.

Hints:

  • You can draw lines with the drawSurface.drawLine(x1, y1, x2, y2) method.
  • Use the Point and Line classes from the previous part.
  • You probably want to keep an array of the previously drawn lines.
  • You may find it helpful to define and use helper methods such as Line generateRandomLine() and void drawLine(Line l, DrawSurface d).

If your intersection points are not correct, you may want to debug the Line class some more!

What to submit?

For this task, you should include all the relevant classes. The run2 target in the build.xml would run the drawing code, which should reside in a class called AbstractArtDrawing.

(all tasks, including this one, will be submitted in the same zip file.) In the final submission, you should have 10 lines, and the screen size should be 800x600.

Notes & Edge cases

  • All notes regarding the previous section still hold.
  • Remember to use a threshold to compare doubles; your solution should treat 4.9999999 & 5 as equals.
  • An intersection point at the end points of a line is still considered an intersection point.
  • Your code must draw 10 lines.
  • The GUI generated by AbstractArtDrawing should be 800x600 pixels size.

A note on checkstyle

For this assignment, because you do not know better yet, it is OK to have the checkstyle error:

Definition of 'equals()' without corresponding definition of 'hashCode()'.

Summary

In addition to the build.xml file, your final submission should include (at least) the following classes (in the src directory), with at least the functionality and methods as described above.

  • Point
  • Line
  • AbstractArtDrawing

Your build.xml includes the targets compile to compile everything, clean to delete the bin folder, check to run the checkstyle and run-gui-example, run1, run2, as described above.

You do not need to include the file biuoop.jar, and can assume it is available in the same directory as build.xml.

**Make sure you submit the assignment according to the submission instructions. ** **Run the task on the university's servers (for example, with Mobax) and on the CMD and make sure that it runs with the running command outside your workspace with the folder you're actually submitting. **

Several End Notes

  • Open your mind - in this assignment (and others to come), we provide you with some classes and methods that are obligatory in your implementation, but it is very likely that you need to implement other classes and methods to make the job right. Good design is a key to your success in the course assignments.
  • You are welcome to search the web for technical issues, mathematical or programming questions, and pretty much everything. Use it. Google will be your mentor for all your professional life. (The only thing you can't do is copy code from published solutions, but you can re-write the same ideas in your own code).
  • As (almost) all the following assignments will be built on top of this one (especially the Line and Ball classes), it is important you test them as much as you can. You'll get feedback on the assignment, but a 100 score does not guarantee that you don't have bugs, which you might only be aware of during the following assignments.
  • You are not allowed to use the java.awt.geom.Line2D class.
  • Think about which cases can be problematic in your code and how you deal with them

Good Luck!