Java编程练习题:面向对象入门

目录

1. Stack类的实现

2. 定义一个Admin类,该类存在,username、password属性,实现一个控制台版的用户注册登录

3. 定义一个猫类(Cat),该猫有名字、性别、年龄、主人、皮毛

4. class57. MyInteger类(类与对象,构造函数)

5. Class58.自定义String类(类与对象,构造函数)


1. Stack类的实现

根据类图,实现Stack类

import java.util.Arrays;

public class Stack {
	public int capacity = 10; // 容量
	public int[] data = new int[capacity]; // 数据
	public int top = 0; 
	
	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 -1;
		}else {
			return data[--top];
		}
	}
	
	public int peek() {
		if (isEmpty()) {
			return -1;
		}else {
			return data[top - 1];
		}
	}
	
	public boolean isEmpty() {
		if (top == 0) {
			return true;
		}else {
			return false;
		}
		
	}
	
	public void clear() {
		top = 0;
	}
	
	public int size() {
		return top;
	}
	
	@Override
	public String toString() {
		return "Stack [data=" + Arrays.toString(data) + ", top=" + top + ", capacity=" + capacity + "]";
	}

	public static void main(String[] args) {
		Stack stack = new Stack();
		
		stack.push(11);
		stack.push(22);
		stack.push(33);
		stack.push(44);
		stack.push(55);
		
		System.out.println(stack);
		System.out.println(stack.peek()); // 5
		stack.push(66);
		System.out.println(stack.isEmpty()); // false
		System.out.println(stack);
		System.out.println(stack.pop()); // 66
		System.out.println(stack.size()); // 5
		stack.clear();
		System.out.println(stack.isEmpty()); // true
		System.out.println(stack);
	}
}

结果:

2. 定义一个Admin类,该类存在,username、password属性,实现一个控制台版的用户注册登录

案例:将注册的用户写在一个数组中。

import java.util.Arrays;
import java.util.Scanner;

/*
 * 定义一个Admin类,该类存在,username、password属性,实现一个控制台版的用户注册登
 * 案例:将注册的用户写在一个数组中。
 */
public class Admin {
	public String username;
	public String passwd;

	public Admin() {
		super();
	}

	public Admin(String username, String passwd) {
		super();
		this.username = username;
		this.passwd = passwd;
	}

	@Override
	public String toString() {
		return "Admin [username=" + username + ", passwd=" + passwd + "]";
	}
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Admin[] userArr = new Admin[10];
		System.out.println("请先注册账号!");
		System.out.println("请输入用户名:");
		String username = scanner.next();
		System.out.println("请输入密码:");
		String passwd = scanner.next();
		Admin admin = new Admin(username, passwd);
		System.out.println(admin);
		for (int i = 0; i < userArr.length; i++) {
			if (userArr[i] == null) {
				userArr[i] = admin;
				break;
			}
		}
		System.out.println(Arrays.toString(userArr));
		
		System.out.println("请登录账号!");
		System.out.println("请输入用户名:");
		String uname = scanner.next();
		System.out.println("请输入密码:");
		String pword = scanner.next();
		if (admin.username.equals(uname) && admin.passwd.equals(pword)) {
			System.out.println(admin.username + ",登录成功!");
		}else {
			System.out.println("用户名或密码不匹配!");
		}
		scanner.close();
	}
}

结果:

3. 定义一个猫类(Cat),该猫有名字、性别、年龄、主人、皮毛

package com.openlab.day11.exer;
/*
 * 定义一个猫类(Cat),该猫有名字、性别、年龄、主人、皮毛
 */
public class Cat {
	public String name;
	public boolean gender;
	public int age;
	public String master;
	public String color;
	
	public Cat(String name, boolean gender, int age, String master, String color) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.master = master;
		this.color = color;
	}

	public Cat() {
		super();
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", gender=" + gender + ", age=" + age + ", master=" + master + ", color=" + color
				+ "]";
	}
	
	public static void main(String[] args) {
		Cat cat = new Cat("小汪", false, 12, "小明", "橘色");
		System.out.println(cat);
		Cat cat1 = new Cat();
		cat1.name = "小喵";
		cat1.gender = true;
		cat1.age = 10;
		cat1.master = "小强";
		cat1.color = "银渐层";
		System.out.println(cat1);
	}
}

结果:

4. class57. MyInteger类(类与对象,构造函数)

import java.util.Scanner;
/*
 * MyInteger类(类与对象,构造函数)
 */
public class MyInteger {
	public int a;

	public MyInteger() {

	}

	public MyInteger(int x) {
		this.a = x;
	}

	public boolean isEven() {
		if (a % 2 == 0)
			return true;
		else
			return false;
	}

	public boolean isOdd() {
		if (a % 2 != 0)
			return true;
		else
			return false;
	}

	public boolean isPrime() {
		int i, k = 0;
		if (a == 2)
			return true;
		for (i = 2; i < a; i++) {
			if (a % i == 0)
				k = 1;
		}
		if (k == 0)
			return true;
		else
			return false;
	}

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

	public boolean equals(int a) {
		if (this.a == a)
			return true;
		else
			return false;
	}
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int x = input.nextInt();
		int y = input.nextInt();
		int z = input.nextInt();
		input.close();

		MyInteger A = new MyInteger(x);
		MyInteger B = new MyInteger(y);
		MyInteger C = new MyInteger();
		System.out.println(C);
		System.out.println(A.isEven());
		System.out.println(A.isOdd());
		System.out.println(A.isPrime());
		System.out.println(A.equals(z));
		System.out.println(A.equals(B));
	}
}

结果:

5. Class58.自定义String类(类与对象,构造函数)

暂时没写

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Golang_HZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值