1.给定一个数字,求该数字的二进制的1的个数
package oupeng.week4.HomeWork;
public class HomeworkDemo1 {
public static void main(String[] args) {
System.out.println(dtoBin(9));
}
public static int dtoBin(int num) {
int count = 0;
for (int i = 8; i >= 0; i--) {
System.out.print((num & (1 << i)) == 0 ? "0" : "1");
if (((num & (1 << i)) == 0 ? 0 : 1) == 1) {
count++;
}
}
System.out.println();
return count;
}
}
2. 给定一个数组,该数组中除了一个元素只出现了一次,其他元素都出现两次
找到这个没有重复的元素
package oupeng.week4.HomeWork;
//给定一个数组,该数组中除了一个元素只出现了一次,其他元素都出现两次
// 找到这个没有重复的元素
public class HomeWorkDemo2 {
public static void main(String[] args) {
int[] a = {8, 2, 2, 4, 3, 3, 4, 5, 5, 6, 6, 7, 7};
System.out.println(findSimple(a));
}
public static int findSimple(int[] a) {
int sum=a[0];
for (int i = 1; i < a.length; i++) {
sum^=a[i];
}
return sum;
}
}
3.给定一个数组,数组的元素共N+1个, 元素是从1到n的联系自然数,其中一个重复值
找到这个重复的元素
package oupeng.week4.HomeWork;
public class HomeWorkDemo3 {
public static void main(String[] args) {
int a[]={1,2,3,4,5,5,6,7,8};
System.out.println(findRepeatElement(a));
}
public static int findRepeatElement(int a[]){
int r=0;
for (int i = 0; i < a.length; i++) {
r^=i^a[i];
}
return r;
}
}
4.Stack类的实现
package oupeng.week4.test;
public class Stack {
public static int capacity = 10;
public int[] data;
public int top = -1;
public Stack() {
this(capacity);
}
public Stack(int capacity) {
if (capacity < 0) {
capacity = 0;
}
if (capacity > 100) {
capacity = 100;
}
this.data = new int[capacity];
}
public void push(int e) {
if (size() == data.length) {
System.out.println("栈已满,无法添加元素" + e);
return;
}
data[top++] = e;
}
public int pop() {
if (isEmpty()) {
System.out.println("栈为空,无法弹栈");
return -1;
}
return data[top--];
}
public int peek() {
if (isEmpty()) {
System.out.println("栈为空,无法获取栈顶元素");
return -1;
}
return data[top];
}
public boolean isEmpty() {
return top == -1;
}
public void clear() {
top = -1;
}
public int size() {
return top + 1;
}
public String toString() {
if (isEmpty()) {
return "[]";
}
String s = "[";
for (int i = 0; i < size(); i++) {
s += data[i];
if (i == size() - 1) {
s += "]";
} else {
s += ",";
}
}
return s;
}
}
5.定义一个Admin类,该类存在,username、password属性,实现一个控制台版的用户注册登录案例
将注册的用户写在一个数组中。
package oupeng.week4.HomeWork;
import java.util.Scanner;
public class Admin {
public String username;
public String password;
}
class TestAdmin{
public static Admin[] users=new Admin[10];
public static int capacity=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag=true;
while (true) {
System.out.print("请输入用户名称:");
String username = sc.nextLine();
System.out.print("请输入用户密码:");
String password = sc.nextLine();
if (username == null || username.trim().equals("")) {
System.out.println("用户名不能为空!!");
flag=false;
}
if (password.length() < 5) {
System.out.println("密码长度不能小于5位!!");
flag=false;
}
if (flag){
Admin user =new Admin();
user.username= username;
user.password=password;
users[capacity++]=user;
System.out.println("注册成功!!");
break;
}else {
System.out.println("注册失败!");
}
}
}
}
6.定义一个猫类(Cat),该猫有名字、性别、年龄、主人、皮毛
package oupeng.week4.test;
public class Cat {
String name;
char sex;
int age;
String color;
Master master;
public Cat(){
}
public Cat(String name,char sex,int age,String color,Master master){
this.name=name;
this.sex=sex;
this.age=age;
this.color=color;
this.master=master;
}
}
//-----------------------------
package oupeng.week4.test;
public class Master {
String name;
int age;
String address;
Cat[] cats;
String sex;
public static void main(String[] args) {
Master m1=new Master();
Cat c=new Cat();
Cat c1=new Cat();
c1.name="x";
m1.name="小大";
m1.age=18;
c.name="小白";
c.age = 1;
c.master=m1;
c1.master=m1;
Cat[] cats={c,c1};
System.out.println(c.master.name);
m1.cats=cats;
}
}
7.MyInteger类(类与对象,构造函数)
package oupeng.week4.HomeWork;
public class MyInteger {
int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isEven() {
return (value & 1) == 0;
}
public boolean isOdd() {
return (value & 1) != 0;
}
public boolean isPrime() {
for (int i = 2; i < value - 1; i++) {
if ((value % i) == 0) {
return false;
}
}
return true;
}
public static boolean isEven(int value) {
return (value & 1) == 0;
}
public static boolean isOdd(int value) {
return (value & 1) != 0;
}
public static boolean isPrime(int value) {
for (int i = 2; i < value - 1; i++) {
if ((value % i) == 0) {
return false;
}
}
return true;
}
public static boolean isEven(MyInteger myInteger) {
return (myInteger.getValue() & 1) == 0;
}
public static boolean isOdd(MyInteger myInteger) {
return (myInteger.getValue() & 1) != 0;
}
public static boolean isPrime(MyInteger myInteger) {
for (int i = 2; i < myInteger.getValue() - 1; i++) {
if ((myInteger.getValue() % i) == 0) {
return false;
}
}
return true;
}
public boolean equals(int value){
return true;
}
public boolean equals(MyInteger myInteger){
return this.value== myInteger.getValue();
}
public static int parseInt(char chars[]){
return Integer.valueOf(new String(chars));
}
public static int parseInt(String s){
return Integer.valueOf(s);
}
public static void main(String[] args) {
MyInteger m=new MyInteger(5);
char c[]={'1','2'};
String s="1542";
System.out.println(m.getValue());
System.out.println(m.isEven());
System.out.println(m.isOdd());
System.out.println(m.isPrime());
System.out.println(MyInteger.isEven(8));
System.out.println(MyInteger.isOdd(8));
System.out.println(MyInteger.isPrime(8));
System.out.println(MyInteger.isEven(m));
System.out.println(MyInteger.isOdd(m));
System.out.println(MyInteger.isPrime(m));
System.out.println(MyInteger.parseInt(s));
System.out.println(MyInteger.parseInt(c));
System.out.println(m.equals(5));
System.out.println(m.equals(m));
}
}
8.自定义String类(类与对象,构造函数)
package oupeng.week4.HomeWork;
import java.util.Arrays;
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[] sub=new char[end-begin];
for (int i = 0; i < sub.length; i++) {
sub[i]=chars[begin+i];
}
return new MyString(sub);
}
public MyString toLowerCase(){
char [] c=new char[this.chars.length];
for (int i = 0; i < c.length; i++) {
if (c[i]>=65&&c[i]<=90){
c[i]=(char)(this.chars[i]+32);
}
else {
c[i]=this.chars[i];
}
}
return new MyString(c);
}
// public boolean equals(MyString s){
// for (int i=0;i<chars.length;i++){
// if (chars[i]!=s.charAt(i)){
// return false;
// }
// }
// return true;
// }
// public static MyString valueOf(int i){
// String str=i+"";
// char [] c=new char[str.length()];
// for (int i1 = 0; i1 < c.length; i1++) {
// c[i]=str.charAt(i);
// }
// return new MyString(c);
// }
@Override
public String toString() {
return "MyString{" +
"chars=" + Arrays.toString(chars) +
'}';
}
public static void main(String[] args) {
char []c={'s','b','c'};
MyString s=new MyString(c);
String s1="ssss";
// System.out.println(s1.equals(s));
System.out.println(s.toLowerCase());
System.out.println(s.substring(0, 1));
}
}