Question
In this homework, you will write a program modify program you wrote in Homework3. 2) Modify the MyList class that you wrote for Programming challenge
In this homework, you will write a program modify program you wrote in Homework3.
2) Modify the MyList class that you wrote for Programming challenge 1 so 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. Input two numbers from the user and insert them into the first instance of Modified MyList.
Input name and City and insert the two strings into the second instance of modified MyList class. Then display the contents of the the two instances.
here is the program to modify
import java.util.ArrayList; import java.util.Scanner;
public class Main { public static void main(String[] args) { // part 1 code. // creating a Number list MyList
// creating a Double list, adding double values to it MyList
System.out.println("PART 1"); // printing largest and smallest of both lists System.out.println("The Integer largest: " + intList.largest()); System.out.println("The Integer smallest: " + intList.smallest()); System.out.println("The Double largest: " + doubleList.largest()); System.out.println("The Double smallest: " + doubleList.smallest());
} }
//MyList class, which is defined outside Generics1 class BUT still within Generics1.java file. class MyList
// constructor initializing list public MyList() { // initializing array list list = new ArrayList<>(); }
// method to add a value to the list public void add(T value) { list.add(value); }
// method to return the largest element on the list public T largest() { // assuming list is not empty, taking first value as largest T max = list.get(0); for (int i = 1; i < list.size(); i++) { // comparing element at i with max using doubleValue() of Number class if (list.get(i).doubleValue() > max.doubleValue()) { max = list.get(i); } } return max; }
// method to return the smallest element on the list public T smallest() { // assuming list is not empty, taking first value as smallest T min = list.get(0); for (int i = 1; i < list.size(); i++) { // comparing element at i with min using doubleValue() of Number class if (list.get(i).doubleValue() < min.doubleValue()) { min = list.get(i); } } return min; }
// returns the elemnt at a given position public T get(int index) { return list.get(index); } }
Here is the sample input/outPut
TestCase1:
PART 2 Please enter a number: 30 Please another number: 20 Please enter your name: John Please enter your City: Los Angeles First number is 30 Second number is 20 Name is: John City is: Los Angeles
TestCase2:
PART 2 Please enter a number: 90 Please another number: 45 Please enter your name: Susan Please enter your City: Culver City First number is 90 Second number is 45 Name is: Susan City is: Culver City
please have the system output matching with the test cases.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started