Create a LinkList
| Previous | Home | Next |
Following program for create the LinkList
//First Program.
/*
* Save as a CirclePoints.java
* Program for Create a LinkList between CirclePoints.java, CircleDraw.java and CircleArea.java.
* First program in LinkList.
*/
package r4r.co.in;
public class CirclePoints implements java.io.Serializable {
public static void main(String[] args) {
//Declear the Co-ordinate of circle
double[][] coords = {{2.1, 8.4}, {3.4, -9.1}, {-6.4, 4.3}, {-9.0, -5.5}};
//Create the CircleDraw from the coords and display on the CircleDraw.java
//Maintain the second node in the LinkList.
CircleDraw circleDraw = new CircleDraw(coords);
//Show the points.
circleDraw.show();
//Add extra Co-ordinate point in the coords
circleDraw.addPoint(0.0, 0.0);
circleDraw.addPoint(1.1, 1.1);
//Display these points
circleDraw.show();
/*
* Maintain the Third node of LinkList
* Create the CircleArea from the radius and display on the CircleArea.java
* Radius
*/
float radius = (float) (100.00 * Math.random());
CircleArea circleArea = new CircleArea(radius);
circleArea.show();
}
}
//Second Program
/*
* Save as a CircleDraw.java
* Second program in LinkList.
*/
package r4r.co.in;
class CircleDraw {
LinkedList circleDrawLink; // The linked list of points.
public CircleDraw(double[][] coords) {
Point[] points = new Point[coords.length]; //make Array from the points.
// Create points from the Co-ordinates.
for (int j = 0; j < coords.length; j++) {
points[j] = new Point(coords[j][0], coords[j][1]);
}
// Create the circleDrawLink from the array[points].
circleDrawLink = new LinkedList(points);
}
// Construct a circleDrawLink from an array[points]
CircleDraw(Point[] points) {
circleDrawLink = new LinkedList(points);
}
void show() {
System.out.println("\n Co-Ordinate of Circle:");
//Read the Co-ordinates and return the First element.
Object nextPoint = circleDrawLink.getFirst();
while (nextPoint != null) {
//Display all points
System.out.println(nextPoint);
nextPoint = circleDrawLink.getNext(); // Get the next point.
}
}
// Add the extra point to Co-ordinate pair in the list.
void addPoint(double x, double y) {
circleDrawLink.addItem(new Point(x, y));
}
}
//Third program
/*
* Save as a CircleArea.java
* Third program in LinkList.
*/
package r4r.co.in;
class CircleArea {
LinkedList circleAreaList; //The LinkList of Area
float area, circumference;
CircleArea(float radius) {
//Calculate the area and circumference
area = (float) (3.14 * (radius * radius));
circumference = (float) (2 * 3.14 * radius);
// Create the circleAreaList from the area.
circleAreaList = new LinkedList(area);
}
void show() {
//Display the area and Circumference
System.out.println("\n Area of Circle:" + area + " sq. unit");
System.out.println(" Circumference of Area: " + circumference + " unit");
}
}
Co-Ordinate of Circle:
Point: 2.1,8.4
Point: 3.4,-9.1
Point: -6.4,4.3
Point: -9.0,-5.5Co-Ordinate of Circle:
Point: 2.1,8.4
Point: 3.4,-9.1
Point: -6.4,4.3
Point: -9.0,-5.5
Point: 0.0,0.0
Point: 1.1,1.1Area of Circle:6782.0215 sq. unit
Circumference of Area: 291.85986 unit
| Previous | Home | Next |