2.9 Are the following statements correct? If so,show the output.
1
2
3
4
5
6
7
8public class Demo { public static void main(String[] args) { System.out.println("25/4 is " + 25 / 4); System.out.println("25/4.0 is " + 25 / 4.0); System.out.println("3*2/4 is " + 3 * 2 / 4); System.out.println("3.0*2/4 is " + 3.0 * 2 / 4); } }
2.18 Show the following output.
1
2
3
4
5
6
7public class Demo { public static void main(String[] args) { float f = 12.5F; int i = (int) f; System.out.println("f is " + f); System.out.println("i is " + i); }
2.20 Which of the following are correct literals for characters?
1
2
3
4
5
6
7
8
9public class Demo { public static void main(String[] args) { float f = 12.5F; int i = (int) f; System.out.println('1'); System.out.println('u3fFa'); System.out.println('b'); } }
2.21 How do you display characters and "?
1
2
3
4
5
6public class Demo { public static void main(String[] args) { System.out.println("\"); System.out.println("""); } }
2.22 Evaluate the following:
1
2
3
4
5
6
7
8
9public class Demo { public static void main(String[] args) { int i = '1'; int j = '1' + '2'; int k = 'a'; char c = 90; System.out.println("i=" + i + " j=" + j + " k=" + k + " c=" + c); } }
2.23 Can the following conversions involving casting be allowed? If so, find the converted result.
1
2
3
4
5
6
7
8
9public class Demo { public static void main(String[] args) { char c = 'A'; float f = 1000.34f; int i = (int) f; i = (int) c; System.out.println("i=" + i + " c=" + c + " f=" + f); } }
1
2
3
4
5
6
7public class Demo { public static void main(String[] args) { double d = 1000.34; int i = (int) d; System.out.println("i=" + i + " d=" + d); } }
1
2
3
4
5
6
7
8public class Demo { public static void main(String[] args) { int i = 97; char c = (char) i; System.out.println("i=" + i + " c=" + c); } }
2.24 Show the output of the following program:
1
2
3
4
5
6
7
8
9public class Demo { public static void main(String[] args) { char x = 'a'; char y = 'c'; System.out.println(++x); System.out.println(y++); System.out.println(x - y); } }
2.25 Show the output of the following statements:
1
2
3
4
5
6
7
8
9public class Demo { public static void main(String[] args) { System.out.println("1" + 1); System.out.println('1' + 1); System.out.println("1" + 1 + 1); System.out.println("1" + (1 + 1)); System.out.println('1' + 1 + 1); } }
2.26 Evaluate the following expressions:
1
2
3
4
5
6
7
8public class Demo { public static void main(String[] args) { System.out.println(1 + "welcome" + 1 + 1); System.out.println(1 + "welcome" + (1 + 1)); System.out.println(1 + "welcome" + ('u0001' + 1)); System.out.println(1 + "welcome" + 'a' + 1); } }
2.6** Write a program that reads an integer between 0 and 100 and adds all the digits in the integer.For example,if an integer is 932,the sum of all its digits is 14.
1
2
3
4
5
6
7
8
9
10
11
12
13import javax.swing.JOptionPane; public class Demo { public static void main(String[] args) { int sum=0,number; String input=JOptionPane.showInputDialog("Please input a number:"); number=Integer.parseInt(input); while(number>0){ sum=sum+(number%10); number/=10; } JOptionPane.showMessageDialog(null, "The digits is "+sum); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import java.util.Scanner; public class Demo { public static void main(String[] args) { int sum=0,number; //String input=JOptionPane.showInputDialog("Please input a number:"); Scanner input=new Scanner(System.in); number=input.nextInt(); while(number>0){ sum=sum+(number%10); number/=10; } //JOptionPane.showMessageDialog(null, "The digits is "+sum); System.out.println("The digits is "+sum); } }
2.24 Give an airplane's acceleration a and take-off speed v,you can compute the minimum runway length needed for an airplane to take off using the following formula:
length=v*v/2a
write a program that prompts the user to enter v in m/s and the acceleration a in m/s*s,and display the minimum runway length.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import javax.swing.JOptionPane; public class Demo { public static void main(String[] args) { String v, a; double v0, a0, length; v = JOptionPane.showInputDialog("Please input the speed v:"); a = JOptionPane.showInputDialog("Please input the acceleration a:"); v0 = Double.parseDouble(v); a0 = Double.parseDouble(a); length = (v0 * v0) / (2 * a0); java.text.DecimalFormat df = new java.text.DecimalFormat("#.###"); JOptionPane.showMessageDialog(null, "The minimum length is " + df.format(length) + "m.", "Result", JOptionPane.INFORMATION_MESSAGE); } }
1
2
3
4
5
6
7
8
9
10
11
12import java.util.Scanner; public class welcome { public static void main(String[] args) { double v0, a0, length; Scanner input = new Scanner(System.in); v0 = input.nextDouble(); a0 = input.nextDouble(); length = (v0 * v0) / (2 * a0); java.text.DecimalFormat df = new java.text.DecimalFormat("#.###"); System.out.println("The minimum length is " + df.format(length) + "m."); } }
最后
以上就是复杂大米最近收集整理的关于第二周 - Elementary Programming的全部内容,更多相关第二周内容请搜索靠谱客的其他文章。
发表评论 取消回复