Question

2)- MyList Modification (Java) Modify the MyList class that you wrote for Programming Challenge 1 so...

2)- MyList Modification (Java)

Modify the MyList class that you wrote for Programming Challenge 1 so that the type parameter T should accept any type that implements the Comparable interface. Test the class in a program that creates one instance of MyList to store Integers, and another instance to store Strings.

Mylist.java

import java.util.*;
import java.math.BigDecimal;
public class MyList <T extends Number>
{

   private ArrayList <T> num = new ArrayList<>();
   static MyList<Number> list = new MyList<>();
   public ArrayList<T> getNum() {
       return num;
   }
   public void setNum(ArrayList<T> num) {
       this.num = num;
   }
   public T Largest()
   {
       T high=(T) list.getNum().get(0);
       for(Number a:num)
       {
           if(a.longValue()>high.longValue())
           {
               high=(T) a;
           }
       }
       return high;
   }
   public T Smallest()
   {
       T low=(T) list.getNum().get(0);
       for(Number a:num)
       {
           if(a.longValue()<low.longValue())
           {
               low=(T) a;
           }
       }
       return low;
   }
   public static <T extends Number> void add(T a)
   {
       list.getNum().add(a);

   }

   public static void main(String args[])
   {
       add(new BigDecimal(1));
       add(new BigDecimal(5));
       add(new BigDecimal(-1));
       add(new BigDecimal(9));
      
      
       System.out.println("Largest:"+list.Largest());
       System.out.println("Lowest:"+list.Smallest());
  
   }
}

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

import java.util.ArrayList;

public class MyList<T extends Comparable<T>> {
   private ArrayList<T> num = new ArrayList<>();
  
   public ArrayList<T> getNum() {
       return num;
   }

   public void setNum(ArrayList<T> num) {
       this.num = num;
   }

   public T Largest() {
       if(num.size()==0)
           return null;
      
       T high = (T) num.get(0);
       for (T a : num) {
           if (a.compareTo(high) > 0) {
               high = (T) a;
           }
       }
       return high;
   }

   public T Smallest() {
       if(num.size()==0)
           return null;
      
       T low = (T) num.get(0);
       for (T a : num) {
           if (a.compareTo(low) < 0) {
               low = (T) a;
           }
       }
       return low;
   }
  
   public void printList() {
       for(T a: num) {
           System.out.println(a);
       }
   }

   public void add(T a) {
       num.add(a);
   }

   public static void main(String args[]) {
      
       MyList<Integer> integerList = new MyList<>();
      
       integerList.add(new Integer(1));
       integerList.add(new Integer(5));
       integerList.add(new Integer(-1));
       integerList.add(new Integer(9));
       System.out.println("Printing Integer List:");
       integerList.printList();
       System.out.println("integerList Largest:" + integerList.Largest());
       System.out.println("integerList Lowest:" + integerList.Smallest());
      
       MyList<String> stringList = new MyList<>();
       stringList.add("First");
       stringList.add("Second");
       stringList.add("Third");
       stringList.add("Fourth");

       System.out.println("\nPrinting StringList:");
       stringList.printList();
       System.out.println("stringList Largest:" + stringList.Largest());
       System.out.println("stringList Lowest:" + stringList.Smallest());
      

   }
}


Sample Output:
AL Problems Tasks 모 Console X E Properties Debug <terminated MyList [Java Application] CAProgram FilesJava ire1.8.0 121 Print

Add a comment
Know the answer?
Add Answer to:
2)- MyList Modification (Java) Modify the MyList class that you wrote for Programming Challenge 1 so...
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
  • ble and instance variable, and then use clas 4. Please discuss the difference between class variable...

    ble and instance variable, and then use clas 4. Please discuss the difference between class variable and instant variable to complete the following code for the count of number of stud public class Student _//q1: fill this line to create a class variable num. public Student 1/ 22: fill this line to increase num. public static int getNum() return num; public static void main(String[] args) { Student stl=new Student O; Student st2-new Student ; Student st3-new Student ; //q3: fill...

  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

  • In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams...

    In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed;    public Racer(String name, int year, int topSpeed){    this.name = name; this.year = year; this.topSpeed = topSpeed;    }    public String toString(){    return name + "-" + year + ", Top...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first number current num in main method but when I store after 10 second i'm not geting the update current number. NOTE: ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

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