Question

Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, wit
Please Do It With Java. Make it copy paste friendly so i can run and test it.
Thnak You.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy

Question 1
MyList.java

//header files
import java.util.*;

//create class name MyList with type parameter T which extends of class Number
public class MyList<T extends Number>
{
   //declare variable as array
    ArrayList<T> list = new ArrayList<T>();

    //add number to array list
    public void add(T add)
    {
        list.add(add);
    }
  
    //which return largest number from the list
    public T largest()
    {
        T largestNum = list.get(0);
        for(int i = 1; i < list.size(); i++)
        {
            if (list.get(i).doubleValue() > largestNum.doubleValue())
            {
                largestNum = list.get(i);
            }
        }
        return largestNum;
    }
  
    //which returns smallest value from the list
    public T smallest()
    {
        T smallestNum = list.get(0);
        for(int i = 1; i < list.size(); i++)
        {
            if (list.get(i).doubleValue() < smallestNum.doubleValue())
            {
                smallestNum = list.get(i);
            }
        }
        return smallestNum;
    }

    //main method
    public static void main(String[] args)
    {

       //create object as integer
        MyList<Integer> numbers = new MyList<Integer>();

        Random randomNum=new Random();
        System.out.print("Numbers are: ");
        for (int i = 0; i < 6; i++)
        {
            int value=5+randomNum.nextInt(1000);
            numbers.add(value);
            System.out.print(value+", ");
        }

        //display largest and smallest number on the screen
        System.out.println("\nLargest num: " + numbers.largest());
        System.out.println("Smallest num: " + numbers.smallest());
    }

}


===================================================================================

Question 2

MyList2.java

//header files
import java.util.*;

//modify MyList class that implements Comparable interface
public class MyList2 <T extends Comparable<T>>
{
   //create list of object using array
    ArrayList<T> list = new ArrayList<T>();

    //add number to arraylist
    public void add(T add)
    {
        list.add(add);
    }

    //it returns largest number from the list
    public T largest()
    {
        if(list.size()>0)
        {
            int largestNum=0;
            for (int i = 1; i <list.size() ; i++)
            {
                if(list.get(i).compareTo(list.get(largestNum))>0)
                largestNum=i;
            }
            return list.get(largestNum);
        }
        return null;
    }

    //it returns smallest number from the list
    public T smallest()
    {
        if(list.size()>0)
        {
            int smallestNum=0;
            for (int i = 0; i <list.size() ; i++)
            {
                if(list.get(i).compareTo(list.get(smallestNum))<0)
                smallestNum=i;
            }
            return list.get(smallestNum);
        }
        return null;
    }

    public static void main(String [] args)
    {

       //create object as type Integer
        MyList2<Integer> integerMyList2=new MyList2<>();
        Random randomNum=new Random();
        System.out.print("Numbers are: ");
      
        for (int i = 1; i < 6; i++)
        {
            int value=5+randomNum.nextInt(1000);
            integerMyList2.add(value);
            System.out.print(value+", ");
        }
      
        //display largest and smallest integer number
        System.out.println("\nLargest number: " + integerMyList2.largest());
        System.out.println("Smallest number: " + integerMyList2.smallest());
        System.out.println("-------------------------------------\n");

        //create object type as String
        MyList2<String> stringMyList2=new MyList2<>();
        String [] names = {"Murat","Nazar","Oraz","Mustafa","Tylla"};
        System.out.print("Names are: ");
        for (String name : names)
        {
            stringMyList2.add(name);
            System.out.print(name + ", ");
        }
      
        //display largest and smallest string
        System.out.println("\nLargest String:   "+stringMyList2.largest());
        System.out.println("Smallest String : "+stringMyList2.smallest());
        System.out.println("-----------------------------------------------");
    }

}

===================================================================================

Question 6

Highest and lowest elements

MyArray.java

//create array which extends comparable interface
public class MyArray<T extends Comparable<T>>
{
   //declare variables
   public T[] values;

   //add values to the list
public MyArray(T[] add)
{
    this.values =add;
}

//display highest value from the list
public T highest()
{
    if(values.length>0)
    {
      int highestNum=0;
      for (int i = 0; i < values.length; i++)
        if(values[i].compareTo(values[highestNum])>0)
          highestNum=i;
      return values[highestNum];
    }
    return null;
}

//display lowest value from the list
public T lowest()
{
    if(values.length>0)
    {
      int lowestNum=0;
      for (int i = 0; i < values.length; i++)
        if(values[i].compareTo(values[lowestNum])<0)
          lowestNum=i;
      return values[lowestNum];
    }
    return null;
}

public static void main(String [] args)
{

      //create object as integer
      Integer[] values ={-55,0,23,99,70,-5};
      MyArray<Integer> integerMyArray=new MyArray<>(values);

      System.out.print("Values are: ");
      for (Integer value : values)
      {
          System.out.print(value + ", ");

      }
  
      //display highest value and lowest value from the list
      System.out.println("\nHighest Value: "+integerMyArray.highest());
      System.out.println("Lowest Value: "+integerMyArray.lowest());
}
}

==============================================================================

Program Screenshot

Question 1

MyList.java

MyList.java X //header files import java.util.*; //create class name MyList with type parameter T which extends of class Numb

MyList.java X T smallestNum = list.get(0); for(int i = 1; i < list.size(); i++) { if (list.get(i).doubleValue() < smallestNum

===================================================================================

Question 2

MyList2.java

MyList.java MyList2.java X 7/header files import java.util.*; 1/modify MyList class that implements Comparable interface publ

MyList.java MyList2.java X //it returns smallest number from the list public T smallest() if(list.size() >0) int smallestNum=

MyList.java MyList2.java X int value=5+randomNum.nextInt(1000); integerMyList2.add(value); System.out.print(value+, ); //di

==================================================================================

Question 6

Highest and Smallest elements

MyList.java D MyList2.java MyArray.java X // create array which extends comparable interface public class MyArray<T extends C

D MyArray.java X MyList.java MyList2.java public I lowest() if(values.length>0) int lowestNum=0; for (int i = 0; i < values.l

==================================================================================

Sample output

Question 1

MyList.java

Problems @ Javadoc . Declaration Console X <terminated> MyList [Java Application] C:\Program Files\Java\jre1.8.0_201\bin\java

=================================================================================

Question 2

MyList2.java

Problems @ Javadoc Declaration Console X <terminated> MyList2 [Java Application] C:\Program Files\Java\jrel.8.O_201\bin\javaw

===================================================================================

Question 6

Highest and smallest elements

MyArray.java

A Problems @ Javadoc . Declaration Console X <terminated> MyArray [Java Application] C:\Program FilesVava\jre1.8.0_201\bin\ja

Add a comment
Know the answer?
Add Answer to:
Please Do It With Java. Make it copy paste friendly so i can run and test...
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
  • 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...

  • 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...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. 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...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. 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...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Please use java language in an easy way and comment as much as you can! Thanks...

    Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.

  • please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational...

    please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational class ****JAVA**** Extend the Number class and implement the Comparable interface to introduce new functionality to your Rational class. The class definition should look like this: public class Rational extends Number implements Comparable<Rational> { // code goes here } In addition a new private method gcd should be defined to ensure that the number is always represented in the lowest terms. Here is the...

  • Create an application that provides a solution for problem 20.8 In addition to requirements specified in...

    Create an application that provides a solution for problem 20.8 In addition to requirements specified in the description. The class must satisfy the following Default Constructor Two argument constructor for data used to initialize "first" and "second" "toString" method for displaying the "first" and "second" data elements Creates a "Comparator" object (to be used by the sort method; i.e. create an instance of a class that implements the interface [must override "compare" and "equals" methods]) The application must create an...

  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from an input file. This program consists of two classes. The first class is the Weight class, which is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods: 1. A public constructor that allows...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

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