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...
How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.HashSet; public class RotationCheck { /** * Rotates the elements in the specified list by the specified distance. After calling this * method, the element at index {@code i} will be the element previously at index * {@code (i - distance) % list.size()}, for all values of {@code i} between {@code 0} and * {@code...
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)...
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...
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 - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...