// 10/4/2017 /* PROBLEM: You are hired by a company that sells carpet. The company * needs a program to give estimates to customers. * The program is going to ask the user: * 1. the length of the room * 2. the width of the room * The program asks the user which level carpet they are buying * There are 3 levels: 1. $5 per sq foot 2. $10 3. $20 * Compute the price of carpeting the room. * Step 1: wrote 3 versions of pseudocode on board * Step 2: we are writing ONE method at a time, testing and then cont. */ import java.util.Scanner; /** This class illustrates methods by calculating * an estimate for a carpet company. */ public class CarpetCo { /** main method calls several methods * to read in data, calculate area and calculate price */ public static void main(String[] args) { int len, width; System.out.print("Enter length of your room in feet: "); len=readLength(); System.out.println("The length of your room in feet rounded " + "up to the nearest foot is: " +len); System.out.print("Enter width of your room in feet: "); width=readWidth(); System.out.println("The width of your room in feet is: " + width); int area=calculateArea(len, width); System.out.println("The area of your room in feet is: " + area); int pricePerSqFt=15; // this has to be changed to a method call //int price=calculatePrice(area, pricePerSqFt); // display price int sum = sum(len, width); System.out.println("First call, the sum of your numbers is: " + sum); // illustrate function overloading - more than one method // has same name sum = sum(); // this calls method with NO params System.out.println("Second call: the sum of your numbers is: " + sum); // sum = sum(78); Compiler error } /** * method * @param val * @param val2 * @return */ public static int readLength() { double length; // local variable Scanner input = new Scanner(System.in); length = input.nextDouble(); // round up length to closest integer double lenn = Math.ceil(length); // ceiling is smallest integer that is larger than # return (int)lenn; // casting forces a type conversion //return Math.round(length); } /** * method * @param val * @param val2 * @return */ public static int readWidth() { int w; // local variable Scanner input = new Scanner(System.in); w = input.nextInt(); return w; } public static int sum(int addend1, int addend2) { return addend1 + addend2; } public static int sum() { Scanner in = new Scanner(System.in); System.out.print("Enter 2 integers to add: "); int x, y; x = in.nextInt(); y = in.nextInt(); return x+y; } /** Compiler Error: 2 methods with same name and same parameters * cannot be defined. A different return type is NOT enough. public static double sum() { Scanner in = new Scanner(System.in); System.out.print("Enter 2 integers to add: "); double x, y; x = in.nextDouble(); y = in.nextDouble(); return x+y; } **/ /** * method * @param val * @param val2 * @return */ public static int calculateArea(int length, int width) { return length*width; } }