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...
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)...
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...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of       ...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of        * the comparator is to compare the alphabetic order of integers. With Q3,        * the order of Integer elements will be sort according to the order of        * digit Unicode.        * For example, the value of {11,3,31,2} will return {11,2,3,31}        * NOTE: You don't need to compare each digit one by one. Just compare them System.out.println("Q3...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT