Question

In: Computer Science

////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...

////Fixme(1) add a statement to import ArrayList class

public class ListManipulation {
public static void main(String[] args) {

//Fixme(2) create an ArrayList of integers and name the ArrayList list.

//Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1

  
displayList(list);
System.out.println();
displayListBackwards(list);
  
  
}
  
public static void displayList(ArrayList<Integer> list) {

for(Integer i: list)
System.out.print(i + " ");
  
}
//Fixme(4) define a method displayListBackwards, which takes an ArrayList as a parameter and returns nothing.
//The method displays each element in the list in the reverse order, and adds a space after each element.



//Fixme(5) define a method maxElement, which takes an ArrayList as a parameter and returns the maximum element in the list.
  
  
//Fixme(6) define a method searchElement, which takes two parameters.
//The first parameter is an ArrayList and the second is an int type value.
//The method searches for value in the list.
//If the value is found in the list, the method returns its index, otherwise, it returns -1.


  
//Fixme(7) define a method eventCount, which takes an ArrayList as a parameter and returns the number of even element in the list.

}

Solutions

Expert Solution

////Fixme(1) add a statement to import ArrayList class
import java.util.ArrayList;

public class ListManipulation {
    public static void main(String[] args) {

//Fixme(2) create an ArrayList of integers and name the ArrayList list.
        ArrayList<Integer> list = new ArrayList<Integer>();

//Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1
        list.add(10);
        list.add(15);
        list.add(7);
        list.add(-5);
        list.add(73);
        list.add(-11);
        list.add(100);
        list.add(20);
        list.add(5);
        list.add(-1);

        displayList(list);
        System.out.println();
        displayListBackwards(list);
    }

    public static void displayList(ArrayList<Integer> list) {
        for (Integer i : list)
            System.out.print(i + " ");
        System.out.println();
    }

    //Fixme(4) define a method displayListBackwards, which takes an ArrayList as a parameter and returns nothing.
//The method displays each element in the list in the reverse order, and adds a space after each element.
    public static void displayListBackwards(ArrayList<Integer> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
    }

    //Fixme(5) define a method maxElement, which takes an ArrayList as a parameter and returns the findMaximumIndex element in the list.
    public static int maxElement(ArrayList<Integer> list) {
        int max = list.get(0);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) > max)
                max = list.get(i);
        }
        return max;
    }

    //Fixme(6) define a method searchElement, which takes two parameters.
//The first parameter is an ArrayList and the second is an int type value.
//The method searches for value in the list.
//If the value is found in the list, the method returns its index, otherwise, it returns -1.
    public static int searchElement(ArrayList<Integer> list, int value) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == value)
                return i;
        }
        return -1;
    }

    //Fixme(7) define a method eventCount, which takes an ArrayList as a parameter and returns the number of even element in the list.
    public static int eventCount(ArrayList<Integer> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) % 2 == 0)
                ++count;
        }
        return count;
    }
}

Related Solutions

import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args)...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    // Reverse the list int[] resultArray = reverseArray(numbers);   ...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args)...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args) {    Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: ");    input = in.nextLine(); int a = 0; int b = 0;    a = Integer.parseInt(input);    System.out.println("Enter the second number: "); input = in.nextLine();    b = Integer.parseInt(input);    int result = 0; result = a/b;    System.out.println("a/b : " + result);    } Copy the code...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT