Вы находитесь на странице: 1из 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

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 variables
double num = 1.0;
double denom = 1.0;
// initialisation:
double pi = 0.0;
for (int i = 0; i <= n; i++) {
pi += num/denom;
num = -num;
denom += 2;
}
pi = pi * 4;
System.out.println("After " + n + " iterations: pi = " + pi);
}
}

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 <= n; i++) {
denom = denom * i;
e += 1.0/denom;
}
System.out.println("After " + n + " iterations: e = " + e);
}
}
4.2
public class Exercise4_2 {
public static int sumDigits(long n) {
int sum = 0;
while (n > 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));
}
}

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.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));
}
}

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 <= n; k += 2) {
s = s + ratio;
ratio = -ratio * x * x / ((k + 1) * (k + 2));
}
return s;
}
public static void main(String[] arg) {
final double PI = 3.1415926536;
System.out.println("Sin(PI/4) = " + sin(PI/4, 100));
}
}

Вам также может понравиться