第二次过程性考核

码云地址:https://gitee.com/lixin-123/16012014_li_xin_hun_two

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]

import java.util.Scanner;
class Student{
    private String name;
    private String sex;
    private int age;
    public Student(){
        this.name = "sss";
        this.sex = "male";
        this.age = 20;
    }
    public void toString(String n, int a, String s){
        this.name = n;
        this.sex = s;
        this.age = a;
        System.out.println("Student [name='"+this.name+"', sex='"+this.sex+"', age="+this.age+"]");
    }
}
public class Test{
    public static void main(String[] args){
        Scanner reader = new Scanner(System.in);
        String n = reader.next();
        int a = reader.nextInt();
        String s = reader.next();
        Student tt = new Student();
        tt.toString(n,a,s);
    }
}

程序设计思路: 定义Student类,定义构造方法和有参构造函数,最后调用子类。

运用到的知识点:定义一个类,子类与继承,调用函数

运行结果:

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  }

补充后代码:

 import java.util.Scanner;
  public class Main {
      public static void main(String[] args) {
                 Scanner in = new Scanner(System.in);
                 int a,b,c,d,e;
                 a = in.nextInt();
                 b = in.nextInt();
                 c = in.nextInt();
                 d = in.nextInt();
                 e = in.nextInt();
                 RR rr = new RR();
                 double dd = rr.fun(a,b,c,d,e);
                 System.out.printf("%.2f",dd);
     }
 }
 class RR{

  public double fun(int a, int b, int c, int d, int e,double i) {
         return (a+b+c+d+e)/5;
         
}
 }

程序设计思路:通过分析给出的代码,问题出现在定义类,定义类RR,然后返回五个数的平均值

运用到的知识点:定义类,return有返回值

运行结果:

7-3 横平竖直

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

import java.util.Scanner;

public class HorizontalAndVertical {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int height, width;
        char status;
        height = in.nextInt();
        width = in.nextInt();
        Board board = new Board(height, width);
        status = board.getStatus();
        System.out.print(status);
    }
}

class Board {
    int height, width;

    public Board(int height, int width) {
        this.height = height;
        this.width = width;
    }

    public char getStatus() {
        if (height <= width) {
            return status(1);
        } else {
            return status(1.0);
        }
    }

    public char status(double rate) {  
        return 'B';
    }

    public char status(int rate) {
        return 'A';
    }
}

程序设计思路:由于此题是程序填空题,题目知,高度比宽度大竖着放,否则平放,如果平放输出A否则输出B,所以return有返回值返回A,B

运用到的知识点:  方法重载,参数传值

运行结果:

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  }

程序设计思路:把Dog定义为Animal的子类(使用了eclipse)

运用到的知识点:定义类,子类继承,

运行结果:

学习内容代码(行)博客(字)
类与对象子类与继承500 500
   

 

转载于:https://www.cnblogs.com/lixin-123/p/9778220.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值