Question

In: Computer Science

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 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 3

Solutions

Expert Solution

I have written where the array is in dynamic in nature . Intially the size of the array will be 0 and once you start adding elment it will increase its size dymanically (Twice of its size) .

It has the follwing methods :

  1. addElement() : which is responsile for adding element into array at the end .
  2. growArraySize() : It will increase the size of the array to its double.
  3.   shrinkSize() : shrink size of array which block unnecessary
  4. sortArray() : bubble sort implementation
  5. getKthMin() : This method receives an integer k and returns k-th minimum value stored in the array.
  6. removeLastElement(): It will remove the last element of the array ;

Code :

import java.util.*;

class  MyArray{
    
    private int array[] ;
    private int count ;
    private int size ;
    
    
    public MyArray(){
        array = new int[1];
        count = 0 ;
        size = 1 ;
    }
    
    
    //function add an element at the end of array 
    public void addElement(int data){
        if(count==size){
            growArraySize() ; 
        }
        
        array[count] = data ;
        count++; 
    }
    
    public void growArraySize(){
        int temp[] = null ;
        if(count == size){
            temp = new int[size*2];
            
            for(int i = 0 ; i<size ; i++){
                temp[i] = array[i] ;
            }
        }
        
        array = temp ; 
        size = size * 2 ; 
        
    }
    
    
    //function shrink size of array which block unnecessary remove them 
    
    public void shrinkSize(){
        int temp[] = null ;
        if(count>0){
            temp = new int[count] ;
            for(int i = 0 ;i<count ;i++){
                temp[i] = array[i];
            }
            size = count ;
            array = temp ; 
        }
    }
    
    
    public void removeLastElement(){
        if(count>0){
            array[count - 1] = 0 ;
            count-- ;
        }
    }
    
    public void getKthMin(int k){
        
        // Sort the given array 
        int tempArray = array.clone();
        tempArray = sortArray(tempArray); 
  
        // Return k'th element in 
        // the sorted array 
        return tempArray[k - 1]; 
    }
    
    public int[] sortArray(int tempArray[]){
      int n = tempArray.length;
      int temp = 0;
      for(int i = 0; i < n; i++) {
         for(int j=1; j < (n-i); j++) {
            if(tempArray[j-1] > tempArray[j]) { 
               temp = arr[j-1]; 
              tempArray[j-1] = tempArray[j];
               tempArray[j] = temp;
            } 
         } 
      }
      return tempArray; 
   }
    
    
    
    public static void main(String[] args) throws Exception {
        MyArray array = new MyArray();
        array.addElement(10);
        array.addElement(80);
        array.addElement(8);
        array.addElement(9);
        array.addElement(34);
        array.addElement(98);
        array.addElement(23);
        array.addElement(87);
        array.addElement(65);
        
        for(int i = 0 ;i<array.size ; i++){
            System.out.print(array.array[i]+" ");
        }
        System.out.println();
        
        System.out.print(array.getKthMin(2)); 
        

    }
}

Hope you got your answer !

if you still have any doubts please let me know in comment box . Thanks ! happy learning ;)  


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...
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...
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...
Please implement the java method addInOrder() that allows you to create and maintain the lists in...
Please implement the java method addInOrder() that allows you to create and maintain the lists in the order required. The addInOrder method must work with different external Comparator objects. (Hint: Consider creating and using a private compare method and a private Comparator reference as members of your SLL class. If your SLL is constructed without any parameter, then you should initialize the internal Comparator object reference to null. Otherwise, you should initialize it to the external Comparator object passed as...
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...
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)...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
Create java class with name BaseballPlayer Instructions for BaseballPlayer class: The BaseballPlayer class is modeled after...
Create java class with name BaseballPlayer Instructions for BaseballPlayer class: The BaseballPlayer class is modeled after a BaseballPlayer and will contain methods to calculate various statistics based on the stats of a player. For this class, you will want to use a static variable for storing a DecimalFormat object. See the API document for the BaseballPlayer class for a list of methods you will write. API Document Constructors: Identifier: BaseballPlayer(String name, int number, int singles, int doubles, int triples, int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT