0% found this document useful (0 votes)
3 views5 pages

Java Program Assignment20

The document describes the implementation of two Java packages: 'mathop' for mathematical operations (finding maximum and minimum) and 'statop' for statistical operations (calculating average and median). It also includes a menu-driven program for creating a single linked list using the 'nodepack' and 'listpack' packages, allowing users to add nodes and display the list. The code demonstrates the use of classes, methods, and user input for performing various operations.

Uploaded by

Parineeta Deb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Program Assignment20

The document describes the implementation of two Java packages: 'mathop' for mathematical operations (finding maximum and minimum) and 'statop' for statistical operations (calculating average and median). It also includes a menu-driven program for creating a single linked list using the 'nodepack' and 'listpack' packages, allowing users to add nodes and display the list. The code demonstrates the use of classes, methods, and user input for performing various operations.

Uploaded by

Parineeta Deb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Create a package named mathop.

Define class MathsOperations with static methods to find


the maximum and minimum of n numbers. Create another package statop. Define class
StatsOperations with methods to find the average and median of n numbers. Import these
packages to use the above methods to perform above operations on n numbers.

package mathop;
public class MathsOperations
{
public static int max(int[] numbers) //can write numbers before brackets also, its
array name{
{
int max = numbers[0];
for(int num : numbers) {
if (num > max) {
max = num;
}
}
return max;
}
public static int min(int[] numbers)
{
int min = numbers[0];
for(int num : numbers)
{
if(num < min)
{
min = num;
}
}
return min;
}
}

package statop;
import [Link];
public class StatsOperations
{
public double average(int[] numbers)
{
int sum=0;
for(int num : numbers)
{
sum+=num;
}
return (double) sum/[Link];
}
public double median(int[] numbers)
{
[Link](numbers);
int n = [Link];
if(n % 2 == 0)
{
return (numbers[n / 2 - 1] + numbers[n / 2]) / 2.0;
}
else
{
return numbers[n / 2];
}
}
}

import [Link];
import [Link];
import [Link];
public class MathStatDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements : ");
int n = [Link]();
int i;
int[] numbers = new int[n];
[Link]("Enter " + n + " numbers : ");
for(i=0;i<n;i++)
{
numbers[i]=[Link]();
}
//Using mathop package
int max = [Link](numbers);
int min = [Link](numbers);
//Using statop package
StatsOperations stats = new StatsOperations();
double avg = [Link](numbers);
double med = [Link](numbers);
//Output
[Link]("Maximum : " + max);
[Link]("Minimum : " + min);
[Link]("Average : " + avg);
[Link]("Median : " + med);
}
}

Output:-
2. Create a package called nodepack which contains the class “Node”. Create another package
called listpack which contains the class “LinkedList” representing methods to create a Single
Linked list, Add a node to the list and traverse the list. Write a menu driven program in main to
create a Single Linked list, Add nodes and display the List. The elements are passed as user
input.

package nodepack;
public class Node {
private int data;
private Node next;
public Node(int data) {
[Link] = data;
[Link] = null;
}
public int getData() { return data; } // Getter for data
public Node getNext() { return next; } // Getter for next node
public void setNext(Node next) { [Link] = next; } // Setter for next node
}

package listpack;
import [Link];

public class LinkedList {


private Node head;
public LinkedList() {
[Link] = null;
}
public void addNode(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while ([Link]() != null) { // Use getter method
temp = [Link]();
}
[Link](newNode); // Use setter method
}
}
public void displayList() {
if (head == null) {
[Link]("List is empty.");
return;
}
Node temp = head;
[Link]("Linked List: ");
while (temp != null) {
[Link]([Link]() + " -> "); // Use getter method
temp = [Link]();
}
[Link]("NULL");
}
}

import [Link];
import [Link];
public class List {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
LinkedList list = new LinkedList();
int choice, data;
do {
[Link]("\nMenu:");
[Link]("1. Add Node");
[Link]("2. Display List");
[Link]("3. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter data: ");
data = [Link]();
[Link](data);
break;
case 2:
[Link]();
break;
case 3:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice. Try again.");
}
} while (choice != 3);
[Link]();
}
}

You might also like