Question

Generics Program

Problem 2 Generics In the following problem you will illustrate how we determine the greatest of 3 arguments using a single Generic method. Youll want to restrict the kinds of types that are allowed to be passed to Comparable objects. Create a GUI application which tests a method called maximum by calling the method 3 times; first with 3 ints, then with 3 doubles, and finally with 3 string objects as parameters. Your method maximum will determine the greatest of the 3 comparable objects. clicking on the Display button will generate output to the JTextArea. You must write a single generic method declaration that can be called with arguments of different types You may simply hard code the data for the arguments when you call method maximum. You may wish to use the Fahrenheit example from module 1 as a guide Maximum Display Max of 3, 4, 5: 5 Max of 6.6, 8.8, 7.7: 8.8 Max of apple, pear, and orange: pear

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

PROGRAM CODE:

package GUI;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

public class MaximumThreeGeneric {

   //Generic method for fins the maximum of three values

   public static <T extends Comparable<T>> String maximum(T arg1, T arg2, T arg3)

   {

       String result = "";

       if(arg1.compareTo(arg2) > 0)

       {

           if(arg1.compareTo(arg3) > 0)

               result = arg1.toString();

           else

               result = arg3.toString();

       }

       else if(arg2.compareTo(arg3) > 0)

       {

           if(arg2.compareTo(arg1) > 0)

               result = arg2.toString();

           else result = arg1.toString();

       }

       else result = arg3.toString();

       return result;

   }

  

  

  

   public static void main(String[] args) {

       JFrame frame = new JFrame("Maximum");

       JPanel pane = new JPanel();

       //creating a textarea to display the result

       String result1 = maximum(3, 4, 5);

       String result2 = maximum(6.6, 8.8, 7.7);

       String result3 = maximum("apple", "pear", "orange");

       JTextArea area = new JTextArea(5, 20);

       area.setEditable(false);

      

       //creating a button to handle the displaying part

       JButton display = new JButton("Display");

       display.setSize(100, 30);

       display.addActionListener(new ActionListener() {

          

           @Override

           public void actionPerformed(ActionEvent e) {

               // TODO Auto-generated method stub

               area.setText("Max of 3, 4, 5: " + result1 + "\n");

               area.append("Max of 6.6, 8.8, 7.7: " + result2 + "\n");

               area.append("Max of apple, pear and orange: " + result3);

           }

       });

      

       //settings for the frame

       pane.setLayout(new BorderLayout());

       pane.add(display, BorderLayout.NORTH);

       pane.add(area, BorderLayout.CENTER);

       frame.setContentPane(pane);

       frame.setVisible(true);

       frame.setMinimumSize(new Dimension(400, 400));

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.pack();

      

   }

  

}

OUTPUT:

Maximum Display Max of 3, 4, 5: 5 Max of 6.6, 8.8, 7.7: 8.8 Max of apple, pear and orange: pear

Add a comment
Know the answer?
Add Answer to:
Generics Program In the following problem you will illustrate how we determine the greatest of 3...
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
  • what is the solution for this Java problem? Generics Suppose you need to process the following...

    what is the solution for this Java problem? Generics Suppose you need to process the following information regarding Students and Courses: A Student has a name, age, ID, and courseList. The class should have a constructor with inputs for setting name, ID and age. The class should have setters and getters for attributes name, ID and age, and a method addCourse, removeCourse, printSchedule. courseList: use the generic class ArrayList<E> as the type of this attribute addCourse: this method takes as...

  • Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we ...

    Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is closest to the end (or the front) of a list, etc. We can imagine writing a simple class which will do this: a class which will let us enter new results one by one, and at any point will...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to...

    Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to display several different types (classes) of objects. Let's say, we want to display three objects of type Deer, two objects of type Tree and one object of type Hill. Each of them contains a method called display. We would like to store their object references in a single array and then call their method display one by one in a loop. However, in Java,...

  • Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu:...

    Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu: Enter Circle Enter Rectangle Remove Shape Draw Shapes Exit Circles – User inputs position, radius, and color. The position is the CENTER of the circle Rectangles – User inputs position, height, width, color. The position is the lower left-hand corner Colors – Allow red, yellow, blue, and green only Remove – Show the number of items in the list and let the user enter...

  • Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template...

    Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template (20 pts) – TempDriver.cpp Template Exercise Write template for two functions called minimum and maximum. Each function should accept two arguments and return the lesser or greater of the two values. Test these templates in a driver program. The template with the following types: int, double, string and FeetInches (which is an object). You will need: The FeetInches class (which was provided in Week...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

  • Assignment You will be developing a speeding ticket fee calculator. This program will ask for a t...

    Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...

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