Question

LAB: What order? (generic methods)

13.4 LAB: What order? (generic methods)

Define a generic method called checkOrder() that checks if four items are in ascending, neither, or descending order. The method should return -1 if the items are in ascending order, 0 if the items are unordered, and 1 if the items are in descending order.

The program reads four items from input and outputs if the items are ordered. The items can be different types, including integers, Strings, characters, or doubles.

Ex. If the input is:

bat hat mat sat
63.2 96.5 100.1 123.5

the output is:

Order: -1
Order: -1



WhatOrder.java

import java.util.Scanner;

public class WhatOrder {
   // TODO: Define a generic method called checkOrder() that 
   //       takes in four variables of generic type as arguments. 
   //       The return type of the method is integer
   
   
      // Check the order of the input: return -1 for ascending, 
      // 0 for neither, 1 for descending



   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      // Check order of four strings
      System.out.println("Order: " + checkOrder(scnr.next(), scnr.next(), scnr.next(), scnr.next())); 

      // Check order of four doubles
      System.out.println("Order: " + checkOrder(scnr.nextDouble(), scnr.nextDouble(), scnr.nextDouble(), scnr.nextDouble()));
   }
}


2 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

WhatOrder.java

import java.util.Scanner;

public class WhatOrder {
   
   public static > Integer checkOrder(T i1, T i2, T i3, T i4) {
      
      if ((i1.compareTo(i2) < 0) && (i2.compareTo(i3) < 0) && (i3.compareTo(i4) < 0)) {
         return -1;
      }
      else if ((i4.compareTo(i3) < 0) && (i3.compareTo(i2) < 0) && (i2.compareTo(i1) < 0)) {
         return 1;
      }
      else {
         return 0;
      }
      
   }
   
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      
      System.out.println("Order: " + checkOrder(scnr.next(), scnr.next(), scnr.next(), scnr.next())); 
      
      System.out.println("Order: " + checkOrder(scnr.nextDouble(), scnr.nextDouble(), scnr.nextDouble(), scnr.nextDouble()));
      
   }
   
}


Add a comment
Know the answer?
Add Answer to:
LAB: What order? (generic methods)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • C++ Define a generic function called CheckOrder() that checks if four items are in ascending, neither,...

    C++ Define a generic function called CheckOrder() that checks if four items are in ascending, neither, or descending order. The function should return -1 if the items are in ascending order, 0 if the items are unordered, and 1 if the items are in descending order. The program reads four items from input and outputs if the items are ordered. The items can be different types, including integers, strings, characters, or doubles. Ex. If the input is: bat hat mat...

  • LAB: Plant information (ArrayList)

    10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10  flower Hydrangea 30 false lilac  flower Rose 6 false white plant Mint 4...

  • LAB: Pet information (derived classes)

    LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information:     Name: Dobby    Age: 2 Pet Information:     Name: Kreacher    Age: 3    Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Pet myPet = new Pet();       Dog myDog = new Dog();              String petName, dogName, dogBreed;       int petAge, dogAge;       petName = scnr.nextLine();       petAge = scnr.nextInt();       scnr.nextLine();...

  • LAB: Book information (overriding member methods)

    10.15 LAB: Book information (overriding member methods)Given main() and a base Book class, define a derived class called Encyclopedia. Within the derived Encyclopedia class, define a printInfo() method that overrides the Book class' printInfo() method by printing not only the title, author, publisher, and publication date, but also the edition and number of volumes.Ex. If the input is:The Hobbit J. R. R. Tolkien George Allen & Unwin 21 September 1937 The Illustrated Encyclopedia of the Universe James W. Guthrie Watson-Guptill 2001 2nd 1the output is:Book Information:     Book Title: The Hobbit    Author: J. R. R. Tolkien    Publisher: George Allen & Unwin    Publication Date: 21 September 1937 Book Information:     Book Title: The Illustrated Encyclopedia of the Universe    Author: James W. Guthrie    Publisher: Watson-Guptill    Publication Date: 2001    Edition: 2nd    Number of Volumes: 1Note: Indentations use 3 spaces.BookInformation.javaimport java.util.Scanner; public class BookInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Book myBook = new Book();...

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

  • 7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the...

    7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the quicksort() and partition() methods to sort the IDs in ascending order using the Quicksort algorithm, and output the sorted IDs one per line. Ex. If the input is: kaylasimms julia myron1994 kaylajones -1 the output is: julia kaylajones kaylasimms myron1994 Code: import java.util.Scanner; import java.util.ArrayList; public class UserIDSorting { // TODO: Write the partitioning algorithm - pick the middle element as the // pivot,...

  • LAB: Instrument information (derived classes)

    10.14 LAB: Instrument information (derived classes)Given main() and the Instrument class, define a derived class, StringInstrument, for string instruments.Ex. If the input is:Drums Zildjian 2015 2500 Guitar Gibson 2002 1200 6 19the output is:Instrument Information:     Name: Drums    Manufacturer: Zildjian    Year built: 2015    Cost: 2500 Instrument Information:     Name: Guitar    Manufacturer: Gibson    Year built: 2002    Cost: 1200    Number of strings: 6    Number of frets: 19InstrumentInformation.javaimport java.util.Scanner; public class InstrumentInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Instrument myInstrument = new Instrument();       StringInstrument myStringInstrument = new StringInstrument();       String instrumentName, manufacturerName, stringInstrumentName, stringManufacturer;       int yearBuilt, cost, stringYearBuilt, stringCost, numStrings, numFrets;       instrumentName = scnr.nextLine();       manufacturerName = scnr.nextLine();       yearBuilt = scnr.nextInt();       scnr.nextLine();       cost = scnr.nextInt();       scnr.nextLine();       stringInstrumentName = scnr.nextLine();       stringManufacturer = scnr.nextLine();       stringYearBuilt = scnr.nextInt();       stringCost = scnr.nextInt();       numStrings = scnr.nextInt();       numFrets = scnr.nextInt();       myInstrument.setName(instrumentName);       myInstrument.setManufacturer(manufacturerName);       myInstrument.setYearBuilt(yearBuilt);       myInstrument.setCost(cost);       myInstrument.printInfo();       myStringInstrument.setName(stringInstrumentName);       myStringInstrument.setManufacturer(stringManufacturer);       myStringInstrument.setYearBuilt(stringYearBuilt);       myStringInstrument.setCost(stringCost);       myStringInstrument.setNumOfStrings(numStrings);       myStringInstrument.setNumOfFrets(numFrets);       myStringInstrument.printInfo();       System.out.println("   Number of strings: " + myStringInstrument.getNumOfStrings());       System.out.println("   Number of frets: " + myStringInstrument.getNumOfFrets());    } }Instrument.javapublic class Instrument {     protected String instrumentName;     protected String instrumentManufacturer;     protected int yearBuilt, cost;     public void setName(String userName) {...

  • LAB: Zip code and population (generic types)

    13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • Grocery shopping list (linked list: inserting at the end of a list)

    import java.util.Scanner;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      ItemNode headNode;  // Create intNode objects                                                         ItemNode currNode;      ItemNode lastNode;      String item;      int i;      // Front of nodes list           ...

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