Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What do the following lines in the attached code do? -line 23-24 import java.uti

ID: 673734 • Letter: W

Question

What do the following lines in the attached code do?

-line 23-24

import java.util.Arrays; public class Main { static Album[] albums; public static void main(String[] args) { populate(); String prompt = "How would you like to sort albums? " + "By singer, writer, number sold, awards won, awards nominated...enter Q to quit"; Scanner in = new Scanner(System. in); String choice = in.nextLine(); while(!choice.equalsIgnoreCase("end")) { if(choice.equals("singer")) forCAlbum a : albums) System. oi/t.printlnCa); else if(choice.equals("writer")) Arrays.sortCalbums, Comparator.comparingQAlbum ::getWriter)); else if(choice.equalsIgnoreCase("number sold")) krrays.sortCalbums, (Album al, Album a2)- >al.getlntegerSold().compareTo(a2.getlntegerSold())); else if(choice.equals("awards won")) krrays.sort(albums,

Explanation / Answer

Assume, Album is a user defined class containing member variables
1. sold: to hold how many albums sold
2. writer: to hold the name of the writer who creates the album
and Member functions
1. getIntegerSold(): to return how many albums sold
2. getWriter(): to return the name of the writer

Explanation for lines 23-24:
  
Arrays.sort(albums,(Album a1, Album a2)->a1.getIntegerSold().compareTo(a2.getIntegerSold()));   

1.albums: Array to hold many albums (see line 8 in program)
2.(Album a1, Album a2): two albums named a1 and a2
3. a1.getIntegerSold():Get the number of albums sold from album a1
4. a2.getIntegerSold():Get the number of albums sold from album a2
5. a1.getIntegerSold().compareTo(a2.getIntegerSold()): It compares the number of albums sold
from album a1 and a2
6. albums,(Album a1, Album a2)->a1.getIntegerSold().compareTo(a2.getIntegerSold()):
Each time it considers two albums from albums array and sort them based on
the number of albums sold.
7. Arrays.sort(): sorts the albums array by considering two albums at time.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote