// Author: Kiri Wagstaff, wkiri@cs.cornell.edu // Date: June 27, 2001 // Brief quiz public class Quiz { public static void main(String[] args) { // 1. Calculate the average of { 1, 2, 3, 4, 5 } // Two possible solutions: System.out.println((1 + 2 + 3 + 4 + 5) / 5); // int output System.out.println((1 + 2 + 3 + 4 + 5) / 5.0); // double output // *But* what if you want the average of { 1, 3, 4, 5 }? // Need to use "floating point division" (divide by double) System.out.println((1 + 3 + 4 + 5) / 4.0); // double output // 2. Everyone gets the same number of (whole) pizza slices. int nSlices = 14; int nPeople = 4; // How many does each person get? int nPerPerson = nSlices / nPeople; // How many are left over? int nLeftover = nSlices % nPeople; System.out.println("Each person gets " + nPerPerson + " slices."); System.out.println("There are " + nLeftover + " left over."); // 3. What if there are more people than total slices? // Write an if-statement that outputs an error if this is true. if (nPeople > nSlices) { System.out.println("Error: not enough pizza!"); } // In a real program, you would move this check up // before calculating nPerPerson and nLeftover. } }