Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 intList = new MyList<>(); // adding the numbers mentioned in the question intList.add(2); intList.add(5); intList.add(-13); intList.add(11); intList.add(12);

// creating a Double list, adding double values to it MyList doubleList = new MyList<>(); doubleList.add(27.3); doubleList.add(5.7); doubleList.add(-23.9); doubleList.add(1.11); doubleList.add(5.12);

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 { // defining an array list as storage ArrayList list;

// 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions