Question

In: Computer Science

Create a database to hold a collection of numbers to be searched and sorted: 1) Create...

Create a database to hold a collection of numbers to be searched and sorted:

1) Create a main class and a database class

2) Collect 5 random numbers from the user in the main class and store in an array. (not in ascending or descending order)

3) Create an instance of the database class in your main class and store the numbers array in the database using its constructor.

4) In the Database class, create a method that performs a bubble sort and returns a sorted array of the data (ascending and descending). Call this method from the main class and print the result to the screen.

5) In the Database class, create methods that each return the min, average, and max respectively. Call each of them from the main class and print the result to the screen.

6) In the Database class, create a method that searches for a value in the array using a binary search and returns the index of that value. Call this method from the main class and print the result to the screen.

BubbleSort and Binary Search Code:

{

public static void main(String args[])

{

SortExample ob = new SortExample();

int[] arr = {64, 34, 25, 12, 22, 11, 90};

ob.bubbleSort(arr);

System.out.println("Sorted array");

ob.printArray(arr);

int key = 22;

int index = ob.BinarySearch(arr, key);

System.out.println("\n" + key + " is at index: " + index);

}

void bubbleSort(int[] arr)

{

int n = arr.length;

for (int i = 0; i < n-1; i++)

for (int j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

{

// swap temp and arr[i]

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

/* Prints the array */

void printArray(int arr[])

{

int n = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i] + " ");

System.out.println();

}

int BinarySearch(int[] arr, int key)

{

int mid = 0;

int low = 0;

int high = arr.length - 1;

while (high >= low)

{

mid = (high + low) / 2;

if (arr[mid] < key) {

low = mid + 1;

}

else if (arr[mid] > key) {

high = mid - 1;

}

else {

return mid;

}

}

return -1;

}

}

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

import java.util.*;


class Database
{
int[] arr;
Database(int[] a)
{
//this.arr=new int[arr.length];
this.arr = a;
}
  
void bubbleSort()
{
int n = arr.length;

for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
  
int min()
{
int min=arr[0];
for (int i = 1; i < arr.length; i++)
{
if(arr[i]<min)
min=arr[i];
}
return min;
}
  
int max()
{
int max=arr[0];
for (int i = 1; i < arr.length; i++)
{
if(arr[i]>max)
max=arr[i];
}
return max;
}
  
double average()
{
double sum=0;
for (int i = 0; i < arr.length; i++)
{
sum+=arr[i];
}
return sum/arr.length;
}
  
int binarySearch(int key)
{
int mid = 0;
int low = 0;
int high = arr.length - 1;
  
while (high >= low)
{
mid = (high + low) / 2;
if (arr[mid] < key)
{
low = mid + 1;
}
  
else if (arr[mid] > key)
{
high = mid - 1;
}
  
else
{
return mid;
}
}
return -1;
}
  
void printArray()
{
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");

System.out.println();
}
}
  

class Main
{
   public static void main (String[] args)
   {
       System.out.print("Enter 5 Numbers: ");
       int[] a=new int[5];
       Scanner sc = new Scanner(System.in);
      
       //read 5 numbers
       for(int i=0;i<5;i++)
       a[i]=sc.nextInt();
      
       //Pass data in the constructor
       Database db = new Database(a);
      
       System.out.print("The Array is: ");
       db.printArray();
         
       System.out.println("Minimum: "+db.min());
       System.out.println("Maximum: "+db.max());
       System.out.println("Average: "+db.average());
      
      
       System.out.println("Search for 10, INDEX = : "+db.binarySearch(10));
       System.out.println("Search for 20, INDEX = : "+db.binarySearch(20));
      
       System.out.print("Sorted Array: ");
       db.bubbleSort();
       db.printArray();
      
   }
}

===============================

SCREENSHOT:


Related Solutions

(JAVA) Create a program that takes in 15 numbers in sorted order from the console and...
(JAVA) Create a program that takes in 15 numbers in sorted order from the console and stores them in a 1D array of size 15. Next, prompt the user for a number to search for in the array (target). Then, print the array. Next, search the array using a linear search – printing out each of the indices (or “indexes”) that are being examined until the algorithm either finds the target or doesn’t. Then, do the same thing for a...
Database: Question 11 Using the ERD from problem 10, create the relational schema.(Create an appropriate collection...
Database: Question 11 Using the ERD from problem 10, create the relational schema.(Create an appropriate collection of attributes for each of the entities. Make sure you use the appropriate naming conventions to name the attributes.)
(JAVA) Create a program that creates a mini database of numbers that allows the user to:...
(JAVA) Create a program that creates a mini database of numbers that allows the user to: reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will declare an array of 10 integers (this is a requirement). Then you will define the following methods: • printArray (int[ ] arr) – this takes in an array and prints it • initArray (int[ ] arr) –...
Database Design A database is an electronic filing system: a collection of data organized for use...
Database Design A database is an electronic filing system: a collection of data organized for use and retrieval. Database design is the process of creating a model of the database. The model can include several attributes and entities, such as users, logic, physical storage locations, relationships, and other parameters. Write a post in which you explain at least three ways that database design and data architecture can work together to improve patient satisfaction, lower care costs, or improve patient outcomes....
1.Create a Database in Access with the information The database must include: Database name: Monaco Enterprise  Mark...
1.Create a Database in Access with the information The database must include: Database name: Monaco Enterprise  Mark Johnson #87451 Table name: Contacts Delete the Primary key. Fields name and data type are (remember to choose the data type): Field Name Data Types Employee Name Short text Name Short text Last Name Short Text Work Yes/No 2.Go to the “Datasheet View” and enter the data. * Remember to save the table. 3.Move the last name field after the employee name. 4.The (data)...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
PartA: Create the database. Name the database doctorWho. Then create a page that allows Doctor Who’s...
PartA: Create the database. Name the database doctorWho. Then create a page that allows Doctor Who’s assistant to add a new patient record. You will need to give the assistant rights to this database. The assistant’s username is 'helper' and the password is 'feelBetter'. For this to work, you will need to create several pages so be sure to include all of them when submitting your work. Name the main page addPatient.php. PartB: Add at least five records to the...
Database exercise: inpatient cases Create database using name RUMKIT Create tables below in that database patient(idPatient,...
Database exercise: inpatient cases Create database using name RUMKIT Create tables below in that database patient(idPatient, fullName, biologicalMother, birthdate, address) doctor(idDr, fullName, specialization, consulRates) inpatient(idPatient, entryTime, outTime, idDr, idRoom). Please make entryTime as column that is going to be filled automatically when care record is being add room(idRoom, roomName, cost) fill the data above to each table Create sql query and relational algebra expressions for the query Please give me detailed answer so I could learn from it. Thank you...
Develop a function to read a document in the MongoDB database “city” in the collection “inspections.”...
Develop a function to read a document in the MongoDB database “city” in the collection “inspections.” Be sure it can handle error conditions gracefully. a. Input -> arguments to function should be the key/value lookup pair to use with the MongoDB driver find API call b. return -> result in JSON format if successful else MongoDB returned error message
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT