Java language Use null,data, head etc for the doubly linked list From Lab Assign
ID: 3587433 • Letter: J
Question
Java language Use null,data, head etc for the doubly linked list From Lab Assignment 3, you know that a sample file may look like the following one. 20 10 8 4.5 8.45 12.2 8.0 2.5 4.0 1.0 15.0 18.0 3.5 3.5 3.5 6.0 5.0 10.0 Each line contains the width, height and length of a box. The dimensions are separated by spaces. In Lab Assignment 3, you could read the input file twice. For Lab assignment 4, you can read the input file exactly once. Yes, sometimes clients can be very difficult. & The client is NOT allowing the use of any array to store Box objects, or java.util lists for this revised software. Assignment: You need to write a program using object oriented programming idea for boxes. That is, each box has to be considered an object. To achieve this, you must write a class named Box. As you already know, this client likes to keep each class in a separate Java file. Therefore, make sure to create a Java file for the Box class. Some other required properties of the Box class are as follows. 1. You are allowed to keep only one of the status variables public. The rest of the status variables of the Box class must be private 2. Write no more than two constructors. 3. The Box class must have a public method named getVolume () that will return the volume of the box 4. The Box class must have a public method named isCube( that will return true if the box is cubic, false otherwise. 5. The Box class must NOT contain any main method. Feel free to write any additional method in the Box class, as you see fit. The program file (the Java file that contains the main method) must be written in Runner.java. Runner must have the following functionalities. Each functionality must be implemented in a separate method in Runner.Explanation / Answer
/*
* Java Program to Implement Doubly Linked List
*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
/* Class Node */
class Box
{
private double width, height, length;
public int position;
protected Box next, prev;
/* Constructor */
public Box()
{
next = null;
prev = null;
width = 0;
height = 0;
length = 0;
position = 0;
}
/* Constructor */
public Box(double wid, double hi, double len)
{
width = wid;
height = hi;
length = len;
next = null;
prev = null;
position = 0;
}
/* Function to set link to next node */
public void setLinkNext(Box n)
{
next = n;
}
/* Function to set link to previous node */
public void setLinkPrev(Box p)
{
prev = p;
}
/* Funtion to get link to next node */
public Box getLinkNext()
{
return next;
}
/* Function to get link to previous node */
public Box getLinkPrev()
{
return prev;
}
/* Funtion to get width */
public double getWidth()
{
return width;
}
/* Function to get height */
public double getHeight()
{
return height;
}
/* Function to get length */
public double getLength()
{
return length;
}
/* Function to set width */
public void setWidth(double n)
{
width = n;
}
/* Function to set height */
public void setHeight(double n)
{
height = n;
}
/* Function to set length */
public void setLength(double n)
{
length = n;
}
public double getVolume()
{
double volume;
volume = length * height * width;
return volume;
}
public boolean isCube()
{
if(length == height && height == width)
return true;
else
return false;
}
}
public class Runner
{
/* Class linkedList */
class LinkedList
{
protected Box start;
protected Box end ;
public int size;
/* Constructor */
public LinkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element at end */
public void insertAtEnd(double wid, double hi, double len)
{
Box nptr = new Box(wid, hi, len);
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLinkPrev(end);
end.setLinkNext(nptr);
end = nptr;
}
end.position = size;
size++;
}
/* Function to find the smallest box */
public Box smallest()
{
Box smallest = new Box();
if (start.getLinkNext() == null)
{
return start;
}
Box ptr = start;
smallest = ptr;
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
if(smallest.getVolume()>ptr.getVolume())
{
smallest = ptr;
}
ptr = ptr.getLinkNext();
}
return smallest;
}
/* Function to find the largest box */
public Box largest()
{
Box largest = new Box();
if (start.getLinkNext() == null)
{
return start;
}
Box ptr = start;
largest = ptr;
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
if(largest.getVolume()<ptr.getVolume())
{
largest = ptr;
}
ptr = ptr.getLinkNext();
}
return largest;
}
/* Function to find the number of cubes */
public int noOfCubes()
{
int counter=0;
if (start.getLinkNext() == null)
{
if(start.isCube())
return 1;
else
return 0;
}
Box ptr = start;
if(ptr.isCube())
counter++;
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
if(ptr.isCube())
counter++;
ptr = ptr.getLinkNext();
}
return counter;
}
/* Function to find the smallest cube */
public Box smallestCube()
{
Box smallest = new Box();
if (start.getLinkNext() == null && start.isCube())
{
return start;
}
Box ptr = start;
smallest = ptr;
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
if(smallest.getVolume()>ptr.getVolume() && start.isCube())
{
smallest = ptr;
}
ptr = ptr.getLinkNext();
}
return smallest;
}
/* Function to find the largest cube */
public Box largestCube()
{
Box largest = new Box();
if (start.getLinkNext() == null && start.isCube())
{
return start;
}
Box ptr = start;
largest = ptr;
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
if(largest.getVolume()<ptr.getVolume() && start.isCube())
{
largest = ptr;
}
ptr = ptr.getLinkNext();
}
return largest;
}
/* Function to find the average volume of all cubes */
public double averageCube()
{
double volume=0;
if (start.getLinkNext() == null)
{
if(start.isCube())
return start.getVolume();
else
return 0;
}
Box ptr = start;
if(ptr.isCube())
volume = start.getVolume();
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
if(ptr.isCube())
volume = volume + ptr.getVolume();
ptr = ptr.getLinkNext();
}
volume = volume/size;
return volume;
}
/* Function to find the average volume of all boxes */
public double average()
{
double volume=0;
if (start.getLinkNext() == null)
{
return start.getVolume();
}
Box ptr = start;
volume = start.getVolume();
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
volume = volume + ptr.getVolume();
ptr = ptr.getLinkNext();
}
volume = volume/size;
return volume;
}
/* Function to Display dimensions of the passed cube */
public void display(Box box){
System.out.println("Following are the info about the box: ");
System.out.println("Position:" + box.position);
System.out.println(" Volume:" + box.getVolume());
System.out.println(" Width:" + box.getWidth());
System.out.println(" Height:" + box.getHeight());
System.out.println(" Length:" + box.getLength());
}
}
public LinkedList readFromFile()
{
double[] a = new double[100];
int i=0, length;
LinkedList linkedList = new LinkedList();
Scanner scanner = new Scanner(new File("file.txt"));
while(scanner.hasNextDouble())
{
a[i++] = scanner.nextInt();
}
length = i;
for(i =0; i<= length; i+=3)
{
linkedList.insertAtEnd(a[i],a[i+1],a[i+2]);
}
return linkedList;
}
public static void main(String[] args)
{
// Call ReadFromFile to read data from the file and store it in linked list as Box Objects
// Call any other methods you require
// After calling any of the above methods like largest(), largestCube(), smallest, etc please
// Pass the returned Box Object to display, to show its position, volume, and dimensions
}
}
----------------------------------------------------------------------------------------------
The above is the java code required by the question. Please write the functions that you need to call in main. The description of each function is provided in a comment above it
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.