Extend the Array class adding the following two new methods: A) the method called min that returns the smallest number in the array. E.g., applying min to the array [−3, 2, 0, −10, 9, 7], we get −10. B) the method called reverse that reverses the array. E.g., applying reverse to the array [−3, 2, 0, −10, 9, 7], we change it to [7, 9, −10, 0, 2, −3].

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Extend the Array class adding the following two new
methods:
A)  the method called min that returns the smallest number
in the array.
E.g., applying min to the array [−3, 2, 0, −10, 9, 7], we get −10.
B) the method called reverse that reverses the array.
E.g., applying reverse to the array [−3, 2, 0, −10, 9, 7], we change it
to [7, 9, −10, 0, 2, −3].

USE THE JAVA CODE BELOW TO ADD ON PLEASE

public class Array {
private int[] elems;
private int nElems;
public Array(int size) {
if (size < 0) {
throw new IllegalArgumentException();
}
elems = new int[size];
}
public void print() {
System.out.print("[");
for (int i = 0; i < nElems; ++i)
if (i < nElems - 1) {
System.out.print(elems[i] + ", ");
} else {
System.out.print(elems[i]);
}
System.out.println("]");
}
public void insert(int element) {
// if the array is full, increases its size
if (elems.length == 0) {
int[] extendedElems = new int[1];
elems = extendedElems;
}
if (elems.length == nElems) {
int[] extendedElems = new int[nElems * 2];
for (int i = 0; i < nElems; ++i) {
extendedElems[i] = elems[i];
}
elems = extendedElems;
}
// otherwise, adds a new element at the end
elems[nElems] = element;
nElems++;
}
public void removeAt(int index) {
if (index < 0 || index >= nElems) {
throw new IllegalArgumentException();
} else {
for (int i = index; i < nElems; ++i) {
elems[i] = elems[i + 1];
}
nElems--;

}
}
public int indexOf(int element) {
for (int i = 0; i < nElems; ++i) {
if (elems[i] == element) {
return i;
}
}
return -1;
}
public boolean contains(int element) {
return (indexOf(element) != -1);
}
}

 

Expert Solution
Step 1

ANSWER:

(A) Smallest Number in Array:

Computer Engineering homework question answer, step 1, image 1

Output:

Computer Engineering homework question answer, step 1, image 2

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY