Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## having trouble getting full average and splitting a LinkedList

I'm working with on a LinkedList assignment, however I've written the methods for splitting the list into two equal lists, but when I apply the average it only applies it to the first half of the list. When I try to calculate the average before I split the list, it gives me a null error, but I know I wrote the method correct because it works if I calculate the average after.

Here is my code.

```import java.util.Random;

public static void main(String[] args){
int sum=0;
Random rand = new Random();
for(int i = 0; i<59; i++){
int randomNum = rand.nextInt(100)+1;
sum = sum + randomNum;
list.insertLast(new IntElement(randomNum));

}
System.out.println("Printing the list.");
list.print();

System.out.println("Deleting the first node.");
list.deleteFront();
list.print();

System.out.println("Deleting the last node.");
list.deleteLast();
list.print();

System.out.println("Inserting node into the front with a number of 68.");
list.insertFirst(new IntElement(68));
list.print();

System.out.println("Inserting node into the back with a number of 75.");
list.insertLast(new IntElement(75));
list.print();

System.out.println("Now splitting the list into to two equal lists.");
list.splitMid(list);
list.print(); //shows the first half of the list

System.out.println("The average is " + list.average());
System.out.println(sum/59);
System.out.println();

}
}```

```public abstract class LinkedList{

protected class LinkedListNode{   // inner class node definition
protected DataElement info;
}

protected LinkedListNode first; //variable to store the address of the first node of the list
protected LinkedListNode last;  // variable to store the address of the last node of the list

protected int count; //variable to store the number of nodes in the list

//default constructor
//initializes the list to an empty state.
//postcondition: first = null, last = null, count = 0

first = null;  //
last = null;  //empty list
count = 0;   //
}

{
copy(list);
}

{

if(list.first == null)
{
first = null;
last = null;
count = 0;
}
else
{
count = list.count;
pointer = list.first;
first.info = pointer.info.getCopy();
last = first;

while(pointer != null)
{
newNode.info = pointer.info.getCopy();
last = newNode;
}
}
}

//method to determine whether the list is empty
//postcondition: returns true if the list is empty; false otherwise

public boolean isEmptyList(){
return (count == 0);
}

//method to initialize the list to an empty state
//postcondition: first = null, last = null, count = 0

public void initializeList(){
first = null;
last = null;
count = 0;
}

//method to output the data contained in each node

public void print(){

if(count == 0){
System.out.println("Cannot print an empty list.");
}
else{

pointer = first;
while(pointer != null){

System.out.println(pointer.info.toString());
}

}
System.out.println();
}

//method to return the number of nodes in the list.
//postcondition: the value of count is returned

public int length(){
return count;
}

//method to return a reference of the object
//containing the data of the first node of the list
//precondition: the list must exist and must not be empty.
//postcondition: the reference of the object that contains
//the info of the first node is returned

public DataElement front(){

return first.info.getCopy();
}

//Method to return a reference of object containing
//the data of the last node of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: The reference of the object that
//contains the info of the last node
//is returned.
public DataElement back(){
return last.info.getCopy();
}

//Method to insert newItem in the list.
//Postcondition: first points to the new list
//               and newItem is inserted at the
//               beginning of the list. Also,
//               last points to the last node and
//               count is incremented by 1.
public void insertFirst(DataElement newItem){
newNode.info = newItem.getCopy();
first = newNode;

if(last == null){

last = newNode;
count++;
}

}

//Method to insert newItem at the end of the list.
//Postcondition: first points to the new list and
//newItem is inserted at the end
//of the list. Also, last points to
//the last node and
//count is incremented by 1.
public void insertLast(DataElement newItem){

newNode.info = newItem.getCopy();
if(first == null){
first = newNode;
last = newNode;
}
else{

last = newNode;
count++;
}
}

public void deleteFront()
{
{
count--;
}
else
{
first = null;
last = null;
count = 0;
}
}

public void deleteLast()
{
{
first = null;
last = null;
count = 0;
}
else
{
last = pointer;
count--;
}
}

{
int stopPoint = list.count / 2;
for(int i = 0; i < stopPoint; i++)
{
}

}

public double average(){
int sum=0;

while(pointer!=null){

pointer = first;
System.out.println("Method avg pointer = " + pointer + " first = " + first);
IntElement temp = (IntElement)pointer.info.getCopy();
sum = sum + temp.getNum();
{
break;
}
else{

System.out.println(sum);
}

}
double average = (sum/59);
return average;

}

//Method to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is found
//in the list; false otherwise.
public abstract boolean search(DataElement searchItem);

//Method to delete deleteItem from the list.
//Postcondition: If found, the node containing
//deleteItem is deleted from the
//list. Also first points to the first
//node, last points to the last
//node of the updated list, and count
//is decremented by 1.
public abstract void deleteNode(DataElement deleteItem);

}```

```public class UnorderedLinkedList extends LinkedList{

super();
}

super(list);
}

//Method to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is found
//in the list; false otherwise.
public boolean search(DataElement searchItem){

pointer = first;
boolean foundData = false;

while(pointer != null && !foundData){
if(pointer.info.equals(searchItem) == true){
foundData = true;
}
else{
}

}
return foundData;

}

//Method to delete deleteItem from the list.
//Postcondition: If found, the node containing
//deleteItem is deleted from the
//list. Also first points to the first
//node, last points to the last
//node of the updated list, and count
//is decremented by 1.
public void deleteNode(DataElement deleteItem){

if(count == 0)
System.out.println("The list is empty.");
else{
if(first.info.equals(deleteItem) == true)
{
first = null;
last = null;
count = 0;
}
else{
count--;
if(count == 1){
last = first;
}
}
}

else{
pointer = first;

while(pointer.info.equals(deleteItem) != true){

last = pointer;
count--;
}

}

}
}

}

}```

```public abstract class DataElement{

public abstract boolean equals(DataElement otherElement);

public abstract int compareTo(DataElement otherElement);

public abstract void makeCopy(DataElement otherElement);

public abstract DataElement getCopy();

}```

```public class IntElement extends DataElement
{

protected int number;

public IntElement()
{
number = 0;
}

public IntElement(int num)
{
number = num;
}

public IntElement(IntElement obj)
{
this.number = obj.getNum();

}

public void setNum(int num)
{

number = num;
}

public String toString()
{
return Integer.toString(number);
}

public int getNum()
{

return number;
}

public boolean equals(DataElement obj)
{

if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
IntElement temp = (IntElement)obj;
return (number == temp.number);
}

public int compareTo(DataElement obj)
{
IntElement temp = (IntElement)obj;
if (number < temp.number) return -1;
else if (number > temp.number) return 1;
else return 0;
}

public void makeCopy(DataElement obj)
{
IntElement temp = (IntElement)obj;
number = temp.number;
}

public DataElement getCopy()
{
DataElement temp = new IntElement(number);
return temp;
}

}```

edit: I just figured out by using the constructor I can make a copy of the list. I was trying to copy using LinkedList object when it's abstract instead of creating a Unordered object. My average method is having trouble. It keeps cutting off the last node's number. Why is it doing this? I'm trying to figure out how to fix it.
```import java.util.Random;

public static void main(String[] args){
int sum=0;
Random rand = new Random();
for(int i = 0; i<59; i++){
int randomNum = rand.nextInt(100)+1;
sum = sum + randomNum;
list.insertLast(new IntElement(randomNum));

}
System.out.println("Printing the list.");
list.print();

System.out.println("Deleting the first node.");
list.deleteFront();
list.print();

System.out.println("Deleting the last node.");
list.deleteLast();
list.print();

System.out.println("Inserting node into the front with a number of 68.");
list.insertFirst(new IntElement(68));
list.print();

System.out.println("Inserting node into the back with a number of 75.");
list.insertLast(new IntElement(75));
list.print();

System.out.println("Now splitting the list into to two equal lists.");
list.splitMid(list);
list.print(); //shows the first half of the list

System.out.println("The average is " + copyList.average());
System.out.println(sum/59.0);
System.out.println();

}
}```

```The average is 51.23728813559322
52.389830508474574```

2. ## Re: having trouble getting full average and splitting a LinkedList

Been racking my brain. I can't figure out now why my average and stdDeviation method aren't working anymore.

```public abstract class LinkedList{

protected class LinkedListNode{   // inner class node definition
protected DataElement info;
}

protected LinkedListNode first; //variable to store the address of the first node of the list
protected LinkedListNode last;  // variable to store the address of the last node of the list

protected int count; //variable to store the number of nodes in the list

//default constructor
//initializes the list to an empty state.
//postcondition: first = null, last = null, count = 0

first = null;  //
last = null;  //empty list
count = 0;   //
}

{
copy(list);
}

{

if(list.first == null)
{
first = null;
last = null;
count = 0;
}
else
{
count = list.count;
pointer = list.first;
first.info = pointer.info.getCopy();
last = first;

while(pointer != null)
{
newNode.info = pointer.info.getCopy();
last = newNode;
}
}
}

//method to determine whether the list is empty
//postcondition: returns true if the list is empty; false otherwise

public boolean isEmptyList(){
return (count == 0);
}

//method to initialize the list to an empty state
//postcondition: first = null, last = null, count = 0

public void initializeList(){
first = null;
last = null;
count = 0;
}

//method to output the data contained in each node

public void print(){

if(count == 0){
System.out.println("Cannot print an empty list.");
}
else{

pointer = first;
while(pointer != null){

System.out.println(pointer.info.toString());
}

}

}

//method to return the number of nodes in the list.
//postcondition: the value of count is returned

public int length(){
return count;
}

//method to return a reference of the object
//containing the data of the first node of the list
//precondition: the list must exist and must not be empty.
//postcondition: the reference of the object that contains
//the info of the first node is returned

public DataElement front(){

return first.info.getCopy();
}

//Method to return a reference of object containing
//the data of the last node of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: The reference of the object that
//contains the info of the last node
//is returned.
public DataElement back(){
return last.info.getCopy();
}

//Method to insert newItem in the list.
//Postcondition: first points to the new list
//               and newItem is inserted at the
//               beginning of the list. Also,
//               last points to the last node and
//               count is incremented by 1.
public void insertFirst(DataElement newItem){
newNode.info = newItem.getCopy();
first = newNode;

if(last == null){

last = newNode;
count++;
}

}

//Method to insert newItem at the end of the list.
//Postcondition: first points to the new list and
//newItem is inserted at the end
//of the list. Also, last points to
//the last node and
//count is incremented by 1.
public void insertLast(DataElement newItem){

newNode.info = newItem.getCopy();
if(first == null){
first = newNode;
last = newNode;
}
else{

last = newNode;
count++;
}
}

public void deleteFront()
{
{
count--;
}
else
{
first = null;
last = null;
count = 0;
}
}

public void deleteLast()
{
{
first = null;
last = null;
count = 0;
}
else
{
last = pointer;
count--;
}
}

{
int stopPoint = list.count / 2;
for(int i = 0; i < stopPoint; i++)
{
}

}

public double average(){
double sum=0;

while(pointer != null){

pointer = first;
IntElement temp = (IntElement)pointer.info.getCopy();
sum = sum + temp.getNum();
System.out.println("temp num: " + temp.getNum());

}
else{
pointer = first;
IntElement temp2 = (IntElement)pointer.info.getCopy();
sum = sum + temp.getNum();
break;
}
}

double average = (sum/59.0);
return average;

}

public double stdDev(double avg){
double stdAvg = avg;
double variance = 0;

while(pointer!= null){

pointer = first;
IntElement temp = (IntElement)pointer.info.getCopy();
variance = variance + ((temp.getNum() - stdAvg)*(temp.getNum() - stdAvg));

}
else{
pointer = first;
IntElement temp2 = (IntElement)pointer.info.getCopy();
variance = variance + ((temp.getNum() - stdAvg)*(temp.getNum() - stdAvg));

}
}
return (Math.sqrt(variance/59));
}

//Method to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is found
//in the list; false otherwise.
public abstract boolean search(DataElement searchItem);

//Method to delete deleteItem from the list.
//Postcondition: If found, the node containing
//deleteItem is deleted from the
//list. Also first points to the first
//node, last points to the last
//node of the updated list, and count
//is decremented by 1.
public abstract void deleteNode(DataElement deleteItem);

}```

```import java.util.Random;

public static void main(String[] args){
Random rand = new Random();
for(int i = 0; i<=59; i++){
int randomNum = rand.nextInt(100)+1;
list.insertLast(new IntElement(randomNum));

}

System.out.println("The average is: " + list.average());

System.out.println("The Standard Deviation is: " + list.stdDev(list.average()));

System.out.println("Printing the list.");
list.print();

System.out.println("Deleting the first node.");
list.deleteFront();
list.print();

System.out.println("Deleting the last node.");
list.deleteLast();
list.print();

System.out.println("Inserting node into the front with a number of 68.");
list.insertFirst(new IntElement(68));
list.print();

System.out.println("Inserting node into the back with a number of 75.");
list.insertLast(new IntElement(75));
list.print();

System.out.println("Now splitting the list into to two equal lists.");
list.splitMid(list);
list.print(); //shows the first half of the list

}
}```

```temp num: 41
temp num: 4
temp num: 2
temp num: 1
The average is: 49.50847457627118
temp num: 41