Question

Java this is my first Java course and I have an assignment before I took C...

Java

this is my first Java course and I have an assignment

before I took C programming, so I feel confused a little bit

the assignment asked me to fix the method in part 1 ( after the compile and run the file)

I compiled and run the A1Tester file

but I don't know how to fix the method

I only need to learn how to fix the method in part 1

all the files are in google docs and I have posted the link (also, there is an explanation for the assignment in pdf)

link: https://drive.google.com/drive/folders/1GgRWZ-lL9xkuGfW2Sq9BaTZxDz9Zy-rM?usp=sharing

thank you

Homework Answers

Answer #1

Here is the completed code for this problem (completed ArrayOperations.java only (part 1)). Now if you run A1Tester program, the tests for all ArrayOperations class methods will pass without any errors. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// ArrayOperations.java

/*

* ArrayOperations

* DO NOT use builtin java Arrays mehthods

* A class with basic array methods to

* - print the values in an array

* - calculate the product of the values in an array

* - calculate the minimum of the values in an array

* - calculate the maximum of the values in an array

* - determine the equality to 2 arrays

* -

*

*/

public class ArrayOperations {

      /*

      * printArray

      *

      * Purpose: prints all the values in the array to the console example

      * format: {1,2,3,4}

      *

      * Parameters: an array of integers

      *

      * Returns: void

      */

      public static void printArray(int[] array) {

            System.out.print("{");

            for (int i = 0; i < array.length; i++) {

                  System.out.print(array[i]);

                  if (i < array.length - 1)

                        System.out.print(",");

            }

            System.out.println("}");

      }

      /*

      * arrayProduct

      *

      * Purpose: computes the product of all values in the input array NOTE:

      * product of 3 numbers n1, n2 and n3 = n1*n2*n3 NOTE: product of no numbers

      * = 1

      *

      * Parameters: an array of integers

      *

      * Returns: product of all values in the array

      */

      public static int arrayProduct(int[] array) {

            // initializing product to 1

            int product = 1;

            // looping through all elements

            for (int i = 0; i < array.length; i++) {

                  // multiplying current element with product and storing in product

                  product = product * array[i];

            }

            return product;

      }

      /*

      * arrayMax

      *

      * Purpose: finds the maximum value in the input array

      *

      * Parameters: an array of integers

      *

      * Preconditions: array contains at least one element

      *

      * Returns: maximum value in the array

      */

      public static int arrayMax(int[] a) {

            int max = 0;

            // looping through each element

            for (int i = 0; i < a.length; i++) {

                  // if this is first element or if current element is bigger than

                  // max, updating max

                  if (i == 0 || a[i] > max) {

                        max = a[i];

                  }

            }

            return max;

      }

      /*

      * arrayMin

      *

      * Purpose: finds the minimum value in the input array

      *

      * Parameters: an array of integers

      *

      * Preconditions: array contains at least one element

      *

      * Returns: minimum value in the array

      */

      public static int arrayMin(int[] a) {

            int min = 0;

            for (int i = 0; i < a.length; i++) {

                  // if this is first element or if current element is smaller than

                  // min, updating max

                  if (i == 0 || a[i] < min) {

                        min = a[i];

                  }

            }

            return min;

      }

      /*

      * arraysEqual

      *

      * Purpose: determines whether the two arrays are equal where equal means

      * array1 and array2 are the same length and the contain the same values in

      * the same order

      *

      * Parameters: two arrays of integers

      *

      * Returns: true if the are equal, false otherwise

      */

      public static boolean arraysEqual(int[] a, int[] b) {

            // NOTE:

            // Because there are only two possible return values

            // from this function, this stub actually passes some

            // of the tests.

            //

            // However, you will not receive those marks unless you

            // change this code to meet the specifications above.

            //

            if (a.length == b.length) {

                  // length is same, comparing each element

                  for (int i = 0; i < a.length; i++) {

                        if (a[i] != b[i]) {

                              // mismatch, not equal

                              return false;

                        }

                  }

                  // all checks are passed, returning true

                  return true;

            }

            return false; // unequal lengths

      }

      /*

      * shiftLeft

      *

      * Purpose: copies every element in the array into a new array of the same

      * length with every element shifted by the left by the specified amount

      *

      * Parameters: an input array of integers, and the number of positions to

      * shift left by

      *

      * Returns: int[] - the new array

      */

      public static int[] shiftLeft(int[] a, int pos) {

            // creating a new array and copying all elements from a

            int copy[] = new int[a.length];

            for (int i = 0; i < a.length; i++) {

                  copy[i] = a[i];

            }

            // if array is empty, returning it

            if (copy.length == 0) {

                  return copy;

            }

            int counter = 0;

            // looping for pos number of times

            while (counter < pos) {

                  // copying first element

                  int first = copy[0];

                  // shifting all elements to the left for one time

                  for (int i = 0; i < copy.length - 1; i++) {

                        copy[i] = copy[i + 1];

                  }

                  // storing old front value to last position of the array

                  copy[copy.length - 1] = first;

                  counter++;

            }

            //returning copy array

            return copy;

      }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
My assignment is listed below. I already have the code complete, but I cannot figure out...
My assignment is listed below. I already have the code complete, but I cannot figure out how to complete this portion: You must create a makefile to compile and build your program. I can't figure out if I need to create a makefile, and if I do, what commands do I use for that? Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments,...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. Create a Java class named HeadPhone to represent a headphone set. The class contains:  Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.  A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.  A private...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT