This action might not be possible to undo. Are you sure you want to continue?

Engineering Software Development in Java

7.15

Exercises

The problems in this section cover the basics, including use of keyboard input, looping and branching constructs, simple arrays, and generation of formatted output. 7.1 It is well known that the formula for converting a temperature in Celsius to Fahrenheit is

o

F =

9 o · C + 32. 5

(7.10)

Write a Java program that will prompt the user for the temperature in degrees Celsius, and compute and print the equivalent temperature in Fahrenheit. 7.2 This problem will give you practice at writing loops, computing arithmetic operations with the modulo operator, and computing a simple summation of values. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write a java program to ﬁnd and print the sum of all the multiples of 3 or 5 below 1000. 7.3 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. Write a java program to ﬁnd and print the smallest number that is evenly divisible by all of the numbers from 1 to 20? Hint. For a given number n, checking that n%2 == 0, n%3 == 0, n%4 == 0, · · · n%19 == 0, n%20 == 0 is excessive. Recall that all numbers have a unique prime factorization – for example, the 4 can be represented a the product 2 times 2. Similarly, 6 can be represented as the product of 2 and 3, and so forth. Therefore if a number is divisible by 6 then it is automatically divisible by 2 and 3. You should use this fact to minimise the number of modulo evaluations. 7.4 Figure 7.12 shows the proﬁle of a 400 m running track. Suppose that an athelete runs “x” meters from the inner track. Write a Java program that computes the distance around the track for values of “x” ranging from 0 m to 1 m, and prints the results in a tidy table.

x (m)

100 m

100 m

100 m

Figure 7.12. Running Track

1. b. The mass is connected to two walls by springs having stifnesses k1 and k2 .12) where s = (a + b + c)/2.. Write a Java program that will prompt the user for the three side lengths of the triangle...0 m ... Mass-Spring System The natural period of the mass-spring system is: T = 2π m k1 + k2 (7. 0....13 shows a mass m resting on a frictionless surface.. ==================== Try to format the output so that distances are printed to one decimal place of accuracy and arranged vertically in a nice tidy format.6 m .. . and c is Area = s(s − a)(s − b)(s − c) (7..5 Figure 7.4 m ..... and k2 .. k1 .13....Chapter 7 191 The output should look something like: X (m) | Distance (m) ==================== 0.2 m ......6 The area of a triangle having side lengths a. check that the supplied values are all greater than zero...8 m . your program should check that all side lengths are greater than zero. 7. and then compute and print the natural period of the mass-spring system.12.. Note.11) Write a Java program that will prompt a user for m. To ensure that the triangle is well deﬁned. k_1 m k_2 Figure 7. and that the sum of the shorter two side lengths exceeds the length of the longest side.. 0.. and then compute and print the area of the triangle via equation 7. 7.0 m 0.0 m 400. 0..

3.7 Figure 7. Write a Java program that will: 1.Math.lang.15 shows the circular cross section of a torus ring having radius R and cross section diameter D. and that D/2 is smaller than R. for (int i = 3. To ensure that the torus is well deﬁned. Write a Java program that will: 1. The volume and surface area of the torus ring is given by: Volume = 2π 2 R D2 4 (7. (x2 .14 shows a triangle deﬁned by the vertex coordinates (x1 . y3 ). public class EulerMath { public static boolean isPrime( long num ) { if (num < 2 || (num % 2 == 0 && num != 2)) return false.sqrt(num).9 The fragment of code: import java. Print the triangle vertices in a tidy table. The method will return true if num is prime. return true. your program should check that R and D are positive. . 7. 7. otherwise it will return false. i += 2) if (num % i == 0) return false. Note. Compute and print the area of the triangle (make sure that it is positive) and its perimeter. y2 ) and (x3 .13) and Surface Area = 4π 2 R D 2 (7. Compute and print the volume and surface area of the torus. Interactively prompt a user for the torus radius R and the cross section diameter D. i <= Math. 2. Interactively prompt a user for the x and y coordinates at each of the triangle vertices. 2.8 Figure 7.192 Engineering Software Development in Java 7. } } deﬁnes a class called EulerMath. y1 ).14) respectively. The method isPrime() that determines whether or not an integer (actually a long integer) integer num is prime.

y_1 ) x Figure 7. R D D Figure 7. Vertex coordinates for triangle. y_2 ) ( x_3 . y_3 ) ( x_1 .15. Cross-section and dimensions of torus.14.Chapter 7 193 y ( x_2 . .

7. A suitable program structure and array declaration is: public class OpenChannel { public static void main ( String args[] ) { // Define two-dimensional array of channel properties.0 0. and the result with either be true or false. namely: √ BH S n B + 2H 2/3 (7.16) where B and H are the width and height of the channel (m)... can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred.5 0...025 0.. ).isPrime(4) evaluates to false).. Write a java program to ﬁnd and print all of the prime numbers less than 1000 in a tidy table.0 ====================================== Use a two-dimensional array to deﬁne and store the array problem parameters.0002 8 1. The longest sum of consecutive primes below one-thousand that adds to a prime.g. and n is the roughness coefﬁcient. To call the method we simply write: EulerMath. Suppose that the properties of three open channels are stored in a rectangular array: -------------------------------------n S B H ====================================== 0. below one-million.. EulerMath. (e. and is equal to 953..0001 10 2. fill in details here .10 The prime 41. }.022 0.15) U= (7.020 0. and then compute the water speed for each of the three channel conﬁgurations. double channel[][] = { { .11 The velocity of water in a rectangular open channel is described by Manning’s equation. contains 21 terms..194 Engineering Software Development in Java Notice that the method declaration includes the keyword static. Hint.isPrime ( . meaning that it can be called without ﬁrst having to create an object. Which prime. . This makes isPrime() a class method.. S is the channel slope. can be written as the sum of the most consecutive primes? 7.0003 15 2.

. 2 (7. and a for-loop construct to compute the sum of matrix element terms.15 The sum of the squares of the ﬁrst ten natural numbers is...14 Write a Java program that uses a looping construct to ﬁnd the least value of “n” so that S = 1 + 2 + 3 · · · + n > 10.18) 1 1 1 1 + + + + ··· 1·2 2·3 3·4 4·5 (7.. 12 + 22 + 32 + · · · + 102 = 385..22) (7.. repeat the experiment using an array for the series coefﬁcients.. + 10]2 = 552 = 3025. (7. 7. } } 195 7..12 Use a simple looping construct to demonstrate that the series summation: S= approaches 1......13 Leibnez’s series is given by: π 1 1 1 1 = 1 − + − + + ··· 4 3 5 7 9 (7.Chapter 7 { .. } }. fill in details here . print the sum S and whether or not the inequality is true or false.. use a for-loop construct to compute the series summation directly. fill in details here . fill in details here .. // Compute and print water speeds .. . First. 000 (7. fill in details here .. }.. [1 + 2 + .. // Print channel properties .20) 7.. . Then.19) is true. The square of the sum of the ﬁrst ten natural numbers is. 7... { . Check your result via the summation formula S = 1 + 2 + 3··· + n = n(n + 1) ..17) Write a Java program to compute Leibniz’s series summation for 1000 terms.21) . For each iteration of the loop.

g.16 Write a computer program that will ﬁnd and print all non-negative integer values of x. respectively. Assuming that L is a constant and L > 2x. 7.17 Write a Java program that will print a list of points (x. then the remaining metal can be folded into a pyramid. or (2) evaluate y(x) for all values of x and then test for various types of error. Things to do: 1. 2.NaN. y and z such that: x2 + y 2 + z 2 = 377.23. x−2 (7. Show that the pyramid volume is given by: Volume(x) = 4x2 3 (L2 − 2Lx)cm3 (7.POSITIVE INFINITY and Double. Write a Java program that will prompt a user for the length L.23) (7.16.196 Engineering Software Development in Java Hence the difference between the sum of the squares of the ﬁrst ten natural numbers and the square of the sum is 3025 . You should ﬁnd that y(0) and y(2) evaluate to not-a-number (NaN) and positive inﬁnity. 7. Compute and print the maximum and minimum values of x+y+z that also satisfy equation 7.18 A square of sheet metal having side length 2L cm has four pieces cut out symmetrically from the corners as shown in Figure 7.. These quantities can be tested for via the error condition constants Double.385 = 2640. 7. and then compute and print volumes for appropriate values of x. e. Organize your output into a tidy table. something like: .24) y(x) = for the range −4 ≤ x ≤ 10 in intervals of 0.25) Note: You can approach this problem in one of two ways: (1) detect a numerical problem before it occurs and print out an appropriate message.25. y) on the graph of the equation x4 + x sin(x) (7. Find the difference between the sum of the squares of the ﬁrst one hundred natural numbers and the square of the sum.26) and that the maximum volume occurs when x = 2L/5 cm.

. .86 p(1 − 0........... ========================================== 3....01 p) m3 /min.. .19 Empirical studies indicate that the water-ﬂow rate.... ..........27) where p is the business district population in 1000s. ... in increments of: .000. the maximum pyramid volume occurs when x = 4cm...000 through 1.... Sheetmetal schematic for a folded pyramid..16... Use your Java program to show that when L = 10 cm. Write a Java program to compute and print a table of water-ﬂow rates for populations varying from 1.....000. (7.. .. 7..... . . needed for business district ﬁreﬁghting is: √ √ Q(p) = 3. L = 20 cm ========================================== x (cm) volume (cmˆ3) ========================================== .. Q.Chapter 7 2L 197 2x 2L 2x Figure 7.

floor(). 0.198 Engineering Software Development in Java 7. Two methods from the Math class library that you may ﬁnd useful are Math. 0.25 5. and conforms to the following speciﬁcations: 1.0F. 0.64 0. . 3.4F. 1.75 4.2F. 000 Population Increment(p) = 10.00 0. 1. 0.28) F (t) = (7.31 1.0F.50 4. 1. 0.2F.0F.0F.00 2.0F. 1. Write a Java program that computes and prints F (t) for 0 ≤ t ≤ 3 seconds.61 1. 0.29) F(t) has units kN.0F. 000 100. 0.8F.6F. regular one second wind gusts produce a forward thrust on a yacht sail corresponding to 4 + 15 · t − 135 · t3 (731 − 171t) /140 0. The wind force should be computed in a method WindForce() having the declaration: public double WindForce ( double fTime ). 1.20 Suppose that during squally conditions.0F. 0.61 0.4F. 0.25 5. 0. 2. 1.00 4.3.0F.0F.00 Hint.1F.8F. 2.61 2. 0. 000 1 < p < 10 10 < p ≤ 100 100 < p ≤ 1. 0. 0.0F.5F.50 4.1F. The program output should look something like the following: Time Thrust (seconds) (kN) ========================= 0. 0.5F.64 2. 0.64 1.0F.50 4.0F.00 4. 0.4F }.pow() and Math. 0. 1.5F.0F. 000 (7.2F. 2.0 1.75 4.25 5.0F.00 4. 2.31 2.75 4.00 4. 0.3 < t ≤ 1. The main() method should use a while or for looping construct to cycle over the time interval 0 through 3 seconds in increments of 0.00 1.0 ≤ t ≤ 0. 3. 7.21 The array of ﬂoating point numbers float fRainFall [] = { 1. 2.0F.05F.31 3.25 seconds.

} meanValue { float rainfall [] ) { .. (7. The mean value of the data points is given by 1 N N µx = xi i=1 (7. public public public public static static static static float float float float minValue { float rainfall [] ) { .. y 4 3 2 1 1 2 3 4 5 6 x Figure 7. method declarations need to explicitly say “this method can be called even if an instance of the class does not exist...” i... } maxValue { float rainfall [] ) { .30) and the standard deviation by N σx = i=1 [xi − µx ]2 N 1/2 = 1 N N 1/2 x2 i i=1 − µx 2 .17.. the numerical result) of type ﬂoat..these details will be covered next). Now suppose that we are intersted in computing the range and statistics of rainfall.. Two-dimensional grid of masses . Write a Java program to compute and print the range.. } stdDeviation { float rainfall [] ) { ..22 Figure 7. Since at this point we are not creating objects (.. 7. and standard deviation of daily rainfall for the one month period..Chapter 7 199 stores the daily rainfall in a rain forest recorded over a one month period. } Each method should return a basic data type (i. mean.e.17 shows a two-dimensional grid of masses..31) where N is the total number of data points...e.

0.34.1.0.4.32) respectively.32 and 11.0. }.200 Engineering Software Development in Java If the total number of point masses is denoted by N.34) .0 5. %. 5 6 7 8 = = = = 1*3 2*3 2*3 2*3 + + + + 2 0 1 3 --> --> --> --> 5%3 6%3 7%3 8%3 evaluates evaluates evaluates evaluates to to to to 2 0 1 2 and so forth. computes the remainder that occurs after an integer m has been divided by a second integer n. }.0. then the mass moments of inertia about the x.y) coordinates and masses are stored in two arrays.3. j) = (i − 1)2 + (j − 1)2 ) %7.0 2. 8. 7.0 }.0.0 4. then it will also be divisible by 49. 3.0 3.23 (Brain Teaser): The modulo operator. Now let A be a (7 × 7) matrix whose elements are given by A(i.and y-axes are given by: N N 2 y i · mi Ixx = i=1 and Iyy = i=1 x2 · m i i (7. For example. Things to do: 1.2. double daaCoord[][] = { { { { { { { { { 1. 3. and (2) output the results of each evaluation. }. } }.0.33) Now suppose that the (x. }. 1.1. double daMass[] = { 1. 2. Extend your Java program to ﬁnd combinations of p and q where p2 + q 2 will be divisible by 7. Now let p and q be integers that cover the interval 0 through 100. 3. Your program should: (1) use simple looping construct to systematically walk along the array elements. 6. 3.0.0. 7. }. m m m m = = = = 5. Prove that if p2 + q 2 is divisible by 7.0 }.2.0 1. 1.0. (7. n n n n = = = = 3.1.0 5.0. 2. 1.0 3. 1.0. 2.0.0.0. 2. Write a Java program to evaluate equations 7. Write a short Java program to evaluate and print equation 7. The polar moment of inertia is given by N 2 x2 + yi · mi i Irr = Ixx + Iyy = i=1 (7. }.3.3.0. It is important to notice that m%n will always return an integer between 0 and (n-1).

0.0.0 2.lang.34. The minimum and maximum polygon coordinates in both the x and y directions.Chapter 7 201 Hint. In Part 3. The last part of this problem is not as difﬁcult as it looks.min() methods in java. y 5 4 3 2 1 1111111111111 0000000000000 111 000 11 00 1 0 111 000 11 00 1 0 111 000 111 000 111 000 111 000 111 000 111 000 111 000 111 000 111 000 11111111 00000000 111 000 11 00 11 00 111 000 11 00 11 00 111 000 111 000 111 000 1 0 111 1 0 00 1 0 1 0 1 0 1 0 1 0 1 0 11 00 1 0 11 00 1 0 1 2 3 4 5 6 7 x Figure 7. 3. . 3. 7.24 Figure 7. }.0. }.0. }.0 5.0. 6. Note. 2.0 1.0 3. 7. 1. 3. Seven-sided irregular polygon Suppose that the x and y vertex coordinates are stored as two columns of information in the array float faaPolygon [ 7 ][ 2 ] = { { { { { { { { 1. Write p as 7 ∗ p1 + r1 and q as 7 ∗ q1 + r2 and then an expression for p2 + q 2 . }. The minimum and maximum distance of the polygon vertices from the coordinate system origin.max() and Math.0 3.0 }. use the Math. For Parts 1 and 2. The result follows directly from the expression and the matrix element values in equation 7.0. }. } }.0.18. 1.0 5. 4. Write a Java program that will compute and print 1.Math. use the fact that the vertices have been speciﬁed in a clockwise manner.18 is a schematic of an irregular polygon having seven sides. The perimeter and area of the polygon.

Show that the depth of the sphere ﬂoating in water is given by solutions to f (x. Write a Java program that will compute the depth to which a sphere will sink as a function of its radius for ρ = 0 to ρ = 1 in increments of 0. ρ) is always less than zero. Only one solution to Equation 7.35 lies within the interval f ([0. 2].x2 + 4. (7.ρ = 0 where x = d/r is a dimensionless quantity. ρ) is always greater than zero and f (2. 1. 2. ρ) and so standard root ﬁnding techniques such as bisection and Newton Raphson will work. This is not as hard as it might seem since f (0. Sphere ﬂoating in water. r d Figure 7.25 Figure 7. ρ) = x3 − 3.202 Engineering Software Development in Java 7.1. The volume of the spherical segment displacing water is 1/3π(3rd2 − d3 ). Hint.19 shows a sphere of radius r and density ρ ﬂoating in water. The weight of the sphere will be 4/3ρπr 3 . Perhaps the most straightforward way of solving this problem is to write a numerical procedure that computes the root of the cubic equation.35) .19.

- sobrino0102
- Olympiad Combinatorics
- xpp_summary commands
- 4
- Looping
- ProblemSet-03_09_2011
- 2013 TCS New Pattern Placement Paper 2013
- (eBook) - Linux-Java-Decompiler-HOWTO.pdf
- Arithmetic Series
- Collections (Java 2 Platform SE v1.4
- Calculus II
- Operating Room Allocation using MILP
- Database Access via Sqlconnection-command-datareader and Adapter
- Math - Series & Sequences
- 3 Partition Problem
- Acknowledgement
- Substructure for Product Set in the Heisenberg Groups
- IMC LQ[2008]Solutions
- 5ACh14(Arithmetic and Geometric Sequences and their Summation)
- Shape 3 4
- Collections Print
- Summation Metering Secure
- Practical Artificial Intelligence Programming In Java - allbooksfree.tk
- Total Gadha Progression
- Java Tutorial
- Labsheet3
- Athena Driver Installation_V4
- compiler Expressions Sol
- EIE3320 Tut01 JavaSyntax
- Binary Search
- 2012-Homework01

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd