Solutions Chapter 3 and 4 Java

6
Introduction to Java Programming Solutions Chapter 3 and 4 Exercises book Review Questions 4.7 Computing a sales commission given the sales amount and the commission rate Answer: non-void method with return type double Printing a calendar for a month Answer: void method Computing a square root Answer: non-void method with return type double Testing whether a number is even and return true if it is Answer: non-void method with return type boolean Printing a message for a specified number of times Answer: void method Computing the monthly payment, given the loan amount, number of years, annual interest rate. Answer: non-void method with return type double. Finding the corresponding uppercase letter given a lowercase letter. Answer: non-void method with return type char. 4.8 Line 2: method1 is not defined correctly. It does not have a return type or void. Line 2: type int should be declared for parameter m. Line 8: parameter type for n should be double to match xMethod(3.4). Line 11: if (n<0) should be removed in xMethod, otherwise a compilation error is reported. 4.9 public class Test { public static double xMethod(double i, double j) { while (i<j) { j--; } return j; } } 1

description

solutions to chapter 3 & 4 intro to java programming

Transcript of Solutions Chapter 3 and 4 Java

  • Introduction to Java Programming Solutions Chapter 3 and 4

    Exercises book

    Review Questions

    4.7

    Computing a sales commission given the sales amount and the commission rateAnswer: non-void method with return type double

    Printing a calendar for a monthAnswer: void method

    Computing a square rootAnswer: non-void method with return type double

    Testing whether a number is even and return true if it isAnswer: non-void method with return type boolean

    Printing a message for a specified number of timesAnswer: void method

    Computing the monthly payment, given the loan amount, number of years, annual interest rate.Answer: non-void method with return type double.

    Finding the corresponding uppercase letter given a lowercase letter.Answer: non-void method with return type char.

    4.8

    Line 2: method1 is not defined correctly. It does not have a return type or void.Line 2: type int should be declared for parameter m.Line 8: parameter type for n should be double to match xMethod(3.4).Line 11: if (n

  • 4.15

    Line 8: int n = 1 is wrong since n is already declared in the method signature.

    4.20

    The output is 15 (5+ 4+ 3+ 2+ 1 = 15).

    Programming Questions

    3.30

    public class Exercise3_30 {

    public static void main(String[] args) {int n = 1000;

    // useful variablesdouble num = 1.0;double denom = 1.0;// initialisation:double pi = 0.0;

    for (int i = 0; i

  • 3.31

    public class Exercise3_31 {

    public static void main(String[] args) {int n = 1000;

    double denom = 1.0;// initialisation:double e = 1.0;

    for (int i = 1; i 0) {

    sum += (int) (n % 10);n = n / 10;

    }return sum;

    }

    public static void main(String[] args) {long number = 13209643;System.out.println("Sum of digits of " + number + " is: "

    + sumDigits(number));}

    }

    3

  • 4.6

    public class Exercise4_6 {

    public static int min(int m, int n) {if (m < n) return m;else return n;

    }

    public static int gcd(int m, int n) {int gcd = min(m, n);while (m % gcd != 0 || n % gcd != 0) {

    gcd--;}return gcd;

    }

    public static void main(String[] args) {int number1 = 25;int number2 = 20;System.out.println("gcd of " + number1 + " and "+ number2 + " is: " + gcd(number1, number2));

    }}

    4

  • 4.22

    public class Exercise4_22 {

    public static int min(int m, int n) {if (m < n) return m;else return n;

    }

    public static int max(int m, int n) {if (m > n) return m;else return n;

    }

    public static int gcd(int m, int n) {int min = min(m, n);int max = max(m, n);if (max % min == 0) return min;else return gcd(min, max % min);

    }

    public static void main(String[] args) {int number1 = 25;int number2 = 20;System.out.println("gcd of " + number1 + " and "+ number2 + " is: " + gcd(number1, number2));

    }}

    4.24

    public class Exercise4_24 {

    public static int sumDigits(long n) {if (n / 10 == 0) return (int) n;else return (int)(n % 10) + sumDigits(n / 10);

    }

    public static void main(String[] args) {long number = 13209643;System.out.println("Sum of digits of " + number + " is: "

    + sumDigits(number));}

    }

    5

  • Extra exercise

    /*** This class contains a solution to the Extra Exercise of chapter 4.

    * @author Marko Boon

    */

    public class TaylorExercise {

    /*** Computes sin(x) using a Taylor series approximation of order n.

    * @param x the coordinate where the sinus function should be evaluated

    * @param n the order of the Taylor series.

    * @return sin(x) using a Taylor series approximation

    */

    public static double sin(double x, int n) {double s = 0;// PRECONDITION: ratio = +/- x^k / k!double ratio = x;for (int k = 1; k