15-110 FALL 2009 (CORTINA)

HOMEWORK 1 - due Friday, September 4, 2009 in class

Please remember that homework must be handed in on time. No late homework will be accepted.

Be sure to read through Chapter 2 of your textbook for more information about Java variables, data types, operators and statements.

  1. (2 pts) Each of the following Java instructions has an error. Identify whether the error is a syntax error or a logical error and explain why. You may assume that the variables used on the right side of each equal sign have already been declared and initialized. The variable names indicate what information is stored in each of the variables.

    double speed = timeInHours / distanceInMiles;
    
    String title = 'Angels and Demons';
    
    int x = 110/2.5;
    
    int trillion = 1000000 * 1000000; 
    

  2. (1 pt)

    1. Using Java, show how to declare an appropriately-named int variable for a zip code and initialize it to the value 15213.

    2. In general, can we store a zip code in a variable using the data type short? Explain.

  3. (1.5 pts) What is the value of each of the following Java expressions? Show your work.

    6 + 5 * 4 - 3 / 2
    
    (int)(60.0 / 9) 
    
    (double)(72 % 5) 
    

  4. (1.5 pts) What is the output of the following Java code fragments? Explain each answer.

    System.out.println(2 + 2 + " = 4");
    System.out.println("4 = " + 2 + 2);
    System.out.println("4 = " + (2 + 2));
    
    int a = 2;
    System.out.println(a);
    a += 3;
    System.out.println(a);
    a *= 4;
    System.out.println(a);
    a -= 5;
    System.out.println(a);
    a /= 6;
    System.out.println(a);
    a++;
    System.out.println(a);
    
    int x = 4;
    int y = 10;
    double a = 10.0;
    double b = 4.0;
    System.out.println((double)(x / y));
    System.out.println((double) x / y);
    System.out.println((int)(a / b));
    System.out.println((int) a / b);
    

  5. (1.5 pts) Assume the int variable num stores a 4-digit positive integer.

    1. Write a Java instruction to print out the one's digit of num.

    2. Write a Java instruction to print out the ten's digit of num.

    3. Write a Java instruction to print out the hundred's digit of num.

  6. (1 pt) The following Java program is written to compute the amount a truck driver has to pay to buy 50 gallons of gasoline at a gas station.

    public class GasolinePaymentComputer
    {
            public static void main(String[] args)
            {
                    double pricePerGallon = 2.239;  // $2.239 per gallon
                    int numGallonsPurchased = 50;
                    System.out.println("Total gas purchase = $" +
                            pricePerGallon * numGallonsPurchased);
            }
    }
    

    Running this program produces the following output:

    Total gas purchase = $111.94999999999999
    

    Explain why the output does not show $111.95 as the expected answer.

  7. (1.5 pts) Complete the following Java class that contains a main method that computes how many first class stamps can be purchased for a given amount of money (in dollars and cents) and how many cents would be left over after the purchase. Your answers should consist of formulas that will work for any valid value for numDollars and numCents. You may assume numCents is always between 0 and 99, inclusive.

    public class StampComputer
    {
    	public static void main(String[] args)
    	{
    		// In this example, the user has $20.09 to buy stamps
    		int numDollars = 20;
    		int numCents = 9;
    		int stampCost = 44;	// each stamp costs 44 cents
    
    		// Compute the maximum number of stamps the user can purchase
    
    		int numStamps = ____________________________________________________;
    
    		// Compute the amount of change (in cents) the user will have left over
    
    		int change =    ____________________________________________________;
    
    		System.out.println("You can purchase " + numStamps + " stamps.");
    		System.out.println("You will have " + change + " cents remaining.");
    	}
    }
    

    For example, the output of this method for the data given should be

    You can purchase 45 stamps.
    You will have 29 cents remaining.