Question: Complete the constructor for the Triangle class. public class Triangle { private double base; private double height; private int area; // constructor // postcondition: the
Complete the constructor for the Triangle class.
public class Triangle { private double base; private double height; private int area; // constructor // postcondition: the instance variables are initialized public Triangle(double b, double h) { } }
Complete the shrink() method of the Circle class.
public class Circle { private double radius; // constructor // postcondition: the instance variable is initialized public Circle(double rad) { radius = rad; } // postcondition: reduces the radius of this Circle by a given percentage. // For example, if radius is 10.0, and percent is 20, then the new radius would be 8.0. public void shrink(int percent) {
} }
This question involves a class named TextFile that represents a text file.
public class TextFile { private String fileName; private ArrayList
}
Assume that two arrays are used to store information about students' grades. One array contains the students' numerical scores (integers in the range 0 100). The other array contains the corresponding letter grades (Strings having possible values of "A", "B", "C", "D", and "F").
For example, if studentScores[0] contains 92, then studentLetters[0] will contain the corresponding letter grade for 92, which is "A" if the professor is using a standard grading scale. Note that the professor might choose a different grading scale, so if one is looking for A grades, one should look in the studentLetters array for values of "A", and not in the studentScores array for values of 90 and above.
public class Gradebook { private int[] studentScores; private String[] studentLetters; // postcondition: returns -1.0 if letterGrade does not appear in studentLetters // otherwise, returns average of all studentScores[n], // for all 0 <= n < studentScores.length, such that // studentLetters[n] is equal to letterGrade public double letterAverage(String letterGrade) { } } Write the method letterAverage. This method returns a double representing the average (arithmetic mean) of the student scores that correspond to a given letter grade. LetterAverage returns -1.0 if none of the student scores corresponds to the given letter grade.
For example, given the following arrays: studentScores: 96 72 84 65 89 60 78 86 75 61 85 studentLetters: A C B D A D B B C D B letterAverage("B") would return the number 83.25, which is the average of the four scores that correspond to the letter grade B. letterAverage("A") would return the number 92.5, which is the average of the four scores that correspond to the letter grade A. letterAverage("F") would return -1.0, because no scores are below 60. Note that the professor might choose a different grading scale, so if one is looking for A grades, one should look in the studentLetters array for values of "A", and not in the studentScores array for values of 90 and above. Complete method letterAverage below:
// postcondition: returns -1.0 if letterGrade does not appear in studentLetters // otherwise, returns average of all studentScores[n], // for all 0 <= n < studentScores.length, such that // studentLetters[n] is equal to letterGrade public double letterAverage(String letterGrade) { }
Examine the following starter code. Use the comments and the code provided to complete the constructor and two methods in the RectangleStats class.
import java.util.ArrayList; class RectangleStats { // Private instance variables // length is an ArrayList which will contain integer values, width is an array which will contain integer // values, and area is an array which will contain decimal values. //code goes here for instance variables goes here // The constructor for the RectangleStats class takes an ArrayList and an array as parameters for // length and width, respectively. // code for constructor goes here // The calcRectangleArea() method calculates the area of rectangles using the length and // the width assigned to the private instance variables and assigns the results to the area array of type // double. This method does not return anything. // code for the calcRectangleArea() method goes here // The printArea() method prints the values assigned to the area array using the most appropriate // type of loop. This method does not return anything. // code for the printArea() method goes here }
// The RectangleStatsTesterClass assigns the length of two rectangles to an ArrayList and assigns the // width of two rectangles to an array. The calcRectangleArea() and printArea() methods are invoked to // calculate and print the area of the two rectangles. public class RectangleStatsTesterClass2 { public static void main(String[] args) { ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
