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[] { 1, 7, 8, 11 })
* 10 == valRange (new double[] { 11, 7, 8, 1 })
* 18 == valRange (new double[] { 1, -4, -7, 7, 8, 11 })
* 24 == valRange (new double[] { -13, -4, -7, 7, 8, 11 })
*
* you should replace the line of code
* labeled TODO with code that achieves the above specification
* </pre>
*/
public static double valRange(double[] list) {
return -1; // TODO 1: fix this code
}
/**
* posOfLargestElementLtOeT returns the position of the largest element in the
* array that is less than or equal to the limit parameter if all values are
* greater than limit, return -1;
*
* Precondition: the array is nonempty and all elements are unique. Your
* solution must go through the array exactly once.
*
* <pre>
* 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0
* 5 == posOfLargestElementLtOeT(3, new double[] { 11, -4, -7, 7, 8, 1 }), // value:1 is in pos 5
* -1 == posOfLargestElementLtOeT(-7, new double[] { 1, -4, -5, 7, 8, 11 }), // all elements are > -7
*
* The code below is a stub version, you should replace the line of code
* labeled TODO with code that achieves the above specification
* </pre>
*/
public static int posOfLargestElementLtOeT(double limit, double[] list) {
return -2; // TODO 2: fix this
}
import java.util.Arrays;
public class Assignment {
public static double valRange(double[] list)
{
// if only one elementt in
list
if (list.length <= 1) {
return 0;
}
else
{
// consider 1
element as both max and min
double min =
list[0];
double max =
list[0];
for (int i =
1; i < list.length ; ++i )
{
// if new max found update max
if(list[i] > max)
{
max = list[i];
}
// if new min found update min
else if (list[i] < min)
{
min = list[i];
}
}
return (max -
min);
}
}
public static int posOfLargestElementLtOeT(double
limit, double[] list)
{
// consider initial index is
-1
int lastIndex = -1;
for (int i = 0; i < list.length
; ++i)
{
// if less
number found then update index
if(list[i] <
limit)
{
lastIndex = i;
}
}
// return index
return lastIndex;
}
public static void main(String[] args)
{
System.out.println("Value of
valRange");
System.out.println(valRange(new
double[] { -7 }));
System.out.println(valRange(new
double[] { 1, 7, 8, 11 }));
System.out.println(valRange(new
double[] { 11, 7, 8, 1 }));
System.out.println(valRange(new
double[] { 1, -4, -7, 7, 8, 11 }));
System.out.println(valRange(new
double[] { -13, -4, -7, 7, 8, 11 }));
System.out.println("\nValue of
posOfLargestElementLtOeT");
System.out.println(posOfLargestElementLtOeT(3, new double[] { -7
}));
System.out.println(posOfLargestElementLtOeT(3, new double[] { 11,
-4, -7, 7, 8, 1 }));
System.out.println(posOfLargestElementLtOeT(-7, new double[] { 1,
-4, -5, 7, 8, 11 }));
}
}
package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment { /** * valRange returns...
package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * * * * Find the 3 Sections marked TODO: * * TODO #1: SumOddsRecursive * TODO #2: ReverseArray * TODO #3: MergeArrays * * You must not add static variables. You MAY add static functions. * * It is okay to add functions, such as * * <pre> * public static double sumOfOddsHelper (double[] list, int i) { * </pre> * * but it is NOT okay to...
***Code to solve***
package functions;
import java.math.*; // for BigInteger
public class NodeTransitionFunction {
public NodeTransitionFunction(Integer exp, Integer
KVal) {
// CONSTUCTOR: Sets the class to
calculate f(x) = (x ^ exp) mod KVal
// TODO
}
public Integer apply(Integer val) {
// PRE: -
// POST: Implements f(val)
return null;
}
public BigInteger apply(BigInteger val) {
// PRE:...
Fix all sections marked TO DO. Must show output. package JavaExcercise; import java.util.Arrays; import stdlib.*; /** * Do not change the declaration of any method. * Each of the functions below is meant to be SELF CONTAINED. This means that * you should use no other functions or classes unless otherwise indicated. * You should not use any HashSets or ArrayLists, or anything else. * In addition, each of your functions should go * through the argument array at most...
import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...
Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...
/** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList { /** * @param args */ public static void main(String[] args) { List groceryList = new ArrayList<>(); groceryList.add("rice"); groceryList.add("chicken"); groceryList.add("cumin"); groceryList.add("tomato"); groceryList.add("cilantro"); groceryList.add("lime juice"); groceryList.add("peppers"); groceryList.remove("cilantro"); for (int i = 0; i if (groceryList.get(i).equals("peppers")) {...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...
Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** * A class that models a bounding ball */ public class Ball { // Minimum and maximum x and y values for the screen private static double minX; private static double minY; private static double maxX; private static double maxY; private Point center; private double radius; // Every time the ball moves, its x position changes by...
package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput; /** Write a program to roll a set of dice. Generate a random number between 1 and 6 for each dice to be rolled, and save the values in an ArrayList. Display the total of all the dice rolled. In some games, rolling the same number on all dice has a special meaning. In your program, check if all dice have the same value,...
ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...