Wednesday, June 27, 2012

Cubic Yard of Dirt Example

Here is the YardCalc.Java

package com.spconger.www;

public class YardCalc {
 /*
  * This class will calculate the cubic yards
  * of soil needed for a yard
  * Steve Conger
  * 6/27/2012
  */
 
 //fields
 
 private double length;
 private double width;
 private final double HEIGHT =.167;
 
 
 //constructor
 
 public YardCalc(){
  length=1;
  width=1;
 }
 
 public YardCalc(double l, double w)
 {
  length=l;
  width=w;
 }
 //Assessors/mutators (gets sets)
 
 public void setLength(double len){
  if (len >0){
  length=len;
  }
  else{
   length=1;
  }
  }
 

 public double getLength(){
  return length;
 }


 public double getWidth() {
  return width;
 }


 public void setWidth(double width) {
  this.width = width;
 }


 
 
 //class methods
 
 public double calcSquareFeet(){
  
  return getLength() * getWidth();
 }
 
 public double calcTotalYards(){
  return calcSquareFeet() /3;
 }
 
 public double calcCubicYards(){
  return calcTotalYards() * HEIGHT;
 }

}


Here is the Display.java

package com.spconger.www;

import java.util.Scanner;



public class Display {
 
 /**
  * This class gets the input and displays
  * the output
  * Steve Conger
  * 6/27/2012
  */
 private YardCalc yc;
 
 public Display()
 {
  getInput();
  showOutput();
 }
 //spconger@gmail.com
 
 private void getInput(){
  
  Scanner scan=new Scanner(System.in);
  
  System.out.println("Enter your yard's length");
  double length = scan.nextDouble();
  System.out.println("Enter your yard's width");
  double width= scan.nextDouble();
  yc=new YardCalc(length,width);
 }
 
 private void showOutput(){
  
  double squareFeet=yc.calcSquareFeet();
  double squareYards=yc.calcTotalYards();
  double cubicYards=yc.calcCubicYards();
  
  String strSquareYards = Double.toString(squareYards);
  String strCubicYards = Double.toString(cubicYards);
  
  System.out.println("the total square feet is" +Double.toString(yc.calcSquareFeet()));
  System.out.println("Your square yards are" + strSquareYards);
  System.out.println("You need " + strCubicYards + " yards of dirt");
  
  
 }

}


Here is the Program.java

package com.spconger.www;

public class Program {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Display d = new Display();

 }

}

No comments:

Post a Comment