Modify the Student class in Listing 8.2 so that it implements the comparable interface. Define the compareTo method to order Student objects based on the value in studentNumber. In a main method create an array of at least five Student objects, sort them using Arrays.sort, and output the students. They should be listed by ascending student number Next, modify the compareTo method so it orders Student objects based on the lexicographic ordering of the name variable. Without modification to the main method, the
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Introduction To Programming Using Visual Basic (11th Edition)
Starting out with Visual C# (4th Edition)
Starting Out with Java: Early Objects (6th Edition)
Starting Out with C++: Early Objects (9th Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
- IN JAVA. Any help is appreciated! Thank you! PART 1 : Automobiles Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by year with Collections.sort(). Finally, use a foreach loop to display the ArrayList sorted by year.arrow_forwardComplete the Course class by implementing the findStudentHighestGpa() method, which returns the Student object with the highest GPA in the course. Assume that no two students have the same highest GPA. Given classes: Class Course represents a course, which contains an ArrayList of Student objects as a course roster. (Type your code in here.) Class Student represents a classroom student, which has three private fields: first name, last name, and GPA. (Hint: GetGPA() returns a student's GPA.) Ex: If the following students and their GPA values are added to a course: Henry Nguyen with 3.5 GPABrenda Stern with 2.0 GPALynda Robison with 3.2 GPASonya King with 3.9 GPA then the findHighestStudent() method returns a student and the program output is: Top student: Sonya King (GPA: 3.9)Code:Course.java: import java.util.ArrayList; //create class Student //create class Courseclass Courses{ private ArrayList<Student> roster; // collection of Student object // class constructor…arrow_forwardyou created a Card class that represents a standard playing card. Use this to design and implement a class called DeckOfCards that stores 52 objects of the Card class using an array. Include methods to shuffle the deck, deal a card, return the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck.Create a separate driver class that first outputs the populated deck to prove it is complete, shuffles the deck, and then deals each card from a shuffled deck, displaying each card as it is dealt along with the number of cards left in the deck. This is my Card class: mport java.util.Random; /*Card.java Richard Mino * This is a program demonstrating the use of random and predetermined objects and values in the form * of a playing card using getter/setter methods and two constructors and printing them out as a string * using the toString method */public class Card { //Set up variables for card rank and…arrow_forward
- Part I Create a Student class that implements Comparable interface from java.lang.Comparable. Student should have name, lastName and studentId properties. Comparison should be done based on the alphabetical order of firstName and lastName. They should be compared on the lastName first, then the firstName Part II Write a main method and test your Student class by creating and sorting a Student array. You can use java.utils.Arrays an Arrays.sort to perform this operation. Note: Do not compress your files, submit only java files. Note: Do not forget to justify your answers with comments.arrow_forwardIn this lab assignment you need to implement the method named middleValue, in a program named ArrayOps.java. The method accepts an array of integer values as a parameter, and returns the value in the middle of the array. For instance, if an array contains five elements, then the third element would be the middle element to be returned. However, if the array contains six elements, both the third and fourth values could be considered to be the middle elements. In this case, only return the first middle value, namely, the third element of the array. Complete the method middleValue, and test the correctness of your method in main(). Particularly, you should create two arrays (arbitrary values), one array with odd length, and the other with even length, and print out the middle values of these two arrays.arrow_forwardplease use the name, phonenumber and phonebookentry to build this. The three parallel arrays are gone — replaced with a single array of type PhonebookEntry. You should be reading in the entries using the read method of your PhonebookEntry class (which in turn uses the read methods of the Name and PhoneNumber classes). Use the equals methods of the Name and PhoneNumber classes in your lookup and reverseLookup methods. Use the toString methods to print out information. Make 100 the capacity of your Phonebook array Throw an exception (of class Exception) if the capacity of the Phonebook array is exceeded. Place a try/catch around your entire main and catch both FileNotFoundExceptions and Exceptions (remember, the order of appearance of the exception types in the catch blocks can make a difference). Do not use BufferedReader while(true) breaks The name of your application class should be Phonebook. Also, you should submit ALL your classes (i.e., Name, Strip off public from all your class…arrow_forward
- javaarrow_forwardMust be in JAVA. Please show in simplest form and with comments. The correct UML Diagram MUST be provided as well with the solution. It will be flagged if the UML Diagram is not provided.arrow_forward1. Declare an array to save 4 Dog objects. The Dog class is provided below. Look into the Dog class and find what attributes are needed for a dog object. Then initialize five dogs with the following information: Lily, 5 years old Jacob, 2 years old Sugar, 8 years old Bush, 3 years old 2. Print out every dog using a for loop (use either regular for loop or for-each loop, your choice). But you may find when you print out, the name and age are not displayed. Why? 3. Fix this problem by implementing the toString method in the Dog class. Dog Class: public class Dog { private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } }arrow_forward
- Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), add each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method. Ex. If the input is: plant Spirea 10 flower Hydrangea 30 false lilac flower Rose 6 false white plant Mint 4 -1 the output is: Plant 1 Information: Plant name: Spirea Cost: 10 Plant 2 Information: Plant name: Hydrangea Cost: 30 Annual: false Color of flowers: lilac Plant 3 Information: Plant name: Rose Cost: 6 Annual: false Color of flowers: white Plant 4 Information: Plant name: Mint Cost: 4 import java.util.Scanner;import java.util.ArrayList;import…arrow_forwardCreate a Golf Game. Allow for two players. Prompt for the player names. Each player plays 18 holes. Use an array or List for each player to hold their number of swings for each hole. Logical methods should be implemented for different tasks (broken out below). It is up to you to determine the parameters and return types of each method as appropriate. Method 1 - For each hole, the player has a number of swings. Determine the player's swings by generating a random number between 1 and 4. Store each players number of swings in the array/list. Method 2 - After completing the 18 holes, add up the number of swings for each hole for each player. The player with the fewest number of swings is the winner. Method 3 - Save the game data into a CSV file in the main project (root) directory. Name the file "Golf_Game_Player1_Player2.csv" using the player's names. The information in the file should look like this: playername, scoreforhole1, scoreforhole2, scoreforhole3, etc... Method 4 - In the…arrow_forwardCreate a new class, called ConstantList that stores a "constant" list of integers (ints).Dnce created the list is fixed (none of the items can be changed, no items can be added or removed, the order is fixed). Use an array to store your list. Your class will have a single constructor that takes an array of ints as input that will be used to specify the constant list. You must provide appropriate methods to give the user of your class access to the list elements. A user should be able to access individual elements (by position) and the entire list of numbers. Your class must use encapsulation. Explain how your solutions solves the problem. That is, explain why your ConstantList really is a constant list of numbers.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT