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.Scanner;

// Define the HighestLowestDemo class to test the HighestLowest class public class Main {

public static void main(final String[] args) {

try (Scanner input = new Scanner(System.in)) { // Collect data for three employee objects and create an array final Employee[] employees = new Employee[3]; for (int i = 0; i < 3; i++) { System.out.println("Collecting Employee" + (i + 1) + " Information"); System.out.println("Please enter Employee" + (i + 1) + " name:"); final String name = input.nextLine(); System.out.println("Please enter Employee" + (i + 1) + " Age:"); final int age = Integer.parseInt(input.nextLine()); employees[i] = new Employee(name, age); }

// Create an instance of HighestLowest class for employees final HighestLowest employeeHL = new HighestLowest<>(employees);

// Get the oldest and youngest employee final Employee oldestEmployee = employeeHL.getHighest(); final Employee youngestEmployee = employeeHL.getLowest(); System.out.println("The oldest employee is: " + oldestEmployee.getName()); System.out.println("The youngest employee: " + youngestEmployee.getName());

// Collect data for three city objects and create an array final City[] cities = new City[3]; for (int i = 0; i < 3; i++) { System.out.println("Collecting City" + (i + 1) + " Information"); System.out.println("Please enter City" + (i + 1) + " name:"); final String name = input.nextLine(); System.out.println("Please enter City" + (i + 1) + " population:"); final int population = Integer.parseInt(input.nextLine()); cities[i] = new City(name, population); }

// Create an instance of HighestLowest class for cities final HighestLowest cityHL = new HighestLowest<>(cities);

// Get the city with highest and lowest population final City highestPopulationCity = cityHL.getHighest(); final City lowestPopulationCity = cityHL.getLowest(); System.out.println("The city with highest population is: " + highestPopulationCity.getName()); System.out.println("The city with lowest population is: " + lowestPopulationCity.getName()); } catch (final NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); }

}

@Override public String toString() { return "Main []"; } }

// Define the generic class HighestLowest with a type parameter constrained to any type that implements Comparable class HighestLowest> {

// Instance variable to store the array of objects private final T[] objects;

// Constructor to initialize the array of objects public HighestLowest(final T[] objects) { this.objects = objects; }

// Method to get the highest object in the array public T getHighest() { T highest = objects[0]; for (int i = 1; i < objects.length; i++) { if (objects[i].compareTo(highest) > 0) { highest = objects[i]; } } return highest; }

// Method to get the lowest object in the array public T getLowest() { T lowest = objects[0]; for (int i = 1; i < objects.length; i++) { if (objects[i].compareTo(lowest) < 0) { lowest = objects[i]; } } return lowest; } }

// Define the Employee class that implements Comparable class Employee implements Comparable {

// Instance variables to store the name and age of an employee private final String name; private final int age;

// Constructor to initialize the name and age of an employee public Employee(final String name, final int age) { this.name = name; this.age = age; }

// Getter method for the name of an employee public String getName() { return name; }

// Getter method for the age of an employee public int getAge() { return age; }

// Method to compare two employee objects based on their age @Override public int compareTo(final Employee other) { if (this.age < other.age) { return -1; } else if (this.age > other.age) { return 1; } else { return 0; } } }

// Define the City class that implements Comparable class City implements Comparable {

// Instance variables to store the name and population of a city private final String name; private final int population;

// Constructor to initialize the name and population of a city public City(final String name, final int population) { this.name = name; this.population = population; }

// Getter method for the name of a city public String getName() { return name; }

// Getter method for the population of a city public int getPopulation() { return population; }

// Method to compare two city objects based on their population @Override public int compareTo(final City other) { if (this.population < other.population) { return -1; } else if (this.population > other.population) { return 1; } else { return 0; } } }

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!