0% found this document useful (0 votes)
4 views

Myarraylinearlist

The document defines a MyArrayLinearList class that extends an ArrayLinearList class and adds additional methods like finding the maximum and minimum value, calculating the sum and average, sorting, and more. It also includes a main method that demonstrates using the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Myarraylinearlist

The document defines a MyArrayLinearList class that extends an ArrayLinearList class and adds additional methods like finding the maximum and minimum value, calculating the sum and average, sorting, and more. It also includes a main method that demonstrates using the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package dataStructures;

import dataStructures.ArrayLinearList;
import dataStructures.LinearList;

import java.util.*;
import utilities.*;

import java.util.Scanner;

public class MyArrayLinearList extends ArrayLinearList {

@Override
public boolean contains(Object currentElement) {
for (int i = 0; i < size; i++) {
if (currentElement.equals(get(i))) {
return true;
}
}
return false;
}

public MyArrayLinearList unique() {


MyArrayLinearList uniqueList = new MyArrayLinearList();

for (int i = 0; i < size; i++) {


Object currentElement = get(i);

// Check if the element is not already in the uniqueList


if (!uniqueList.contains(currentElement)) {
uniqueList.add(i, currentElement);
}
}

return uniqueList;
}

public void reverse() {


int left = 0;
int right = size - 1;

while (left < right) {


// Swap the elements at the left and right indices
Object temp = element[left];
element[left] = element[right];
element[right] = temp;

// Move the indices towards the center


left++;
right--;
}
}

public Object max() {


Object maxElement = null;

for (int i = 0; i < size; i++) {


Object currentElement = get(i);
if (currentElement instanceof Integer) {
Integer currentInteger = (Integer) currentElement;

if (maxElement == null || currentInteger > (Integer) maxElement)


{
maxElement = currentInteger;
}
}
}

return maxElement;
}

public Object min() {


Object minElement = null;

for (int i = 0; i < size; i++) {


Object currentElement = get(i);

if (currentElement instanceof Integer) {


Integer currentInteger = (Integer) currentElement;

if (minElement == null || currentInteger < (Integer) minElement)


{
minElement = currentInteger;
}
}
}

return minElement;
}

public int sum() {


int sum = 0;

for (int i = 0; i < size; i++) {


Object currentElement = get(i);

if (currentElement instanceof Integer) {


int currentInteger = (Integer) currentElement;
System.out.println("Элемент: " + currentInteger);
sum += currentInteger;
}
}

return sum;
}

public double average() {


return 1.0*sum()/size();
}
public void sort() {
Arrays.sort(element, 0, size);
}

public void display() {


for (int i = 0; i < size; i++) {
System.out.print(element[i] + " ");
}
System.out.println();
}

public static void main(String arg[]) {


Scanner scanner = new Scanner(System.in);

MyArrayLinearList x= new MyArrayLinearList();

x.add(0, new Integer(2));


x.add(1, new Integer(6));
x.add(2, new Integer(1));
x.add(3, new Integer(4));
x.add(4,new Integer(6));
while (true) {
System.out.println(x);
System.out.println("Дараах үйлдлүүдээс сонгоно уу:");
System.out.println("1. Max утгыг олох");
System.out.println("2. Min утгыг олох");
System.out.println("3. Нийлбэр олох");
System.out.println("4. Дундаж утгыг олох");
System.out.println("5. Сортлогч");
System.out.println("6. Unique - Давхардсан утгыг устгах");
System.out.println("7. Reverse");
System.out.println("8. Гарлаа.");
System.out.print("Сонголт: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.println("Max утга: " + x.max());
break;
case 2:
System.out.println("Min утга: " + x.min());
break;
case 3:
System.out.println("Нийлбэр: " + x.sum());
break;
case 4:
System.out.println("Дундаж утга: " + x.average());
break;
case 5:
x.sort();
System.out.println("Жагсаалт сортлогдлоо." );
x.display();
break;
case 6:
MyArrayLinearList uniqueList = x.unique();
System.out.println("Давхардсан утгууд:");
uniqueList.display();

break;
case 7:
x.reverse();
System.out.println("Жагсаалт эсрэгээрээ эрэмбэлэгдлээ.");
x.display();
break;

case 8:
System.out.println("Гарлаа.");
scanner.close();
System.exit(0);
default:
System.out.println("Та буруу сонголт хийсэн байна. Дахин
оролдоно уу.");
}
}
}

You might also like