Question

Question 0: package hw3; public class Question0 { /* * TODO: Complete the method isSorted that...

Question 0:

package hw3;

public class Question0 {

        /*
         * TODO: Complete the method isSorted that
         * takes in as input an array of String 
         * and returns back a boolean value whether it 
         * is sorted in Lexicographical order or not. 
         * You can read up more on this here: 
         * https://en.wikipedia.org/wiki/Lexicographical_order
         */
        public static boolean isSorted(String[] array) 
        { 
                
                
                
                
                
                
                
                
                
                
        }


        
        /*
         * Do not write any code inside the main method and expect it to get marked. 
         * Any code that you write inside the main method will be ignored. However, 
         * you are free to edit the main function in any way you like for 
         * your own testing purposes. 
         */
        public static void main(String[] args)
        {
        
                String[] items={"a","b","c"};
                System.out.println(Question0.isSorted(items)); //must print true
                
                
                String[] items1={"c","b","a"};
                System.out.println(Question0.isSorted(items1)); //must print false
                
                String[] items2={"111","112","131","132"};
                System.out.println(Question0.isSorted(items2)); //must print true
                
                String[] items3={"a","aa","b","bb","c","ca"};
                System.out.println(Question0.isSorted(items3)); //must print true
                
                String[] items4={"z","zz","zzz","zzzz"};
                System.out.println(Question0.isSorted(items4)); //must print true
                
        }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package hw3;

public class Question0 {

    /*
     * TODO: Complete the method isSorted that
     * takes in as input an array of String
     * and returns back a boolean value whether it
     * is sorted in Lexicographical order or not.
     * You can read up more on this here:
     * https://en.wikipedia.org/wiki/Lexicographical_order
     */
    public static boolean isSorted(String[] array)
    {
        for (int i = 1; i < array.length; i++) {
            if (array[i-1].compareTo(array[i]) > 0) {
                return false;
            }
        }
        return true;
    }
    
    /*
     * Do not write any code inside the main method and expect it to get marked.
     * Any code that you write inside the main method will be ignored. However,
     * you are free to edit the main function in any way you like for
     * your own testing purposes.
     */
    public static void main(String[] args)
    {

        String[] items={"a","b","c"};
        System.out.println(Question0.isSorted(items)); //must print true


        String[] items1={"c","b","a"};
        System.out.println(Question0.isSorted(items1)); //must print false

        String[] items2={"111","112","131","132"};
        System.out.println(Question0.isSorted(items2)); //must print true

        String[] items3={"a","aa","b","bb","c","ca"};
        System.out.println(Question0.isSorted(items3)); //must print true

        String[] items4={"z","zz","zzz","zzzz"};
        System.out.println(Question0.isSorted(items4)); //must print true

    }
}
Add a comment
Know the answer?
Add Answer to:
Question 0: package hw3; public class Question0 { /* * TODO: Complete the method isSorted that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing...

    LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops. The methods you will be writing have nothing to do with each other but allow you to use the skills you have gained so far to solve a series of small problems. Power For this method you will practice using methods from the Math class. The first step you must take is to write the...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

  • Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** *...

    Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** * A class that models a bounding ball */ public class Ball {    // Minimum and maximum x and y values for the screen    private static double minX;    private static double minY;    private static double maxX;    private static double maxY;    private Point center;    private double radius;    // Every time the ball moves, its x position changes by...

  • What will be the output of the following Java code? class Output { public static void...

    What will be the output of the following Java code? class Output { public static void main(String args[]) { boolean a = true; boolean b = false; boolean c = a ^ b; System.out.println(!c); } } a) 0 b) 1 c) false d) true

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...

    Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...

  • 10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use...

    10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use the main method to test your code. Submit the file AppendRec.java. There is no need to delete the main method, the autograder will only grade appendNTimes. The method appendNTimes is recursive and takes two arguments, a string and an integer. It returns the original string appended to the original string n times. The method signature is as follows public static String appendNTimes ( String...

  • help with java OOP, here is the started code: package P2; public class Farm {   ...

    help with java OOP, here is the started code: package P2; public class Farm {    private double availableFood;    private Animal[] animals;    public Farm() {        setAvailableFood(1000);        animals = new Animal[4];        animals[0] = new Chicken();        animals[1] = new Cow();        animals[2] = new Llama();        animals[3] = new Llama();    }    public void makeNoise(){           // all animals make their sound (Moo, Cluck, etc)        for(Animal...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT