Write the following methods and provide a program to test them.boolean allTheSame(int x, int y, int z),returning true if the arguments are all the sameboolean allDifferent(int x, int y, int z),returning true if the arguments are all differentboolean sorted(int x, int y, int z),returning true if the arguments are sorted with the smallest one coming first
Question
Write the following methods and provide a program to test them.boolean allTheSame(int x, int y, int z),returning true if the arguments are all the sameboolean allDifferent(int x, int y, int z),returning true if the arguments are all differentboolean sorted(int x, int y, int z),returning true if the arguments are sorted with the smallest one coming first
Solution
Sure, here are the methods:
public static boolean allTheSame(int x, int y, int z) {
return x == y && y == z;
}
public static boolean allDifferent(int x, int y, int z) {
return x != y && x != z && y != z;
}
public static boolean sorted(int x, int y, int z) {
return x <= y && y <= z;
}
And here is a simple program to test them:
public static void main(String[] args) {
int x = 5, y = 5, z = 5;
System.out.println("All the same: " + allTheSame(x, y, z)); // should return true
x = 3; y = 4; z = 5;
System.out.println("All different: " + allDifferent(x, y, z)); // should return true
x = 1; y = 2; z = 3;
System.out.println("Sorted: " + sorted(x, y, z)); // should return true
}
This program first tests the allTheSame method with three identical numbers, then tests the allDifferent method with three distinct numbers, and finally tests the sorted method with three numbers in ascending order. The expected output for each method call is commented next to it.
Similar Questions
Write a functionbool equals(int a[], int a_size, int b[], int b_size)that checks whether two arrays have the same elements in the same order.
Write a Comparator class with the following 3 overloaded compare methods: boolean compare(int a, int b): Return true if int a = int b, otherwise return false.boolean compare(string a, string b): Return true if string a = string b, otherwise return false.boolean compare(int[] a, int[] b): Return true if both of the following conditions hold true: Otherwise, return false.Arrays a and b are of equal length.For each index i (where 0 ≤ i < |a|, |b|), a[i] = b[i].
Match the following logical functions with their use.1. IF A – Return TRUE if all of its arguments are TRUE.2. NOT B – Specifies a logical test to perform3. AND C – Reverses the logic of its argument 1-A, 2-C, 3-B1-B, 2-A, 3-C1-B, 2-C, 3-A1-A, 2-B, 3-CClear ResponseSave & Next
Write a function sort3(int& a, int& b, int& c) that swaps its three arguments toarrange them in sorted order. For example,int v = 3;int w = 4;int x = 1;sort3(v, w, x)
Write a functional program that sorts 10 input integers (signed) using either bubble sort orselection sort algorithm. The output window must show both unsorted array of integers andsorted array of integers.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.