第二次过程性考核

码云链接
7-1 学生类-构造函数

定义一个有关学生的Student类,内含类成员变量: String name、String sex、int age,所有的变量必须为私有(private)。

1.编写有参构造函数:

能对name,sex,age赋值。

2.覆盖toString函数:

按照格式:类名 [name=, sex=, age=]输出。使用idea自动生成,然后在修改成该输出格式

3.main方法中

输入1行name age sex , 调用上面的有参构造函数新建对象。

输入样例:

tom 15 male

输出样例:

Student [name='tom', sex='male', age=15]

代码解析:

 1 import java.util.Scanner;
 2 
 3 class Student {
 4     private String name;
 5     private String sex;
 6     private int age;
 7 
 8     public Student() {       //定义Student的构造方法
 9         this.name = "tom";
10         this.sex = "male";
11         this.age = 15;
12     }
13 
14     public void toString(String n, int a, String s) {   //定义toString方法,按格式输出
15         this.name = n;
16         this.sex = s;
17         this.age = a;
18         System.out.println("Student [name='" + this.name + "', sex='" + this.sex + "', age=" + this.age + "]");
19     }
20 
21 }
22 
23 public class StudentClass {
24     public static void main(String[] args) {
25         // TODO Auto-generated method stub
26         //@SuppressWarnings("resource")
27         /*
28          * 程序在前面声明了一个Scanner变量reader,那么系统就为reader开辟了一个空间, 而当程序结束后,系统并不会自动收回reader所占用的空间,
29          * 造成资源浪费。于是提示你reader永远不会close。这样,在不需要使用reader的时候,调用reader.close()函数,
30          * 手动收回空间就好了。可把reader.close()放在main函数最后一句就可以解决了
31          */
32         
33         /*
34          * java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。可以标注在类、字段、方法、参数、构造方法,
35          * 以及局部变量上。 作用:告诉编译器忽略指定的警告,不用在编译完成后出现警告信息。
36          */
37         Scanner reader = new Scanner(System.in);
38         String n = reader.next();
39         int a = reader.nextInt();
40         String s = reader.next();
41         Student ww = new Student();
42         ww.toString(n, a, s);
43         reader.close();
44 
45     }
46 }

 

7-2 定义类

请补充以下代码,完成输出要求。

 1 import java.util.Scanner;
 2 public class Main {
 3     public static void main(String[] args) {
 4                 Scanner in = new Scanner(System.in);
 5                 int a,b,c,d,e;
 6                 a = in.nextInt();
 7                 b = in.nextInt();
 8                 c = in.nextInt();
 9                 d = in.nextInt();
10                 e = in.nextInt();
11                 RR rr = new RR();
12                 double dd = rr.fun(a,b,c,d,e);
13                 System.out.printf("%.2f",dd);
14     }
15 }
16 class RR{
17 
18 
19 }

输入格式:

在一行中给出5个不超过1000的正整数。

输出格式:

输出5个整数的平均值,保留小数点后两位

输入样式:

1 2 3 4 5

输出样式:

3.00

代码解析:

 

 1 import java.util.Scanner;
 2 
 3 public class ClassOfDefined {
 4     public static void main(String[] args) {
 5         // @SuppressWarnings("resource")
 6 
 7         /*
 8          * 程序在前面声明了一个Scanner变量w,那么系统就为w开辟了一个空间, 而当程序结束后,系统并不会自动收回w所占用的空间,
 9          * 造成资源浪费。于是提示你w永远不会close。这样,在不需要使用w的时候,调用w.close()函数,
10          * 手动收回空间就好了。可把w.close()放在main函数最后一句就可以解决了
11          */
12         /*
13          * java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。可以标注在类、字段、方法、参数、构造方法,
14          * 以及局部变量上。 作用:告诉编译器忽略指定的警告,不用在编译完成后出现警告信息。
15          */
16         Scanner w = new Scanner(System.in);
17         int a, b, c, d, e;
18         a = w.nextInt();
19         b = w.nextInt();
20         c = w.nextInt();
21         d = w.nextInt();
22         e = w.nextInt();
23         RR rr = new RR();
24         double dd = rr.fun(a, b, c, d, e);
25         System.out.printf("%.2f", dd);
26         w.close();
27     }
28 }
29 
30 class RR {  //定义RR类,求键盘输入五个数的平均值
31     double z;
32 
33     public double fun(int a, int b, int c, int d, int e) {
34         z = (a + b + c + d + e) / 5;
35         return z;
36     }
37 }

 

 

 

7-3 横平竖直

程序填空题。根据题目要求完善下面的代码。请提交完整代码。 一个木块如果高度比宽度大,我们说它是竖着放的,否则我们说它是平放的。 读入一个木块的高度和宽度。如果它是平放的,则输出A,否则输出B。

 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String[] args){
 4         Scanner in = new Scanner(System.in);
 5         int height, width;
 6         char status;
 7         height = in.nextInt();
 8         width = in.nextInt();
 9         Board board = new Board(height, width);
10         status = board.getStatus();
11         System.out.print(status);
12     }
13 }
14 class Board{
15    int height, width;
16    public Board(int height, int width){
17        this.height = height;
18        this.width = width;
19    }
20    public char getStatus(){
21        if(height<=width){
22           return status(1);
23        }else{
24          return status(1.0);
25        }
26    }
27    public char status(double rate){
28 
29     } 
30     public char status(int rate){
31 
32     }
33 } 

 输入格式:

输入在一行中给出2个绝对值不超过1000的正整数A和B。

输出格式:

在一行中输出一个字符A或者B。

输入样例:

50 50

输出样例:

A

代码解析:

 1 import java.util.Scanner;
 2 
 3 public class HorizontalAndVertical {
 4     public static void main(String[] args) {
 5         @SuppressWarnings("resource")
 6         /*
 7          * 程序在前面声明了一个Scanner变量in,那么系统就为in开辟了一个空间, 而当程序结束后,系统并不会自动收回in所占用的空间,
 8          * 造成资源浪费。于是提示你in永远不会close。这样,在不需要使用in的时候,调用in.close()函数,
 9          * 手动收回空间就好了。可把in.close()放在main函数最后一句就可以解决了
10          */
11 
12         /*
13          * java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。可以标注在类、字段、方法、参数、构造方法,
14          * 以及局部变量上。 作用:告诉编译器忽略指定的警告,不用在编译完成后出现警告信息。
15          */
16         Scanner in = new Scanner(System.in);
17         int height, width;
18         char status;
19         height = in.nextInt();
20         width = in.nextInt();
21         Board board = new Board(height, width);
22         status = board.getStatus();
23         System.out.print(status);
24         //in.close();
25     }
26 }
27 
28 class Board {
29     int height, width;
30 
31     public Board(int height, int width) {
32         this.height = height;
33         this.width = width;
34     }
35 
36     public char getStatus() {
37         if (height <= width) {
38             return status(1);
39         } else {
40             return status(1.0);
41         }
42     }
43 
44     public char status(double rate) {  //定义了重载的方法,方法名相同,但是传入参数的类型不同,根据传入参数的类型进行匹配
45         return 'B';
46     }
47 
48     public char status(int rate) {
49         return 'A';
50     }
51 }

 

7-4 程序改错题

程序改错题。以下代码存在错误,请修改后提交。

 1 public class Main {
 2     public static void main(String[] args) {
 3         Animal animal = new Dog();
 4         animal.shout();
 5         animal.run();
 6     }
 7 }
 8 
 9 class Animal {
10     void shout() {
11         System.out.println("animal shout!");
12     }
13 }
14 
15 class Dog extends Animal {
16     void shout() {
17         super.shout();
18         System.out.println("wangwang……");
19     }
20 
21     void run() {
22         System.out.println("Dog is running");
23     }
24 }

输出样例:

 

animal shout!
wangwang……
Dog is running
代码解析:

方法1.

 

 1 public class ProgramErrorCorrection {
 2     public static void main(String[] args) {
 3         // TODO Auto-generated method stub
 4         Animal animal = new Dog();
 5         animal.shout();
 6         ((Dog) animal).run();
 7         /*
 8          * 因为animal是Dog的上转型对象,所以,操作不了子类新增的方法, 可通过强制转换把上转型对象转换为子类的对象。
 9          */
10     }
11 }
12 
13 class Animal {
14     void shout() {
15         System.out.println("animal shout!");
16     }
17 }
18 
19 class Dog extends Animal {
20     void shout() {
21         super.shout();
22         System.out.println("wangwang……");
23     }
24 
25     void run() {
26         System.out.println("Dog is running");
27     }
28 }

 

 

 

方法2.
 1 public class ProgramErrorCorrection2 {
 2     public static void main(String[] args) {
 3         // TODO Auto-generated method stub
 4         Animal animal = new Dog();
 5         animal.shout();
 6         animal.run();
 7         /*
 8          * 因为animal是Dog的上转型对象,所以,操作不了子类新增的方法,但可以操作继承或重写的方法。
 9          * 所以,可以在父类添加一个run方法,然后,子类重写了继承的run方法,所以,当animal调用run方法时,调用子类Dog重写的run方法
10          */
11     }
12 }
13 
14 class Animal {
15     void shout() {
16         System.out.println("animal shout!");
17     }
18 
19     public void run() {
20         // TODO Auto-generated method stub
21 
22     }
23 }
24 
25 class Dog extends Animal {
26     void shout() {
27         super.shout();
28         System.out.println("wangwang……");
29     }
30 
31     public void run() {
32         System.out.println("Dog is running");
33     }
34 }
学新内容 代码行数 博客
类,对象,继承 600 1500
 
posted @ 2018-10-09 21:10  x-alchemist  阅读(365)  评论(0编辑  收藏  举报