Question

In: Computer Science

public class AllEqual { // You must define the allEqual method, which will return // true...

public class AllEqual {
    // You must define the allEqual method, which will return
    // true if either:
    // 1.) The given array contains fewer than two elements, or...
    // 2.) All elements of the array are equal to each other.
    //     As a hint, you only need to compare the first element
    //     to all subsequent elements for this check.
    //
    // TODO - define your code below this comment
    //

    // DO NOT MODIFY parseStrings!
    public static int[] parseStrings(String[] strings) {
        int[] retval = new int[strings.length];
        for (int x = 0; x < strings.length; x++) {
            retval[x] = Integer.parseInt(strings[x]);
        }
        return retval;
    }

    // DO NOT MODIFY main!
    public static void main(String[] args) {
        int[] argsAsInts = parseStrings(args);
        boolean areEqual = allEqual(argsAsInts);
        System.out.println("Are equal: " + areEqual);
    }
}

Solutions

Expert Solution

Below is the complete program(including allequal function) -

public class AllEqual 
{
    public static boolean allEqual(int n[])
    {
        int k=n.length;
        boolean b=false;
        if(k<2)
        {
           b=true;
        }
        for(int i=1;i<k;i++)
        {
            if(n[0]==n[i])
            {
                b=true;
            }
        }
        return b;
    }
    public static int[] parseStrings(String[] strings) 
    {
        int[] retval = new int[strings.length];
        for (int x = 0; x < strings.length; x++) {
            retval[x] = Integer.parseInt(strings[x]);
        }
        return retval;
    }

    // DO NOT MODIFY main!
    public static void main(String[] args) 
    {
        int[] argsAsInts = parseStrings(args);
        boolean areEqual = allEqual(argsAsInts);
        System.out.println("Are equal: " + areEqual);
    }
}

Below is the only allEqual() function that needs to be copied to the actual program (in above program, everything is mentioned) (this function is subpart of it) -

public static boolean allEqual(int n[])
{
    int k=n.length;
    boolean b=false;
    if(k<2)
    {
        b=true;
    }
    for(int i=1;i<k;i++)
    {
        if(n[0]==n[i])
        {
            b=true;
        }
    }
    return b;
}

Related Solutions

public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
public class AddValueNewArray { // You must define the addValueNew method, which behaves // similarly to...
public class AddValueNewArray { // You must define the addValueNew method, which behaves // similarly to the addValueTo method in AddValueToArray.java. // However, instead of adding the given value to the given // array, it will return a _new_ array, where the new array // is the result of adding the value to each element of the // given array. To be clear, the given array NEVER CHANGES. // // TODO - define your code below this comment // //...
import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing...
import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing a letter // grade (e.g., "A", "A-", "B+", etc.). // The letter grade is determined by the given percentage, according // to the scale specified on page 2 of the class syllabus (available // here: https://kyledewey.github.io/comp110-spring18/syllabus.pdf). // You may assume that the given percentage is between 0.0 and 100.0 // TODO - write your code below this comment.    public static String letterGrade(double percentage){...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // As a hint, you should not have to use the name of each // month more than once. // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month...
import java.util.Scanner; public class Months { // You will need to write a method that makes...
import java.util.Scanner; public class Months { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month = input.nextInt(); String output = monthAsString(month); System.out.println(output); } }
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT