day_7:面向对象和面向过程区别、五子棋

面向过程与面向对象的区别

  面向过程:围绕功能进行的,为每一个功能写一个函数,需要考虑其中的每一个细节,以步骤划分,可以这么比喻: 面向过程的程序=算法+数据结构

  面向对象:像是组装,先确定一个系统是由哪些对象组成,再分别去设计这些对象,将它们像零件一样组装起来形成有完整功能的系统,以功能划分,相当于:对象=算法+数据结构

  区别:面向过程程序只用函数实现,而面向对象程序是用类实现各个功能模块。

案例一:(五子棋)

package com.cmlx.test04;

public class WuZiQi {
    private int length = 20;
    private String add = "╬";

    private String[] nums = { "⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖", "⒗", "⒘", "⒙",
            "⒚", "⒛" };
    String[][] qiPan = new String[length][length];

    {
        init();
        printWuZiQi();
    }

    public int luoZi(int x, int y) {
        if (x < 0 || x > length - 2 || y < 0 || y > length - 2) {
            return -2;
        }
        if (!qiPan[x][y].equals(add)) {
            return -1;
        }
        return 1;

    }

    private boolean isOutOfIndex(int x, int y) {
        if (!qiPan[x][y].equals(add)) {
            return false;
        }
        return true;
    }

    private void init() {
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                if (i == length - 1) {
                    qiPan[i][j] = nums[j];
                } else if (j == length - 1) {
                    qiPan[i][j] = nums[i];
                } else
                    qiPan[i][j] = add;
            }
        }
    }

    public void printWuZiQi() {
        for (String[] strings : qiPan) {
            for (String string : strings) {
                System.out.print(string);
            }
            System.out.println();
        }
    }

}
WuZiQi.java
package com.cmlx.test04;

import java.util.Scanner;

public class WuZiQiTest {

    public static void main(String[] args) {
        String black = "●";
        String white = "○";
        boolean bool = true;
        WuZiQi wuZiQi = new WuZiQi();
        Scanner scan = new Scanner(System.in);
        
        while(true) {
            System.out.println("请"+((bool)?"黑":"白")+"子落棋(x,y):");
            String next = scan.next();
            String[] string = next.split(",");
            int x = Integer.parseInt(string[0])-1;
            int y = Integer.parseInt(string[1])-1;
            
            int z = wuZiQi.luoZi(x, y);
            if(z == -2) {
                System.out.println("棋子坐标越界。。。");
                continue;
            }else if(z == -1) {
                System.out.println("此处已经有棋子。。。");
                continue;
            }else {
                wuZiQi.qiPan[x][y]=(bool)?black:white;
            }
            wuZiQi.printWuZiQi();
            bool = !bool;
            
            
         }
    }
}
WuZiQiTest

案例二:(学生管理系统)

package com.cmlx.test04;

public class StudentManagerSystem2 {

    public static void main(String[] args) {
        add("赤名莉香", "18", "女", "1807", "13");
        add("关口里美", "22", "女", "1807", "16");
        add("水菜丽", "20", "女", "1807", "19");
        add("郑强", "18", "男", "1807", "14");

        add("周芷若", "18", "女", "1808", "13");
        add("和贺夏树", "56", "男", "1808", "14");
        add("永尾完治", "22", "男", "1808", "18");

        delete("1807", "19");
        printStu();

    }

    public static String[][] stus = new String[3][5];
    public static int point = 0;

    // 删除学员
    public static boolean delete(String classId, String id) {
        // 判断信息是否合法
        if (!isClassId(classId) || !isId(id)) {
            return false;
        }
        // 判断是否有该学员
        int stu = query(classId, id);
        if (stu == -2 || stu == -1) {
            return false;
        }
        // 删除
        for (int i = stu; i < point - 1; i++) {
            stus[i] = stus[i + 1];
        }
        point--;
        return true;
    }

    // 添加学员
    public static boolean add(String name, String age, String sex, String classId, String id) {
        // 判断学生信息是否合法
        if (!isStu(name, age, sex, classId, id)) {
            return false;
        }
        // 判断是否已有此学生
        int x = query(classId, id);
        if (x == -2 || x != -1) {
            return false;
        }
        // 扩容
        if (point == stus.length) {
            KuoRong();
        }
        // 添加
        stus[point][0] = name;
        stus[point][1] = sex;
        stus[point][2] = age;
        stus[point][3] = classId;
        stus[point][4] = id;
        point++;
        return true;
    }

    public static void printStu() {
        for (int i = 0; i < point; i++) {
            for (int j = 0; j < stus[i].length; j++) {
                System.out.print(stus[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static void KuoRong() {
        String[][] newStus = new String[stus.length * 2][5];
        for (int i = 0; i < point; i++) {
            newStus[i] = stus[i];
        }
        stus = newStus;
    }

    public static int query(String classId, String id) {
        if (!isClassId(classId) || !isId(id)) {
            return -2;
        }
        for (int i = 0; i < point; i++) {
            if (stus[i][3].equals(classId) && stus[i][4].equals(id)) {
                return i;
            }
        }
        return -1;
    }

    public static boolean isStu(String name, String age, String sex, String classId, String id) {
        if (!isName(name) || !isAge(age) || !isSex(sex) || !isClassId(classId) || !isId(id)) {
            return false;
        }
        return true;
    }

    public static boolean isName(String name) {
        if (name.length() < 2 || name.length() > 6) {
            return false;
        }
        return true;
    }

    public static boolean isAge(String age) {
        int newage = Integer.parseInt(age);
        if (newage < 0 || newage > 100) {
            return false;
        }
        return true;
    }

    public static boolean isSex(String sex) {
        if (!sex.equals("男") && !sex.equals(sex)) {
            return false;
        }
        return true;
    }

    public static boolean isClassId(String classId) {
        if (classId.length() != 4) {
            return false;
        }
        return true;
    }

    public static boolean isId(String id) {
        if (id.length() != 2) {
            return false;
        }
        return true;
    }

}
StudentManagerSystem2.java

 

posted @ 2018-10-24 19:52  cmlx  阅读(311)  评论(0)    收藏  举报