Question

In: Computer Science

(a) Write code segments to perform the following: (i) declare and create a boolean array flagArray...

(a) Write code segments to perform the following:

(i) declare and create a boolean array flagArray of size 5

(ii) declare and initialize an array amount which contains 39.8, 50 and 45

(iii) declare a Keyboard array of size 3 with name keyboard and initialize it with Keyboard objects using one statement

(b) A incomplete definition of a class Cash is given below: public class Cash { private double value[] = {10.5, 20, 5.5, 7.8}; }

(i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of the method as the answers to this part.

(ii) Write another class TestCash in a separate file with a method main() to test the class Cash. In main(), create a Cash object cash and print its values by calling toString(). Run the program. Copy the content of the file and the output showing the message as the answers to this part.

(iii) Add a method decreasePercent(int index, double percent) to the Cash class which decreases the element with subscript index of the value array by the percentage percent without returning anything. Also add a method getValue(int index) to return value[index]. Copy the content of the methods as the answers to this part.

(iv) Add another method countBelow(double threshold) to the Cash class which returns the number of values which are less than threshold. Copy the content of the method as the answers to this part.

(v) Add another method minimumIndex() to the Cash class which returns the index of the minimum value in the array. Copy the content of the method as the answers to this part. You can assume there is only one minimum value.

(vi) Add another method trimmedMean() to the Cash class which returns the average value which excludes the (unique) largest value and (unique) smallest value in the calculation. You should use minimumIndex() to get the index of the minimum value. Note that this method should work without any modifications when the number of values, which is at least three, is changed. Copy the content of the method as the answers to this part.

(vii) Perform the tasks listed below in main() of TestCash: * print the second value from the left, decrease it by 10% using the method decreasePercent(), and print the new value; * calling countBelow()to count the number of values less than 15 and then print it; * print the index of the minimum value and the trimmed mean. Run the program. Copy the content of the class and the output as the answers to this part.

Solutions

Expert Solution

Answer :

(a)

i) Declare and create a boolean array flagArray of size 5
boolean flagArray[] = new boolean[5];
      

ii) Declare and initialize an array amount which contains 39.8, 50 and 45
       double amount[] = {39.8, 50, 45};
      

iii) Declare a Keyboard array of size 3 with name keyboard and initialize it with Keyboard objects using one statement
       Keyboard keyboard[] = new Keyboard[3];

b).

i)

public String toString() {
       String str = "";
      
       for(int i=0; i<size; i++)
           str += String.valueOf(value[i]) + " ";
      
       return str;
   }

Output :

10.5 20.0 5.5 7.8

ii)

public static void main(String[] args) {
       Cash cash = new Cash();
      
       System.out.println(cash.toString());

   }

}

iii)

public void decreasePercent(int index, double percent) {
   value[index] -= (value[index]*percent)/100;
}

public double getValue(int index) {
   return value[index];
}

iv)

public int countBelow(double threshold) {
       int count = 0;
      
       for(int i=0; i<size; i++) {
           if(value[i] < threshold)
               count++;
       }
      
       return count;

}

v)

public int minimumIndex() {
       double min = value[0];
       int minIndex = 0;
      
       for(int i=1; i<size; i++) {
           if(min > value[i]) {
               min = value[i];
               minIndex = i;
           }
       }
      
       return minIndex;
   }

vi)

public double trimmedMean() {
       if(size < 3)
           return -1;
      
       double sum = 0;
       double smallest = value[minimumIndex()];
       double largest = 0;
      
       for(int i=0; i<size; i++)
       {
           sum += value[i];
           if(largest < value[i])
               largest = value[i];
       }
      
       return (sum - smallest - largest)/ size-2;
   }

vii)

public class TestCash {

   public static void main(String[] args) {
       Cash cash = new Cash();
      
       System.out.println(cash.toString());
      
       System.out.println("Second value: "+ cash.getValue(2));
       cash.decreasePercent(2, 10);
       System.out.println("Second value(updated): "+ cash.getValue(2));
      
       System.out.println("Less than 15: "+ cash.countBelow(15));
      
       System.out.println("Index of minimum Value: "+ (cash.minimumIndex()+1));
      
       System.out.println("Trimmed mean: "+ cash.trimmedMean());

   }

}

Output :

10.5 20.0 5.5 7.8
Second value: 5.5
Second value(updated): 4.95
Less than 15: 3
Index of minimum Value: 3
Trimmed mean: 2.5749999999999993

cash.java

public class Cash {
   private double value[] = {10.5, 20, 5.5, 7.8};
   private int size = 4;
  
   public String toString() {
       String str = "";
      
       for(int i=0; i<size; i++)
           str += String.valueOf(value[i]) + " ";
      
       return str;
   }
  
   public void decreasePercent(int index, double percent) {
       value[index] -= (value[index]*percent)/100;
   }
  
   public double getValue(int index) {
       return value[index];
   }
  
   public int countBelow(double threshold) {
       int count = 0;
      
       for(int i=0; i<size; i++) {
           if(value[i] < threshold)
               count++;
       }
      
       return count;
   }
  
   public int minimumIndex() {
       double min = value[0];
       int minIndex = 0;
      
       for(int i=1; i<size; i++) {
           if(min > value[i]) {
               min = value[i];
               minIndex = i;
           }
       }
      
       return minIndex;
   }
  
   public double trimmedMean() {
       if(size < 3)
           return -1;
      
       double sum = 0;
       double smallest = value[minimumIndex()];
       double largest = 0;
      
       for(int i=0; i<size; i++)
       {
           sum += value[i];
           if(largest < value[i])
               largest = value[i];
       }
      
       return (sum - smallest - largest)/ size-2;
   }
  
}

Hope you like it

Any Query? Comment Down!

I have written for you, Please upvote the answer as it encourage us to serve you Best !


Related Solutions

Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Declare and initialize an array to store the course name or code.
Declare and initialize an array to store the course name or code.
• In this script, write MATLAB code to create an array A of size 100 ×...
• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
Write the MATLAB code to Create a new figure. Define a theta array such that ?...
Write the MATLAB code to Create a new figure. Define a theta array such that ? 2 ≤ ? ≤ 9? 2 with increment of ? 10 ; Create a sixmember array of ones called r. Create a new polar plot of ? versus r
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT