hooyantsing's Blog

第13次课程

字数统计: 1.9k阅读时长: 10 min
2019/05/11

源辰74班

第13次课程

05.11.2019

**内容    **

OOP_1

1.老师和学生类

Class Student\Class Teacher\Main

1
2
3
4
5
6
7
8
9
public class Student {
    int id;
    String name;
    char sex;
    
    public void introduce() {
       System.out.println("hello,我叫:"+name+",性别:"+sex);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
public class Teacher {
    String name;
    String password;
    
    public void inputNameAndPassword() {
       Scanner sc = new Scanner(System.in);
       System.out.println("老师,请输入登陆用户名:");
       name = sc.nextLine();
       System.out.println("请输入登陆密码:");
       password = sc.nextLine();
       
    }
    
    public void showInfo() {
       System.out.println("用户名:"+name+",密码:"+password);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Main1 {
    public static void main(String[] args) {
       //实例化
       Student s = new Student();
       // . 成员访问运算符
       s.id = 1;
       s.name = "张三";
       s.sex = '男';
       
       s.introduce();
       
       Teacher t = new Teacher();
       t.inputNameAndPassword();
       t.showInfo();
    }
}

2.体重

**Class **person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Person {
    String name;
    double weight;
    double height;
    
    //吃食物    1斤食物 -> 0.25斤体重
    public void eat(double foods) {
       weight = weight + foods/4;
    }
    
    //运动  1小时 -> 0.5斤体重
    public void execise(double hours) {
       weight = weight - hours*0.5;
    }
    
    public void showInfo() {
       System.out.println(name+",您的体重为:"+weight);
    }
    
    //bmi = 体重(公斤)*身高(米)^2
    public void showBmi() {
       double bmi;
       bmi = (weight/2)/(Math.pow(height,  2));
       System.out.println("bim值为:"+bmi);
       if(bmi<18.5) {
           System.out.println("体重过轻");
       }else if(bmi<24.5) {
           System.out.println("体重正常");
       }else if(bmi<28) {
           System.out.println("肥胖前期");
       }else if(bmi<30) {
           System.out.println("肥胖");
       }else {
           System.out.println("放弃治疗...");
       }
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Main2 {
    public static void main(String[] args) {
       Person p = new Person();
       p.name = "容闸总";
       p.weight = 100;
       p.height = 1.75;
       
       p.eat(1);
       p.execise(0.5);
       
       p.eat(0.5);
       p.execise(0.5);
       
       p.showInfo();
       p.showBmi();
    }
}

3.关于private、this.、

替换快捷键:ALT + SHIFT + R

体重(增强版)实验:

Class person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class Person {
    //private 私有化 成员仅能在当前类内被访问
    private String name;
    private double weight;
    private double height;
    
    //类内加入逻辑,使程序更为健壮
    //this.xxx 表示xxx是本类内的成员变量
    public void setName(String name) {
       if(name==null || "".equals(name)) {
           this.name = "匿名用户";
       }else {
           this.name = name;
       }
    }
    
    public void setWeight(double weight) {
       if(weight<30 || weight>800) {
           this.weight = 110;
           System.out.println("录入体重不合法,已使用标准体重:110斤");
       }else {
           this.weight = weight;
       }
    }
    
    public void setHeigth(double height) {
       if(height<1 || height>2.5) {
           this.height = 1.7;
           System.out.println("录入身高不合法,已使用标准身高:1.7米");
       }else {
           this.height = height;
       }
    }
    
    //吃食物    1斤食物 -> 0.25斤体重
    public void eat(double foods) {
       weight = weight + foods/4;
    }
    
    //运动  1小时 -> 0.5斤体重
    public void execise(double hours) {
       weight = weight - hours*0.5;
    }
    
    public void showInfo() {
       System.err.println(name);
       System.out.println("您的体重为:"+weight+",身高为:"+height);
       
    }
    
    //bmi = 体重(公斤)*身高(米)^2
    public void showBmi() {
       double bmi;
       bmi = (weight/2)/(Math.pow(height,  2));
       System.out.println("bim值为:"+bmi);
       if(bmi<18.5) {
           System.out.println("体重过轻");
       }else if(bmi<24.5) {
           System.out.println("体重正常");
       }else if(bmi<28) {
           System.out.println("肥胖前期");
       }else if(bmi<30) {
           System.out.println("肥胖");
       }else {
           System.out.println("放弃治疗...");
       }
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Main2 {
    public static void main(String[] args) {
       Person p = new Person();
       
       p.setName("容闸总");
       p.setWeight(110);
       p.setHeigth(1.72);
       
       //午饭时段
       p.eat(1);
       p.execise(0.5);
       
       //晚饭时段
       p.eat(0.5);
       p.execise(0.5);
       
       //显示
       p.showInfo();
       p.showBmi();
    }
}

4.构造方法和构造方法重载

商品实验:

Class Goods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package Goods;
public class Goods {
    private String gname;
    private double price;
    
    //构造方法定义:1.与类同名 2.无返回值  3.new调用
    //构造方法的作用:初始化成员
    //类内可以存在多个构造方法(重载)
    //若不定义构造方法,则系统自动定义:String  -> null,double -> 0.0,int -> 0,boolean ->  false
    public Goods() {
       this.gname = "匿名商品";
       this.price = 1;
    }
    
    //构造方法重载
    public Goods(String gname,double price)  {
       this.gname = gname;
       this.price = price;
    }
    
    public Goods(String gname) {
       this.gname = gname;
       
    }
}

Main

1
2
3
4
5
6
7
8
package Goods;
public class Main3 {
    public static void main(String[] args) {
       Goods g1 = new Goods();
       Goods g2 = new Goods("苹果",3000);
       Goods g3 = new Goods("华为");
    }
}

5.[项目]学生总成绩和平均成绩

题目:

实现一个Student类。学生应有姓名和总分。提供一个适当的构造方法以及getName(),addQuiz(int score),getTotalScore()和getAverageScore()方法。

为计算后者,需要存储学生参加测验的次数。

Class Student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package Student;
public class Student {
    private String name;
    private int totalScore;
    //根据需求增加隐含变量
    private int number;
    
    //构造方法
    public Student() {
       this.name = "匿名用户";
    }
    public Student(String name) {
       this.name = name;
    }
    //功能方法
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getTotalScore() {
       return totalScore;
    }
    public void addQuiz(int Score) {
       if(Score<0) {
           System.out.println("分数异常");
       }
       this.totalScore += Score;
       this.number++;
    }
    public int getAverageScore(){
       if(this.number<=0) {
           return 0;
       }
       return this.totalScore/this.number;
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
package Student;
public class Main4 {
    public static void main(String[] args) {
       Student s = new Student();
       s.setName("容狗蛋");
       s.addQuiz(56);
       s.addQuiz(71);
       s.addQuiz(35);
       System.out.println(s.getName()+",总成绩为:"+s.getTotalScore()+",平均成绩为:"+s.getAverageScore());
    }
}

6.[项目]矩形的周长和面积

Class Rectangle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package Rectangle;
public class Rectangle {
    private double length;
    private double width;
    
    
    public Rectangle() {
       this.length = 1;
       this.width = 1;
    }
    public Rectangle(double length,double  width) {
       this.length = length;
       this.width = width;
    }
    
    
    public double calPerimeter() {
       return(this.length+this.width)*2;
    }
    public double calArea() {
       return this.length*this.width;
    }
    
    
    public double getLength() {
       return length;
    }
    public void setLength(double length) {
       this.length = length;
    }
    public double getWidth() {
       return width;
    }
    public void setWidth(double width) {
       this.width = width;
    }
}

Main

1
2
3
4
5
6
7
package Rectangle;
public class Main {
    public static void main(String[] args) {
       Rectangle r = new Rectangle(3,4);
       System.out.println("长为:"+r.getLength()+",宽为:"+r.getWidth()+",周长为:"+r.calPerimeter()+",面积为:"+r.calArea());
    }
}

7.[项目]发票

Class Invoice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package Invoice;
import java.util.Date;
public class Invoice {
    private String companyName;
    private Date date = new Date();
    
    private final int NUM = 10;
    
    public Invoice() {
       this.companyName = "空";
    }
    public Invoice(String companyName) {
       this.companyName = companyName;
    }
    
    private Item[] items = new Item[NUM];
    
    
    public String getCompanyName() {
       return companyName;
    }
    public void setCompanyName(String  companyName) {
       this.companyName = companyName;
    }
    public void printInvoice() {
       System.out.println("*******************************************");
       System.out.println("\t\t"+companyName);
       System.out.println("*******************************************");
       int length = size();
       double total = 0;
       for(int i=0;i<length;i++) {
           Item item = items[i];
           System.out.println(item.getItemInfo());
           total = item.getSmallCount();
       }
       System.out.println("总计:\t\t\t"+total);
       System.out.println("开票日期:"+date);
       System.out.println("*******************************************");
    }
    public void addItem(Item item) {
       int index = size();
       if(index==NUM) {
           System.out.println("发票最多打印"+NUM+"个商品");
           return;
       }
       items[index] = item;
    }
    private int size() {
       for(int i=0;i<items.length;i++) {
           if(items[i]==null) {
               return i;
           }
       }
       return NUM;
    }
    
}

Class Item

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Invoice;
public class Item {
    //成员
    private String name;
    private double price;
    private int number;
    
    
    //构造方法
    public Item() {
       this.name = "空";
       this.price = 0.0;
       this.number = 0;
    }
    public Item(String name,double price,int  number) {
       this.name = name;
       this.price = price;
       this.number = number;
    }
    
    
    //基本方法
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public double getPrice() {
       return price;
    }
    public void setPrice(double price) {
       this.price = price;
    }
    public int getNumber() {
       return number;
    }
    public void setNumber(int number) {
       this.number = number;
    }
    
    
    //功能方法
    public double getSmallCount() {
       return this.price*this.number;
    }
    public String getItemInfo() {
       return  name+"\t"+number+"\t"+price+"\t"+this.getSmallCount();
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
package Invoice;
public class Main {
    public static void main(String[] args) {
       Invoice inv = new Invoice("胡氏集团总公司");
       Item i1 = new  Item("iPhone5S",6088,1);
       inv.addItem(i1);
       Item i2 = new Item("iPhone  X",9688,1);
       inv.addItem(i2);
       
       inv.printInvoice();
    }
}
CATALOG