Skip to content

Commit 42d4586

Browse files
committed
init hw 3
1 parent ac8aa9c commit 42d4586

File tree

5 files changed

+266
-0
lines changed

5 files changed

+266
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/******************************************************************************
2+
* Compilation: javac Point.java
3+
* Execution: java Point
4+
* Dependencies: none
5+
*
6+
* An immutable data type for points in the plane.
7+
* For use on Coursera, Algorithms Part I programming assignment.
8+
*
9+
******************************************************************************/
10+
11+
import java.util.Comparator;
12+
import edu.princeton.cs.algs4.StdDraw;
13+
14+
public class Point implements Comparable<Point> {
15+
16+
private final int x; // x-coordinate of this point
17+
private final int y; // y-coordinate of this point
18+
19+
/**
20+
* Initializes a new point.
21+
*
22+
* @param x the <em>x</em>-coordinate of the point
23+
* @param y the <em>y</em>-coordinate of the point
24+
*/
25+
public Point(int x, int y) {
26+
/* DO NOT MODIFY */
27+
this.x = x;
28+
this.y = y;
29+
}
30+
31+
/**
32+
* Draws this point to standard draw.
33+
*/
34+
public void draw() {
35+
/* DO NOT MODIFY */
36+
StdDraw.point(x, y);
37+
}
38+
39+
/**
40+
* Draws the line segment between this point and the specified point
41+
* to standard draw.
42+
*
43+
* @param that the other point
44+
*/
45+
public void drawTo(Point that) {
46+
/* DO NOT MODIFY */
47+
StdDraw.line(this.x, this.y, that.x, that.y);
48+
}
49+
50+
/**
51+
* Returns the slope between this point and the specified point.
52+
* Formally, if the two points are (x0, y0) and (x1, y1), then the slope
53+
* is (y1 - y0) / (x1 - x0). For completeness, the slope is defined to be
54+
* +0.0 if the line segment connecting the two points is horizontal;
55+
* Double.POSITIVE_INFINITY if the line segment is vertical;
56+
* and Double.NEGATIVE_INFINITY if (x0, y0) and (x1, y1) are equal.
57+
*
58+
* @param that the other point
59+
* @return the slope between this point and the specified point
60+
*/
61+
public double slopeTo(Point that) {
62+
/* YOUR CODE HERE */
63+
}
64+
65+
/**
66+
* Compares two points by y-coordinate, breaking ties by x-coordinate.
67+
* Formally, the invoking point (x0, y0) is less than the argument point
68+
* (x1, y1) if and only if either y0 < y1 or if y0 = y1 and x0 < x1.
69+
*
70+
* @param that the other point
71+
* @return the value <tt>0</tt> if this point is equal to the argument
72+
* point (x0 = x1 and y0 = y1);
73+
* a negative integer if this point is less than the argument
74+
* point; and a positive integer if this point is greater than the
75+
* argument point
76+
*/
77+
public int compareTo(Point that) {
78+
/* YOUR CODE HERE */
79+
}
80+
81+
/**
82+
* Compares two points by the slope they make with this point.
83+
* The slope is defined as in the slopeTo() method.
84+
*
85+
* @return the Comparator that defines this ordering on points
86+
*/
87+
public Comparator<Point> slopeOrder() {
88+
/* YOUR CODE HERE */
89+
}
90+
91+
92+
/**
93+
* Returns a string representation of this point.
94+
* This method is provide for debugging;
95+
* your program should not rely on the format of the string representation.
96+
*
97+
* @return a string representation of this point
98+
*/
99+
public String toString() {
100+
/* DO NOT MODIFY */
101+
return "(" + x + ", " + y + ")";
102+
}
103+
104+
/**
105+
* Unit tests the Point data type.
106+
*/
107+
public static void main(String[] args) {
108+
/* YOUR CODE HERE */
109+
}
110+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.algs4.hw3;
2+
3+
public class BruteCollinearPoints
4+
{
5+
6+
public static void main(String[] args)
7+
{
8+
// TODO Auto-generated method stub
9+
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.algs4.hw3;
2+
3+
public class FastCollinearPoints
4+
{
5+
6+
public static void main(String[] args)
7+
{
8+
// TODO Auto-generated method stub
9+
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.algs4.hw3;
2+
3+
public class LineSegment
4+
{
5+
6+
public static void main(String[] args)
7+
{
8+
// TODO Auto-generated method stub
9+
10+
}
11+
12+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.algs4.hw3;
2+
3+
/******************************************************************************
4+
* Compilation: javac Point.java
5+
* Execution: java Point
6+
* Dependencies: none
7+
*
8+
* An immutable data type for points in the plane.
9+
* For use on Coursera, Algorithms Part I programming assignment.
10+
*
11+
******************************************************************************/
12+
13+
import java.util.Comparator;
14+
import edu.princeton.cs.algs4.StdDraw;
15+
16+
public class Point implements Comparable<Point> {
17+
18+
private final int x; // x-coordinate of this point
19+
private final int y; // y-coordinate of this point
20+
21+
/**
22+
* Initializes a new point.
23+
*
24+
* @param x the <em>x</em>-coordinate of the point
25+
* @param y the <em>y</em>-coordinate of the point
26+
*/
27+
public Point(int x, int y) {
28+
/* DO NOT MODIFY */
29+
this.x = x;
30+
this.y = y;
31+
}
32+
33+
/**
34+
* Draws this point to standard draw.
35+
*/
36+
public void draw() {
37+
/* DO NOT MODIFY */
38+
StdDraw.point(x, y);
39+
}
40+
41+
/**
42+
* Draws the line segment between this point and the specified point
43+
* to standard draw.
44+
*
45+
* @param that the other point
46+
*/
47+
public void drawTo(Point that) {
48+
/* DO NOT MODIFY */
49+
StdDraw.line(this.x, this.y, that.x, that.y);
50+
}
51+
52+
/**
53+
* Returns the slope between this point and the specified point.
54+
* Formally, if the two points are (x0, y0) and (x1, y1), then the slope
55+
* is (y1 - y0) / (x1 - x0). For completeness, the slope is defined to be
56+
* +0.0 if the line segment connecting the two points is horizontal;
57+
* Double.POSITIVE_INFINITY if the line segment is vertical;
58+
* and Double.NEGATIVE_INFINITY if (x0, y0) and (x1, y1) are equal.
59+
*
60+
* @param that the other point
61+
* @return the slope between this point and the specified point
62+
*/
63+
public double slopeTo(Point that) {
64+
/* YOUR CODE HERE */
65+
return Double.NaN;
66+
}
67+
68+
/**
69+
* Compares two points by y-coordinate, breaking ties by x-coordinate.
70+
* Formally, the invoking point (x0, y0) is less than the argument point
71+
* (x1, y1) if and only if either y0 < y1 or if y0 = y1 and x0 < x1.
72+
*
73+
* @param that the other point
74+
* @return the value <tt>0</tt> if this point is equal to the argument
75+
* point (x0 = x1 and y0 = y1);
76+
* a negative integer if this point is less than the argument
77+
* point; and a positive integer if this point is greater than the
78+
* argument point
79+
*/
80+
public int compareTo(Point that) {
81+
/* YOUR CODE HERE */
82+
if (this.y <= that.y && this.x < that.x)
83+
return -1;
84+
else if (this.x == that.x && this.y == that.y)
85+
return 0;
86+
else
87+
return 1;
88+
}
89+
90+
/**
91+
* Compares two points by the slope they make with this point.
92+
* The slope is defined as in the slopeTo() method.
93+
*
94+
* @return the Comparator that defines this ordering on points
95+
*/
96+
public Comparator<Point> slopeOrder() {
97+
/* YOUR CODE HERE */
98+
return null;
99+
}
100+
101+
102+
/**
103+
* Returns a string representation of this point.
104+
* This method is provide for debugging;
105+
* your program should not rely on the format of the string representation.
106+
*
107+
* @return a string representation of this point
108+
*/
109+
public String toString() {
110+
/* DO NOT MODIFY */
111+
return "(" + x + ", " + y + ")";
112+
}
113+
114+
/**
115+
* Unit tests the Point data type.
116+
*/
117+
public static void main(String[] args) {
118+
/* YOUR CODE HERE */
119+
}
120+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy