public class lab0 extends Object
Modifier and Type | Method and Description |
---|---|
static int |
compareTo(Range r1,
Range r2)
[Question 8]
Compares two
Range instances by their widths. |
static boolean |
contains(int[] arr,
int a)
[Question 4]
Write a Java program to test if an array contains a specific value.
|
static int |
contains1(double x,
Range range)
[Question 7]
Determine if a value
x is strictly inside the given
Range . |
static double |
gaussian(double x,
int sigma)
[Question 3]
Computes the standard Gaussian distribution probability density function (see
the lab document for the actual formula) with scale parameter.
|
static boolean |
hasValidLengthAndSeparator(String s)
[Question 9]
Partially validates a string that is supposed to represent a time on a
24-hour clock.
|
static boolean |
isDivisible(int a,
int b)
[Question 1]
|
static boolean |
isPrime(int n)
[Question 2]
|
static int |
maximum(int[] arr)
[Question 5]
Java program to find largest number in an array
This maximum method check all elements of the input array of integers and
returns the maximum integer value
|
static int |
one()
Returns the value 1.
|
static int[] |
reverseOf(int[] inputarray)
[Question 6]
This method reverseOf received an array of integers, then return the input
array in reverse element order.
|
static List<Character> |
shuffle(List<Character> t)
Returns a new list of characters formed by shuffling the
characters of the given list.
|
static void |
sort2(List<Integer> t)
[Question 10a]
Sorts a list of two integers so that the elements are in ascending order.
|
static void |
toRadians(List<Double> t)
[Question 10b]
Replaces the elements of a list of angles in degrees with the equivalent
angles in radians.
|
static String |
toString(Range r)
Returns a string representation of a
Range that is different
than the one returned by Range.toString . |
public static int one()
public static boolean isDivisible(int a, int b)
Divisibility : When dividing an integer by a second nonzero integer, the quotient may or may not be an integer.
For example, 12/3 = 4 while 9/4 = 2.25.
Definition : If a
and b
are integers
with a
is not equal to zero , we say that b
divided by a
if there exists an integer c
such that b = ac
.
This method take two integers a
and b
, then it return true if
b
divided by a
Example: isDivisible ( 3, 5) returns false isDivisible ( 5, 21) returns false isDivisible ( 75, 512) returns false isDivisible ( 5, 10) returns true isDivisible ( 22, 198) returns true isDivisible ( 64, 512) returns true
a
- integer not equal to zerob
- integer not equal to zerob
divided by a
a != 0
, and b != 0
public static boolean isPrime(int n)
Primes
A positive integer n > 1
is called prime
if the only positive factors of n
are 1
and n
. A
positive integer that is greater than one and is not prime is called
composite.
An integer n
is composite if and only if there
exists an integer a
such that a
divides
n
and 1 < a < n
.
Hint: 1 is neither prime nor composite. It forms its own special category as a "unit".
This method checks the positive integer if it is prime or not.
Example: isPrime ( -5) returns false isPrime ( 6) returns false isPrime ( 25) returns false isPrime ( 2) returns true isPrime ( 3) returns true isPrime ( 13) returns true isPrime ( 17) returns true isPrime ( 29) returns true
n
- positive integern
is prime, else false @pre. n > 0
public static double gaussian(double x, int sigma)
x
- a valuesigma
- scale parameterpublic static boolean contains(int[] arr, int a)
You can assume that the array is not empty and contains at least 1 one element.
For example:
if arr = {3, 6} and a=4 then return false if arr = { 1, 1, 1} and a =1 then return true
arr
- is the input array of integersa
- is the separate integer to be found out from the arraypublic static int maximum(int[] arr)
You can assume that the array is not empty and contains at least 1 one element.
Your are forbidden to use any library class, e.g., Arrays.sort.
For example:
if arr = { 4, 2, 5} then return 5
arr
- is the input array of integerspublic static int[] reverseOf(int[] inputarray)
You can assume that the array is not empty and contains at least 1 one element.
You are forbidden to use any library class, e.g., Arrays.sort.
For example:
if inputarray = { 4, 2, 5} then return {5, 2, 4}
inputarray
- input array of integer need to be reverse element-wisepublic static int contains1(double x, Range range)
x
is strictly inside the given
Range
. A value exactly at the minimum or maximum of the
range is considered outside of the range.x
- a valuerange
- a Range to checkpublic static int compareTo(Range r1, Range r2)
Range
instances by their widths.r1
- a Ranger2
- a second Rangepublic static boolean hasValidLengthAndSeparator(String s)
true
if:
5
, AND
:
and false
otherwise.
s
- a string to validatepublic static void sort2(List<Integer> t)
t
- a listIllegalArgumentException
- if the size of list is not equal to 2public static void toRadians(List<Double> t)
t
- a list of angles in degreespublic static String toString(Range r)
Range
that is different
than the one returned by Range.toString
.
The string contains the minimum and maximum values separated by a comma and space all inside of a pair of parentheses. For example, the string
(-3.14, 3.14)
represents the range whose minimum value is -3.14
and whose
maximum value is 3.14
r
- a Range instancepublic static List<Character> shuffle(List<Character> t)
To shuffle the characters in t, imagine splitting the list t in half so that the first (n / 2) characters of t are in one sublist, and the remaining (n / 2) characters of t are in the second sublist. The new returned list is formed by adding the first character of the first sublist to the new list, then adding the first character of the second sublist, then adding the second character of the first sublist, then adding the second character of the second sublist, and so on, until all of the characters in the two sublists are added to the new list.
For example, if t was the list:
['a', 'b', 'c', 'd', 'e', 'f']
then splitting t into two sublists yields:
['a', 'b', 'c'] and ['d', 'e', 'f']
Take the first two characters of each sublist and add them to the new list:
['a', 'd']
Then take the next two characters of each sublist and add them to the new list:
['a', 'd', 'b', 'e']
Then take the next two characters of each sublist and add them to the new list:
['a', 'd', 'b', 'e', 'c', 'f']
t
- a non-null list of characters