Home Work 1
Home Work 1
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:
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().