// Doing computations in java import java.util.Scanner; // for the input import javax.swing.JOptionPane; //for the dialog boxes /** * A MakingCoasters object has a rectangular piece of fabric, defined by its * width and length.
* It can compute the number of coasters of a certain radius that can be cut out * of that piece of fabric and the percentage of fabric that goes unused */ public class MakingCoasters { // instance variables // Dimensions of the piece of fabric (in feet) private int width; private int length; // Note: using the same name for more than one method (here for // the constructors) is an example of overloading. /** * Default constructor for objects of type MakingCoasters.
* The dimensions of the piece of fabric are input by the user.
* If any input is negative or 0, the corresponding dimension is set to 1 * foot. */ public MakingCoasters() { // Initialize the dimensions of the piece of fabric // Input from the user via an Scanner object Scanner input = new Scanner(System.in); System.out.print("Width of the piece of fabric: "); width = input.nextInt(); System.out.print("Length of the piece of fabric: "); length = input.nextInt(); /* * Could also do: Input input = new Input(); width = * input.nextInt("Width of the piece of fabric: "); length = * input.nextInt("Length of the piece of fabric: "); * * Requires importing the Input class import uwcse.io.Input; */ // Check that the input is valid (dimension>0) // Use a method checkWidthAndLength() // (we will use it again in the other constructor) checkWidthAndLength(); } /** * MakingCoasters constructor
* Width and length are set to w and l if w and l are positive integers.
* If any of them is negative or zero, the corresponding dimension is set to * one foot. * * @param w * the width of the piece of fabric * @param l * the length of the piece of fabric */ public MakingCoasters(int w, int l) { width = w; length = l; checkWidthAndLength(); } /** * Compute the number of coasters of a given radius that can be cut out of * the piece of fabric. * * @param radius * the radius of one coaster in feet. */ public int computeNumberOfCoasters(int radius) { // If the radius is negative or 0, no coaster is cut out // Display an error message and return 0 if (radius <= 0) { // Note: you can't write a String in Java on several lines, i.e. // "Hello, // how are you?" gives an error // But you can write // "Hello, "+ // "how are you?" JOptionPane.showMessageDialog(null, "Can't compute a number of " + "coasters of non positive radius", "computeNumberOfCoasters error", JOptionPane.ERROR_MESSAGE); return 0; } else { // Multiply the number of coasters that fit along the length by the // number of coasters that fit along the width // IMPORTANT NOTE: / between integers is the integer division // 50/3 is 16 (not 16.6666) int nl = length / (2 * radius); int nw = width / (2 * radius); int number = nl * nw; return number; // Could have written // return (length/(2*radius))*(width/(2*radius)); // but not // return length/(2*radius)*width/(2*radius); // which is syntactically correct, but is not the same expression! // e.g. try length is 10, width is 1 and radius is 1 // (length/(2*radius))*(width/(2*radius)) is 0 // length/(2*radius)*width/(2*radius) is 2 } } /** * Compute the percentage of fabric that goes unused when cutting out * coasters of a given radius * * @param radius * the radius of one coaster in feet */ public double computePercentageUnused(int radius) { // If the radius is negative or 0, no coaster is cut out if (radius <= 0) { JOptionPane.showMessageDialog(null, "Can't have coasters with " + "a non positive radius", "computePercentageUnused error", JOptionPane.ERROR_MESSAGE); return 100.0; // . is not necessary. Just to make it obvious that // a double is returned. } else { // Area of the piece of fabric double fabricArea = width * length; // compute the area used for the coasters // Math.PI is a constant within the Math class (=3.1415...) // There is no exponent in Java. To square radius, write // radius*radius // (for higher exponents, use the Math.pow method) double usedArea = computeNumberOfCoasters(radius) * Math.PI * radius * radius; // area unused double unusedArea = fabricArea - usedArea; // return it as a percentage return unusedArea / fabricArea * 100.0; } } /** * Check that the width and length are positive integers.
* Set to one foot any of them that is negative or 0 and display an error * message. */ private void checkWidthAndLength() { if (width <= 0) { width = 1; JOptionPane.showMessageDialog(null, "Invalid value for the width." + "\nThe width is set to 1 ft", "Input error", JOptionPane.ERROR_MESSAGE); } if (length <= 0) { length = 1; JOptionPane.showMessageDialog(null, "Invalid value for the length." + "\nThe length is set to 1 ft", "Input error", JOptionPane.ERROR_MESSAGE); } } }