Question

Modify the given Customer.java to implement the Comparable interface so that the customer will be sorted...

Modify the given Customer.java to implement the Comparable interface so that the customer will be sorted by their last name in ascending order. Implement a toString() method for the Customer class. Using the CustomerTester.java, add a least 5 new Customers to an ArrayList. Print the unsorted list. Sort the list of customers, and then print out the sorted ArrayList of Customers. Make sure your output is meaningful and readable. (21 points)

Submit Customer.java and CustomerTester.java.

Sample output:
Customer 1 is Tugg Thomson
Customer 2 is Fred Jones
Customer 3 is Sally Stevens
Customer 4 is Tim James
Customer 5 is George Thompson


Customer 1 is Tim James
Customer 2 is Fred Jones
Customer 3 is Sally Stevens
Customer 4 is George Thompson
Customer 5 is Tugg Thomson

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As per question there is no Customer.java file given for modification and implement same.

Based on the question I understand the concept that what you’re looking for, and assuming the expected outcome based on sample output I have developed the following java code

Customer.java

class Customer implements Comparable<Customer>{ // implements the Comparable interface to Customer class

String first_name; //Declaring Customer first_name variable as a String

String last_name;   //Declaring Customer first_lastname variable as a String

Customer(String first_name,String last_name){ //Constructor for assigning variables

this.first_name=first_name;

this.last_name=last_name;

}

public int compareTo(Customer cust){ //compareTo() method will compare the Customer data

if(last_name.equals(cust.last_name)) //A conditional statement for equals of last_names

return 0; // If the conditional statement is true returns 0

else if(last_name.compareTo(cust.last_name)>0)

return 1;

else

return -1;

}

}

CustomerTester.java

import java.util.*; // import util classes

import java.io.*; // import IO classes

public class CustomerTester{ // class CustomerTester begins

public static void main(String[] args){ // declaring main method

int i=1, j=1; // an integers for printing customer serial number

ArrayList<Customer> al=new ArrayList<Customer>(); // al object creation

al.add(new Customer("Tugg","Thomson")); // adding customer data to ArrayList

al.add(new Customer("Fred","Jones"));

al.add(new Customer("Sally","Stevens"));

al.add(new Customer("Tim","James"));

al.add(new Customer("George","Thompson"));

System.out.println();

System.out.println("Before Sorting Last Name");

System.out.println();

for(Customer cust:al){ // loop for print before sorting data

System.out.println("Customer "+i+" is "+cust.first_name+" "+cust.last_name);

    i++;

}

System.out.println();

System.out.println("After Sorting Last Name");

System.out.println();

Collections.sort(al);

for(Customer cust:al){   // loop for print after sorting data

System.out.println("Customer "+j+" is "+cust.first_name+" "+cust.last_name);

    j++;

}

}

}

Output (Screenshot): The following screenshot also guide you how to compile and execute the code for output

.Administrator: C:\Windowslsystem321cmd.exe C:NUsers Shiva\Desktop java>javac Customer.java C:NUsers Shiva\Desktop java>javac

Add a comment
Know the answer?
Add Answer to:
Modify the given Customer.java to implement the Comparable interface so that the customer will be sorted...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • URGENT I have 3 hours for Submit this Homework so you can share yout codes with...

    URGENT I have 3 hours for Submit this Homework so you can share yout codes with me in 150mins Preliminary Notes: You have 4 hours to submit your solution. No late submission is accepted. Do not leave your submission to the last minute. You are not allowed to copy any code from anywhere. Your code will be checked by a software for similarity. Implement the code on your own and never share it with someone else. A test code is...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

  • The following are screen grabs of the provided files Thanks so much for your help, and have a n...

    The following are screen grabs of the provided files Thanks so much for your help, and have a nice day! My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT