Question

In: Computer Science

JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the...

JAVA Practice Exam 2

The project practice_exam_2 contains three (3) classes:

  • Testing – This is the file you will use to execute your program and test your cases. Each section refers to

    one or more specific array(s). From here, you can run the whole program or one section at a time.

  • TestCases – This file contains all test cases. NO NEED FOR YOU TO PAY ATTENTION TO IT.

  • YourCode – This is where you will be writing your code.

    Implement the body of the seven (7) methods that are declared in the class YourCode as follows:

  • Method section_1

    •  Given an array of integers, print all the elements in the array in reverse order.

    •  Consider the case when the array is empty and output the message, “The array is empty.”

  • Method section_2

    •  Given an array of integers, overwrite all the odd numbers in the array with 0.

    •  Do NOT print the array; the testing section will print the array.

  • Method section_3

    •  Given an array of integers, find the index of the smallest element in the array and print it out on

      the screen.

    •  Do NOT print the array; the testing section will print the array.

  • Method section_4

    •  Given an array of integers, count all elements in the array that are multiples of 3, but do NOT

      count number 3 itself, and print out the total count.

    •  Do NOT print the array; the testing section will print the array.

  • Method section_5

    •  Given an array of integers, sum up every other element in the array starting at index 0, and print

      out the result.

    •  Use a while loop.

    •  Do NOT print the array; the testing section will print the array.

  • Method section_6

    •  Given a two arrays (a and b) with equal length and that have at least one element, for every

      element in array a, consider the corresponding element in array b (at the same index) and add

      the element from array b to the element in array a (modifying the elements in array a).

    •  Example:

      a is: 4 2 6 5
      b is: 2 3 7 4
      Resulting state of a is: 6 5 13 9 (b remains the same)

    •  Do NOT print the array; the testing section will print the array.

  • Method section_7

    •  Create a new array of integers with length equal to the length of the given array, and copy every

      other element of the given array into the new array, starting at index 0.

    •  Use two (2) for loops: The first will copy the array and the second will print it.

    •  Consider the case when the given array is empty and output the message, “The array to copy

      from is empty.”

    •  Print out the new array.

Solutions

Expert Solution

package april_2;

public class YourCode {
  
   //method to print array in reverse
   public static void section_1(int arr[])
   {
       if(arr.length == 0)
       {
           System.out.println("The array is empty.");
           return;
       }
       System.out.println("Array is reverse order: ");
       for(int i = arr.length-1 ; i >= 0 ; i--)
       {
           System.out.print(arr[i] + " ");
       }
       System.out.println();
   }
  
   //method to replace odd elements with 0.
   public static void section_2(int arr[])
   {
       for(int i = 0 ; i < arr.length ; i++)
       {
           if(arr[i] % 2 != 0)
           {
               arr[i] = 0;
           }
       }
   }
   //method to findthe index of the smallest element in the array and print it out on
   public static void section_3(int arr[])
   {
       int ind = 0;
       int min = arr[0];
      
       for(int i = 1 ; i < arr.length ; i ++)
       {
           if(arr[i] < min)
           {
               min = arr[i];
               ind = i;
           }
       }      
       System.out.println("Minimum is: " + min + " and is found at index: " + ind);
   }
   //count all elements in the array that are multiples of 3, but do NOT count number 3
   public static void section_4(int arr[])
   {
       int count = 0;
       for(int i = 0 ; i < arr.length ; i++)
       {
           if(arr[i] % 3 == 0 && arr[i] != 3)
               count++;
       }
      
       System.out.println("There are "+ count +" multiples of 3.");
   }
  
   //sum up every other element in the array starting at index 0, and print
   public static void section_5(int arr[])
   {
       int sum = 0;
       int i = 0;
       while(i < arr.length)
       {
           sum += arr[i];
           i++;
       }
      
       System.out.println("Sum of every other element of the array is: " + sum);
   }
   //add the element from array b to the element in array a
   public static void section_6(int a[], int b[])
   {
       for(int i = 0 ; i < a.length ; i++)
       {
           a[i] = a[i] + b[i];
       }
   }
   //copy every other element of the given array into the new array, starting at index 0.
   public static void section_7(int a[])
   {
       if(a.length == 0)
       {
           System.out.println("The array to copy from is empty.");
           return;
       }
       int[] b = new int[a.length];
       for(int i = 0 ; i < a.length ; i++)
       {
           b[i] = a[i];
       }
       System.out.println("Printing the new array.");
       section_1(b);      
   }
}


Related Solutions

JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience using classes and arrays of classes. Use the following as the beginning of a student abstract data type. public class Student { private String fName ; private String lName ; private double[] grades; } This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: -...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: - have one that defines an exception - have that exception throw(n) in one method and handled in another -has the program continue even if the user inputs incorrect data -be creative/unique in some way
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes...
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes (Session and Problems) and one driver class and ensure the user inputs cannot cause the system to fail by using exception handling. Overview from Project . Implement a Java program that creates math flashcards for elementary grade students. User will enter his/her name, the type (+,-, *, /), the range of the factors to be used in the problems, and the number of problems...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise. 2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle. 3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula....
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
For Java Let's get more practice testing! Declare a public class TestRotateString with a single void...
For Java Let's get more practice testing! Declare a public class TestRotateString with a single void static method named testRotateString. testRotateString receives an instance of RotateString as a parameter, which has a single method rotate. rotate takes a String and an int as arguments, and rotates the passed string by the passed amount. If the amount is positive, the rotation is to the right; if negative, to the left. If the String is null, rotate should return null. Your testRotateString...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT