hooyantsing's Blog

第14次课程

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

源辰74班

第14次课程

05.13.2019

**内容    **

OOP_1

1.计算器

Class Calculator

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
package Calculator;
public class Calculator {
    int x;
    int y;
    
    public Calculator() {
       super();
    }
    public Calculator(int x, int y) {
       super();
       this.x = x;
       this.y = y;
    }
    
    public int getX() {
       return x;
    }
    public void setX(int x) {
       this.x = x;
    }
    public int getY() {
       return y;
    }
    public void setY(int y) {
       this.y = y;
    }
    
    public int add() {
       return this.x+this.y;
    }
    public int sub() {
       return this.x-this.y;
    }
    public int mul() {
       return this.x*this.y;
    }
    public int div() {
       if(y==0) {
           System.err.println("被除数不能为0");
       }
       return this.x/this.y;
    }
}

2.计算体积

3.结婚和离婚

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package Person;
public class Person {
    private String name;
    private int age;
    private boolean gender;
    private Person partner;
    
    //结婚
    public void marry(Person other) {
       if(other==null) {
           System.out.println("不能与空对象结婚");
           return;
       }
       if(other==this) {
           System.out.println("不能与自己结婚");
           return;
       }
       if(this.gender==other.gender) {
           System.out.println("同性不能结婚,性别都是:"+this.gender);
           return;
       }
       if(this.partner!=null) {
           System.out.println(this.name+"已婚,已婚对象为:"+this.partner.name);
           return;
       }
       if(other.partner!=null) {
           System.out.println(other.name+"已婚,已婚对象为:"+other.partner.name);
           return;
       }
       if(isAgeOk(this)==false) {
           System.out.println(this.name+"未到结婚年龄");
           return;
       }
       if(isAgeOk(other)==false) {
           System.out.println(other.name+"未到结婚年龄");
           return;
       }
       //OK
       this.partner=other;
       other.partner=this;
       System.out.println(this.name+"与"+other.name+"喜结良缘,早生贵子");
    }
    public boolean isAgeOk(Person P) {
       if(P.gender==true&&P.age<24 ||  P.gender==false&&P.age<22) {
           return false;
       }
       return true;
    }
    //离婚
    public void divoice(Person other) {
       if(other==null) {
           System.out.println(this.name+"不能与空对象离婚");
           return;
       }
       if(this.partner==null) {
           System.out.println(this.name+"还未结婚,无法离婚");
           return;
       }
       if(other.partner==null) {
           System.out.println(other.name+"还未结婚,无法离婚");
           return;
       }
       if(this.partner!=other &&  other.partner!=this) {
           System.out.println(this.name+"与"+other.name+"不是夫妻关系,无法离婚");
           return;
       }
       //OK
       this.partner=null;
       other.partner=null;
       System.out.println(this.name+"与"+other.name+"离婚成功,重获自由");
    }
    
    //自动生成代码
    public Person() {
       super();
    }
    public Person(String name, int age,  boolean gender) {
       super();
       this.name = name;
       this.age = age;
       this.gender = gender;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }
    public boolean isGender() {
       return gender;
    }
    public void setGender(boolean gender) {
       this.gender = gender;
    }
    public Person getPartner() {
       return partner;
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Person;
public class Main1 {
    public static void main(String[] args) {
       Person p1 = new Person("张三",25,true);
       Person p2 = new Person("小红",24,false);
       Person p3 = new Person("李四",18,true);
       Person p4 = new Person("王五",30,true);
       
       //p1与p2结婚,p3与p4结婚
       p1.marry(p2);
       p3.marry(p4);
       //p1与p2离婚,p2与p4结婚
       p1.divoice(p2);
       p2.marry(p4);
       //p2与p4离婚,p2与p3结婚
       p2.divoice(p4);
       p2.marry(p3);
    }
}

4.栈

Class MyStack

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
package MyStack;
public class MyStack {
    private final int NUM = 10;
    
    private int[] arr;
    private int top = 0;
    
    public MyStack(){
       super();
       arr = new int[NUM];
       top = 0;
    }
    
    public int capacity() {
       return arr.length;
    }
    public int size() {
       return top;
    }
    public void push(int x) {
       if(top==arr.length) {
           //扩容
           int[] temp = new  int[arr.length*2];
           for(int i=0;i<arr.length;i++) {
               temp[i] = arr[i];
           }
           arr = temp;
           //垃圾回收
           System.gc();
       }
       arr[top] = x;
       top++;
    }
    public int pop() {
       if(top==0) {
           System.out.println("栈空");
           return -1;
       }
       top--;
       return arr[top];
    }
    public int peek() {
       if(top==0) {
           System.out.println("栈空");
           return -1;
       }
       return arr[top-1];
    }
    public void clear() {
       arr = new int[NUM];
       top = 0;
       System.gc();
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package MyStack;
public class Main2 {
    public static void main(String[] args) {
       MyStack m = new MyStack();
       m.push(3);
       m.push(2);
       m.push(1);
       System.out.println("栈总容量:"+m.capacity());
       System.out.println("已存元素个数:"+m.size());
       System.out.println("栈顶元素:"+m.peek());
       System.out.print("出栈元素:");
       while(m.size()!=0) {
           int ele = m.pop();
           System.out.print(ele+"\t");
           }
    }
}

5.在二维数组中找出最大值

Class Location

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
package Location;
public class Location {
    private int row;
    private int column;
    private int maxValue;
    public Location() {
       super();
    }
    public Location(int row, int column, int  maxValue) {
       super();
       this.row = row;
       this.column = column;
       this.maxValue = maxValue;
    }
    public int getRow() {
       return row;
    }
    public void setRow(int row) {
       this.row = row;
    }
    public int getColumn() {
       return column;
    }
    public void setColumn(int column) {
       this.column = column;
    }
    public int getMaxValue() {
       return maxValue;
    }
    public void setMaxValue(int maxValue) {
       this.maxValue = maxValue;
    };
}

Main

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
package Location;
import java.util.Random;
public class Main3 {
    public static void main(String[] args) {
       int[][] arr = new int[10][10];
       Random r = new Random();
       for(int i=0;i<arr.length;i++) {
           for(int j=0;j<arr[i].length;j++)  {
               arr[i][j] = r.nextInt(100);
               System.out.print(arr[i][j]+"\t");
           }
           System.out.println();
       }
       Location location =  locateLargest(arr);
       System.out.println(location.getRow()+"行"+location.getColumn()+"列,最大值为:"+location.getMaxValue());
    }
    
    public static Location  locateLargest(int[][] arr) {
       Location location = new Location();
       int max = arr[0][0];
       for(int i=0;i<arr.length;i++) {
           for(int j=0;j<arr[i].length;j++)  {
               if(max<=arr[i][j]) {
                  max = arr[i][j];
                  location.setRow(i);
                  location.setColumn(j);
                  location.setMaxValue(max);
               }
           }
       }
       return location;
    }
}

6.模拟股票

Class Stock

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
package Stook;
public class Stock {
    private String symbol;
    private String name;
    private double closePrice;
    private double openPrice;
    private double topPrice;
    private double bottomPrice;
    public Stock(String symbol, String name)  {
       super();
       this.symbol = symbol;
       this.name = name;
    }
    public double getChangePercent() {
       return (closePrice -  openPrice)/openPrice;
    }
    public double getClosePrice() {
       return closePrice;
    }
    public void setClosePrice(double  closePrice) {
       this.closePrice = closePrice;
    }
    public double getOpenPrice() {
       return openPrice;
    }
    public void setOpenPrice(double  openPrice) {
       this.openPrice = openPrice;
    }
    public double getTopPrice() {
       return topPrice;
    }
    public void setTopPrice(double topPrice)  {
       this.topPrice = topPrice;
    }
    public double getBottomPrice() {
       return bottomPrice;
    }
    public void setBottomPrice(double  bottomPrice) {
       this.bottomPrice = bottomPrice;
    }
    public String getSymbol() {
       return symbol;
    }
    public String getName() {
       return name;
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package Stook;
import java.util.Random;
public class Main4 {
    public static void main(String[] args) {
       Stock s = new Stock("000002","万科A");
       Random r = new Random();
       for(int i=0;i<10;i++) {
           s.setOpenPrice(r.nextDouble()*2+10);
           s.setClosePrice(r.nextDouble()*2+10);
           s.setBottomPrice(r.nextDouble()*2+10);
           s.setTopPrice(s.getBottomPrice()+r.nextDouble()*2);
           System.out.println(s.getName()+"\t涨跌:"+s.getChangePercent());
       }
    }
}

7.ATM

Class Account

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
package ATM;
public class Account {
    private int id;
    private int balance;
    public Account(int id) {
       super();
       this.id = id;
       this.balance = 100;
    }
    public int getId() {
       return id;
    }
    
    public void check() {
       System.out.println("账户"+id+"的余额:"+balance);
    }
    public void deposit(int money) {
       balance += money;
       System.out.println("存款操作完成");
    }
    public void widthdraw(int money) {
       if(money>balance) {
           System.out.println("余额不足,请确认后操作");
           return;
       }
       balance -= money;
       System.out.println("取款操作完成");
    }
}

Class Bank

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package ATM;
public class Bank {
    private Account[] accounts = new  Account[10];
    
    //通过银行的构造方法初始化10个账户
    public Bank() {
       for(int i=0;i<10;i++) {
           accounts[i] = new Account(i+1);
       }
    }
    public Account getAccounts(int id) {
       for(int i=0;i<accounts.length;i++) {
           if(accounts[i].getId()==id) {
               return accounts[i];
           }
       }
       return null;
    }
}

Main

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
package ATM;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
       Date d = new Date();
       SimpleDateFormat sdf = new  SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
       Scanner sc = new Scanner(System.in);
       Bank b = new Bank();
       
       do {
           System.out.println("\t\t中国银行湖南工学院分行欢迎您");
           System.out.println("当前时间:"+sdf.format(d));
           System.out.println("请输入您的卡号:");
           int id = sc.nextInt();
           //根据id查找账户
           Account account =  b.getAccounts(id);
           if(account!=null) {
               showMenu(account);
           }else {
               System.out.println("账户不存在");
           }
       }while(true);
       
    }
    private static void showMenu(Account  account) {
       Scanner sc = new Scanner(System.in);
       int ch = -1;
       int money = -1;
       do {
           System.out.println("\t\t操作菜单:");
           System.out.println("\t\t1.查询余额");
           System.out.println("\t\t2.取款");
           System.out.println("\t\t3.存款");
           System.out.println("\t\t4.退出");
           System.out.println("请输入你的选项:");
           ch = sc.nextInt();
           switch(ch) {
           case 1:
               account.check();
               break;
           
           case 2:
               System.out.println("请输入您要取款的金额:");
               money = sc.nextInt();
               account.widthdraw(money);
               break;
           case 3:
               System.out.println("请你输入要存款的金额");
               money = sc.nextInt();
               account.deposit(money);
               break;
           }
       }while(ch!=4);
       
    }
}
CATALOG