In: Computer Science
(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.
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 !