CS302 Fall 2012 Exam 2 Solution // Part I: Write a complete program import java.util.Scanner; public class DairyDelivery { public static void main(String[] args) { final double MILK_PRICE = 2.75; final double BOTTLE_DEPOSIT = 1.50; final int MIN_ORDER = 3; final double DISC1 = 10.0; // DISCOUNT LEVELS final double DISC2 = 20.0; final double DELIVERY = 2.5; // $ 0- 9.99 is $2.50 final double DELIVERY1 = 1.5; // $10-19.99 is $1.50 final double DELIVERY2 = 0; // $20+ is $0.00 Scanner stdin = new Scanner(System.in); int halfgals = -1, bottles = -1; // Get number of bottles ordered while ( halfgals < MIN_ORDER ) { System.out.print("Number of half gallons? "); halfgals = stdin.nextInt(); if (halfgals < MIN_ORDER) { System.out.println( "Error! Please enter an int >= "+MIN_ORDER); } } // Get number of empty bottles returned while ( bottles < 0 ) { System.out.print("Number of bottles returned? "); bottles = stdin.nextInt(); if ( bottles < 0 ) { System.out.println( "Error! Please enter an int >= 0"); } } // Compute invoice items double milk = halfgals * MILK_PRICE; double deposit = halfgals * BOTTLE_DEPOSIT; double refund = bottles * BOTTLE_DEPOSIT; double delivery = 2.50; // delivery chg is $2.50 if ( milk >= DISC2 ) delivery = DELIVERY2; // if >= 20, chg is FREE else if ( milk >= DISC1 ) delivery = DELIVERY1; // else if >= 10, chg is $1.50 double ppg = (milk + delivery) / (halfgals / 2.0); // price per gallon System.out.println( "Milk Subtotal: $" + milk + "\n" + "Bottle Deposit Charge: + " + deposit + "\n" + "Bottle Return Refund: - " + refund + "\n" + "Delivery Charge: + " + delivery + "\n" + "Total Bill: = $" + (milk+deposit-refund+delivery) + "\n" + "Cost Per Gallon: = $" + ppg + "/gal" ); } // end main } // end class // Part II: #1 Write a code fragment that replaces Wisc. or Wis. with WI //assume city references a city, state and zip code // for example "Windsor, Wis. 53598"; String WI = "WI "; String S = "Wis. "; if ( city.contains(S) ) { int i = city.indexOf(S); city = city.substring(0,i) + WI + city.substring(i+S.length()); } S = "Wisc. "; if ( city.contains(S) ) { int i = city.indexOf(S); city = city.substring(0,i) + WI + city.substring(i+S.length()); } // Part II: #2 Complete the update(double t) of the Ship class. public void update( double t ) { if ( ! destroyed ) { updatePosition(t); if ( getPosition().getY()-getRadius() <= 0 || getPosition().getY()+getRadius() >= Config.GAME_HEIGHT-1 ) { setRotation( -getRotation() ); } if ( getPosition().getX()-getRadius() <=0 || getPosition().getX()+getRadius() >= Config.GAME_WIDTH-1 ) { destroy(); } } } // Part III: Write an Instantiable Class import java.util.Random; public class ProblemGen { public static final int ADD = 0; public static final int SUB = 1; public static final int MUL = 2; public static final int DIV = 3; private final int MIN; // private final int MAX; // private final int OP; // private int op1, op2; // the operands private String question; // the problem private double answer; // the answer private static Random rng = new Random(); public ProblemGen (int low, int hi, int op) { MIN = low; MAX = hi; OP = op; nextQuestion(); } private void nextQuestion() { op1 = rng.nextInt(MAX-MIN+1)+MIN; // randomly pick each operand op2 = rng.nextInt(MAX-MIN+1)+MIN; // set question and answer fields char c = '?'; answer = 0; if ( OP == ADD ) { c = '+'; answer = op1 + op2; } else if ( OP == SUB ) { c = '-'; answer = op1 - op2; } else if ( OP == MUL ) { c = '*'; answer = op1 * op2; } else if ( OP == DIV ) { c = '/'; answer = op1 / (double)op2; } question = op1 + " " + c + " " + op2 ; } public String getQuestion() { return question; } public String toString() { return question + " = " + answer; } public boolean isCorrect( double possible ) { double err = 0.001; // returns true if possible is close to answer return ( answer-err < possible && possible < answer+err ); } // Part IV: #1 Write a code fragment that finds players with most points // Assume: ArrayList team = new ArrayList(); int mostPoints = 0; for ( int i = 0; i < team.size(); i++ ) { if ( team.get(i).getPoints() > mostPoints ) mostPoints = team.get(i).getPoints(); } for ( int i = 0; i < team.size(); i++ ) { if ( team.get(i).getPoints() == mostPoints ) System.out.println( "Most points scored by " + team.get(i) ); } // Part IV: #2 Write a complete method that counts individuals public static int[] countIndividuals( int [][] world ) { int uninfected = 0; int recovered = 0; int other = 0; for ( int i = 0; i < world.length; i++ ) { for ( int j = 0; j < world[i].length; j++ ) { if ( world[i][j] == Config.UNINFECTED ) uninifected++; else if ( world[i][j] == Config.RECOVERED ) recovered++; else if ( world[i][j] != Config.EMPTY ) other++; } } int [] counts = { uninfected, recovered, other }; return counts; }