IST 297D – Introduction to Application Programming Chapter 5 Problem Set Name _ _____ 1. Briefly explain the concept of a black box as discussed Chapter 5. (1 point) Black Box is a concept used by engineers for a device with a given specification, but unknown implementation. Causes programmers to want to use your methods without knowing what goes on inside. 2. Briefly explain the concept of functional decomposition as discussed in class. (1 point) It's the same thing as a stepwise refinement. In order to solve a difficult task, you break it down into simpler tasks. Then keep breaking down the simpler tasks until you are left with tasks you can solve, like in the making coffee example.
3. Write a Java method named addNumbers, which accepts two numbers as integers and returns their sum as an integer. (1 point) public class AddNumbersMethod { /** * @param args the command line arguments */ public static double numberAdd(double total){ double sum; int a = 10; int b = 4; if (total >= 0) { sum = a+b; } else { sum = 0; } return sum; } public static void main(String[] args){ double a = 10; double b = 4; double sum = a+b; System.out.println("Your integers when added equal " +sum);
} }
4. Write a method that calculates the area of a rectangle by multiplying its length by its width. The length and width parameters are double values and the method returns a double. (1 point) public class AreaofRectangleMethod { /** * @param args the command line arguments */ public static double areaRectangle(double length, double width) { double baseArea = length * width; return baseArea * 2; } public static void main(String[] args) { double lengthN = areaRectangle(9, 10); double widthN = areaRectangle(4, 5); double areaBase = lengthN * widthN; System.out.println("Length: " +lengthN + " inches."); System.out.println("Width: " +widthN + " inches."); System.out.println("Total area of the rectangle is " +areaBase + " inches."); } }
5. Briefly explain what is wrong with the program below, and what you would do to fix the problem. (1 point) public static void main(String[] args) { String firstName = "Joe"; String fullName = getFullName(); } public static String getFullName(){ String lastName = "Bloggs"; String fullName = firstName + " " + lastName; return fullName;
} In order for the string method to work, the programmer needs to import java.util Scanner; Then declare the Scanner in = new Scanner(System.in); into the main method. Afterwards, move the String firstName to the public static method where it should look like.... public static String getFullName(){ String firstName = "Joe"; String lastName = "Bloggs"; String fullName = firstName +" " + lastName; } Then fix the main method so it looks like.... public static void main(String[] args) { String fullName = getFullName(); return fullName; System.out.println("Name: " +fullName); } By making these simple corrections, the program will be able to print the person's name as "Joe Briggs".
6. Write a Java method called nameFormetter. The method takes two strings, firstName and lastName as input parameters and returns the full name formatted as: Bloggs, Joe. (1 point) public class NameFormatterMethod { /** * @param args the command line arguments */ public static void main(String[] args){ String firstNameString = "Joe"; String lastNameString = "Briggs,"; System.out.println(firstNameString + " " + lastNameString);
String myReversedString = lastNameString + firstNameString; System.out.println(lastNameString + " " + firstNameString); } public static String reverseString(String theStringToReverse){ String reversedString = "Briggs, Joe"; for(int i = (theStringToReverse.length()-1); i >= 0; i--){
reversedString = reversedString + theStringToReverse.charAt(i); } return reversedString; } }
7. Write a Java statement to call the method your wrote in problem #6 above. (1 point) The Call Print theStringToReverse(4) calls Print theStringToReverse (3) The Call Print theStringToReverse(3) calls Print theStringToReverse (2) The Call Print theStringToReverse(2) calls Print theStringToReverse (1) The Call Print theStringToReverse(1) calls Print theStringToReverse (0) The Call Print theStringToReverse(0) returns, doing nothing. The Call Print theStringToReverse(1) print firstNameString. The Call Print theStringToReverse(2) print lastNameString. The Call Print theStringToReverse(3) print lastNameString + firstNameString. The Call Print theStringToReverse(4) print lastNameString + " " + firstNameString.
8. Write a complete Java program called CalcTotalPrice. The program must include five methods: getSaleTotal, getSalePrice, getSaleWeight, calcTax, and calcShipping. getSaleTotal takes no input parameters and returns a double, which is the sale total, and which it computes by calling the other four methods. getSalePrice returns a double, which it gets from the user at the command line. getSaleWeight returns a double, which it gets from the user at the command line. calcTax takes a double as a parameters (the sale price) and returns the tax amount as a double (use 6% as a fixed tax rate). calcShipping takes a double as a parameter (the sale weight) and returns the shipping amount as a double (calculate shipping as $10 if weight is less than 10 and $20 if weight is 10 or greater). getSaleTotal should print the sale price amount, tax amount, shipping amount, and sale total amount to the command line. (3 points) import java.util.Scanner; public class CalcTotalPrice { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner in = new Scanner(System.in); double getSalePrice; String getStoreItem; double getWeight;
System.out.println("Welcome to the online store!"); System.out.println("What did you choose to buy? "); getStoreItem = in.next(); System.out.println("You selected " +getStoreItem); System.out.println("How much is the item? "); getSalePrice = in.nextDouble(); System.out.println("The item costs $" +getSalePrice + " dollars."); System.out.println("How much does it weigh? "); getWeight = in.nextDouble(); System.out.println("It weighs " +getWeight + " pounds."); System.out.println("Ok, now we need to calculate the tax!"); double calcTax = getSalePrice * 0.06; System.out.println("Taxes are $" +calcTax + " dollars."); System.out.println("Your subtotal comes to $" + (getSalePrice + calcTax) + " dollars." ); double calcShipping = 10; double calcShippingX = 20; System.out.println("WARNING: All of our products include a shipping fee depending on your items weight."); System.out.println("Items less than 10 pounds will cost $10 and $20 for those weighing more than 10 pounds!"); do{ if(getWeight < 10);{ System.out.println("Option 1: $" +calcShipping + " dollars."); } }while(getWeight > 10);{ System.out.println("Option 2: $" +calcShippingX + " dollars."); } System.out.println("Based off of your item's weight, the computer will automatically pick the proper shipping option!"); System.out.println("Your Shipping fee is: $" +calcShipping + " dollars."); double getSaleTotal; System.out.println("With all of your fee's, your total comes to: $" + (getSalePrice +calcTax +calcShipping) + " dollars please!"); } }