JAVA语言程序设计课后习题----第八单元解析(仅供参考)

1  本题主要考的是方法的克隆,与c++里面的拷贝有点相似,具体看书本p147

 1 import java.util.Objects;
 2 
 3 public class Square implements Cloneable{
 4     private int length;
 5 
 6     public Square(int length) {
 7         this.length = length;
 8     }
 9 
10     @Override
11     public String toString() {
12         return "Square[" +
13                 "length=" + length +
14                 ']';
15     }
16 
17     @Override
18     public boolean equals(Object o) {
19         if (this == o) return true;
20         if (o == null || getClass() != o.getClass()) return false;
21         Square square = (Square) o;
22         return length == square.length;
23     }
24 
25     @Override
26     public int hashCode() {
27         return Objects.hash(length);
28     }
29 
30     public static void main(String[] args) throws CloneNotSupportedException{
31         Square s1=new Square(100);
32         Square  s2=(Square)s1.clone();
33         System.out.println(s1==s2);
34         System.out.println(s1.equals(s2));
35         System.out.println(s2.toString());
36 
37     }
38 }

2  本题水题,主要就是考数组,把产生的需求数存在数组里

 1 public class Suiji {
 2     public static int a[]=new int[6];
 3     public static void main(String[] args) {
 4         Suiji s=new Suiji();
 5         System.out.println("随机产生1000个1-6的数");
 6         s.stc(6);
 7 
 8         for (int i = 1; i <=6 ; i++) {
 9             System.out.println(i+"的概率是"+(a[i-1]*1.0/1000)*100+"%");
10         }
11         System.out.println("****************************");
12         System.out.println("随机产生1000个1-1000的数");
13         for (int i = 0; i < a.length; i++) {
14             a[i]=0;
15         }
16         s.stc(1000);
17         for (int i = 1; i <=6 ; i++) {
18             System.out.println(i+"的概率是"+(a[i-1]*1.0/1000)*100+"%");
19         }
20 
21     }
22     public void stc(int num){
23         for (int i = 0; i <1000 ; i++) {
24             switch ((int)(Math.random()*num+1)){
25                 case 1:a[0]++;
26                     break;
27                 case 2:a[1]++;
28                     break;
29                 case 3:a[2]++;
30                     break;
31                 case 4:a[3]++;
32                     break;
33                 case 5:a[4]++;
34                     break;
35                 case 6:a[5]++;
36                     break;
37             }
38         }
39     }
40 }

3  本题注意sinx x是弧度即可

 1 public class Area {
 2     public static void main(String[] args) {
 3         double a=4.0,b=5.0;
 4 
 5         double t=(Math.sin(30*(Math.PI/180)));
 6        // System.out.println("三角形的面积为:"+(1*1.0/2)*a*b*t);
 7         System.out.println(((1*1.0/2)*a*b*t));
 8     }
 9 
10 }

4  本题只要记得如何求最大值、最小值的封装方法即可(MAX_VALUE、MIN_VALUE

 1 public class Baozhuang {
 2     //    Integer integer;
 3     public static void main(String[] args) {
 4         Integer integer = null;
 5         System.out.println("Integer的最大值"+integer.MAX_VALUE+"\t"+"Integer的最小值"+integer.MIN_VALUE);
 6         Long l=null;
 7         System.out.println("Long的最大值"+l.MAX_VALUE+"\t"+"Long的最小值"+l.MIN_VALUE);
 8         Float f=null;
 9         System.out.println("Float的最大值"+f.MAX_VALUE+"\t"+"Float的最小值"+f.MIN_VALUE);
10         Double d=null;
11         System.out.println("Double的最大值"+d.MAX_VALUE+"\t"+"Double的最小值"+d.MIN_VALUE);
12         Character character=null;
13         System.out.println(" Character的最大值"+character.MAX_VALUE+"\t"+" Character的最小值"+character.MIN_VALUE);
14         Byte b=null;
15         System.out.println(" Byte的最大值"+b.MAX_VALUE+"\t"+" Byte的最小值"+b.MIN_VALUE);
16         Short s=null;
17         System.out.println("Short的最大值"+s.MAX_VALUE+"\t"+" Short的最小值"+s.MIN_VALUE);
18 
19     }
20 }

5  本题自行百度查看

6  本题水题,plusDays就是给当前对象增加几天

1 import java.text.ParseException;
2 import java.time.LocalDate;
3 public class ch06 {
4     public static void main(String[] args) throws ParseException {
5         LocalDate pday=LocalDate.of(2017,1,1).plusDays(256);
6         System.out.println(pday);
7     }
8 }

7  本题主要就是知道如何求天数,对什么值求模

 1 import java.text.ParseException;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Date;
 4 import java.util.Scanner;
 5 
 6 public class ch07 {
 7     public static void main(String[] args) throws ParseException {
 8         Scanner input =new Scanner(System.in);
 9         String dbtime1 = input.next();  //第二个日期
10         String dbtime2 = input.next();  //第一个日期
11 //算两个日期间隔多少天
12         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
13         Date date1 = format.parse(dbtime1);
14         Date date2 = format.parse(dbtime2);
15 
16         int a = (int) ((date1.getTime() - date2.getTime()) / (1000*3600*24));
17         System.out.println(a);
18     }
19 }

8  本题具体自行查看书本p159

 1 import java.time.LocalDate;
 2 import java.time.format.TextStyle;
 3 import java.util.Locale;
 4 import java.util.Scanner;
 5 
 6 public class PromtCalendar {
 7     public static void main(String[] args) {
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("输入一个年份(如2018):");
10         int year = input.nextInt();
11 
12         for (int j = 1; j <=12; j++) {
13             //        返回本月第一天
14             LocalDate dates = LocalDate.of(year,j, 1);
15             String monthName = dates.getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault());
16 //        返回当月的天数
17             int dayOFmonth = dates.lengthOfMonth();
18             System.out.println(year + "年     " + monthName);
19             System.out.println("-------------------------------");
20             System.out.printf("%4s%3s%3s%3s%3s%3s%3s%n", "一", "二", "三", "四", "五", "六", "日");
21             int dayOfWeek = dates.getDayOfWeek().getValue();
22             for (int i = 2; i <= dayOfWeek; i++) {
23                 System.out.printf("%4s", " ");
24             }
25             for (int i = 1; i <= dayOFmonth; i++) {
26                 System.out.printf("%4d", i);
27                 if ((dayOfWeek + i - 1) % 7 == 0)
28                     System.out.println();
29             }
30             System.out.println();
31         }
32     }
33 }

9  本题主要考的数组里面值的存入问题,也可以用if语句逐一进行判断,注意要月份和天数都要满足

 1 import java.util.Scanner;
 2 
 3 public class xingzuo {
 4    /* public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         System.out.println("输入一个年份(如2018):");
 7 int year=input.nextInt();
 8         int month = input.nextInt();
 9 //       输入天数
10         int day = input.nextInt();
11         LocalDate dates3 = LocalDate.of(year, month, day).plusDays(30);
12         //dates3.plusDays(30);
13         System.out.println(dates3);
14 
15         LocalDate[] desk = {LocalDate.of( 0,3, 20), LocalDate.of(0, 4, 20),
16                 LocalDate.of(0, 4, 19), LocalDate.of(0, 4, 21),
17                 LocalDate.of(0, 5, 20), LocalDate.of(0, 6, 22),
18                 LocalDate.of(0, 6, 21), LocalDate.of(0, 7, 23),
19                 LocalDate.of(0, 7, 22), LocalDate.of(0, 8, 23),
20                 LocalDate.of(0, 8, 22), LocalDate.of(0, 9, 24),
21                 LocalDate.of(0, 9, 22), LocalDate.of(0, 10, 24),
22                 LocalDate.of(0, 10, 23), LocalDate.of(0, 11, 23),
23                 LocalDate.of(0, 11, 22), LocalDate.of(0, 12, 22),
24                 LocalDate.of(0, 12, 21), LocalDate.of(0, 1, 20),
25                 LocalDate.of(0, 1, 19), LocalDate.of(0, 2, 19),
26                 LocalDate.of(0, 2, 18), LocalDate.of(0, 3, 21)};
27 
28 
29         if (dates3.isAfter(desk[0]) && dates3.isBefore(desk[1])) {
30 
31             System.out.println("白羊座" );
32         }
33         else if (dates3.isAfter(desk[2]) && dates3.isBefore(desk[3])){
34             System.out.println("金牛座");
35         }
36         else if (dates3.isAfter(desk[4]) && dates3.isBefore(desk[5])){
37             System.out.println("双子");
38         }
39         else if (dates3.isAfter(desk[6]) && dates3.isBefore(desk[7])){
40             System.out.println("巨蟹");
41         }
42         else if (dates3.isAfter(desk[8]) && dates3.isBefore(desk[9])){
43             System.out.println("狮子");
44         }
45         else if (dates3.isAfter(desk[10]) && dates3.isBefore(desk[11])){
46             System.out.println("处女");
47         }
48         else if (dates3.isAfter(desk[12]) && dates3.isBefore(desk[13])){
49             System.out.println("天平");
50         }
51         else if (dates3.isAfter(desk[14]) && dates3.isBefore(desk[15])){
52             System.out.println("天蝎");
53         }
54         else if (dates3.isAfter(desk[16]) && dates3.isBefore(desk[17])){
55             System.out.println("射手");
56         }
57         else if (dates3.isAfter(desk[18]) && dates3.isBefore(desk[19])){
58             System.out.println("摩羯");
59         }
60         else if (dates3.isAfter(desk[20]) && dates3.isBefore(desk[21])){
61             System.out.println("水平");
62         }
63         else if (dates3.isAfter(desk[22]) && dates3.isBefore(desk[23])){
64             System.out.println("双鱼");
65         }
66     }*/
67 
68     public static void main(String[] args) {
69 
70         Scanner input = new Scanner(System.in);
71         System.out.println("请输入出生日期如(5 20)");
72         System.out.println(
73                 getConstellation(input.nextInt(),input.nextInt())
74         );
75     }
76     private final static int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 };
77     private final static String[] constellationArr = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座" };
78     public static String getConstellation(int month, int day) {
79         return day < dayArr[month - 1] ? constellationArr[month - 1] : constellationArr[month];
80     }
81 }

猜你喜欢

转载自www.cnblogs.com/PerZhu/p/10926071.html