Upload files to "OOP/Java/Lab/Week5"
This commit is contained in:
parent
cb924d0b60
commit
8c2e55a281
5 changed files with 413 additions and 0 deletions
101
OOP/Java/Lab/Week5/IntArray.java
Normal file
101
OOP/Java/Lab/Week5/IntArray.java
Normal file
|
@ -0,0 +1,101 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
class IntArray {
|
||||
|
||||
private int[] arr;
|
||||
|
||||
public IntArray() {
|
||||
arr = new int[10];
|
||||
}
|
||||
|
||||
public void inputValues() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Enter 10 integer values:");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
arr[i] = scanner.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
public void displayValues() {
|
||||
System.out.println("Array values:");
|
||||
for (int value : arr) {
|
||||
System.out.print(value + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public void displayLargest() {
|
||||
int largest = arr[0];
|
||||
for (int i = 1; i < arr.length; i++) {
|
||||
if (arr[i] > largest) {
|
||||
largest = arr[i];
|
||||
}
|
||||
}
|
||||
System.out.println("Largest value: " + largest);
|
||||
}
|
||||
|
||||
public void displayAverage() {
|
||||
int sum = 0;
|
||||
for (int value : arr) {
|
||||
sum += value;
|
||||
}
|
||||
double average = (double) sum / arr.length;
|
||||
System.out.println("Average value: " + average);
|
||||
}
|
||||
|
||||
public void sortArray() {
|
||||
for (int i = 0; i < arr.length - 1; i++) {
|
||||
for (int j = 0; j < arr.length - i - 1; j++) {
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Array sorted in ascending order.");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
IntArray intArray = new IntArray();
|
||||
java.util.Scanner scanner = new java.util.Scanner(System.in);
|
||||
int choice;
|
||||
|
||||
do {
|
||||
System.out.println("\nMenu:");
|
||||
System.out.println("1. Input Values");
|
||||
System.out.println("2. Display Values");
|
||||
System.out.println("3. Display Largest Value");
|
||||
System.out.println("4. Display Average");
|
||||
System.out.println("5. Sort Array");
|
||||
System.out.println("0. Exit");
|
||||
System.out.print("Enter your choice: ");
|
||||
choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
intArray.inputValues();
|
||||
break;
|
||||
case 2:
|
||||
intArray.displayValues();
|
||||
break;
|
||||
case 3:
|
||||
intArray.displayLargest();
|
||||
break;
|
||||
case 4:
|
||||
intArray.displayAverage();
|
||||
break;
|
||||
case 5:
|
||||
intArray.sortArray();
|
||||
break;
|
||||
case 0:
|
||||
System.out.println("Exiting program. Goodbye!");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid choice. Please try again.");
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue