AP Java Code for traversal, Lecture notes of Computer science

Code is written in java to perform 1D traversal

Typology: Lecture notes

2022/2023

Uploaded on 01/07/2023

Spyroqz
Spyroqz 🇺🇸

6 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Input:
import java.util.Scanner;
public class arrayTraversal {
// code to implement method Largest
public static int Largest(int[] a) {
int max = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
return max;
}
// code to implement method Difference
public static void Difference(int[] a) {
int max = Largest(a);
for (int i = 0; i < a.length; i++) {
System.out.println(max - a[i]);
}
}
// code to implement method searchKey
public static void searchKey(int[] a) {
Scanner keyboard = new Scanner(System.in);
int key;
boolean found = false;
int location = -1;
System.out.println("Enter the key: ");
key = keyboard.nextInt();
for (int i = 0; i < a.length; i++) {
if (a[i] == key) {
found = true;
location = i;
}
}
if (found) {
pf3
pf4
pf5

Partial preview of the text

Download AP Java Code for traversal and more Lecture notes Computer science in PDF only on Docsity!

Input: import java.util.Scanner; public class arrayTraversal { // code to implement method Largest public static int Largest(int[] a) { int max = 0; for (int i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; } } return max; } // code to implement method Difference public static void Difference(int[] a) { int max = Largest(a); for (int i = 0; i < a.length; i++) { System.out.println(max - a[i]); } } // code to implement method searchKey public static void searchKey(int[] a) { Scanner keyboard = new Scanner(System.in); int key; boolean found = false; int location = -1; System.out.println("Enter the key: "); key = keyboard.nextInt(); for (int i = 0; i < a.length; i++) { if (a[i] == key) { found = true; location = i; } } if (found) {

System.out.println("key is in index location:" + location); } else { System.out.println("key is at index location:" + location); } keyboard.close(); } // code to implement method Update public static int[] Update(int[] a) { int[] douArray = new int[a.length]; for (int i = 0; i < a.length; i++) { douArray[i] = 2 * a[i]; System.out.println(douArray[i]); } return douArray; } public static int Menu() { Scanner keyboard = new Scanner(System.in); System.out.println(" \t 1. Finding the largest value (max) in an array \n"

  • " \t 2. Difference by which each element differs from max \n" + " \t 3. Search for an input key \n"
  • " \t 4.Update data times two \n" + " \t Make a selection or 0 to end processing"); return (keyboard.nextInt()); } // end of menu public static void endOfProcessing() { System.out.println("\n \t End of Processing \n"); } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //declare a one dimensional array called list that stores the following test data 16, 55, 24, 8, 12,
  1. NOTE These items are test data and can change during testing of your code and grading. int[] list = new int[6]; System.out.println("Enter 6 numbers: "); for (int i = 0; i < list.length; i++) { list[i] = keyboard.nextInt(); }

4.Update data times two Make a selection or 0 to end processing 1 Max: 55

  1. Finding the largest value (max) in an array
  2. Difference by which each element differs from max
  3. Search for an input key 4.Update data times two Make a selection or 0 to end processing 2 39 0 31 47 43 22
  4. Finding the largest value (max) in an array
  5. Difference by which each element differs from max
  6. Search for an input key 4.Update data times two Make a selection or 0 to end processing 3 Enter the key: 1 key is at index location:-
  7. Finding the largest value (max) in an array
  8. Difference by which each element differs from max
  9. Search for an input key 4.Update data times two Make a selection or 0 to end processing
  10. Finding the largest value (max) in an array
  11. Difference by which each element differs from max
  12. Search for an input key 4.Update data times two Make a selection or 0 to end processing 4 32 110 48 16 24 66
  13. Finding the largest value (max) in an array
  1. Difference by which each element differs from max
  2. Search for an input key 4.Update data times two Make a selection or 0 to end processing 0 Error in input End of Processing