• 第二次过程性考核


    码云练习地址:https://gitee.com/bubblerui/16012019/tree/master

    码云考核地址:https://gitee.com/bubblerui/1601201902/tree/master

    7-1 学生类-构造函数

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

    1.编写有参构造函数:能对name,sex,age赋值。

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

    3.对每个属性生成setter/getter方法。

    4.main方法中:输入1行name age sex , 调用上面的有参构造函数新建对象。

    输入样例:

    tom 15 male

    输出样例:

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

    相关知识点:

     类声明通过使用class关键字来定义类,类名是java的合法标识符。 
     进行类声明并且为对象赋值为"this.对象名",为方法赋参数,主函数调用

    设计思路;

      创建类并实例化类并进行调用

     程序代码:

    1 import java.util.Scanner;
     2 class Student{                   //类声明(年龄,姓名,性别)
     3     private String name;        
     4     private String sex;         
     5     private int age;            
     6     public Student(){            //实例化成员变量
     7         this.name = "TOM";
     8         this.sex = "male";
     9         this.age = 15;
    10     }
    11     public void toString(String t, int m, String o){         //参数他t,m,o是tostring方法的参数
    12         this.name = n;
    13         this.sex = s;                                            
    14         this.age = a;
    15         System.out.println("Student [name='"+this.name+"', sex='"+this.sex+"', age="+this.age+"]");  //输出
    16     }
    17 }
    18 public class Main{                                           //定义主函数
    19     public static void main(String[] args){
    20         Scanner reader = new Scanner(System.in);
    21         String n = reader.next();
    22         int s = reader.nextInt();
    23         String s = reader.next();
    24         Student tt = new Student();
    25         tt.toString(n,s,a);                                  
    26     }
    27 }

     运行结果:

    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 
    20 }
    复制代码

    输入格式:在一行中给出5个不超过1000的正整数。

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

    输入样例:

    1 2 3 4 5

    输出样例:

    3.00

    输入格式:在一行中给出5个不超过1000的正整数。

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

    输入样例:

    1 2 3 4 5

    输出样例:

    3.00

    知识点:参数传值,运算过程,return有返回值

    设计思路:引用类型进行参数传值

    程序代码:

     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     public double fun(int a, int b, int c, int d, int e)
    18     {
    19         return (a+b+c+d+e)/5;
    20     }
    21 }

     

    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    }
    34 }
    复制代码

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

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

    输入样例:

    50 50

    输出样例:

    A

    知识点;return作用:   

    (1)返回方法指定类型的值(这个值总是确定的),也可以是对象   

    (2)方法的结束       

    两种形式:

    (1)有返回类型 例如:return i;    

    (2)无返回类型  return;    

    有void代表无返回值,没有void有返回值。

    程序代码;

    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    private static final char A = 0;
    16    private static final char B = 0;
    17    int height, width;
    18    public Board(int height, int width){
    19        this.height = height;
    20        this.width = width;
    21    }
    22    public char getStatus(){
    23        if(height<=width){
    24           return status(1);
    25        }else{
    26          return status(1.0);
    27        }
    28    }
    29    public char status(double rate){
    30     return 'B';
    31 
    32 
    33    }
    34    public char status(int rate){
    35     return 'A';
    36 
    37    }
    38 }

    运行结果:

    7-4 程序改错题2

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

    复制代码
     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 public class Main {
     2     public static void main(String[] args) {
     3         Animal animal = new Dog();
     4         animal.shout();
     5         ((Dog) 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 }
    

    运行结果:

    学习内容 代码(行) 博客(字)
    定义类 47 100
    继承 27 21
    构造方法与对象 17 -

     

  • 相关阅读:
    composer npm bower 版本依赖符号说明
    FastAdmin 速极后台框架从 v1.0 到 v1.2 的数据库升级
    FastAdmin 也可以出书了
    FastAdmin 开发时用到的 Git 命令 (2020-09-26)
    FastAdmin用什么弹窗组件
    笔记:Linux 文件权限
    笔记:使用源代码在 Centos 7 安装 Git 2
    php gd 生成表格 图片
    easyui datagrid 清空
    mysql 去重
  • 原文地址:https://www.cnblogs.com/bubblerui/p/9781340.html
Copyright © 2020-2023  润新知