Write a program to implement a method that can do additions of 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The additions of
2 fractions should be equal to a fraction.
Use 1/2+1/3 as the test.
Hints:
Fraction f1, f2;
f1.add(f2);
星期五, 11月 21, 2008
Class Definition 3
Do Display 4.7 (3rd, 2nd ed.) or 4.5 (1st ed.). Then use Display 4.8 to call 4.7.
Question
In Display 4.7, if the method setDate has the parameter as setDate(int month, int day, int year), what kind of changes should be made in its body of codes?
Question
In Display 4.7, if the method setDate has the parameter as setDate(int month, int day, int year), what kind of changes should be made in its body of codes?
星期三, 11月 19, 2008
星期一, 11月 17, 2008
星期五, 10月 31, 2008
Answer to Quiz
1. Write a Java program to calculate the trianular function as follows:
Cos(x)=1 - 2!/x 2 + 4!/x 4- 6!/x 6...
double power=1, fact=1, sum=0;
int k=-1;
for(int i=1; i<10; i++)
{
power=power*x;
fact=fact*i;
if(i%2==1)
{ k=k*(-1);
sum+=k*power/fact;
}
}
6. Write a complete Java program that uses a for loop to compute the sum of odd numbers between 1 and 25.
int evenSum=oddSum=0;
for(i=1; i<=25; i++)
{
if(i%2!=0)
oddSum+=i;
}
Cos(x)=1 - 2!/x 2 + 4!/x 4- 6!/x 6...
double power=1, fact=1, sum=0;
int k=-1;
for(int i=1; i<10; i++)
{
power=power*x;
fact=fact*i;
if(i%2==1)
{ k=k*(-1);
sum+=k*power/fact;
}
}
6. Write a complete Java program that uses a for loop to compute the sum of odd numbers between 1 and 25.
int evenSum=oddSum=0;
for(i=1; i<=25; i++)
{
if(i%2!=0)
oddSum+=i;
}
lab class definition 2
Study Display 4.4 (2nd ed. and 3rd ed.) or Display 4.2 & Display 4.3 (1st ed.) and then
1. Comment out date.setDate(6, 17, year); by // date.setDate(6, 17, year);
2. At the next line below, add date.readInput();
3. Run the program again. Fix any problems you may encouter along the way.
4. At the last line of your program, add System.out.println(date.month);
and see what happens. Why?
1. Comment out date.setDate(6, 17, year); by // date.setDate(6, 17, year);
2. At the next line below, add date.readInput();
3. Run the program again. Fix any problems you may encouter along the way.
4. At the last line of your program, add System.out.println(date.month);
and see what happens. Why?
訂閱:
文章 (Atom)