Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hey guys Im having trouble with a few simple functions i need to do for my class in Java... Any help would be greatly appreciated
Hey guys Im having trouble with a few simple functions i need to do for my class in Java... Any help would be greatly appreciated ...
class BetterLoop | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
public static boolean contains(int [] values, int v) | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
/* TO DO: 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. | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} __________________________________________________________________________
|
_________________________________________________________
class SimpleArray | |
{ | |
public static int [] squareAll(int values[]) | |
{ | |
/* TO DO: 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. | |
/* TO DO: 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 squareAll(List values) | |
{ | |
List newValues = new LinkedList(); | |
/* TO DO: 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; | |
} | |
} |
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