//*************************************** // Program Name: calccube.java * // Author: Pat Moss * // Date: Oct 20, 2005 Rev: 01 * // * // Demonstrate the class calccube, etc. * //*************************************** import java.util.Scanner; import java.text.DecimalFormat; public class calccube { private double glbheight = 0.0; // the cube height private double glbwidth = 0.0; // the cube width private double glblength = 0.0; // the cube length private double glbarea = 0.0; // the cube surface area private double glbvolume = 0.0; // the cube volume Scanner scan = new Scanner(System.in); // Constructor: Initialize the cube values public calccube() { initcube(); } // Initialize the cube values private void initcube() { glbheight = 0.0; // the cube height glbwidth = 0.0; // the cube width glblength = 0.0; // the cube length glbarea = 0.0; // the cube surface area glbvolume = 0.0; // the cube volume } // Request cube dimensions // Then calculate the surface area and volume // And display the results to the user public void docalccube() { initcube(); System.out.println ("Key in cube dimensions: height, width, length"); System.out.println ("on three separate lines."); System.out.println ("For example: 10.0 and 20.0 and 30.0"); glbheight = scan.nextDouble(); glbwidth = scan.nextDouble(); glblength = scan.nextDouble(); // calculate and display the surface area of the cube calcsurfacearea(); // calculate and display the volume of the cube calcvolume(); } // calculate and display the surface area of the cube private void calcsurfacearea() { double area1 = glbheight * glbwidth; double area2 = glbheight * glblength; double area3 = glbwidth * glblength; double totalarea = 2 * (area1 + area2 + area3); DecimalFormat fmt = new DecimalFormat ("0.#"); System.out.println (""); System.out.println ("The surface area is: " + fmt.format(totalarea)); } // calculate and display the volume of the cube private void calcvolume() { double area1 = glbheight * glbwidth; double area2 = glbheight * glblength; double area3 = glbwidth * glblength; double totalvolume = glbheight * glbwidth * glblength; DecimalFormat fmt = new DecimalFormat ("0.#"); System.out.println (""); System.out.println ("The volume is: " + fmt.format(totalvolume)); } // face Height accessor public double getFaceHeight() { return glbheight; } // face Width accessor public double getFaceWidth() { return glbwidth; } // face Length accessor public double getFaceLength() { return glblength; } // face Height mutator public void setFaceHeight(double inputheight) { if ((inputheight < 0.0) || (inputheight > 1000.0)) { // error -- so do nothing here } else { glbheight = inputheight; } } // face Width mutator public void setFaceWidth(double inputwidth) { if ((inputwidth < 0.0) || (inputwidth > 1000.0)) { // error -- so do nothing here } else { glbwidth = inputwidth; } } // face Length mutator public void setFaceLength(double inputlength) { if ((inputlength < 0.0) || (inputlength > 1000.0)) { // error -- so do nothing here } else { glblength = inputlength; } } // return a string representation of the cube dimension values public String toString() { String msg1 = ""; msg1 += "The height/width/length values are:"; msg1 += " " + glbheight; msg1 += " " + glbwidth; msg1 += " " + glblength; String result = msg1; return result; } }