Concept explainers
Modify the unsortedArrayAccess class by adding a method that find the smallest element and average in the array.
import java.util.*;
public class unsortedArrayAccess
{
public static double[] arr;
public int arraySize;
public unsortedArrayAccess(int scale)
{
arr = new double[scale];
arraySize = 0;
}
public int search(double Key)
{
int i = 0;
while ((i < arraySize) && (arr[i] != Key) )
{
i = i + 1;
}
if (i < arraySize)
{
return i;
}
else
{
System.out.println("There is no such item!");
return -1;
}
}
public void append(double Item)
{
arr[arraySize] = Item;
arraySize = arraySize + 1;
}
public double remove()
{
if (arraySize == 0)
{
System.out.println("There is no item in the array!");
return -1;
}
double x = arr[arraySize - 1];
arraySize = arraySize - 1;
return x;
}
public void deletion(double Key)
{
int k = search(Key);
if (k != -1)
{
for (int i = k; i < arraySize; i++)
{
arr[i] = arr[i + 1];
}
arraySize = arraySize - 1;
};
}
public void display()
{
if (arraySize == 0)
{
System.out.println("Array is empty!");
}
else
{
for (int i = 0; i < arraySize; i++)
{
System.out.println(arr[i]);
}
};
System.out.println("array size is " + arraySize);
}
public double smallest()
{
double small = arr[0];
for(int i =1; i<arraySize; i++)
{
if(arr[i]<small)
small=arr[i];
}
return small;
}
public double average()
{
double sum=0.0;
for(int i=0; i<arraySize; i++)
sum = sum +arr[i];
if( arraySize>0)
return sum/arraySize;
else
return -999999.0;
}
public static void main (String[] args) {
Main a=new Main(50);
a.append(150);
a.display();
}
}
Program modification:
- Define the smallest() function that is called by the called by class object
- Initialize a variable small that holds the first element of the array
- Iterate over the array to compare the array value with the variable small and swap the current element if it is smaller than the variable small
- Return the value of the variable small
- Define the average() function that is called by the called by class object
- Initialize a variable sum that holds the sum of array elements
- Iterate over the array and add the elements to variable sum
- Evaluate the average using the below-mentioned expression
- print the average of the array elements
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 4 images
- import java.util.Scanner;import java.util.ArrayList;import java.util.StringTokenizer; public class PlantArrayListExample { // TODO: Define a printArrayList method that prints an ArrayList of plant (or flower) objects public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String input; // TODO: Declare an ArrayList called myGarden that can hold object of type plant // TODO: Declare variables - plantName, plantCost, flowerName, flowerCost, colorOfFlowers, isAnnual input = scnr.next(); while(!input.equals("-1")){ // TODO: Check if input is a plant or flower // Store as a plant object or flower object // Add to the ArrayList myGarden input = scnr.next(); } // TODO: Call the method printArrayList to print myGarden }}arrow_forwardMath 130 Java Programming Does my code pass the requirements? May I have more explanation? Is my code readable? Is my code well commented? How may I better organize my code? Are there whitespaces to appropriate help separate distinct parts? import java.util.*; public class NumberArray { //method definition of printArray() which accepts //array as parameter public static void printArray(int[] array) { System.out.printf(" "); //displaying the array elements for(int j = 0; j < array.length; j++) { if(j > 0 && j % 6 ==0) { System.out.printf("\n"); System.out.printf(" "); } System.out.printf("%3d ", array[j]); } } //method definition of getTotal() //which accepts array as parameter public static double getTotal(int[]array) { //declare variable sum as double and set 0 double sum = 0; //using for loop, calculate the total of all elements in the array for(int j = 0; j < array.length; j++) { sum+= array[j]; } //returning sum to the calling method return sum; }…arrow_forward6. Show the printout of the following code: public class ArraysBasic { public static void main(String[] args) { int A[] = {1, 2, 3}; } System.out.println("array A: " + A[0] +""+A[1]+""+ A[2]); System.out.println("array B: " + A[0] + " " + A[1] + " " + A[2]); zeroArray(A); A = new int[5]; System.out.println("array C: " + A[0] + " " + A[1] + " " + A[3] + " " + A[4]); } } public static void zeroArray(int[] list) { for (int j = 0; j < list.length; ++j) { list[j] = 99;arrow_forward
- fill in the ? makesarrow_forwardQuestion 2. package sortsearchassigncodeex1; import java.util.Scanner; import java.io.*; // // public class Sortsearchassigncodeex1 { // public static void fillArray(Scanner inputFile, int[] arrIn){ int indx = 0; //Complete code { arrIn[indx] = inputFile.nextInt(); indx++; } }arrow_forwardJAVA programming language i need to create an "addFront" method in the bottom of the code that adds an element at the front. [-1, -3, -5] A.add(-9) ===> [-9, -1, -3, -5]. Its all the way in the bottom, please help me. public class ourArray { //define an array of integers private int[] Ar; //Ar is a reference //declare private int capacity = 100; private int size = 0; private int increment = 10; //default constructor public ourArray() { Ar = new int[100]; capacity = 100; size = 0; increment = 10; } //Constructor that accepts the capacity (C). It creates an array of C integersm, sets capacity to C // Increment to 10 public ourArray(int C) { Ar = new int [C]; size = 0; capacity = C; increment = 10; } //Constructor that accepts two integers (c, i) c for capacity and i for increment public ourArray (int C, int I) { Ar = new int[C]; size = 0; capacity = C; increment = I; } //setter for Increment public void setincrement(int I) { increment = I; } //getter for size, capacity…arrow_forward
- Write an application that displays the strings in the provided array alphabetically in ascending order. import java.util.*; public class StringSort { publicstaticvoidmain(String[] args) { String[] values = {"mouse","dog","cat","horse","cow", "moose","tiger","lion","elephant","bird","hamster", "guinea pig","leopard","aardvark","hummingbird"}; }arrow_forwardimport java.util.Arrays; import java.util.Random; public class Board { /** * Construct a puzzle board by beginning with a solved board and then * making a number of random moves. That some of the possible moves * don't actually change what the board looks like. * * @param moves the number of moves to make when generating the board. */ public Board(int moves) { throw new RuntimeException("Not implemented"); } The board is 5 X 5. You can add classes and imports like rand.arrow_forwardQuestion 16 What is output? import java.util.ArrayList; public class SimpleCar { @Override public String toString(){ return "I drive fast"; } public static void main(String[] args) { ArrayList myStuff; myStuff = new ArrayList(); myStuff.add(new String("Greetings")); myStuff.add(new Object()); myStuff.add(new SimpleCar()); for(Object item : myStuff) { System.out.println(item.toString()); } String Object SimpleCar java.lang.Object@19cc java.lang.Object@23fb java.lang.Object@ab79 java.lang.String@169b java.lang.Object@23fb java.lang.SimpleCar@a42b Greetings java.lang.Object@169b I drive fast }arrow_forward
- *JAVA* complete method Delete the largest valueremoveMax(); Delete the smallest valueremoveMin(); class BinarHeap<T> { int root; static int[] arr; static int size; public BinarHeap() { arr = new int[50]; size = 0; } public void insert(int val) { arr[++size] = val; bubbleUP(size); } public void bubbleUP(int i) { int parent = (i) / 2; while (i > 1 && arr[parent] > arr[i]) { int temp = arr[parent]; arr[parent] = arr[i]; arr[i] = temp; i = parent; } } public int retMin() { return arr[1]; } public void removeMin() { } public void removeMax() { } public void print() { for (int i = 0; i <= size; i++) { System.out.print( arr[i] + " "); } }} public class BinarH { public static void main(String[] args) { BinarHeap Heap1 = new BinarHeap();…arrow_forwardMethod Details: public static java.lang.String getArrayString(int[] array, char separator) Return a string where each array entry (except the last one) is followed by the specified separator. An empty string will be return if the array has no elements. Parameters: array - separator - Returns: stringThrows:java.lang.IllegalArgumentException - When a null array parameter is providedarrow_forwardPlease help fastarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education