Question

In: Computer Science

Design a class named ArraySort and it contains the following method: Public int search(int target): if...

Design a class named ArraySort and it contains the following method:

Public int search(int target): if the target is found in the array, return the number of its showing up. If the target is not found in the array, return -1. If the array is empty, return -1;

Public int maximum():Return the maximum number in the array if the array is nonempty, otherwise return -1;

Public int minimum(): Return the minimum number in the array if the array is nonempty, otherwise return -1;

Public int getElement(int index): Return the value at index. If index does not exist, throw an IndexOutOfBoundsException.

Public void setElement(int index, int value): Set the value at index. If index does not exist, throw an IndexOutOfBoundsException.

Public void addeElement(int value): Set the value in the cell at the end of the array. If array is full, you need to call the a private method to resize your array(double the size and copy all elements in original array to the new array).

Public void printArray(): Display all of elements in the array.

Public void delete(int value): Delete all the elements in the array with given value and reorganize the array without any empty spaces btw any two elements.

Solutions

Expert Solution

public class ArraySort
{
//data member declaration
int size;
private int[] array= new int[size];
  
//method to search and element
public int search(int target)
{
for(int i=0; i<array.length; i++)
{
if(array[i] == target)
return i;
}
return -1;
}
  
//method to find teh maximum value
public int maximum()
{
int max = array[0];
if(array.length>0)
{
for(int i=0; i<array.length; i++)
{
if(array[i] > max)
max = array[i];
}
return max;
}
return -1;
}
  
//method to find the minimum value
public int minimum()
{
int min = array[0];
if(array.length>0)
{
for(int i=0; i<array.length; i++)
{
if(array[i]<min)
min = array[i];
}
return min;
}
return -1;
}
  
//method to get the element at the index
public int getElement(int index)
{
int element = 0;
try
{
element = array[index];
}
catch(IndexOutOfBoundsException e)
{
System.out.println("The index is out of bound");
}
return element;
}
  
//method to set the elemtent
public void setElement(int index, int value)
{
try
{
array[index] = value;
}
catch(IndexOutOfBoundsException e)
{
System.out.println("The index is out of bound");
}
}
  
//method to add the element
public void addeElement(int value)
{
if(array.length >= size)
{
resizingArray(2*size);
size = 2*size;
}
array[size] = value;
}
  
//method to resize an array
private void resizingArray(int size)
{
this.size = size;
array = new int[size];
}
  
//method to print an array
public void printArray()
{
for(int i=0; i<array.length; i++)
{
System.out.print(array[i] + " ");
}
}
  
//method to delete an array element
public void delete(int value)
{
for(int i=0; i<array.length; i++)
{
if(array[i] == value)
{
for (int j = i; j < array.length-1; j++)
{
array[j] = array[j + 1];
}
}
}
}
}


Related Solutions

In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
Design a class named Account that contains: ■ A private int data field named id for...
Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. ■ A private Date data field named dateCreated that stores the date when the account was created. ■ A no-arg constructor that creates...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT