Question

Start a new project in NetBeans that defines a class Person with the following: Instance variables...

Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes objects instantiated from this class immutable since they subsequently cannot be updated) A toString() method that returns a String representation of an object in the form: John H. Johnson (1) instantiate a comparator class PersonComparator that implements the Comparator interface; (2) in the compare() method of this comparator class, sort "Person" objects by the last name and then first name and then middle initial; (3) instantiate and use a new "PersonComparator" object within a Collections.sort() to sort the List; and (4) iterate through and display the toString() methods of each element of the sorted List using an Iterator object

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

Source Code:-
======================
package com.venkanna;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;
class Person
{
String firstName;
String lastName;
char middleInitial;
public Person(String firstName,char middleInitial,String lastName)
{
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public String toString()
{
return this.firstName + " " + this.middleInitial +" " + this.lastName;
}
}
class SortbyFirstname implements Comparator<Person>
{
public int compare(Person a, Person b)
{
return a.firstName.compareTo(b.firstName);
}
}
class SortbyLastname implements Comparator<Person>
{
public int compare(Person a, Person b)
{
return a.lastName.compareTo(b.lastName);
}
}
class SortbyInitials implements Comparator<Person>
{
Character c1, c2;
public int compare(Person a, Person b)
{
c1 = new Character(a.middleInitial);
c2 = new Character(b.middleInitial);
return c1.compareTo(c2);
}
}
public class Driver_Menu
{
public static void main(String[] args)
{
LinkedList<Person> obj2 = new LinkedList<Person>();
Scanner sc=new Scanner(System.in);
int size;
String FirstName,LastName;
char initials;
System.out.println("Please Enter How Person Details you want to read");
size=sc.nextInt();
for(int i=0;i<size;i++)
{
System.out.println("Please read "+(i+1)+" Person Information");
System.out.println("Please Enter FirstName");
FirstName=sc.next();
System.out.println("Please Enter Initials");
initials=sc.next().charAt(0);
System.out.println("Please Enter LastName");
LastName=sc.next();
obj2.add(new Person(FirstName,initials,LastName));
}
System.out.println("The Given Details are");
System.out.println("--------------------------");
for (int i=0; i<obj2.size(); i++)
System.out.println(obj2.get(i));
Collections.sort(obj2, new SortbyFirstname());
System.out.println("\n--------------------------");
System.out.println("Sort the List by FirstName");
for(int i=0;i<obj2.size();i++)
{
System.out.println(obj2.get(i));
}
Collections.sort(obj2, new SortbyLastname());
System.out.println("\n--------------------------");
System.out.println("Sort the List by LastName");
for(int i=0;i<obj2.size();i++)
{
System.out.println(obj2.get(i));
}
Collections.sort(obj2, new SortbyInitials());
System.out.println("Sort the List By Initials");
for(int i=0;i<obj2.size();i++)
{
System.out.println(obj2.get(i));
}
sc.close();
}
}


Sample Output:-
====================
Please Enter How Person Details you want to read
3
Please read 1 Person Information
Please Enter FirstName
venkanna
Please Enter Initials
mr
Please Enter LastName
koothada
Please read 2 Person Information
Please Enter FirstName
sindhuja
Please Enter Initials
ms
Please Enter LastName
sindeti
Please read 3 Person Information
Please Enter FirstName
narayana
Please Enter Initials
kum
Please Enter LastName
chekka
The Given Details are
--------------------------
venkanna m koothada
sindhuja m sindeti
narayana k chekka
--------------------------
Sort the List by FirstName
narayana k chekka
sindhuja m sindeti
venkanna m koothada
--------------------------
Sort the List by LastName
narayana k chekka
venkanna m koothada
sindhuja m sindeti
Sort the List By Initials
narayana k chekka
venkanna m koothada
sindhuja m sindeti

D Reversejava 旦Console X L DataBaseConnectionjava D problemX2java D curriedformaava terminated> Driver Menu [Java Application

Add a comment
Know the answer?
Add Answer to:
Start a new project in NetBeans that defines a class Person with the following: Instance variables...
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
  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to...

    Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...

  • Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

    Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...

  • 1. Do the following a. Write a class Student that has the following attributes: - name:...

    1. Do the following a. Write a class Student that has the following attributes: - name: String, the student's name ("Last, First" format) - enrollment date (a Date object) The Student class provides a constructor that saves the student's name and enrollment date. Student(String name, Date whenEnrolled) The Student class provides accessors for the name and enrollment date. Make sure the class is immutable. Be careful with that Date field -- remember what to do when sharing mutable instance variables...

  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

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