1. Engineering
  2. Computer Science
  3. question 1 10 points write a program to print your...

Question: question 1 10 points write a program to print your...

Question details

Question 1 (10 points): Write a program to print your name on console screen.

Note:Your program needs to have a method that takes any arbitrary string as a a parameter and print it.

Question 2 (10 points): Write a program to evaluate the value of the following real valued function.

Note: Your program should read x, y and z from the keyboard. Provide the function f as a method that takes these parameters and returns the value.

Question 3 (10 points): Write a program that inputs a non negative integer, separates the integer into its digits and prints them separated by tabs each. For example, if the user types in 19081, the program should print:


Note: Do not convert your integer to a String and use String class methods to print it. You need to have a method that takes any arbitrary positive integer as an argument. Use a paper and pencil to figure out the algorithm. Think about how to exploit integer division and remainder operator.

Question 5 (20 points): Write a program to determine all pairs of positive integers, (a, b), such that a < b < n and (a2 + b2 + 1)/(ab) is an integer. Here n is an integer and n > 2.

Note: Your program should have a method that takes n as a parameter and print all valid pairs as (a, b). Show the corresponding pairs for
n = 1000 as a test case.

Question 6 (20 points) Write a program to approximate the area under a circle with radius r. Note that you should forget the existence of the well known formula area = πr2.

Method: The equation of a circles with radius r, centered at origin is
x2 + y2 = r2. Divide the area under the top of half (above x axis) in to small rectangles of width of your choice – smaller the better and you should pass this as a parameter to your method– and add these areas of all these rectangles to approximate the area of the upper half of the circle. Multiplying that value by 2 give the approximate area of the circle.

You must test your results with known radius values (Say, if you set your radius to 1. Then you should see the π as the answer for the area).

You don’t need any Calculus knowledge to solve this problem.


Question 7 (20 points) Monte Carlo methods are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. One of the basic examples of getting started with the Monte Carlo algorithm is the estimation of Pi.

Basic Idea: The idea is to simulate random (x, y) points in a 2-D plane with domain as a square of side 1 unit. Imagine a circle inside the same domain with same diameter and inscribed into the square. We then calculate the ratio of number points that lied inside the circle and total number of generated points. Refer to the image below:

We know that area of the square is 1 unit sq while that of circle isπ ∗ ( 1 )2 = π . Now for a very large number of generated points,


Areacircle = Number of points generated inside cicle Areasquare Total number of points generated

i.e. π = 4 × Number of points generated inside cicle Total number of points generated

The beauty of this algorithm is that we don’t need any graphics or simulation to display the generated points. In randomized and simulation algorithms like Monte Carlo, the more the number of iterations, the more accurate the result is. Thus, it is “estimating the value of π” and not “Calculating the value of π”. Implement this algorithm in Java and estimate the value of π.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution