Question
Could someone help me write these class definitions for Java? Test cases below. Thanks! class SimpleIf { public static double max(double x, double y) {
Could someone help me write these class definitions for Java? Test cases below. Thanks!
class SimpleIf
{
public static double max(double x, double y)
{
/* Write an if statement to determine which
argument is larger and return that value.
*/
return 0; // clearly not correct -- but testable
}
}
___________________________________________________________________________________________________
class SimpleLoop
{
public static int sum(int low, int high)
{
/* Return the sum of the integers between
low and high (inclusive). Yes, this can be
done without a loop, but the point is to
practice the syntax for a loop.
*/
return 0;
}
}
_____________________________________________________________________________________________________
class SimpleArray
{
public static int [] squareAll(int values[])
{
/* This size is not right. Fix it to work with any
input array. The length of an array is accessible through
an array's length field (e.g., values.length).
*/
int [] newValues = new int[1]; // This allocates an array of integers.
/* The output array, newValues, should hold as
its elements the square of the corresponding element
in the input array.
Write a loop to compute the square of each element from the
input array and to place the result into the output array.
*/
return newValues;
}
}
________________________________________________________________________________________________________
import java.util.List;
import java.util.LinkedList;
class SimpleList
{
public static List
{
List
/* The output list, newValues, should hold as
its elements the square of the corresponding element
in the input list.
Write a loop to add the square of each element from the
input list into the output list. Use a "foreach".
*/
return newValues;
}
}
_______________________________________________________________________________________________________________________
class BetterLoop
{
public static boolean contains(int [] values, int v)
{
/* if value v is in the array, return true.
If not, return false. Use a "foreach" loop.
*/
return true; // A bit optimistic, but a real boolean value.
}
}
_____________________________________________________________________________________________________
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
class ExampleMap
{
public static List
Map
{
List
/*
Build a list of the names of students currently enrolled
in a number of units strictly greater than the unitThreshold.
*/
return overEnrolledStudents;
}
}
/*
* This file should remain unchanged.
*/
__________________________________________________________________________________________
class Course
{
private final String name;
private final int numUnits;
public Course(final String name, final int numUnits)
{
this.name = name;
this.numUnits = numUnits;
}
public String getName()
{
return name;
}
public int getNumUnits()
{
return numUnits;
}
}
_____________________________________________________________________________________
Test cases:
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
public class TestCases
{
private static final double DELTA = 0.00001;
@Test
public void testSimpleIf1()
{
assertEquals(1.7, SimpleIf.max(1.2, 1.7), DELTA);
}
@Test
public void testSimpleLoop1()
{
assertEquals(7, SimpleLoop.sum(3, 4));
}
@Test
public void testSimpleArray1()
{
/* What are those parameters? They are newly allocated arrays
with initial values. */
assertArrayEquals(
new int[] {1, 4, 9},
SimpleArray.squareAll(new int[] {1, 2, 3}));
}
@Test
public void testSimpleList1()
{
List
new LinkedList
List
new ArrayList
assertEquals(expected, SimpleList.squareAll(input));
}
@Test
public void testBetterLoop1()
{
assertTrue(BetterLoop.contains(new int[] {7, 5}, 5));
}
@Test
public void testExampleMap1()
{
List
Map
courseListsByStudent.put("Sal",
Arrays.asList(
new Course("CS 123", 4),
new Course("CS101", 4),
new Course("CS 102", 4),
new Course("CS 103", 4),
new Course("CS 225", 4)));
courseListsByStudent.put("Tom",
Arrays.asList(
new Course("CS 101", 4),
new Course("CS 102", 4),
new Course("CS 103", 4),
new Course("CS 225", 4)));
courseListsByStudent.put("Jared",
Arrays.asList(
new Course("CS 123", 4),
new Course("CS 103", 4),
new Course("CS 474", 4),
new Course("CS 473", 4),
new Course("CS 476", 4),
new Course("CS 574", 4)));
/*
* Why compare HashSets here? Just so that the order of the
* elements in the list is not important for this test.
*/
assertEquals(new HashSet<>(expected),
new HashSet<>(ExampleMap.highEnrollmentStudents(
courseListsByStudent, 16)));
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started