第二次考核

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 = "tom";
        this.sex = "male";
        this.age = 15;
    }

    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 StudentClass {
    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 ww = new Student();
        ww.toString(n, a, s);
        reader.close();

    }
}

程序设计思路:先引入函数,再定义类,之后定义构造方法,并按格式输出,最后在Main函数中调用子类。

 知识点:综合运用类与对象、子类与继承

运行结果如下:

7-2 定义类

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 { 
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     }

程序设计思路:本题只需补全RR类,先定义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,方法类型为char。

如上所示,只要return ‘返回值’,即可完成。

知识点:方法重载,参数传值

运行结果如下:

7-4 程序改错题

public class ProgramErrorCorrection {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Animal animal = new Dog();
        animal.shout();
        ((Dog) animal).run();
    }
}

class Animal {
    void shout() {
        System.out.println("animal shout!");
    }
}

class Dog extends Animal {
    void shout() {
        super.shout();
        System.out.println("wangwang……");
    }

    void run() {
        System.out.println("Dog is running");
    }
}

程序设计思路:这道题是修改题,Animal是Dog的对象,即不能操作子类新增的成员变量,也不能调用子类新增的方法;

需要将对象转换到一个子类对象,该子类对象具备了子类所有的属性和功能。

知识点:继承

运行结果如下:

代码详见“码云”

——————https://gitee.com/girlchujiu/codes——————

初九girl

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

猜你喜欢

转载自www.cnblogs.com/chujiugirl/p/9771929.html
今日推荐