Question

In: Computer Science

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}; } [6]

(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 TestTemperature in a separate file with a method main() to test the class Temperature. In main(), create a Temperature object temperature 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 Temperature 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 countNotLessThan(double threshold) to the Temperature class which returns the number of values which are not less than threshold. Copy the content of the method as the answers to this part.

(v) Add another method minimumIndex() to the Temperature 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 Temperature 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 TestTemperature: * print the second value from the left, decrease it by 10% using the method decreasePercent(), and print the new value; * calling countNotLessThan()to count the number of values not less than 37 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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

1)

int freqArray[]=new int[8];

____________________
2)

double weight[]={48.5,80,68};

____________________
3)

Mouse mouse[]={new Mouse(),new Mouse()};

____________________

4)

// Temperature.java

public class Temperature {
   private double value[] = { 36.5, 40, 37, 38.3 };

   public String toString() {
       String str = "";
       // This Logic will Sort the Array of elements in Ascending order
       double temp;
       for (int i = 0; i < value.length; i++) {
           for (int j = i + 1; j < value.length; j++) {
               if (value[i] > value[j]) {
                   temp = value[i];
                   value[i] = value[j];
                   value[j] = temp;
               }
           }
       }
       for (int i = 0; i < value.length; i++) {
           str += value[i] + "\n";
       }
       return str;
   }

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

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

   public int countNotLessThan(double threshold) {
       int cnt = 0;
       for (int i = 0; i < value.length; i++) {
           if (!(value[i] < threshold)) {
               cnt++;
           }
       }
       return cnt;
   }

   public int minimumIndex() {
       int minIndx = 0;
       double min = value[0];
       for (int i = 0; i < value.length; i++) {
           if (min > value[i]) {
               min = value[i];
               minIndx = i;
           }
       }
       return minIndx;

   }

   public double trimmedMean() {
       double sum = 0;
       double max = value[0], min = value[0];
       int maxIndx = 0;
       int minIndx = minimumIndex();
       for (int i = 0; i < value.length; i++) {
           if (max < value[i]) {
               max = value[i];
               maxIndx = i;
           }
       }
       int cnt = 0;
       for (int i = 0; i < value.length; i++) {
           if (i != minIndx && i != maxIndx) {
               cnt++;
               sum += value[i];
           }

       }
       return sum / cnt;

   }
  
  

}
________________________

// TestTemperature.java

public class TestTemperature {

   public static void main(String[] args) {
Temperature t=new Temperature();
System.out.println(t);
t.decreasePercent(2,10.0);
System.out.println("Index 2 Elemnt after 10% reduce:"+t.getValue(2));
System.out.println("No of values not less than 37 :"+t.countNotLessThan(37));
System.out.println("Index of the minimum value :"+t.minimumIndex());
System.out.println("Trimmed mean :"+t.trimmedMean());
  
  

   }

}
______________________________

Output:

36.5
37.0
38.3
40.0

Index 2 Elemnt after 10% reduce:34.47
No of values not less than 37 :2
Index of the minimum value :2
Trimmed mean :36.75


_______________Could you plz rate me well.Thank You


Related Solutions

Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
In java language how would I write the following? Create an array called “boyNames” and store...
In java language how would I write the following? Create an array called “boyNames” and store all names from the BoyNames.txt file Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file Create a text menu that allowing users to:          1. Enter a name and the application must display a message indicating if the name is   among the most popular (found on either array) If the name is found, tell the user if it is a boy’s...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i //...
Questions: 1) // declare integer variable sum equal to zero // declare variable integer i // declare while loop condition where i is less then 25 // inside of brackets calculate the sum of i (addition) // increment i // outside the loop print the sum of values ============================================= 2) Create a sentinel value example if I press number 0 it will display the sum of data // create a scanner // prompt the user to to enter the numbers...
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 MIPS assembly language to transpose a square integer matrix in code
Write a MIPS assembly language to transpose a square integer matrix in code
Write the BNF for mini Java language based of the following information: Data Types Integer Int...
Write the BNF for mini Java language based of the following information: Data Types Integer Int Long Double Boolean Char References Complex Data Structures Arrays int v[30]; Classes member variables class Name { int a; char b; char name[25]; } Methods Return data type Primitive data type Void Method Name Parameter list Could be empty Statement Block { Variable declarations Executable Statements } Program Variable Declarations Class Definitions Methods Only one method named "main" but must have one method named...
Write the EBNF for mini Java language based of the following information: Data Types Integer Int...
Write the EBNF for mini Java language based of the following information: Data Types Integer Int Long Double Boolean Char References Complex Data Structures Arrays int v[30]; Classes member variables class Name { int a; char b; char name[25]; } Methods Return data type Primitive data type Void Method Name Parameter list Could be empty Statement Block { Variable declarations Executable Statements } Program Variable Declarations Class Definitions Methods Only one method named "main" but must have one method named...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT