
正文
java lesson10homework
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1. 输入三个整数x, y, z,请把这三个数由小到大输出。
2. package Homework10;
3. //;类方法
4. public class Sort {
5. void main( int x, int y, int z)
6. //排序
7. { int temp;
8. if (x>y)
9. {temp=x;x=y;y=temp;}
10.
11.
12. if (x>z)
13. {temp=x;x=z;z=temp;}
14.
15.
16. if (y>z)
17. {temp=y;y=z;z=temp;}
18.
19. String xyz;
20. System. out .println("这三个数的顺序是:"+x+" "+y+" "+z);
21.
22. }
23.
24. }
25. package Homework10;
26. //主方法
27. import java.util.Scanner;
28.
29. public class Main1 {
30. public static void main(String[] args)
31. {
32. Scanner scanner= new Scanner(System. in );
33. System. out .println("请输入三个数据");
34. int a=scanner.nextInt();
35. int b=scanner.nextInt();
36. int c=scanner.nextInt();
37.
38. Sort sort= new Sort();
39. sort.main(a,b,c);
40.
41.
42.
43. }
44. }
2. 接口回调的定义是什么?阐述自己对接口回调的理解,用文字描述。
接口回调是指可以把实现某一接口的类创建的对象赋给该接口声明的接口变量中。那么该接口变量就可以调用被类实现的接口中的方法。
(1)如果类实现了接口,那么下列说法正确的有(ABCD )
A.接口中的方法都是公有的。B.接口中的方法都是抽象的。
C.实现类必须实现接口的全部方法。D.接口回调对象不能调用子类中新增加的方法。
(2)下列对抽象类的描述正确的有(ABCD )
A.抽象类可以实现接口,但是必须实现接口中声明的所有方法。
B.如果一个类继承了抽象类,那么必须实现抽象类中声明的所有抽象方法。
C.如果一个类中包含抽象方法,则这个类必然是抽象类。
D.抽象类不能被实例化。
3. 题目:创建一个接口【交通工具】,声明一些必要的方法,再创建一些具体的交通工具类(例如:汽车、火车、飞机、轮船、自行车等)实现【交通工具】接口,并且在各个类中实现交通工具接口声明的方法,通过Scanner用户输入的信息,体现多态的性质。
package Homework10_1;
//抽象类 :交通工具
public abstract class Transpotation {
//抽象方法:不能有方法体
abstract void run();
}
package Homework10_1;
//继承抽象类:自行车
public class Bicycle extends Transpotation{
String name="自行车";
//必须对父类中的抽象方法进行重写
void run()
{
System. out .println(name+"在地上走");
}
}
package Homework10_1;
//继承抽象类 :飞机
public class Plane {
String name="飞机";
void run()
{
System. out .println(name+"在天上飞");
}
}
package Homework10_1;
//继承抽象类:轮船
public class Steam {
String name="轮船";
void run()
{
System. out .println(name+"在水里游");
}
}
package Homework10_1;
//测试
public class Test {
public static void main(String[] args)
{Bicycle bicycle= new Bicycle();
Plane plane= new Plane();
Steam steam= new Steam();
bicycle.run();
plane.run();
steam.run();
}
}
4. 编程输出斐波那契数列的前30项 1、1、2、3、5、8、13、21 ……
package Homework10_2;
public class FeiBoNaQi {
void main()
{ int a=1,b=1,c;
System. out .print(a+" "+b+" ");
for ( int i=0;i<30;i++)
{c=a+b;
a=b;
b=c;
System. out .print(c+" ");
}
}
}
package Homework10_2;
public class Test {
public static void main(String[] args)
{FeiBoNaQi fbnq= new FeiBoNaQi();
fbnq.main();
}
}
5. 编程输出如下图形
**
** **
** **
** **
** **
** **
**
package Homework;
public class Diamond {
public void diamond(){
int i,j;
for (i = 1;i <= 7;i++){
if (i<=4){
for (j = 0;j <= 4-i;j++){
System. out .print(" ");
}
for (j = 1; j <= (i)*2 - 1;j++){
if (j == 1 || j == i*2-1)
System. out .print("**");
else
System. out .print(" ");
}
} else if (i > 4){
for (j = 0;j <= i - 4;j++){
System. out .print(" ");
}
for (j = 1; j <= (8-i)*2 - 1;j++){
if (j == 1 || j == (8-i)*2 - 1)
System. out .print("**");
else
System. out .print(" ");
}
}
System. out .println();
}
}
public static void main(String[] args) {
Diamond d = new Diamond();
d.diamond();
}
}
6. 设有4位正整数,其各个位数和的3次方等于这个数本身,求这样的4位数都是多少?
package Homework10_2;
public class Judge {
void main()
{ int a,b,c,d,num,i;
for (i=1000;i<10000;i++)
{num=i;
a=num%10;
num/=10;
b=num%10;
num/=10;
c=num%10;
num/=10;
d=num%10;
if ((a+b+c+d)*(a+b+c+d)*(a+b+c+d)==i)
{System. out .print(i+" ");}
}
}
}
package Homework10_2;
public class Test2 {
public static void main(String[] args)
{Judge jd= new Judge();
jd.main();
}
}
7. 设有4位正整数,其各个位数和的4次方等于这个数本身,求这个4位数=?
package Homework10_2;
public class Judge {
void main()
{ int a,b,c,d,num,i;
for (i=1000;i<10000;i++)
{num=i;
a=num%10;
num/=10;
b=num%10;
num/=10;
c=num%10;
num/=10;
d=num%10;
if ((a+b+c+d)*(a+b+c+d)*(a+b+c+d)*(a+b+c+d)==i)
{System. out .print(i+" ");}
}
}
}
package Homework10_2;
public class Test2 {
public static void main(String[] args)
{Judge jd= new Judge();
jd.main();
}
}






