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.)
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...
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...
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...
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i <...
An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i < j. For example, the array A = [-49, 75, 103, -147, 164, -197, -238, 314, 348, -422], though not sorted in the standard sense, is abs-sorted. Design an algorithm that takes an abs-sorted array A and a number k, and returns a pair of indices of elements in A that sum up to k. For example if k = 167 your algorithm should output...
Using your downloaded DBMS (MS SQL Server), create a new database. Create the database tables based...
Using your downloaded DBMS (MS SQL Server), create a new database. Create the database tables based on your entities defining The attributes within each table The primary and foreign keys within each table *****Show your database tables, tables attributes, primary and foreign keys***** Do not forget to check the lesson slides and videos that show you how to convert an ER/EER into a database schema, and how to create a database and tables using MS SQL Server.
Information on the following Database: create database Sales_Co use Sales_Co create table Vendor (v_code integer, v_name...
Information on the following Database: create database Sales_Co use Sales_Co create table Vendor (v_code integer, v_name varchar(35) not null, v_contact varchar(15) not null, v_areacode char(3) not null, v_phone char(8) not null, v_state char(2) not null, v_order char(1) not null, primary key (v_code)); create table product (p_code varchar(10) constraint product_p_code_pk primary key, p_descript varchar(35) not null, p_indate datetime not null, p_qoh integer not null, p_min integer not null, p_price numeric (8,2) not null, p_discount numeric (4,2) not null, v_code integer, constraint...
create two random numbers between 1 and 6. if when the sum of two numbers are...
create two random numbers between 1 and 6. if when the sum of two numbers are added togethere their sum is less than 5 or greater than 12, output to the console: "you win". if is not, output "you lose" C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT