Intro to JAVA see below:
-----------------------------------------------
Rewrite the algorithm for computing the maximum using an enhanced for loop. Complete the following code:
CODE:
import java.util.ArrayList;
public class ArrayListUtil
{
/**
Computes the maximum value of a
nonempty array list.
@param values a non-empty array
list
@return the largest value in the
array list
*/
public static double maximum(ArrayList<Double>
values)
{
// Compute the largest value, using
an enhanced for loop
. . .
for (. . . : . . .)
{
. . .
}
}
}
import java.util.ArrayList;
import java.util.Random;
public class ArrayListUtil {
public static void main(String[] args) {
// create instance of Random class
Random r = new Random();
ArrayList<Double> arrayList=new ArrayList<>();
//Creating ArrayList of 10 elements with random values
for(int i=0;i<10;i++)
{
arrayList.add((r.nextDouble()*100)); //multiply with 100 because it is generating too small values
}
//printing the arrayList
System.out.println(arrayList);
//printing the maximum number by calling the method maximum()
System.out.println("Maximum nuumber is : "maximum(arrayList));
}
public static double maximum(ArrayList<Double> values)
{
double max= values.get(0); //storing first element of ArrayList
for(double var :values) //enhanced for loop
{
if(var>max) //comparing the values
{
max=var; //saving the largest value in max variable
}
}
return max;
}
}
Output:
[62.687960432641695, 37.9760416525705, 64.93375388379197,
62.36734779294045, 84.4402779196424, 79.81372841881966,
78.46949647816666, 14.467514688378348, 46.86527059244102,
56.290937510586545]
Maximum nuumber is : 84.4402779196424
Intro to JAVA see below: ----------------------------------------------- Rewrite the algorithm for computing the maximum using an enhanced...
Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...
USE JAVA Using recursion, find the largest element in an array. Skeleton code is provided. Please do not change the DataSetDemo code. Hint: Find the largest element in the subset containing all but the last element. Then compare that maximum to the value of the last element. Skeleton Code: DataSet: /** Computes the maximum of a set of data values. */ public class DataSet { private int[] values; private int first; private int last; /** Constructs a DataSet object. @param...
(JAVA NetBeans)
Write programs in java
Example 9.13~9.15
//Book.Java
public class Book {
private String title;
private String author;
private double price;
/**
* default constructor
*/
public Book() {
title = "";
author = "";
price = 0.0;
}
/**
* overloaded constructor
*
* @param newTitle the value to assign to title
* @param newAuthor the value to assign to author
* @param newPrice the value to assign to price
*/
public Book(String newTitle, String newAuthor, double newPrice)...
In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...
Rewrite the following loops without using the enhanced for loop construct. Here, values is an array of floating-point numbers. a. for (double x : values) { total = total + x; } b. for (double x : values) { if (x == target) { return true; } } c. int i = 0; for (double x : values) { values[i] = 2 * x; i++; }
Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditional and iterative control structures that repeat actions as needed. The unit measurement is missing from the final output and I need it to offer options to lbs, oz, grams, tbsp, tsp, qt, pt, and gal. & fl. oz. Can this be added? The final output...
Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm Write a static function called RemoveNullElements() without creating another array. that remove Null elements and shift all the rest of the elements to the left/front (small indexes) of the array without changing the size or length of the original array( this meant the front of the array will have all the not Null elements, and the other haft will be empty/Null. Please provide test...
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...
package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment { /** * valRange returns the difference between the maximum and minimum values in the * array; Max-Min. Precondition: the array is nonempty. Your solution must go * through the array at most once. * * Here are some examples (using "==" informally): * * <pre> * 0 == valRange (new double[] { -7 }) * 10 == valRange (new double[]...