Question

In: Computer Science

A incomplete definition of a class Temperature is given below: public class Temperature { private double...

A incomplete definition of a class Temperature is given below: public class Temperature {

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

[6]

  1. (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.

  2. (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.

  3. (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.

  4. (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.

  5. (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.

  6. (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

class Temperature {

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

    /***
     *
     * @return the string of each value that appear on a line in the ascending order of their indexes.
     */
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        for (double v : value) {
            result.append(v).append("\n");
        }
        return result.toString();
    }

    /***
     *
     * @param index
     * @param percent
     * decreases the element with subscript index of the value array by the percentage percent without returning anything
     */
    void decreasePercent(int index, double percent) {
        this.value[index] = this.value[index] - (this.value[index] * percent / 100);
    }

    /***
     *
     * @param index
     * @return value at given index
     */
    double getValue(int index) {
        return this.value[index];
    }

    /***
     *
     * @param threshold
     * @return the number of values which are not less than threshold
     */
    int countNotLessThan(double threshold) {
        int c = 0;
        for (double v : value) {
            if (v >= threshold) {
                c++;
            }
        }
        return c;
    }

    /***
     *
     * @return the index of the minimum value in the array
     */
    int minimumIndex() {
        double minimum = value[0];
        int minIndex = 0;
        for (int i = 0; i < value.length; i++) {
            if (value[i] <= minimum) {
                minIndex = i;
                minimum = value[i];
            }
        }
        return minIndex;
    }

    private int maxIndex() {
        double max = value[0];
        int index = 0;
        for (int i = 0; i < value.length; i++) {
            if (value[i] >= max) {
                index = i;
                max = value[i];
            }
        }
        return index;
    }

    /***
     *
     * @return the average value which excludes the (unique) largest value and (unique)
     * smallest value in the calculation
     */
    double trimmedMean() {
        int sum = 0;
        int minIndex = minimumIndex();
        int max_Index = maxIndex();
        //find sum
        for (int i = 0; i < value.length; i++) {
            //skip min , max values
            if (i != minIndex && i != max_Index) {
                sum += value[i];
            }

        }


        return sum / (value.length - 2); //returns avg

    }
}

class TemperatureTest {
    public static void main(String[] args) {
        Temperature temperature = new Temperature();
        System.out.println(temperature.toString()); //testing to string
        System.out.println("Second Vale From Left: " + temperature.getValue(1));
        temperature.decreasePercent(1, 10);
        System.out.println("Second Vale From Left After decreasing: " + temperature.getValue(1));
        System.out.println("Number of values not less than 37 : " + temperature.countNotLessThan(37));
        System.out.println("Minimum value: " + temperature.getValue(temperature.minimumIndex()));
        System.out.println("Trimmed Mean value: " + temperature.trimmedMean());
    }
}

Related Solutions

Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
Given the definition for a Point class that holds the coordinates of the point as double...
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ". Hint: Rhymes with Bythagorean Beorem. #include <iostream> #include...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
Given the following definition of class Aclass:             class Aclass { private char letter;                  
Given the following definition of class Aclass:             class Aclass { private char letter;                         private int value; public Aclass( ) { Letter =  ‘A’;         value = 0;                         }                         public Aclass( char ch, int num) { letter = ch; value = num;                         }                         public int getvalue( ) { return value; } public void print( ) { System.out.println( “letter =” + letter + “\n value =” + value);                         }             } A.   ( 20 pts ) Write the definition of the class named Bclass as...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT