1. (简答题)(Display five messages) Write a program that displays Welcome to Java five times.
public class Demo2 {
public static void main(String[] args) {
for(int i = 0;i<5;i++) {
if (i<5) {
System.out.println("Welcome to Java");
}
}
}
}
2. (简答题)(Summation of a series) Write a program that displays the result of
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9.
public class Demo3 {
public static void main(String[] args) {
System.out.println(1+2+3+4+5+6+7+8+9);
}
}
3. (简答题)(Approximate PI) PI can be computed using the following formula:
Write a program that displays the result of
and
Use 1.0 instead of 1 in your program.
public class Demo4 {
public static void main(String[] args) {
System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11));
System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11+1.0/13));
}
}
4. (简答题)(Algebra: solve 2 * 2 linear equations) You can use Cramer’s rule to solve the following 2 * 2 system of linear equation:
Write a program that solves the following equation and displays the value for x and y:
public class Demo5 {
public static void main(String[] args) {
double x , y , a , b , c , d , e , f;
a = 3.4;
b = 50.2;
c = 2.1;
d = 0.55;
e = 44.5;
f = 5.9;
x = (e*d-b*f)/(a*d-b*c);
y = (a*f-e*c)/(a*d-b*c);
System.out.println("x="+x);
System.out.println("y="+y);
}
}