Home Work 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Home Work 1

Q1. Warm-up (You may use compiler and try to understand the reason)
Determine the output:
a. byte b=10;
b=b+10;
System.out.println("The value of b is " + b);
b. char c = 3;
int a = 65;
char d = a;
System.out.println("The value of d is " + d);
c. float f = 1.3;
System.out.println("The value of f is " + f);
d. float f = 6.0/2.0;
System.out.println("The value of f is " + f);
e. byte b;
final int a = 10;
final int x = a;
b = x;
System.out.println("The value of b is " + b);
f. public class precedence{
public static void main(String[] args) {
int i = 0;
i=i++;
i=i++;
i=i++;
i=i++;
System.out.println("The value of i is " + i);
int arr []= new int [5];
int index= 0;
arr [index]= index = 3;
System.out.println("The value of first element is " + arr[0]);
System.out.println("The value of fourth element is " + arr[3]);
}
}
g. System.out.println(1+2+3);
System.out.println(1+2+3);
Q2. The associative law for multiplication states that (ab)c = a(bc). Run this code:
float a=0.25e-12f;
float b=4e12f;

float c=5e35f;
System.out.println((a*b)*c);
System.out.println(a*(b*c));
What happens? Change all the floats into doubles, and try again. What has
changed? Can you create a similar problem with doubles?
Q3. Accumulating Rounding Errors
Write a program which evaluates 2 n/100 for each n = 1, 2, . . . 16. Each value
should be determined in two different ways. First evaluate
(float)numerator/100.0f where numerator = 2 n; this gives a good result. The
second way is very naive: simply add (float)1/(float)100 to itself 2 n times!
Q4. Write a program that will start with a double called sum set to zero. Add 0.1
to sum 1000 times by using a for loop.
Print the sum, and then print a statement of whether or not the sum is equal to
100.
Q5. The Power Problem
The following program makes use of a class BigNo which is used to represent large
integers and contains methods for operating on these integers.
public class Power
{
public static void main(String[] args)
{
BigNo b = power(2,2241);
System.out.printf("%s%n", b);
}
private static BigNo power(int m, int n)
{
BigNo p, s;
p = n%2 !=0 ? new BigNo(m) : new BigNo(1);
s = new BigNo(m);
while (n>1)
{
s = s.multiply(s);
n /= 2;
if (n%2 != 0)
p = p.multiply(s);
}
return p;
}
}
class BigNo
{
private static final int BASE = 10000;
private static final int DIGS = 4;
private int[] value = new int[200];
private BigNo()
{

this(0);
}
public BigNo(int n) // This converts an ordinary int into a BigNo
{
int i;
for (i=0; n>0; n /= BASE)
value[++i] = n%BASE;
value[0] = i;
}
public String toString() // This converts a BigNo into a String
{
.
.
}
public BigNo multiply(BigNo that) // This multiplies one BigNo
{
. // by another
.
.
}
}
The method power takes two parameters m and n and raises m to the power of n
by repeatedly using the method multiply in BigNo objects. As a test case 2 is raised
to the power of 2241 and the result is printed out. Complete the program. The
method power should work for all m, n > 0 provided mn is no more than 800 digits
long.
Q6. There once was a wise servant who saved the life of a princess. The king
promised to pay whatever the servant could dream up. Knowing that the king
loved chess, the servant told the king he would like to have grains of wheat. One
grain on the first square of a chess board. Two grains on the next. Four on the third,
and so on.
There are 64 squares on a chessboard.
Write a program that shows how many grains were on each square and the total
number of grains.
In other words, I want to type java Grains and see
square 1: 1 grain
square 2: 2 grains
square 3: 4 grains
square 4: 8 grains
etc.
To complete this assignment, you will need to find a class in the Java standard
library that you have not used yet. The search for this class is the real meat of this
assignment!
Q7. Write a program that will show different time and date information based on
what "code" you send it. The codes are:

0 - number of milliseconds since January 1, 1970


1 - number of seconds since January 1, 1970
2 - number of days since January 1, 1970
3 - current date and time
In other words, I want to type:
GeekWatch 2
and see something like
days since January 1, 1970: 10727
Be sure to read up on what the Date class can do for you.
Q8. The Date of Easter Problem
A convenient algorithm for determining the date of Easter in a given year was
devised in 1876 and first appeared in Butchers Ecclesiastical Handbook. The
algorithm is valid for all years in the Gregorian calendar. Subject to minor
adaptations, the algorithm is as follows:
1. Let y be the year
2. Set a to y%19
3. Set b to y/100 and c to y%100
4. Set d to b/4 and e to b%4
5. Set f to (b+8)/25
6. Set g to (b-f+1)/3
7. Set h to (19*a+b-d-g+15)%30
8. Set i to c/4 and k to c%4
9. Set l to (32+2*e+2*i-h-k)%7
10. Set m to (a+11*h+22*l)/451
11. Set n to (h+l-7*m+114)/31 and p to (h+l-7*m+114)%31
12. Determine 10*(p+1)+n
Note that all identifiers represent integers.
The value of n gives the month (3 for March and 4 for April) and the value of p+1
gives the day of the month. These two values can be combined as 10*(p+1)+n
when 23 April would be given as 234.
Write a method private static int easter(int y) which, when presented with a
year y, returns the date of Easter in the form shown at step 12.
Incorporate this method into a complete test program. Verify that the method
gives the correct date of Easter for the current year.
Optional extra: consider the length of the Easter cycle. What is the minimum value
of n (where n>0) such that for all y (in the Gregorian calendar) the dates of Easter
in years y and y+n are the same.
Warning: the length of the Easter cycle can be determined by a pencil and paper
analysis of the integer constants given in the algorithm. Verifying the length of the
cycle by program can consume a considerable amount of computer time.
Q9. The two roots of a quadratic equation can be obtained using the some formula:
is called the discriminant of the quadratic equation. If it is positive, the equation
has two real roots. If it is zero, the equation has one root. If it is negative, the
equation has no real roots. Write a program that prompts the user to enter values

for a, b, and c and displays the result based on the discriminant. If the discriminant
is positive, display two roots. If the discriminant is 0, display one root. Otherwise,
display The equation has no real roots.
Q10. Write a program that calculates the monthly payments you would have to
make over a given number of years to pay off a loan at a given interest rate
compounded continuously, taking the number of years t, the principal P, and the
annual interest rate r as command-line arguments. The desired value is given by
the formula Pert. Use Math.exp().

You might also like