Question

In: Computer Science

goal Find the average of the elements in the list (list could have any number of...

goal Find the average of the elements in the list (list could have any number of elements).
If the average is a decimal number, return only the integer part. Example: if average=10.8, return 10
Example: if list={10, 20, 30, 40, 50}, the method should return: 30

import java.io.*;
import java.util.Scanner;

class Node {
int data;
Node next;
Node(int d){ // Constructor
   data = d;
   next = null;
}
}

class LinkedList {// a Singly Linked List
   Node head; // head of list
   public void insert(int data){ // Method to insert a new node
       Node new_node = new Node(data); // Create a new node with given data
       new_node.next = null;
       if (head == null) // If the Linked List is empty, then make the new node as head
           head = new_node;
       else {// Else traverse till the last node and insert the new_node there
           Node last = head;
           while (last.next != null)
               last = last.next;
           last.next = new_node; // Insert the new_node at last node
       }
   }
}

class Main {
public static void main(String[] args)
   {
       LinkedList list = new LinkedList();/* Start with the empty list. */
       Scanner scan = new Scanner(System.in);
       int num;
       for (int i=0; i<10; i++){//Read list values
           num = scan.nextInt();
           list.insert(num);
       }
System.out.println(""+getAvg(list));
   }

   
public static int getAvg(LinkedList list) {
       //goal Find the average of the elements in the list (list could have any number of elements).
      
       //If the average is a decimal number, return only the integer part. Example: if average=10.8, return 10
       //Example: if list={10, 20, 30, 40, 50}, the method should return: 30

      
   }

}

Solutions

Expert Solution

import java.io.*;
import java.util.Scanner;

class Node {
    int data;
    Node next;
    Node(int d){ // Constructor
        data = d;
        next = null;
    }
}

class LinkedList {// a Singly Linked List
    Node head; // head of list
    public void insert(int data){ // Method to insert a new node
        Node new_node = new Node(data); // Create a new node with given data
        new_node.next = null;
        if (head == null) // If the Linked List is empty, then make the new node as head
            head = new_node;
        else {// Else traverse till the last node and insert the new_node there
            Node last = head;
            while (last.next != null)
                last = last.next;
            last.next = new_node; // Insert the new_node at last node
        }
    }
}

public class Main {
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();/* Start with the empty list. */
        Scanner scan = new Scanner(System.in);
        int num;
        for (int i=0; i<5; i++){//Read list values
            num = scan.nextInt();
            list.insert(num);
        }
        System.out.println(""+getAvg(list));
    }


    public static int getAvg(LinkedList list) {
        // declare variables
        int num = 0;
        int sum = 0;
        
        // if head is null means list is empty
        if (list.head == null) {
            return -1;
        }
        
        // get the current 
        Node current = list.head;
        
        // loop till end
        while (current != null) {
            num += 1;   // count the length
            sum += current.data;    // calculate sum
            current = current.next;     // make list point to next
        }
        
        // return the average
        return sum/num;

    }

}

FOR HELP PLEASE COMMENT.
THANK YOU

FOR HELP PLEASE COMMENT.
THANK YOU,


Related Solutions

Do you find all the elements as you have learnt, or do you find any difference?...
Do you find all the elements as you have learnt, or do you find any difference? if yes bring out the differences. Auditors' Report to the sharehoders Scope of aduit: We have aduited the accompanying consolidated balance sheet of x industries corporation and its subsudaries (the "Group") as at 31 December 2016 and the related consoildated statments of income, cash flows and shareholders' equity forr the Group's management and have been prepared by them in accordance with the requirememtns of...
Which of these muscles involves any joint: (list the number or highlight the ones that have...
Which of these muscles involves any joint: (list the number or highlight the ones that have joints involved) - you don't have tell me what specific joints. just yes or no for each Masseter & temporalis Lateral rectus (of the eye) Sternocleidomastoid Rectus abdominus Multifidi Trapezius External oblique & internal oblique (one on the right and one on the left) Pectoralis major Deltoid Latissimus dorsi Supraspinatus, infraspinatus & teres minor (three of the four rotator cuff muscles) Biceps brachii &...
in JAVA, Hash table The goal is to count the number of common elements between two...
in JAVA, Hash table The goal is to count the number of common elements between two sets. Download the following data sets, and add them to your project: girlNames2016.txt boyNames2016.txt These files contain lists of the 1,000 most popular boy and girl names in the US for 2016, as compiled by the Social Security Administration. Each line of the file consists of a first name, and the number of registered births that year using that name. Your task is to...
Suppose the sets A and B have both n elements. 1. Find the number of one-to-one...
Suppose the sets A and B have both n elements. 1. Find the number of one-to-one functions from A to B. 2. Find the number of functions from A onto B. 3. Find the number of one-to-one correspondences from A to B.
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or...
Suppose you have a list containing k integer numbers. Each number could be positive, negative, or zero. Write pseudocode for a program that first asks the user for each of the numbers. After that, it should determine and display the number from the list that is nearest to the positive number five (5). Notes: 1. Try working this out for k = 4 and k = 5. See if you can find an algorithmic pattern that can be extended for...
Java: The goal is to find the number of unique words found in a story file...
Java: The goal is to find the number of unique words found in a story file text.txt but there some conditions each such word must be found in the dictionary WordList.txt file, each such word should not be among the commonly used ones such as a, it, etc. Such forbidden words are listed in a separate file called stopwords.txt. Your output will be the single number which is the number of unique words in the story file. Use only ArrayLists...
Java: The goal is to find the number of unique words found in a story file...
Java: The goal is to find the number of unique words found in a story file text.txt but there some conditions 1. each such word must be found in the dictionary WordList.txt file, 2. each such word should not be among the commonly used ones such as a, it, etc. Such forbidden words are listed in a separate file called stopwords.txt. Output will be the single number which is the number of unique words in the story file. Use only...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
The number 986 could have a variety of meanings. They could be used as a code...
The number 986 could have a variety of meanings. They could be used as a code (Dx code 986 in ICD-9-CM), a radio station (98.6 FM) or even a temperature (98.6 Degrees F). The raw numbers 986 equal data that is then transferred into information, which has meaning. Think about and discuss the concepts of data and information. What is the data, information, knowledge continuum? Also, discuss how data supports quality assessment and the importance of data quality for outcome...
Name ANY company and list, and discuss a few elements of its external environment ? Name...
Name ANY company and list, and discuss a few elements of its external environment ? Name a opportunities and threat ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT