|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?快速注册
×
java私塾初级教程--第三章课后作业答案
java私塾跟我学系列-java篇 初级部分课后题答案,参考学习。。。。
- package cn.javass.c3;
- //1:写一个MyPoint类完全封装类,其中含有私有的int类型的x和y属性,分别用公有的getX、getY和setX、setY
- //方法访问,定义一个toString方法用来显示这个对象的x、y的值。如显示(1,2),最后用main方法测试。
- //2:在MyPoint类中增加equals()、toString()方法,根据命令行参数个数测试:若不传参数,则显示(0,0);
- //若传一个参数,则打印(此参数值,0);若传两个参数,则打印(第一个参数值,第二个参数值)。
- public class MyPoint {
- private int x;
- private int y;
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public void setX(int x) {
- this.x = x;
- }
- public void setY(int y) {
- this.y = y;
- }
- public String toString() {
- return "(" + x + "," + y + ")";
- }
- public boolean equals(Object obj) {
- return true;
- }
- public String toString(String[] args) {
- if (args.length == 0) {
- x = 0;
- y = 0;
- }
- if (args.length == 1) {
- x = Integer.parseInt(args[0]);
- y = 0;
- }
- if (args.length == 2) {
- x = Integer.parseInt(args[0]);
- y = Integer.parseInt(args[1]);
- }
- return "(" + x + "," + y + ")";
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- MyPoint mp = new MyPoint();
- mp.setX(1);
- mp.setY(2);
- System.out.println(mp.getX());
- System.out.println(mp.getY());
- System.out.println(mp.toString());
- System.out.println(mp.toString(args));
- }
- }
复制代码
- package cn.javass.c3;
- //3:有一个序列,首两项为0,1,以后各项值为前两项值之和。写一个方法来实现求这个序列的和
- public class OrderSum {
- /**
- * @param args
- */
- //方法一:使用for循环求序列的前n项和
- public int orderSum1(int a1,int a2,int n){
- int s=a1;
- int temp;
- for (int i=1;i<n;i++){
- s=s+a2;
- temp=a2;
- a2=a1+a2;
- a1=temp;
- }
- return s;
- }
- //方法二:使用递归求序列的前n项和
- public int orderSum2(int a1,int a2,int n){
- if (n==1){
- return a1;
- }else{
- return a1+orderSum2(a2,a1+a2,n-1);
- }
- }
- public static void main(String[] args) {
-
- OrderSum o=new OrderSum();
- System.out.println("s="+o.orderSum1(0, 1, 6));
- System.out.println("s="+o.orderSum2(0,1,6));
- }
复制代码- package cn.javass.c3;
- //4:请编写一个方法实现如下功能:将1至7的数字转换为星期日到星期六的字符串。
- public class T4 {
- public String numberToString(int n) {
- String s = "";
- switch (n) {
- case 1:
- s = "Sunday";
- break;
- case 2:
- s = "Monday";
- break;
- case 3:
- s = "Tuesday";
- break;
- case 4:
- s = "Wednesday";
- break;
- case 5:
- s = "Thursday";
- break;
- case 6:
- s = "Friday";
- break;
- case 7:
- s = "Saturday";
- }
- return s;
- }
- public static void main(String[] args) {
- T4 t = new T4();
- int n = 4;
- String s = t.numberToString(n);
- System.out.println(n + ":" + s);
- }
- }
复制代码
- package cn.javass.c3;
- //5:请编写一个方法实现如下功能:有任意三个整数a,b,c,请输出其中最大的
- public class T5 {
-
- public int getMax(int a, int b, int c) {
- int max = a > b a : b;
- max = max > c max : c;
- return max;
- }
- public static void main(String[] args) {
- T5 t = new T5();
- int max = t.getMax(7, 12, 8);
- System.out.println("The max is " + max);
- }
- }
复制代码
- package cn.javass.c3;
- //6:请编写一个方法实现如下功能:有任意三个整数a,b,c,请按从小到大的顺序输出
- public class T6 {
- public void printOrder(int a, int b, int c) {
- int temp;
- if (a > b) {
- temp = a;
- a = b;
- b = temp;
- }
- if (a > c) {
- temp = a;
- a = c;
- c = temp;
- }
- if (b > c) {
- temp = b;
- b = c;
- c = temp;
- }
- System.out.println("====after Order===");
- System.out.println(a + "," + b + "," + c);
- public static void main(String[] args) {
- int a = 78;
- int b = 34;
- int c = 234;
- System.out.println("====before Order===");
- System.out.println(a + "," + b + "," + c);
- T6 t = new T6();
- t.printOrder(a, b, c);
- }[code]
-
- [code]package cn.javass.c3;
- //7:请编写一个方法实现如下功能:用程序找出每位数的立方和等于该数值本身值的所有3位数。(水仙花数)
- public class T7 {
- public void daffodilNumber() {
- for (int i = 100; i <= 999; i++) {
- int a = i / 100;
- int b = i % 100 / 10;
- int c = i % 10;
- if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) {
- System.out.println(i);
- }
- }
- }
- public static void main(String[] args) {
- T7 t = new T7();
- t.daffodilNumber();
- }
复制代码
- package cn.javass.c3;
- //8:请编写一个方法实现如下功能:计算1加到n(n>=2的整数)的总和。
- public class T8 {
- public int getSum(int n) {
- int sum = 0;
- for (int i = 1; i <= n; i++) {
- sum += i;
- }
- return sum;
- }
- public static void main(String[] args) {
- T8 t = new T8();
- System.out.println("sum=" + t.getSum(100));
- }
- }
复制代码
- package cn.javass.c3;
- //9:请编写一个方法实现如下功能:得到一个整数的绝对值。
- public class T9 {
- public int absolute(int n) {
- return n >= 0 n : -n;
- }
- public static void main(String[] args) {
- T9 t = new T9();
- int n = -90;
- System.out.println(n + "的绝对值是:" + t.absolute(n));
- }
- }
复制代码
|
|