This lab is a review of Java. It involves all Java concepts learned in pre-requi
ID: 3877808 • Letter: T
Question
This lab is a review of Java. It involves all Java concepts learned in pre-requisites classes. Do not use standard library datastructures (Like ArrayList or LinkedList) available via import but build all datastructures from scratch using primitive data types and arrays as needed. Use contructs and techniques to maximize the user friendliness of the interface while optimizing code. While not required, I would expect to see a full debug infrastructure...
It MUST:
1 - Write a java class called Point to represent a N-Dimensional point (With coordinates)
- The constructor should take any number of coordinates, and should have dimension available (Number of coordinates)
- The class should have accessor method for any coordinates
- Keep track of the number of points and every created point
- Write toString() and copy() and equals() helper methods
2 - Write a java class called Line to represent a line (with a starting point and an ending point)
- The constructor arguments are the start and end points
- The constructor MUST handle points with dissimilar dimensions by throwing an exception
- Write a toString() and copy() and equals() helper method
- Provide a getLineLength() method - Look up "Euclidian Distance" on wiki
- Keep track of the number of lines and every created line
3 - Write a tester class with main() to prompts and reads coordinates for 2 points from the user (CLI)
* Allow as many dimensions as the user wants for each point
* Allows abreviations for the random keyword and the exit keyword
* Allow user to request help
* Do not allow user errors to cause abnormal termination
- Allow the user to request a random value
- Created the 2 points
- Create a line with those two points
- Display the starting / ending points and line length
- Loop and repeat step 3 until user request to exit
4 - When the user exits display final statistics:
* Number of points created
* List all the points created
* Number of lines created
* List all the lines created
5 - Document test cases
NOTE: Pay attention to details!!!!!! You must use classes and helper methods and instanciate objects.
Example:
Enter point # 1 dimension # 1 or "Random" or "Exit" or "Help" or blank line to proceed: 3.14
Enter point # 1 dimension # 2 or "Random" or "Exit" or "Help" or blank line to proceed: 0
Enter point # 1 dimension # 3 or "Random" or "Exit" or "Help" or blank line to proceed:
Enter point # 2 dimension # 1 or "Random" or "Exit" or "Help" or blank line to proceed: 0
Enter point # 2 dimension # 2 or "Random" or "Exit" or "Help" or blank line to proceed: hEl
**************** CS lab 1 by Student *********************************
* This program allows you specify points in N-dimensional space:
- Each point can have different number of coordinate
- You may request a random number for any coordinate by typing "RANDOM"
- When you are finished entering the cordinate just press the <Enter> key
* Pairs of point are used to create a lines
- If the 2 points have mismatched dimensions and error will be shown
- When a line is created, the line distance is provided
* When you are done specifying points and lines type "EXIT" to display final operation statistics
* All key words are case insensitive and can be abreviated
* Random number will be scaled between -1,000.00 and +1,000.00
Enter point # 2 dimension # 2 or "Random" or "Exit" or "Help" or blank line to proceed: 2.71
Enter point # 2 dimension # 3 or "Random" or "Exit" or "Help" or blank line to proceed:
The distance between ( 3.14 , 0.0) and ( 0.0 , 2.71) is 4.147734321289154
Enter point # 1 dimension # 4 or "Random" or "Exit" or "Help" or blank line to proceed: Random
--> -75.1234
Enter point # 1 dimension # 5 or "Random" or "Exit" or "Help" or blank line to proceed:
Enter point # 2 dimension #1 or "Random" or "Help" or "Exit" or blank line to proceed: RA
--> 38.12851983534693
Enter point # 2 dimension #2 or "Random" or "Help" or "Exit" or blank line to proceed: rAnD
--> 74.31366638262983
Enter point # 2 dimension #3 or "Random" or "Help" or "Exit" or blank line to proceed:
Ouch - You tried to create a line with points of disimilar dimension!
Enter point # 1 dimension # 1 or "Random" or "Exit" or "Help" or blank line to proceed: EXIT
You created 4 points:
( 3.14 , 0.0 )
( 0.0 , 2.71 )
( -75.1234 )
( 38.12851983534693 , 74.31366638262983 )
You created 1 lines:
( 3.14 , 0.0 ) to ( 0.0 , 2.71 ) with length 4.147734321289154
Explanation / Answer
1. I have added two classes Point and Line , both have all required methods.
import java.util.Arrays;
import java.util.Iterator;
public class Test {
public static myList<Point> points = new myList<>();
public static myList<Line> lines = new myList<>();
public static void main(String[] args) {
Point a = new Point(1,2);
points.add(a);
Point b = new Point(1,2);
points.add(b);
Line l = new Line(a,b);
lines.add(l);
System.out.println("points created "+ points.length());
for(Point p : points) {
System.out.println(p);
}
System.out.println("lines created "+ points.length());
for(Line line : lines) {
System.out.println(line);
}
}
}
class Point {
float[] coordinates;
public Point(float... coordinates) {
this.coordinates = coordinates;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(coordinates);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (!Arrays.equals(coordinates, other.coordinates))
return false;
return true;
}
@Override
public String toString() {
return Arrays.toString(coordinates);
}
public int dimension() {
return coordinates.length;
}
public Point copy() {
return new Point(coordinates);
}
}
class Line {
Point start ;
Point end;
public Line(Point a , Point b) {
if(a.dimension()!=b.dimension()) {
throw new RuntimeException("unequal dimensions");
}
this.start =a;
this.end = b;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((end == null) ? 0 : end.hashCode());
result = prime * result + ((start == null) ? 0 : start.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Line other = (Line) obj;
if (end == null) {
if (other.end != null)
return false;
} else if (!end.equals(other.end))
return false;
if (start == null) {
if (other.start != null)
return false;
} else if (!start.equals(other.start))
return false;
return true;
}
public Line copy() {
return new Line(start,end);
}
public double getLineLength() {
double distanceSquareSum =0 ;
for(int i =0;i<start.dimension();i++) {
double dist = (start.coordinates[i]-end.coordinates[i]);
distanceSquareSum = distanceSquareSum+dist*dist;
}
return Math.sqrt(distanceSquareSum);
}
@Override
public String toString() {
return "start "+start+" end" + end;
}
}
class myList<T> implements Iterable<T> {
T[] list = (T[]) new Object[100];
int i = 0;
void add(T item) {
list[i] = item;
i++;
}
T get(int i) {
return list[i];
}
@Override
public Iterator<T> iterator() {
return new Iterator() {
@Override
public boolean hasNext() {
return list[i + 1] != null;
}
@Override
public T next() {
i++;
return list[i];
}
};
}
public int length() {
return i;
}
}
SAMPLE OUTPUT :
points created 2
lines created 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.