关于类的练习

Stack

public class Stack {
    public int[] data= new int[10];
    public int top=0;
    public int capacity=10;

    public Stack(){
    }

    public Stack(int capacity){
        this.capacity=capacity;
    }

    public void push(int e){
        if (capacity==top){
            //扩容
            System.out.println("扩容");
        }else {
            data[top++]=e;
        }
    }

    public int pop(){
        if (!isEmpty()){
            return data[--top];
        }
        return -1;
    }

    public int peek(){
        if (!isEmpty()){
            return data[top-1];
        }
        return -1;
    }

    public boolean isEmpty(){
        if (top==0){
            return true;
        }
        return false;
    }

    public void clear(){
        top=0;
    }

    public int size(){
        return data.length;
    }

    public String toString(){
        String str="";
        for (int i:data) {
            str+=i+", ";
        }
        return "["+ str +"]";
    }

    public static void main(String[] args) {
        Stack stack = new Stack();

        stack.push(123);
        stack.push(456);
        stack.push(789);
        stack.push(100);
        stack.push(20);

        System.out.println(stack);
        System.out.println(stack.peek());

        stack.push(200);
//        System.out.println(stack.peek());
//        stack.pop();
//        stack.pop();
//        stack.pop();
//        stack.pop();
//        stack.pop();
//        stack.pop();

//        stack.clear();

        System.out.println(stack);
        System.out.println(stack.peek());
        System.out.println(stack.top);
    }

}

Order

public class Order {
        //static在加载的时候就会执行,故而会优于构造函数
        static int num = 0;
        String name = "qqqqqq";
        static String name2 = "wwwwwwwwwww";
        //这段调用函数执行一次,调用时 走类 寻找构造函数
        static Order parentClass = new Order();

        Order(){
            System.out.println("这里是构造函数*************");
        }

        {
            System.out.println("name1:" + name);
            System.out.println("这里是块1============");
        }

        static {
            num += 1;
            System.out.println("parentClass.name:"+parentClass.name);
            System.out.println("这里是静态块*************" + num);
        }


        public static void main(String[] args) {
            // 构造函数的执行顺序问题
            Order o = new Order();
        }
}

课程信息类

public class Course {
    static String[] stu = new String[50];
    static String courseName;
    static int numberOfStudents;

    public Course(String[] stu, String courseName, int numberOfStudents) {
        this.stu = stu;
        this.courseName = courseName;
        this.numberOfStudents = numberOfStudents;
    }

    public Course(){}

    public void Course(String courseName){
        this.courseName=courseName;
    }

    public String getCourseName(){
        System.out.println(this.courseName);
        return this.courseName;
    }

    public static void addStudent(String student){
        for (int i=0;i<stu.length;i++){
            if(stu[i]==null){
                stu[i]=student;
                break;
            }
        }
    }

    public static void dropStudent(String student){
        for (int i=0;i<stu.length;i++){
            if(stu[i]==student){
                stu[i]=null;
                break;
            }
        }
    }

    public static String[] getStudents(){
        for (int i=0;i<stu.length;i++){
            if (stu[i] != null) {
                System.out.print(stu[i]+", ");
            }else {
                break;
            }
        }
        return stu;
    }

    public static int getNumberOfStudents(){
        int num=0;
        for (int i=0;i<stu.length;i++){
            if (stu[i]!=null){
                num=i+1;
            }else {
                break;
            }
        }
        System.out.println("学生人数:"+num);
        return num;
    }

    public static void main(String[] args) {
        Course c =new Course();
        c.addStudent("秋秋");
        c.addStudent("林林");
        c.addStudent("杰杰");
        c.dropStudent("杰杰");
        c.getStudents();
        c.getNumberOfStudents();
        c.Course("数学");
        c.getCourseName();
    }

}

MyInteger类

public class MyInteger {
    public int value;

    public MyInteger(int value) {
        this.value = value;
    }

    public MyInteger(){}

    public int getValue(){
        System.out.println(this.value);
        return this.value;
    }

    //有参
    public static boolean isEven(int value){
        if(value%2==0)
            return true;
        return false;
    }

    public static boolean isOdd(int value){
        if(value%2==1)
            return true;
        return false;
    }

    public static boolean isPrime(int value) {
        boolean flag = true;
        for (int i = 2; i < value; i++) {
            if (value % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    //无参
    public boolean isEven(){
        if(this.value%2==0)
            return true;
        return false;
    }

    public boolean isOdd(){
        if(this.value%2==1)
            return true;
        return false;
    }

    public boolean isPrime() {
        boolean flag = true;
        for (int i = 2; i < this.value; i++) {
            if (this.value % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public boolean equals(int value){
        if(value==this.value){
            return true;
        }
        return false;
    }

    public boolean equals(MyInteger value){
        if(value.getValue()==this.value){
            return true;
        }
        return false;
    }

    public static int parseInt(char[] c){
        String s=new String(c);
        return Integer.parseInt(s);
    }

    public static int parseInt(String s){
        return Integer.parseInt(s);
    }


    //MyInteger
    public static boolean isEven(MyInteger value){
        if(value.getValue()%2==0)
            return true;
        return false;
    }

    public static boolean isOdd(MyInteger value){
        if(value.getValue()%2==1)
            return true;
        return false;
    }

    public static boolean isPrime(MyInteger value) {
        boolean flag = true;
        for (int i = 2; i < value.getValue(); i++) {
            if (value.getValue() % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }


    public static void main(String[] args) {
        MyInteger m = new MyInteger(20);
        System.out.println(m.isEven());
        System.out.println(m.isOdd());
        System.out.println(m.isPrime());
        System.out.println();
        System.out.println(isEven(20));
        System.out.println(isOdd(20));
        System.out.println(isPrime(20));
        System.out.println();
        MyInteger m1 =new MyInteger(50);
        System.out.println(isEven(m1));
        System.out.println(isOdd(m1));
        System.out.println(isPrime(m1));
        System.out.println();
        System.out.println(m.equals(20));
        System.out.println(m.equals(m1));
        System.out.println();
        char[] c=new char[]{'1','2','3'};
        String s="123456";
        System.out.println(parseInt(c));
        System.out.println(parseInt(s));

    }

}

MyString类

public class MyString {
        // 字符串底层是一个字符数组
        public char[] chars;

        public MyString(char[] chars) {
            this.chars = chars;
        }

        public MyString() {
            chars = new char[0];
        }

        public char charAt(int index) {
            if (index >= this.length()) {
                // 如果下标越界,则抛出异常
                throw new RuntimeException("对不起,下标越界了,字符串没有这个下标");
            }
            return chars[index];
        }

        public int length() {
            return chars.length;
        }

        public MyString substring(int begin, int end) {
            char[] substr = new char[end - begin];
            for (int i = 0; i < substr.length; i++) {
                substr[i] = chars[begin + i];
            }
            return new MyString(substr);
        }

        public MyString tolower(MyString s) {
            char[] substr = new char[this.chars.length];
            for (int i = 0; i < substr.length; i++) {
                char temp = this.chars[i];
                if (temp >= 65 && temp <= 90) {
                    substr[i] = (char)(this.chars[i] + 32);
                } else {
                    substr[i] = this.chars[i];
                }
            }
            return new MyString(substr);
        }

        public boolean equals(MyString s1) {
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] != s1.charAt(i)) {
                    return false;
                }
            }
            return true;
        }

        public static MyString valueOf(int i) {
            String str = i + "";
            char[] substr = new char[str.length()];
            for (int j = 0; j < substr.length; j++) {
                substr[j] = str.charAt(j);
            }
            return new MyString(substr);
        };


        public static void main(String[] args) {
            char[] arr = {'t', 'h', 'i', 's', ' ', 'a',  ' ', 'b', 'o', 'o', 'k'};
            MyString myString = new MyString(arr);
//		System.out.println(myString);
            System.out.println(myString.charAt(0));
            System.out.println(myString.charAt(5));
            System.out.println(myString.length());

        }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值