Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create two classes (two files). The Contact class should have String fields for the name and the phone number. It should have --getters and setters

Create two classes (two files).

The Contact class should have String fields for the name and the phone number. It should have

--getters and setters for each field,

--a setter with parameters for both,

--a constructor with parameters for both,

--a toString to return both fields with a space between,

--a compareTo(Contact other) method to return a call to compareToIgnoreCase comparing the name fields in this and other.

The driver class should contain a main method that creates an array of 1000 Contact objects. It should prompt the user for a name and a phone number, then add a new Contact to the array and increment the count of elements in the array until the user just hits enter in response to the prompt for the name.

Then, it should sort the array by name, using the compareTo method in the Contact class to compare Contacts, using bubble sort.

Then, it should repeatedly prompt for a name or just enter to quit and search for the Contact using binary search and print the Contact, or a message saying that the name was not found.

______________________________________________________________________________________________________________________________________________________

This is program that I have but when I click "RUN".....it said "The section cannot be launched, and there are no recent launches"

public class Contact { private String name; private String phone;

Contact(String name, String phone) { this.name = name; this.phone = phone; }

public String getName() { return name; }

public String getPhone() { return phone; }

public void setName(String name) { this.name = name; }

public void setPhone(String phone) { this.phone = phone; }

public void setInfo(String name, String phone) { this.name = name; this.phone = phone; }

public String toString() { return this.name + " " + this.phone; }

public int compareTo(Contact contact) { return this.name.compareToIgnoreCase(contact.getName()); } }

__________________________________________________________________

import java.util.Scanner;

class ContactDriver { public static void bubbleSort(Contact[] contacts,int size){ for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (contacts[j].compareTo(contacts[i]) < 0) { Contact temp = contacts[i]; contacts[i] = contacts[j]; contacts[j] = temp; } } } }

public static int binarySearch(Contact[] contacts, int size, String name) { int l = 0, r = size - 1; while (l <= r) { int m = l + (r - l) / 2; if (name.compareTo(contacts[m].getName()) == 0) return m; if (name.compareTo(contacts[m].getName()) > 0) l = m + 1; else r = m - 1; } return -1; }

public static void main(String[] args) { Contact[] contacts = new Contact[1000]; int noOfContacts = 0; Scanner keyboard = new Scanner(System.in); String name, phone; while (true) { System.out.print("Enter Name:"); name = keyboard.nextLine(); if (name.compareTo("") == 0) break; System.out.print("Enter Phone Number:"); phone = keyboard.nextLine(); contacts[noOfContacts] = new Contact(name, phone); noOfContacts++; } bubbleSort(contacts, noOfContacts); while (true) { System.out.print("Enter a name for search:"); name = keyboard.nextLine(); if (name.compareTo("") == 0) break; int index = binarySearch(contacts, noOfContacts, name); if (index == -1) System.out.println("Contact was not found!"); else System.out.println(contacts[index]); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

What is meant by 'Wealth Maximization ' ?

Answered: 1 week ago

Question

3. You can gain power by making others feel important.

Answered: 1 week ago