Question

In: Computer Science

Program in Java Create a class and name it MyArray and implement following method. * NOTE:...

Program in Java

Create a class and name it MyArray and implement following method.

* NOTE: if you need more methods, including insert(), display(), etc. you can also implement those.

Method name: getKthMin(int k)

This method receives an integer k and returns k-th minimum value stored in the array.

* NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods).

Example:

Items in the array:

4, 6, 9, 10, 3, 11, 1, 2, 5, 7, 8

call the method to get 3rd min value

myArray.getKthMin(3); -> this will return 4

Solutions

Expert Solution

JAVA PROGRAM

import java.util.*;
public class MyArray
{
//called function which returns the k th position
public static int getKthMin(int k)
{
//array declaration
int arr[] = {4,6,9,10,3,11,1,2,5,7,8};
int i = 0 ,temp=0;
//runs the loop it sorts the array in ascending order
for ( i = 0; i < arr.length; i++)
{
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//retunrs the k position of array
return arr[k];
}
//main function
   public static void main(String[] args)
   {
   //creating a object
   MyArray obj = new MyArray();
   // calling a function & passing the parameter 7 returned value is stored in res variable
   int res = obj.getKthMin(3);
   //printing the retuned value
   System.out.println(res);
      
   }
}

OUTPUT


Related Solutions

Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Create a class and name it MyArray. This class must have an internal array of integers...
Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions: 1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows. 2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out)....
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed When writing your CSV file, the first row (header row)...
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
10) Create a java program that will ask for name and age with method age where...
10) Create a java program that will ask for name and age with method age where it will check the condition that id you are greater than 18 you can vote.
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT