第八章章节作业

案列:
定义Person类,里面有name、age属性,并提供compareTo比较方法
用于判断是否和另外一个人相等,提供测试类testPerson用于测试
名字和年龄完全一样,就返回true,否则返回false;

 1 public class testPerson{
 2     public static void main(String[]args){
 3              Person p1 = new Person("jack",27);
 4              Person p2 = new Person("marry",26);
 5              System.out.println("p1和p2的比较结果为"+p1.compareTo(p2));
 6     }
 7 }
 8 
 9 /*
10 
11 定义Person类,里面有name、age属性,并提供compareTo比较方法
12 用于判断是否和另外一个人相等,提供测试类testPerson用于测试
13 名字和年龄完全一样,就返回true,否则返回false;
14 */
15 class Person{
16       int age;
17      String name;
18      //构造器
19     public  Person(String name,int age){
20           this.name=name;         
21           this.age=age;         
22     }
23 
24     public boolean compareTo(Person p){
25          if (this.name.equals(p.name)&& this.age==p.age) {
26              return true;
27          }else {
28              return false;
29          }
30          //上面的if语句还可以这样写:
31         // return this.name.equals(p.name)&&this.age ==p.age;
32     }
33 }

案列2:编写类A01,定义方法max,实现求某个double数组的最大值,并返回

 1 public class homeWork01{
 2     public static void main(String[] args){
 3 
 4             A01 p1= new A01();
 5          // p1.max();
 6           double [] arry={15.0,78.5,89.5,65.0,56};
 7           Double res =p1.max(arry);//注意这个Double是包装类
 8           if (res !=null) {
 9                System.out.println("arry数组最大元素="+p1.max(arry));
10           }else {
11               System.out.println("arry数组输出有误.......");
12           }
13          
14     }
15 }
16 
17 //案列2:编写类A01,定义方法max,实现求某个double数组的最大值,并返回
18 
19 class A01{
20     public Double max(double[] arry){//Double为包装类
21 
22         if (arry!=null  && arry.length>0) {//防止出现数组为空或者长度为0时
23             double max=arry[0];
24             int IndexNum=0;
25         
26            for (int i= 1;i<arry.length ;i++ ) {
27               if (max<arry[i]) {
28                  max=arry[i];
29                  IndexNum=i;
30                 }
31             }
32           return max;    
33         }else {
34             return null;
35         }
36     }
37 }

 

案列3:编写类A02,定义方法find,实现查找某个字符串数组中的元素查找,并返回索引

案列3:编写类Book,定义方法updatePritce,实现更改某书的价格;具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则保持不变。

 

 

案列5:

编程创建一个Cale类,在其中定义两个变量表示两个操作数;
定义四个方法实现求和、差、乘、除(要求除数为0的话,要提示)并创建两个对象,
分别测试

 1 public class homework05{
 2     public static void main(String[] args){
 3              Cale can = new Cale(10,15);
 4              // double res = can.sum();
 5              System.out.println("和="+can.sum());
 6              System.out.println("差="+can.minus());
 7              System.out.println("乘="+can.mul());
 8               Double res = can.divison();//注意此时的类型是Double,是包装类:
 9              if (res!=null) {
10                  System.out.println("除="+res);
11              }
12              
13     }
14 }
15 
16 /*
17 编程创建一个Cale类,在其中定义两个变量表示两个操作数;
18 定义四个方法实现求和、差、乘、除(要求除数为0的话,要提示)并创建两个对象,
19 分别测试
20 分析:类名Cale:
21       方法名:四个方法名
22       返回类型:
23       形参:int
24 */
25  class Cale{
26      //定义初始化变量的类型
27      double num1;
28      double num2;
29      //创建一个构造器,给成员变量初的始化赋值
30      public  Cale(double num1,double num2){
31          this.num1 = num1;
32          this.num2 = num2;
33      }
34      //求和
35      public double sum(){
36          return num1+num2;
37      }
38      //求差
39      public double minus(){
40          return num1-num2;
41      }
42      //求乘
43      public double mul(){
44          return num1*num2;
45      } 
46      //求除
47      public Double divison(){
48          if (num2==0) {//除法,分母不能为0;
49              System.out.println("分母num2不能为0");
50              return null;
51          }else {
52              return num1/num2;
53          }
54          
55      }
56  }

 案列6:

定义Music类,里面有音乐名name,音乐实长time属性。
并播放play功能和返回本身属性信息的功能方法

 1 public class homeWork02{
 2     public static void main(String[] args){
 3               Music can = new Music("晴天",350);
 4               can.play();
 5               System.out.println(can.info());
 6 
 7     }
 8 }
 9 
10 /*
11 定义Music类,里面有音乐名name,音乐实长time属性。
12 并播放play功能和返回本身属性信息的功能方法
13 */
14 
15 class Music{
16      String name;
17      int time;
18      public Music(String name,int time){
19          this.name = name;
20          this.time = time;
21      }
22      //播放功能
23      public void play(){
24          System.out.println(name+"音乐正在播放中...... "+"播放时长"+time+"秒");
25      }
26      //属性返回
27      public String info(){
28          return "音乐"+name+"播放时间"+time+"秒";
29      }
30 
31 }

案列7:

题目要求:
(1)定义一个Circle类,包含一个double型的redius属性代表圆的半径,findArea方法返回圆的面积。
(2)定义一个类 passObject类,在类中定义一个方法printAreas(),该方法的定义如下:
pubilc void printArea(Circle C,int times)
(3)在printAreas方法中打印输出1到times之间的每个整数半径值,以及对应的面积,如times为5
(4)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。

 1 public class homework10{
 2     public static void main(String[] args){
 3                 Circle can = new Circle();
 4                 // double s1 = can.findArea();
 5                 // System.out.println(s1);
 6                 Dog six = new Dog();
 7                 six.printArea(can,3);
 8     }
 9 }
10 
11 /*
12  题目要求:
13  (1)定义一个Circle类,包含一个double型的redius属性代表圆的半径,findArea方法返回圆的面积。
14  (2)定义一个类 passObject类,在类中定义一个方法printAreas(),该方法的定义如下:
15  pubilc void printArea(Circle C,int times)
16  (3)在printAreas方法中打印输出1到times之间的每个整数半径值,以及对应的面积,如times为5
17  (4)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。
18 */
19 
20 //创建一个类
21 class Circle{
22     //定义属性类型
23     double radius;
24     public Circle(){
25 
26     }
27     //创建构造器,给属性初始化赋值
28     public Circle(double radius){
29     this.radius =radius;
30     }
31     //创建方法
32     public double findArea(){
33         return Math.PI *radius*radius;
34     }
35     public void setRadius(double radius){
36             this.radius = radius;
37         }
38   }
39 
40 class Dog{
41        public void printArea(Circle can,int times){
42        System.out.println("radius\tarea");
43         for (int i=1;i<=times;i++ ) {
44             can.setRadius(i);
45             System.out.println((double )i+"\t"+can.findArea());
46        }
47        }
48             
49   }

 案列8:

有个人Tom,设计他的成员变量,成员方法,可以电脑猜拳,电脑每次都会随机生成0,1,2
0代表石头,1表示剪刀,2表示布,要求可以显示tom的输赢次数(清单)

 1 import java.util.Random;
 2 import java.util.Scanner;
 3 /*
 4 有个人Tom,设计他的成员变量,成员方法,可以电脑猜拳,电脑每次都会随机生成0,1,2
 5 0代表石头,1表示剪刀,2表示布,要求可以显示tom的输赢次数(清单)
 6 */
 7 public class homework14{
 8     public static void main(String[]args){
 9                Tom t = new Tom();
10                //用来记录输赢的次数
11                int iswinCount = 0;
12 
13                //创建一个二维数组,用来接收局数,tom出拳情况及电脑出拳情况
14                int[][] array = new int [3][3];
15                int j = 0;
16                //创建一个一维数组,用来接收输赢情况
17                String[] arr1 = new String[3];
18 
19               Scanner  myscanner = new Scanner(System.in);
20               for (int i=0;i<3 ;i++ ) {
21                   //获取玩家出的拳
22                   System.out.println("请输入你要出的拳 (0-拳头,1-剪刀,2-布:)");
23                   int num = myscanner.nextInt();
24                   t.setTomGuessNum(num);
25                   int tomGuessNum = t.getTomGuessNum();
26                   array[i][j+1] = tomGuessNum;
27 
28                   //电脑出拳的获取
29                   int comGuess = t.computerNum();
30                   array[i][j+2] = comGuess;
31                   //将玩家出的拳与电脑出的拳做比较
32                   String iswin = t.vscomputer();
33                   arr1[i]= iswin;
34                   array[i][j] = t.count;
35                   //对每一局的情况进行输出
36                   System.out.println("===================================================");
37                   System.out.println("局数\t玩家的出拳\t 电脑的出拳\t 比赛输赢情况");
38                   System.out.println(t.count+"\t"+tomGuessNum+"\t\t"+comGuess+"\t\t"+iswin);
39                   System.out.println("============== =====================================");
40                   System.out.println("\n\n");
41                   //iswinCount = t.winCount(iswin);
42               }
43     }
44 }
45 
46 
47 class Tom{
48     //定义玩家出拳的类型
49      int tomGuessNum;
50     //定义电脑猜拳的类型
51      int comGuessNum;
52     //定义玩家的次数
53      int winCountNum;
54      //比赛的次数
55      int count = 1;//一共比赛3次;
56 
57      public void showInfo(){
58 
59      }
60      //电脑随机生成猜拳的数字方法
61     public int computerNum(){
62          Random r= new Random();
63          comGuessNum = r.nextInt(3);
64          return comGuessNum;
65     }
66     //设置玩家猜拳的数字方法的范围
67     public void setTomGuessNum(int tomGuessNum){
68         if (tomGuessNum>2||tomGuessNum<0) {
69             System.out.println("数字输入异常......请输入0到2之间的整数");
70         }
71         this.tomGuessNum = tomGuessNum;
72     }
73     public int getTomGuessNum(){
74         return tomGuessNum;
75     }
76 
77     //比较猜拳的结果
78     public String vscomputer(){
79         if (tomGuessNum == 0 && comGuessNum==2 ) {
80             return "恭喜,你赢了";
81         }else if (tomGuessNum ==1 && comGuessNum==0) {
82             return"恭喜,你赢了";
83         }else if (tomGuessNum ==1 && comGuessNum==2) {
84             return"恭喜,你赢了";
85         }
86         else if (tomGuessNum == comGuessNum) {
87             return"此局平手";
88         }else {
89             return"抱歉,你输了";
90         }
91     }
92 
93 }

 

posted @ 2022-03-02 16:42  捞月亮的渔夫  阅读(54)  评论(0)    收藏  举报