java code for sample projects methods

4
Methods Project Page 1 Methods Project PaintEstimate.java File importjavax.swing.JOptionPane; importjava.text.DecimalFormat; public class PaintEstimate { public static void main(String[] args) { String strNumberRooms; intnumberRooms; String strRoomWallsSqFt; doubleroomWallSqFt; doublelaborCostPerHour = 18; String strGallonCost; doublegallonCost; doubletotalSquareFootage = 0 ; doubletotalJobCharge = 0 ; introomNumberCounter = 1; doublenumJobGallons = 0; doubletotalJobHours = 0; doubletotalPaintCharge = 0; doubletotalLaborCharge =0; JavaCode for Sample Projects

Upload: jwjablonski

Post on 04-Jul-2015

1.080 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Code for Sample Projects Methods

Methods Project Page 1

Methods Project

PaintEstimate.java File

importjavax.swing.JOptionPane; importjava.text.DecimalFormat; public class PaintEstimate { public static void main(String[] args) { String strNumberRooms; intnumberRooms; String strRoomWallsSqFt; doubleroomWallSqFt; doublelaborCostPerHour = 18; String strGallonCost; doublegallonCost; doubletotalSquareFootage = 0 ; doubletotalJobCharge = 0 ; introomNumberCounter = 1; doublenumJobGallons = 0; doubletotalJobHours = 0; doubletotalPaintCharge = 0; doubletotalLaborCharge =0;

JavaCode for Sample Projects

Page 2: Java Code for Sample Projects Methods

Methods Project Page 2

DecimalFormatdf = new DecimalFormat("0.00");

strNumberRooms = JOptionPane.showInputDialog("Welcome to the Painting Cost Estimator. Please enter the number of rooms to be painted." );

numberRooms = Integer.parseInt(strNumberRooms); while (roomNumberCounter<= numberRooms) { //take string input and convert to double

strRoomWallsSqFt = JOptionPane.showInputDialog("Please enter the total wall space in square feet for room #" + roomNumberCounter );

roomWallSqFt = Double.parseDouble (strRoomWallsSqFt); //increment total square footage variable totalSquareFootage = totalSquareFootage + roomWallSqFt; //increment roomNumberCounter roomNumberCounter = roomNumberCounter + 1; } strGallonCost = JOptionPane.showInputDialog("Please enter the cost per gallon of the paint that you have selcted." ); gallonCost = Double.parseDouble(strGallonCost); numJobGallons = calculateGallons (totalSquareFootage); totalJobHours = calculateJobHours (totalSquareFootage); totalPaintCharge = calculatePaintCharge (numJobGallons, gallonCost ); totalLaborCharge = calculateLaborCharge (totalJobHours, laborCostPerHour); totalJobCharge = totalPaintCharge + totalLaborCharge;

Page 3: Java Code for Sample Projects Methods

Methods Project Page 3

System.out.println("PAINT COST ESTIMATE");

System.out.println("JOB SPECIFICATIONS: Number of Rooms: " + numberRooms + "; Total Wall Space: " + totalSquareFootage + " square feet." );

System.out.println("COST PER GALLON OF PAINT SELECTED: $" + df.format(gallonCost)); System.out.println("TOTAL PAINT COST FOR JOB: $" + df.format(totalPaintCharge));

System.out.println ("TOTAL LABOR COSTS based on an estimate of " + df.format(totalJobHours) + " hours of labor at $" + df.format(laborCostPerHour) + " per hour: $" + df.format(totalLaborCharge));

System.out.println("ESTIMATED TOTAL COST FOR PROJECT: $" + df.format(totalLaborCharge)); } public static double calculateGallons (double sqFootage) { doublecoveragePerGal = 115; doublenumGallons; numGallons = sqFootage/coveragePerGal; returnnumGallons; } public static double calculateJobHours (double sqFootage) { //double hoursPerSqFoot; doublenumJobHours; // //convert given ratio of 8 hours to 115 sq feet to hours per sq foot //hoursPerSqFoot = 8/115; //apply number of hours per sq foot to total sq footage //multiply total sq feet by conversion of hours to sq feet given numJobHours = sqFootage * 8/115; returnnumJobHours; }

Page 4: Java Code for Sample Projects Methods

Methods Project Page 4

public static double calculatePaintCharge (double numGallons, double costPerGallon ) { doublepaintCharge; paintCharge = numGallons * costPerGallon; returnpaintCharge; } public static double calculateLaborCharge (double numJobHours, double laborCostPerHour) { doubletotalLaborCost; totalLaborCost = numJobHours * laborCostPerHour; returntotalLaborCost; } }