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...
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)....
create a java class with name ModernArt: For the first part of the assignment, you will...
create a java class with name ModernArt: For the first part of the assignment, you will be extending the JApplet class and creating a work of modern using the Graphics class to draw various shapes to the applet window. As part of your masterpiece, you should incorporate the following elements: At least 1 non-solid rectangle At least 1 non-solid oval At least 1 solid oval At least 1 non-solid Polygon At least 1 solid Polygon At least 3 lines At...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should take in an int and return arr at that index if it is a valid index notEqual The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal. operator-(float) Will subtract the float value that is passed in from each of the values in arr Copy constructor Will take...
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...
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)....
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you...
JAVA the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT