From 4fef3701327dfcf0b3b5bce7de8b69cbaf4467fc Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Thu, 17 Nov 2022 14:49:22 -0600 Subject: [PATCH] lab 16 --- .../ArrayCopy1.java | 31 +++++++ .../ArrayCopy2.java | 33 ++++++++ .../ArrayCopy3.java | 29 +++++++ .../ArrayCopy4.java | 43 ++++++++++ .../ArrayShift1.java | 19 +++-- .../ArrayShift2.java | 32 ++++++++ .../ArraysAndMethods.java | 81 +++++++++++++++++++ .../nbproject/private/private.xml | 5 +- 8 files changed, 262 insertions(+), 11 deletions(-) create mode 100644 Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy1.java create mode 100644 Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy2.java create mode 100644 Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy3.java create mode 100644 Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy4.java create mode 100644 Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift2.java create mode 100644 Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArraysAndMethods.java diff --git a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy1.java b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy1.java new file mode 100644 index 0000000..b6dab2d --- /dev/null +++ b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy1.java @@ -0,0 +1,31 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.lab16_arrays2_calebfontenot; + +/** + * + * @author caleb + */ +public class ArrayCopy1 { + + public static void main(String[] args) + { + int[] sourceArray = {1, 2, 3, 4, 5}; + int[] targetArray = new int[sourceArray.length]; + // Print the source array + for (int i = 0; i < sourceArray.length; i++) { + System.out.print(sourceArray[i] + " "); + } + System.out.println(); + // Copy the source array to the target array + for (int i = 0; i < sourceArray.length; i++) { + targetArray[i] = sourceArray[i]; + } + // Print the target array + for (int i = 0; i < sourceArray.length; i++) { + System.out.print(targetArray[i] + " "); + } + } +} diff --git a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy2.java b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy2.java new file mode 100644 index 0000000..18080d9 --- /dev/null +++ b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy2.java @@ -0,0 +1,33 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.lab16_arrays2_calebfontenot; + +/** + * + * @author caleb + */ +public class ArrayCopy2 { + + public static void main(String[] args) + { + int[] sourceArray = {1, 2, 3, 4, 5}; + int[] targetArray = new int[sourceArray.length]; + // Print the source array + for (int i = 0; i < sourceArray.length; i++) { + System.out.print(sourceArray[i] + " "); + } + System.out.println(); + // Copy the source array to the target array in reversed order + int j = (sourceArray.length - 1); + for (int i = 0; i < sourceArray.length; i++) { + targetArray[i] = sourceArray[j]; + j--; + } + // Print the target array + for (int i = 0; i < sourceArray.length; i++) { + System.out.print(targetArray[i] + " "); + } + } +} diff --git a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy3.java b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy3.java new file mode 100644 index 0000000..546ffa3 --- /dev/null +++ b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy3.java @@ -0,0 +1,29 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.lab16_arrays2_calebfontenot; + +/** + * + * @author caleb + */ +public class ArrayCopy3 { + + public static void main(String[] args) + { + int[] sourceArray = {1, 2, 3, 4, 5}; + int[] targetArray = new int[sourceArray.length]; + // Print the source array + for (int i = 0; i < sourceArray.length; i++) { + System.out.print(sourceArray[i] + " "); + } + System.out.println(); + // Copy the source array to the target array + System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); + // Print the target array + for (int i = 0; i < sourceArray.length; i++) { + System.out.print(targetArray[i] + " "); + } + } +} diff --git a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy4.java b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy4.java new file mode 100644 index 0000000..fa6ab7c --- /dev/null +++ b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayCopy4.java @@ -0,0 +1,43 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.lab16_arrays2_calebfontenot; + +import java.util.Arrays; + +/** + * + * @author caleb + */ +public class ArrayCopy4 { + + public static void main(String[] args) + { + int[] array = {1, 2, 3, 4, 5}; + int[] array1 = Arrays.copyOfRange(array, 2, 5); + int[] array2 = new int[array.length - 2]; + int[] array3 = new int[array.length]; + System.out.println("array1: (copy with java.util.Arrays.copyOfRange(); )"); + for (int i: array1) { + System.out.print(i + " "); + } + System.out.println(); + // Copy array into array2 with a for loop and print it + System.out.println("array2: (copy with System.arraycopy(); )"); + System.arraycopy(array, 2, array2, 0, 3); + for (int i : array2) { + System.out.print(i + " "); + } + System.out.println(); + // Copy array manually + System.out.println("array3: (manually copy with for loop)"); + for (int i : array3) { + array[i] = array3[i]; + } + for (int i : array1) { + System.out.print(i + " "); + } + System.out.println(); + } +} diff --git a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift1.java b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift1.java index d40a4c3..949f8a8 100644 --- a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift1.java +++ b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift1.java @@ -9,18 +9,23 @@ package com.calebfontenot.lab16_arrays2_calebfontenot; * @author caleb */ public class ArrayShift1 { + public static void main(String[] args) { int[] ar = {1, 2, 3, 4, 5}; int temp = ar[0]; + for (int value : ar) { + System.out.print(value + " "); + } + System.out.println(); + for (int i = 1; i < ar.length; ++i) { - ar[i-1] = ar[i]; - - ar[ar.length - 1] = temp; - - for (int value: ar) { - System.out.println(value + " "); - } + ar[i - 1] = ar[i]; + } + ar[ar.length - 1] = temp; + + for (int value : ar) { + System.out.print(value + " "); } } } diff --git a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift2.java b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift2.java new file mode 100644 index 0000000..96aa61b --- /dev/null +++ b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArrayShift2.java @@ -0,0 +1,32 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.lab16_arrays2_calebfontenot; + +/** + * + * @author caleb + */ +public class ArrayShift2 { + + public static void main(String[] args) + { + int[] ar = {1, 2, 3, 4, 5}; + int temp = ar[ar.length - 1]; + + for (int value : ar) { + System.out.print(value + " "); + } + System.out.println(); + + for (int i = (ar.length -1); i > 0 ; --i) { + ar[i] = ar[i -1]; + } + ar[0] = temp; + + for (int value : ar) { + System.out.print(value + " "); + } + } +} diff --git a/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArraysAndMethods.java b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArraysAndMethods.java new file mode 100644 index 0000000..8d32978 --- /dev/null +++ b/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArraysAndMethods.java @@ -0,0 +1,81 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.lab16_arrays2_calebfontenot; + +/** + * + * @author caleb + */ +public class ArraysAndMethods { + + public static void printArray(int[] arrayInput) + { + for (int i : arrayInput) { + System.out.print(i + " "); + } + System.out.println(); + } + + public static void printArray(char[] arrayInput) + { + for (int i : arrayInput) { + System.out.print((char) i + " "); + } + System.out.println(); + } + + public static void printArray(double[] arrayInput) + { + for (double i : arrayInput) { + System.out.print(i + " "); + } + System.out.println(); + } + + public static int[] reverse(int[] arrayToReverse) + { + int arrayResult[] = new int[arrayToReverse.length]; + int j = (arrayToReverse.length - 1); + for (int i = 0; i <= arrayToReverse.length - 1; i++) { + arrayResult[i] = arrayToReverse[j]; + j--; + } + return arrayResult; + } + + public static int linearSearch(int list[], int key) + { + int indexFound = -1; + for (int i = 0; i < list.length; ++i) { + if (list[i] == key) { + indexFound = i; + } + } + return indexFound; + } + + public static void main(String[] args) + { + int[] array = {1, 2, 3, 4, 5}; + printArray(array); + printArray(new int[]{3, 1, 2, 6, 4}); + printArray(reverse(array)); + + System.out.println(linearSearch(array, 5) != 1 + ? "found at index " + Integer.toString(linearSearch(array, 5)) + : "not found at index " + Integer.toString(linearSearch(array, 5)) + ); + double[] doubles = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; + // Use the built-in sort method to sort the double. + java.util.Arrays.sort(doubles); + + char[] chars = {'a', 'A', '4', 'F', 'D', 'P',}; + //Sort again + java.util.Arrays.sort(chars); + // Now print the sorted arrays. + printArray(doubles); + printArray(chars); + } +} diff --git a/Exams/Exam2_CalebFontenot/nbproject/private/private.xml b/Exams/Exam2_CalebFontenot/nbproject/private/private.xml index 6e2a37a..6807a2b 100644 --- a/Exams/Exam2_CalebFontenot/nbproject/private/private.xml +++ b/Exams/Exam2_CalebFontenot/nbproject/private/private.xml @@ -2,9 +2,6 @@ - - file:/home/caleb/ASDV-Java/Exams/Exam2_CalebFontenot/src/exam2_calebfontenot/Problem3.java - file:/home/caleb/ASDV-Java/Exams/Exam2_CalebFontenot/src/exam2_calebfontenot/Problem1.java - +