Java programing

profileabash1

This homework assignment is due on Wednesday, September 13, 2017 at 12:00 PM (noon), and will be accepted no later than 5:00 PM on the same day.

On page 25 of Big Java: Late Objects, complete the following Review Exercises:

  • R1.7 What does this program print?

    public class Test
    {
       public static void main(String[] args)
       {
          System.out.println("39 + 3");
          System.out.println(39 + 3);
       }
    }
     
  • R1.7 What does this program print?

    public class Test
    {
       public static void main(String[] args)
       {
          System.out.println("39 + 3");
          System.out.println(39 + 3);
       }
    }
     
  • R1.9 What is the compile-time error in this program?

    public class Test
    {
       public static void main(String[] args)
       {
          System.out.println("Hello", "World!");
       }
    }
     
  • R1.13 Write an algorithm to settle the following question: A bank account starts out with $10,000. Interest is compounded monthly at 6 percent per year (0.5 percent per month). Every month, $500 is withdrawn to meet college expenses. After how many years is the account depleted?
    Use the Design Recipe to develop this algorithm.
     
  • R1.14 Consider the question in Exercise R1.13. Suppose the numbers ($10,000, 6 percent, $500) were user selectable. Are there values for which the algorithm you developed would not terminate? If so, change the algorithm to make sure it always terminates.
     
  •  R1.15 In order to estimate the cost of painting a house, a painter needs to know the surface area of the exterior. Develop an algorithm for computing that value. Your inputs are the width, length, and height of the house, the number of windows and doors, and their dimensions. (Assume the windows and doors have a uniform size.)
     
  • R1.19 The ancient Babylonians had an algorithm for determining the square root of a number a. Start with an initial guess of a / 2. Then find the average of your guess g and a / g. That’s your next guess. Repeat until two consecutive guesses are close enough. Write pseudocode for this algorithm.

On pages 26-27 of Big Java: Late Objects, complete the following Practice Exercises:

  • E1.1 Write a program that prints a greeting of your choice, perhaps in a language other than English.
     
  • E1.4 Write a program that prints the balance of an account after the first, second, and third year. The account has an initial balance of $1,000 and earns 5 percent interest per year.
     
  • E1.5 Write a program that displays your name inside a box on the screen.  Do your best to approximate lines with characters such as | - +.
     
  • E1.6 Write a program that prints your name in large letters, such as
    *   *    **    ****    ****   *   *
    *   *   *  *   *   *   *   *  *   *
    *****  *    *  ****    ****    * *
    *   *  ******  *   *   *   *    *
    *   *  *    *  *    *  *    *   *
  • E1.7 Write a program that prints your name in Morse code, like this:
    .... .- .-. .-. -.--
    Use a separate call to System.out.print for each letter.
     
  • E1.8 Write a program that prints a face similar to (but different from) the following:
       /////
      +"""""+
     (| o o |)
      |  ^  |
      | '-' |
      +-----+
  • E1.10W rite a program that prints a house that looks exactly like the following:
          +
         + +
        +   +
       +-----+
       | .-. |
       | | | |
       +-+-+-+ 
     
  • E1.11 Write a program that prints an animal speaking a greeting, similar to (but different from) the following:
     /\_/\     -----
    ( ' ' )  / Hello \
    (  -  ) <  Junior |
     | | |   \ Coder!/
     
  • E1.13 Write a program that prints a poem of your choice. If you don’t have a favorite poem, search the Internet for “Emily Dickinson” or “e e cummings”
     
  • E1.15 Type in and run the following program. Then modify it to show the message “Hello, your name!”.
    import javax.swing.JOptionPane;

    public class DialogViewer
    {
       public static void main(String[] args)
       {
          JOptionPane.showMessageDialog(null, "Hello, World!");
       }
    }
     
  • E1.16 Type in and run the following program. Then modify it to print “Hello, name!”, displaying the name that the user typed in.
    import javax.swing.JOptionPane;

    public class DialogViewer
    {
       public static void main(String[] args)
       {
          String name = JOptionPane.showInputDialog("What is your name?");
          System.out.println(name);
       }
    }
    E1.17 (OPTIONAL) Modify the program from Exercise E1.16 so that the dialog continues with the message “My name is Hal! What would you like me to do?” Discard the user’s input and display a message such as
    I'm sorry, Dave. I'm afraid I can't do that.
    Replace Dave with the name that was provided by the user.

    E1.18 Type in and run the following program. Then modify it to show a different greeting and image.
    import java.net.URL;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;

    public class Test
    {
       public static void main(String[] args) throws Exception
       {
          URL imageLocation = new URL(
                "http://horstmann.com/java4everyone/duke.gif");
          JOptionPane.showMessageDialog(null, "Hello", "Title",
                JOptionPane.PLAIN_MESSAGE, new ImageIcon(imageLocation));
       }
    }

On pages 71-73 of Big Java: Late Objects, complete the following Review Exercises:

  • R2.1 Write declarations for storing the following quantities. Choose between integers and floating-point numbers. Declare constants when appropriate.
    a. The number of days per week

    b. The number of days until the end of the semester

    c. The number of centimeters in an inch

    d. The height of the tallest person in your class, in centimeters
  • R2.2 What is the value of mystery after this sequence of statements?
    int mystery = 1;
    mystery = 1 - 2 * mystery;
    mystery = mystery + 1;
  • R2.3 What is wrong with the following sequence of statements?
    int mystery = 1;
    mystery = mystery + 1;
    int mystery = 1 - 2 * mystery;
  • R2.4 Write the following mathematical expressions in Java.

    R 2.4 Math Expressions
  • R2.5 Write the following Java expressions in mathematical notation.
    a. dm = m * (Math.sqrt(1 + v / c) / Math.sqrt(1 - v / c) - 1);

    b. volume = Math.PI * r * r * h;

    c. volume = 4 * Math.PI * Math.pow(r, 3) / 3;

    d. z = Math.sqrt(x * x + y * y);
  • R2.6 What are the values of the following expressions? In each line, assume that
    double x = 2.5;
    double y = -1.5;
    int m = 18;
    int n = 4;

    a. x + n * y - (x + n) * y

    b. m / n + m % n

    c. 5 * x - n / 5

    d. 1 - (1 - (1 - (1 - (1 - n))))

    e. Math.sqrt(Math.sqrt(n))
  • R2.7 What are the values of the following expressions, assuming that n and m have type int, n is 17, and m is 18?
    a. n / 10 + n % 10

    b. n % 2 + m % 2

    c. (m + n) / 2

    d. (m + n) / 2.0

    e. (int) (0.5 * (m + n))

    f. (int) Math.round(0.5 * (m + n))
  • R2.8 What are the values of the following expressions? In each line, assume that
    String s = "Hello";
    String t = "World";

    a. s.length() + t.length()

    b. s.substring(1, 2)

    c. s.substring(s.length() / 2, s.length())

    d. s + t

    e. t + s
  • R2.11 Find at least five compile-time errors in the following program.
    public class HasErrors
    {
       public static void main();
       {
          System.out.print(Please enter two numbers:)
          x = in.readDouble;
          y = in.readDouble;
          System.out.printline("The sum is " + x + y);
       }
    }
  • R2.12 Find three run-time errors in the following program.
    public class HasErrors
    {
       public static void main(String[] args)
       {
          int x = 0;
          int y = 0;
          Scanner in = new Scanner("System.in");
          System.out.print("Please enter an integer:");
          x = in.readInt();
          System.out.print("Please enter another integer: ");
          x = in.readInt();
          System.out.println("The sum is " + x + y);
       }
    }
  • R2.13 Consider the following code segment.
    double purchase = 19.93;
    double payment = 20.00;
    double change = payment - purchase;
    System.out.println(change);
    The code segment prints the change as 0.07000000000000028. Explain why. Give a recommendation to improve the code so that users will not be confused.
     
  • R2.14 Explain the differences between 2, 2.0, '2', "2", and "2.0".
  • R2.15 Explain what each of the following program segments computes.
    a. x = 2;
      y = x + x;

    b. s = "2";
      t = s + s;
  • R2.18 Write pseudocode for a program that computes the first and last digit of a number. For example, if the input is 23456, the program should print 2 and 6. Hint: %, Math.log10.
     
  • R2.19 Modify the pseudocode for the program in How To 2.1 so that the program gives change in quarters, dimes, and nickels. You can assume that the price is a multiple of 5 cents. To develop your pseudocode, first work with a couple of specific values.
     
  • R2.20 A cocktail shaker is composed of three cone sections.

    R 2.20 Cocktail Shaker

    Using realistic values for the radii and heights, compute the total volume, using the formula given in Self Check 25 for a cone section. Then develop an algorithm that works for arbitrary dimensions.
     
  • R2.21 You are cutting off a piece of pie like this, where c is the length of the straight part (called the chord length) and h is the height of the piece.

    R 2.21 Image

    There is an approximate formula for the area: R 2.21 Formula

    However, h is not so easy to measure, whereas the diameter d of a pie is usually well-known. Calculate the area where the diameter of the pie is 12 inches and the chord length of the segment is 10 inches. Generalize to an algorithm that yields the area for any diameter and chord length.
     
  • R2.25 For each of the following computations in Java, determine whether the result is exact, an overflow, or a roundoff error.
    a. 2.0 – 1.1

    b. 1.0E6 * 1.0E6

    c. 65536 * 65536

    d. 1_000_000L * 1_000_000L
  • R2.26 Write a program that prints the values
    3 * 1000 * 1000 * 1000
    3.0 * 1000 * 1000 * 1000
    Explain the results.
     
  • R2.27 This chapter contains a number of recommendations regarding variables and constants that make programs easier to read and maintain. Briefly summarize these recommendations.
     

On pages 75-76 of Big Java: Late Objects, complete the following Practice Exercises:

  • E2.1 Write a program that displays the dimensions of a letter-size (8.5 × 11 inches) sheet of paper in millimeters. There are 25.4 millimeters per inch. Use constants and comments in your program.
     
  • E2.2 Write a program that computes and displays the perimeter of a letter-size (8.5 × 11 inches) sheet of paper and the length of its diagonal.
     
  • E2.3 Write a program that reads a number and displays the square, cube, and fourth power. Use the Math.pow method only for the fourth power.
     
  • E2.4 Write a program that prompts the user for two integers and then prints
     
    • The sum
    • The difference
    • The product
    • The average
    • The distance (absolute value of the difference)
    • The maximum (the larger of the two)
    • The minimum (the smaller of the two)

      Hint: The max and min functions are declared in the Math class.
       
  • E2.5 Enhance the output of Exercise E2.4 so that the numbers are properly aligned:
    Sum:             45
    Difference:      -5
    Product:        500
    Average:         22.50
    Distance:         5
    Maximum:         25
    Minimum:         20
  • E2.6 Write a program that prompts the user for a measurement in meters and then converts it to miles, feet, and inches.
     
  • E2.7 Write a program that prompts the user for a radius and then prints
     
    • The area and circumference of a circle with that radius
    • The volume and surface area of a sphere with that radius
       
  • E2.8 Write a program that asks the user for the lengths of the sides of a rectangle. Then print
     
    • The area and perimeter of the rectangle
    • The length of the diagonal (use the Pythagorean theorem)
       
  • E2.9 Improve the program discussed in How To 2.1 to allow input of quarters in addition to bills.
     
  • E2.10 Write a program that helps a person decide whether to buy a hybrid car. Your program’s inputs should be:
     
    • The cost of a new car
    • The estimated miles driven per year
    • The estimated gas price
    • The efficiency in miles per gallon
    • The estimated resale value after 5 years

      Compute the total cost of owning the car for five years. (For simplicity, we will not take the cost of financing into account.) Obtain realistic prices for a new and used hybrid and a comparable car from the Web. Run your program twice, using today’s gas price and 15,000 miles per year. Include pseudocode and the program runs with your assignment.
       
  • E2.11 Write a program that asks the user to input
     
    • The number of gallons of gas in the tank
    • The fuel efficiency in miles per gallon
    • The price of gas per gallon

      Then print the cost per 100 miles and how far the car can go with the gas in the tank.
       
  • E2.14 Write a program that reads a number between 1,000 and 999,999 from the user and prints it with a comma separating the thousands. Here is a sample dialog; the user input is in color:
    Please enter an integer between 1000 and 999999: 23456
    23,456
  • E2.16 Write a program that reads in an integer and breaks it into a sequence of individual digits. For example, the input 16384 is displayed as
    1 6 3 8 4
    You may assume that the input has no more than five digits and is not negative.
     
  • E2.17 Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times. Here is a sample run. User input is in color.
    Please enter the first time: 0900
    Please enter the second time: 1730
    8 hours 30 minutes

What Do I Hand In?

Once you are done, submit your answers as any one of:

  • MS Word document (.docx or .doc file)
     
  • Rich Text Format document (.rtf file)
     
  • PDF document
     
  • Plain text document (.txt or .text file)
     
    • 7 years ago
    • 120
    Answer(0)