Java SE 小白学习笔记 周周测 从小白到大牛

周测01

一:填空题,每空1分,共20分

  1:java的基本数据类型有(int)(byte)(short)(long)(double)(float)(Boolean)(char)
  2:请写出你接触过的关键字(public)(static)(void)(main)(for)(while)(if)
  3:标识符可以由三类东西组成,包括(大写字母)(小写字母)(数字)
  不能以(数字)开头,不能是(标识符),下划线,$除外.

二:简答题,每题4分

​ 1:请写出99的二进制,八进制,十六进制

​ 需要过程(截图或拍照放在文档中)

   2  	8 	10 	16
   0b 	0		0x                            

​ 2:请写出while,do while,for的语法格式,并说明while与do while的区别

    while(条件语句){
                       需要执行的代码块

}
     do{
                    需要执行的代码块

}while(条件语句)
      区别:do while 先执行一遍代码块在进行条件判断,表示必需执行一遍.

​ 3:计算下列表达式的结果,写出运算过程

​ int x=4,y=5;

​ int z=(x++)*(y–);

​ x,y,z的值分别是什么

    x=5 y=4 z=20

​ 4:给每一个基本数据类型定义一个变量,并附上一个合理的初始值。

 int a = 1; byte b = 1; short c = 1; long d = 1; double e = 22; float f = 1.0;
   boolean g = true; char h = ‘嘎’;

​ 5: 三目运算符的语法格式是什么?

​ 条件表达式?值1:值2;

三:编程题,每题6分,共60分

​ 1:使用if else完成下列功能

​ 评价一个快递员的素质根据送货量来计算,

​ 送货的总的区间是0—200,

​ 50以下打印最差,

​ 50–100打印及格,

​ 100–150之间打印中等,

​ 150–200之间打印优秀

import java.util.Scanner;
public class Demo03 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入今天的送货量:");
		int num = scanner.nextInt();
		
		if (num>=0 && num<=200) {
			if (num<50) {
				System.out.println("最差");
			}else if (num>=50 && num<100) {
				System.out.println("及格");
			}else if (num>=100 && num<150) {
				System.out.println("中等");
			}else if (num>=150 && num<=200) {
				System.out.println("优秀");
			}
		}else {
			System.out.println("输入有误???请重新输入!!!");
			main(args);
		}
	}
}

​ 2:使用switch来完成下列功能 (参考switch语句格式去写)

​ 定义一个人的年龄 年龄的值范围1—30

​ 3岁 打印 世界真奇妙

​ 6岁 打印 十万个为什么

​ 14岁 打印 我上中学了,认识了世界的美好

​ 20岁 打印 我上大学了,认识了世界的丑恶

​ 24 打印 我毕业了,慢慢认识到了真实的世界

import java.util.Scanner;
public class Demo04 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("欢迎来到人生轨迹选择系统");
		System.out.println("请输入你的年龄:");
		int age = scanner.nextInt();
		
		switch (age) {
		case 3:
			System.out.println("世界真奇妙");
			break;
		case 6:
			System.out.println("十万个为什么");
			break;
		case 14:
			System.out.println("我上中学了,认识了世界的美好");
			break;
		case 20:
			System.out.println("我上大学了,认识的世界的丑陋");
			break;
		case 24:
			System.out.println("我毕业了,慢慢认识到了真实的世界");
			break;
		default:
			System.out.println("你输入的年龄需要开通VIP才可以判断,请重新输入!!!");
			main(args);
			break;
		}
	}
}

4: 打印0~100数字的和

public class Demo05 {
	public static void main(String[] args) {
		int num =0;
		for (int i = 0; i <= 100; i++) {
			num +=i;
		}
		System.out.println(num);
	}
}

5:键盘录入两个数据,比较两个数据是否相等。

 import java.util.Scanner;
//键盘录入两个数据,比较两个数据是否相等。
public class Demo05 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入数据1:");
		String a = scanner.next();
		System.out.println("请输入数据2:");
		String b = scanner.next();
		if(a.equals(b)) {
			System.out.println("数据相等");
		}else {
			System.out.println("数据不相等");
		}
	}
}

6:键盘录入三个数据,获取三个数据中的最大值,分别使用三元运算符和if语句完成!

import java.util.Scanner;
//键盘录入三个数据,比较三个数据的最大值。
public class Demo05 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入数据1:");
		int a = scanner.nextInt();
		System.out.println("请输入数据2:");
		int b = scanner.nextInt();
		System.out.println("请输入数据3:");
		int c = scanner.nextInt();
		int m = a>b? a:b;
		int M = m>c? m:c;
		System.out.println("最大值为"+M);
		if(a>b) {
			if (a>c) {
				System.out.println(a);
			}else {
				System.out.println(c);
			}
		}else {
			if(b>c) {
				System.out.println(b);
			}else {
				System.out.println(c);
			}
		}
	}
}

7: 分别使用if语句和switch语句实现 :键盘录入一个数据,判断该月份的季节

​ 3,4,5月---->春季

​ 6,7,8月---->夏季

​ 9,10,11月—>秋季

​ 12,1,2---->冬季

import java.util.Scanner;
public class Season {
	public static void main(String[] args) {
		Scanner sc =  new Scanner(System.in);
		System.out.println("请输入月份:");
		int month = sc.nextInt();
		if (month>=3 && month<=4) {
			System.out.println("春季");
		}else if (month>=6 && month<=8) {
			System.out.println("夏季");
		}else if (month>=9 && month<=11) {
			System.out.println("秋季");
		}else if (month==2 || month==12 || month==1) {
			System.out.println("冬季");
		}else {
			System.out.println("没有该月份,错误!!!");
		}
		switch (month) {
		case 3:
		case 4:
		case 5:
			System.out.println("春季");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("夏季");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("秋季");
			break;
		case 12:
		case 1:
		case 2:
			System.out.println("冬季");
			break;
		default:
			break;
		}
	}
}

​ 8:使用for循环完成(for循环–)

​ 请在控制台输出满足如下条件的五位数

​ 个位等于万位

​ 十位等于千位

​ 个位+十位+千位+万位=百位

import java.util.Scanner;

public class Demo06 {
	public static void main(String[] args) {
		for(int i=10000;i<=99999;i++) {
			if (i%10==i/10000%10 && i/10%10==i/1000%10 && i%10+i/10%10+i/1000%10+i/10000%10 == i/100%10) {
				System.out.println(i);
			}
		}
	}
}

附加题:(根据个人情况,选择完成)

​ 1)使用for循环打印九九乘法表

public class Demo08 {
	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <=i; j++) {
				System.out.print(i+"*"+j+"="+i*j+"\t");
				
			}
			System.out.println();
		}
	}
}

​ 2). (for 循环)*“百钱买百鸡”是我国古代的著名数学题。题目这样描述:3 文

钱可以买1只公鸡,2 文钱可以买一只母鸡,1 文钱可以买3 只小鸡。用100 文

钱买100 只鸡,那么各有公鸡、母鸡、小鸡多少只?

public class Demo07 {
	public static void main(String[] args) {
		int i,j,j2;
		for( i = 1; i <= 33; i++) {
			for ( j = 0; j <= 50; j++) {
				for ( j2 = 1; j2 <= 100; j2++) {
					
					if(3*i+j*2+j2/3 ==100 && i+j+j2 == 100 && j2%3 == 0) {
						System.out.println("公鸡有:"+i+"只,母鸡有:"+j+"只,小鸡有:"+j2+"只");
					}
				}			
			}
//错误原因(1/3)=0.3333....因为数据类型为整形所(1/3)的实际值称为了0;所以答案错误
//			for (int j = 1; j <=50; j++) {
//				for (int j2 = 0; j2 < 300; j2++) {
//					if(3*i+j*2+j2*(1/3)==100 && i+j+j2 == 100 && j2%3 == 0) {
//						System.out.println("公鸡有:"+i+"只,母鸡有:"+j+"只,小鸡有:"+j2+"只");
//					}
//				}
//			}
		}
	}
}

周测02

一.简答题(每题5分,共30分)

1.简述成员变量和局部变量?

成员变量:类中方法外,常量区
局部变量:类中方法中,堆

2.简述普通方法和构造方法的区别。

构造方法没有返回值类型修饰符(没有返回值)
构造方法名必须是类名,大小写都要一致
普通方法:使用对象调用
构造方法:使用new关键字调用

3.简述Student s = new Student() ;完了哪些事情?

创建了一个Student类的对象,并在堆内存开辟了一片空间
给s对象附初始值
执行Student中无参构造函数
将栈中的引用s指向步骤1中的堆内存的地址

4.请简述静态变量和成员变量地区别


5.简述this关键字,它的作用是什么?

this区分一个类中的成员变量与局部变量重复时
在本类构造函数中调用本类其他构造函数,必须在构造函数的第一行,不能形成死循环

二.编程题(共70分)

1.编写程序,已知数组int[] arr = {43,65,3,6,76},分别对数组冒泡进行排序和遍历

public class Demo01 {
	
	public static void main(String[] args) {
		int[] arr = {43,65,3,6,76};
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length; j++) {
				if(arr[i]>arr[j]) {
					arr[i]^=arr[j];
					arr[j]^=arr[i];
					arr[i]^= arr[j];
				}
			}
		}
		
		for (int i : arr) {
			System.out.println(i);
		}
	}
}

2.将下面的需求写完后进行测试!
编写一个矩形类Rect,包含:
矩形的宽width;矩形的高height。
两个构造方法:
一个带有两个参数的构造方法,用于将width和height属性初化;
一个不带参数的构造方法,将矩形初始化为宽和高都为10。
两个方法:
求矩形面积的方法area()
求矩形周长的方法perimeter()

public class Rect {

	private int width = 10;
	private int height = 10;
	
	public Rect() {
		
	}
	public Rect(int width , int height) {
		this.height = height;
		this.width = width;
	}
	
	public void area() {
		int area = (height+width)*2;
		System.out.println("矩形的周长为:"+area);
	}
	public void perimeter() {
		int perimeter = height*width;
		System.out.println("矩形的面积为:"+perimeter);
	}
}

3:定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,
​ 2个构造器Point()和Point(int x0,int y0),
​ 以及一个movePoint(int dx,int dy)方法实现点的位置移动,
​ 创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标

//测试类
public class Test {

	public static void main(String[] args) {
		Point p1 = new Point();
		p1.movePoint(2, 4);
		Point p2 = new Point(3, 5);
		p2.movePoint(1, 1);
	}
}

//point类
public class Point {
	private int x0;
	private int y0;
	
	public Point() {
		
	}
	public Point(int x0,int y0) {
		this.x0 = x0;
		this.y0 = y0;
	}
	
	public void movePoint(int dx,int dy) {
		System.out.println("移动前的坐标为:("+x0+","+y0+")"+"移动后的坐标为:("+(x0+dx)+","+(y0+dy)+")");
	}
}

4:定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。
​ 1) 无参和有参的两个构造方法;
​ 有参构造方法可以在创建对象的同时为每个属性赋值;
​ 2) 输出笔记本信息的方法
​ 3) 然后编写一个测试类,测试笔记本类的各个方法

public class Test {

	public static void main(String[] args) {
		Note note = new Note('黄',188);
		note.print();
		Note note2 = new Note();
		note2.print();//char为null,int为0;
	}
}

//笔记本类
public class Note {

	private char colour;
	private int cpu;
	
	public Note() {
		
	}
	public Note(char colour , int cpu) {
		this.colour = colour;
		this.cpu = cpu;
	}
	
	public void print() {
		System.out.println("该笔记本的颜色为:"+colour+",型号为:"+cpu);
	}
}

5:封装一个Person类
​ 属性:name sex age
​ 完成对每个属性的get和set方法
​ 静态属性 guoJi 显示"cn"
​ 再完成一个静态方法 获取国籍
​ 使用标准类的写法完成上面的需求并测试

6:定义两个类,描述如下:
​ 1)定义一个人类Person:
​ 1.1)定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
​ 1.2)有三个属性:名字、身高、年龄
​ 2)定义一个PersonCreate类:
​ 2.1)创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
​ 2.2)分别调用对象的sayHello()方法。

public class PersonCreate {

	public static void main(String[] args) {
		Person p1 = new Person("zhangsan", 33, 1.73);
		Person p2 = new Person("lishi", 44, 1.74);
		p1.sayHello();
		p2.sayHello();
	}
}

public class Person {

	private String name;
	private int age;
	private double height;
	
	public Person() {
		super();
	}

	public Person(String name, int age, double height) {
		super();
		this.name = name;
		this.age = age;
		this.height = height;
	}
	
	public void sayHello() {
		System.out.println("hello,my name is "+name);
	}
}

7.(先思考,后面讲完继承将这个类,可以不做,涉及到形参是类的情况以及继承)
设计一个台灯类(Lamp)其中台灯有灯泡类(Buble)这个属性,还有开灯(on)这个方法。
设计一个灯泡类(Buble),灯泡类有发亮的方法,其中有红灯泡类(RedBuble)和绿灯泡类(GreenBuble)
他们都继承灯泡类(Buble)一个发亮的方法。

package weekend02;

public class Buble {

	public void FaLiang() {
		System.out.println("发亮方法");
	}
}

public class Lamp {
	private Buble buble;
	
	public Lamp() {
		
	}
	public Lamp(Buble buble) {
		this.buble = buble;
	}
	
	public void on() {
		System.out.println("开关方法");
	}
}

public class RedBuble extends Buble {
	
	public void FaLiang() {
		System.out.println("发红光");
	}

}

public class GreenBuble extends Buble{

	public void FaLiang() {
		System.out.println("发绿光");
	}
}


//测试类
public class Test {
	public static void main(String[] args) {
		Buble buble = new Buble();
		Lamp lamp = new Lamp(buble);
		lamp.on();
		
		RedBuble redBuble = new RedBuble();
		redBuble.FaLiang();
		
		GreenBuble greenBuble = new GreenBuble();
		greenBuble.FaLiang();
	}
}
附加题(选做题)

​ .假如我们在开发一个系统时需要对员工类类进行设计,
​ 程序员是员工,包含3个属性:姓名、工号以及工资。
​ 经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。
​ 请使用继承的思想设计出程序员类和经理类。要求类中提供必要的方法进行属性访问。

package weekend02;

public class Staff {

	private String name;
	private int num;
	private double money;
	
	public Staff() {
		super();
	}

	public Staff(String name, int num, double money) {
		super();
		this.name = name;
		this.num = num;
		this.money = money;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public double getMoney() {
		return money;
	}

	public void setMoney(double money) {
		this.money = money;
	}	
}


public class Manager extends Staff{
	private double bonus;
	
	public Manager() {
		super();
	}
	public Manager(String name,int num , double money,double bonus) {
		super(name, num ,money);
		this.bonus = bonus;
	}
	public double getBonus() {
		return bonus;
	}
	public void setBonus(double bonus) {
		this.bonus = bonus;
	}
}


public class Programmer extends Staff{
	public Programmer() {
		super();
	}
	public Programmer(String name,int num,double money) {
		super(name, num, money);
	}
}

周测03

一:简答题(每题4分)

​ 1:请描述下static,fianl,abstract可以修饰什么?有什么用?

Static:静态的 可以用来修饰属性 类 方法
		静态方法中不能直接使用非静态成员
		静态代码块在类加载是被JVM调用
Fianl:最终的 可以用来修饰类属性(修饰完不能重写)
		方法不能被重写
		类不能被继承
Abstract:抽象的 可以用来修饰类 方法 (抽象方法必须是抽象类 抽象类不一定有抽象方法)
		

​ 2:分别描述重写与重载?

重写:指的是重写该父类的方法,语法格式与该方法一致,访问权限修饰符,返回值类型,形参列表都一致,方法体不同

重载:除方法名以外可以重新定义该方法的返回值类型,形参列表,方法体

​ 3:局部变量和成员变量的区别?

成员变量:又名属性,类中方法外,一个类的成员变量随着对象的创建而创建,随着对象的消失而消失(没有访问权限修饰符,不能使用static修饰)

局部变量:类中,方法中,一个类的成员变量随着所在方法的调用而创建,随着方法的消失而消失(作用域:取决于访问权限修饰符)

​ 4:简述成员内部类,静态内部类,局部内部类,匿名内部类如何创建对象?

外部类类名.内部类类名 对象名 = 外部类对象.new 内部类类名(实参列表);
外部类类名.内部类类名 对象名 = new 外部类类名.内部类类名(实参列表);
内部类类名 对象名 = new 内部类类名();
父类名/父接口名 对象名 = new 父类名/父接口名(){属性 方法 方法体} (没有构造函数)

​ 5:成员变量和静态变量有什么区别?

  成员变量属于该类的特定对象(不能在静态方法或静态代码块使用),静态变量属于该类的所有对象

​ 6:抽象类和接口的区别?

    抽象类只能继承一个抽象类
    	可以有属性,方法,构造函数,代码块,静态代码块...
    接口可以继承多个接口,可以被多个类实现
    	只能有公共静态常量,公共静态方法,公共抽象方法,default修饰的方法,

二:编程题

​ 1:需求:鸟和飞机都能飞行,使用代码写一个方法完成鸟的子类比如鹰,麻雀的飞行,还有飞机的飞行(10分)

​ 提示:写一个飞的接口

​ 写一个父类鸟,实现接口飞

​ 写具体的鸟的子类,类继承鸟类

​ 写一个飞机的类,实现飞的接口

public interface Fly {
	void fly();
}
public class Bird implements Fly{
	private String name;
	private String sex;
	private int age;
	
	public Bird() {
		super();
	}
	
	public Bird(String name, String sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Bird [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	@Override
	public void fly() {	
	}
}
public class LaoYing extends Bird{
	public void fly() {
		System.out.println("老鹰飞");
	}
}
public class MaQue extends Bird{
	public void fly() {
		System.out.println("麻雀飞");
	}
}
public class Plane implements Fly{
	@Override
	public void fly() {
		System.out.println("飞机飞");
	}
}

2.请简述冒泡思想,用代码实现排序并遍历元素,要求格式[元素1,元素2,元素3…]
相邻两元素进行两两比较

public class MaoPao {
	public static void main(String[] args) {
		int[] i = {2,41,25,26,21,1,3,36};
		for (int j = 0; j < i.length-1; j++) {
			for (int j2 = 0; j2 < i.length-1; j2++) {
				if(i[j2] > i[j2+1]) {
					int m = 0;
					m=i[j2+1];
					i[j2+1]=i[j2];
					i[j2]=m;
				}
			}
		}
		for (int j : i) {
			System.out.println(j);
		}
       //要特定格式 
        String s = Arrays.toString(i);
        System.out.println(i);
	}
}

3.请将int类型数组 int[] = {11,22,33,44,55}; 拼接成字符串 (使用方法调用改进)(10分)

public class ShuZhu {
	
	public static void main(String[] args) {
		int[] num = {11,22,33,44,55};
		a(num);
	}
	public static void a(int[] num) {
		StringBuilder sb = new StringBuilder();
		for (int i : num) {
			sb.append(i+"");
		}
		System.out.println(sb.toString());
	}
}

4.给数组中存入Dog对象(15分)

​ 小黑,3,黑

​ 小白,2,白

​ 小白,1,花

​ 小白,1,花

public class Dog {
	private String name;
	private String sex;
	private int age;
	public Dog() {
		super();
	}
	public Dog(String name, String sex, int age) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Dog [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
	
}
public class Test {
	public static void main(String[] args) {

		Dog dog1 = new Dog("小黑", "黑", 3);
		Dog dog2 = new Dog("小白", "白", 2);
		Dog dog3 = new Dog("小白", "白", 1);
		Dog dog4 = new Dog("小白", "花", 1);
		Dog[] dogs = {dog1,dog2,dog3,dog4};
		for (Dog dog : dogs) {
			System.out.println(dog);
		}
	}
}

5.利用接口做参数,写个计算器,能完成加减乘除运算。

​ (1)定义一个接口Compute含有一个方法int computer(int n, int m)。

​ (2)设计四个类分别实现此接口,完成加减乘除运算。

​ (3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。

​ (4)设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。(15分)

public class Test {
	public static void main(String[] args) {		
		Add add = new Add();
		Subtract subtract = new Subtract();
		Multiply multiply = new Multiply();
		Divide divide = new Divide();
		UseCompute useCompute = new UseCompute();
		useCompute.useCom(add, 1, 2);
		useCompute.useCom(subtract, 6, 2);
		useCompute.useCom(multiply, 3, 2);
		useCompute.useCom(divide, 5, 0);
	}
}
public interface Computer {
	int computer(int a, int b);
}
public class Add implements Computer{
	@Override
	public int computer(int a, int b) {
		int c = a+b;
		return c;
	}
}	
public class Subtract implements Computer{
	@Override
	public int computer(int a, int b) {
		int c = a - b;
		return c;
	}
}
public class Multiply implements Computer{
	@Override
	public int computer(int a, int b) {
		int c = a*b;
		return c;
	}
}

public class Divide implements Computer {
	@Override
	public int computer(int a, int b) {
		if (b != 0) {
			int c = a / b;
			return c;
		}
		return -1;
	}
}

public class UseCompute {
	//private Computer com;
	public void useCom(Computer com, int one, int two) {
		int i = com.computer(one, two);
		System.out.println(i);
	}
}

6.笔记本电脑(laptop)通常具备使用USB设备的功能。

​ 在生产时,笔记本都预留了可以插入USB设备的USB接口, 但具体是什么USB设备,笔记本厂商并不关心,

​ 只要符合USB规格的设备都可以。

​ 定义USB接口,具备最基本的开启功能和关闭功能。

​ 鼠标和键盘要想能在电脑上使用,那么鼠标和键盘也必须遵守 USB规范,实现USB接口,

​ 否则鼠标和键盘的生产出来也无法使用。(使用面向对象思想编程:接口,多态等)(16分)

public class Test {
	public static void main(String[] args) {
		Mouse mouse = new Mouse();
		Laptop laptop = new Laptop();
		laptop.setUsb01(mouse);
		laptop.open();
		
	}
}
public class Laptop {
	private USb usb01;
	private USb usb02;
	private boolean isOpen = false;
	
	public  void setUsb01(USb usb01) {
		if(this.usb01 != null && this.isOpen) {
			this.usb01.close();
		}
		
		this.usb01 = usb01;
		if(this.isOpen) {
			this.usb01.conn();
		}
	}
	
	public  void setUsb02(USb usb02) {
		if(this.usb02 != null && this.isOpen) {
			this.usb01.close();
		}
		
		this.usb02 = usb02;
		if(this.isOpen) {
			this.usb02.conn();
		}
	}
	
	public void open() {
		System.out.println("准备开机中");
		if (this.usb01 != null) {
			this.usb01.conn();
		}

		if (this.usb02 != null) {
			this.usb02.conn();
		}

		this.isOpen = true;
		System.out.println("开机成功");
	}
	public void off() {
		System.out.println("准备关机中");
		if (this.usb01 != null) {
			this.usb01.close();
		}

		if (this.usb02 != null) {
			this.usb02.close();
		}

		this.isOpen = true;
		System.out.println("关机成功");
	}
}
public interface USb {
	void conn();
	void close();
}
public class Mouse implements USb{
	public void conn() {
		System.out.println("鼠标连接成功");
	}
	public void close() {
		System.out.println("鼠标断开连接");
	}
}
public class Key implements USb{
	@Override
	public void conn() {
		System.out.println("键盘连接成功");
	}
	@Override
	public void close() {
		System.out.println("键盘断开连接");
	}
}

三: 附加题

​ 7:创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。

​ 创建类Student实现Person接口,并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息

public class Test {
	public static void main(String[] args) {
		Student student = new Student();
		Student.Students studens = student.new Students();
		studens.setStudents("12134564", "计算机科学与技术");
		student.setStudent(studens);
		student.setDate("张强", "女", "10月8日");
		
		System.out.println(student.toString());
	}
}
package weekend03;

public class Student implements Person{
	private String name;
	private String sex;
	private String brithday;
	private Students students;
	
	public class Students{
		String sID;
		String speciality;
		public void setStudents(String sID,	String speciality) {
			this.sID = sID;
			this.speciality = speciality;
		}
		public String getStudents() {
			return "sID:"+sID+ "  专业:"+speciality;
		}
		
	}
	public void setStudent(Students students) {
		this.students = students;
	}
	public void setDate(String name, String sex, String brithday) {
		this.name = name;
		this.sex = sex;
		this.brithday = brithday;
	}

	public String getDate() {
		return name + sex + brithday;
	}

	@Override
	public String toString() {
		String i = students.getStudents();
		return "Student [name=" + name + ", sex=" + sex + ", 
				brithday=" + brithday+","+name+"的学校信息=" + "["+i+"]" + "]";
	}

}
public interface Person {
	void setDate(String name,String sex,String brithday);
	String getDate();
}


//方法二:(一方法思路不清复杂化题目)
public class Test {
	public static void main(String[] args) {
		Student student = new Student();
		student.setStudent("12134564", "计算机科学与技术");
		student.setDate("张强", "女", "10月8日");
		
		System.out.println(student.toString());
	}
}

public class Student implements Person{
	private String name;
	private String sex;
	private String brithday;
	private String sID;
	private String speciality;
	public void setDate(String name, String sex, String brithday) {
		this.name = name;
		this.sex = sex;
		this.brithday = brithday;
	}
	public String getDate() {
		return name + sex + brithday;
	}
	public void setStudent(String sID,	String speciality) {
		this.sID = sID;
		this.speciality = speciality;
	}
	public String getStudent() {
		return sID + speciality;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", brithday=" + brithday+",sID="+sID+",speciality="+speciality+"]";
	}
}
public interface Person {
	void setDate(String name,String sex,String brithday);
	String getDate();
}

​ 8:定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(),

​ 然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法calculateArea(),

​ 分别计算圆和矩形的面积

package weekend03;

public class MyCircle implements Area{
	@Override
	public double calculateArea(double π, double r) {	
		double c = π*r*r;
		return c;
	}
}
public class MyRectangle implements Area{
	@Override
	public double calculateArea(double i, double m) {
		double c = i*m;
		return c;
	}
}
public interface Area {
	double calculateArea(double i,double m);
}
public class Test {
	public static void main(String[] args) {
		MyCircle myCircle = new MyCircle();
		double d = myCircle.calculateArea(3.14, 5);
		System.out.println(d);	
	}
}

​ 9.创建一个名称为 Vehicle类并将它声明为抽象类,在Vehicle类中声明一个NoOfWheels方法,使它返回一个字符串值.

​ 创建两个类,Car类和Motorbike类 继承自 Vehicle类,并在这两个类中实现 NoOfWheels方法.在Car类中,应该显示

​ "四轮车"信息;而在Motorbike类中,应当显示"双轮车"信息.创建一个带main方法的类,在该类中创建 Car和Motorbike的实例,

​ 并在控制台显示信息!

public class Test {
	public static void main(String[] args) {
		Car car = new Car();
		Motorbike motorbike = new Motorbike();
		System.out.println(motorbike.NoOfWheels());
		System.out.println(car.NoOfWheels());
	}
}
public abstract class Vehicle {
	public abstract String NoOfWheels();
}
public class Car extends Vehicle{

	@Override
	public String NoOfWheels() {
		// TODO Auto-generated method stub
		return "四轮车";
	}
}
public class Motorbike extends Vehicle{

	@Override
	public String NoOfWheels() {
		// TODO Auto-generated method stub
		return "双轮车";
	}
}

周测04

一: 简答题

1.String和StringBuffer的区别?

String:不可变字符串,会存储在常量区多的时候会卡顿
StringBuffer:可变字符串,在堆内存

2.static与final的区别

Static:静态的
	该修饰符修饰的方法直接可以用类名.调用
	修饰的属性属于所有对象
Final:最终的
	修饰的方法不能被继承
	修饰的属性不可再更改要赋初值

3.使用==和equals方法比较两个字符串的区别

==比较两字符串内存地址
Equals比较两字符串内容

4.字符串常量相加和字符串变量相加有什么区别?

字符串常量相加是拼接,

5.接口和抽象类的区别?

接口:可以继承多个接口,里面可以定义抽象方法,属性有默认修饰
抽象类:可以实现接口,可以继承类,可以定义抽象方法

6.分析代码

interface IA { 
	void m1(); 
	public void m2(); 
	public abstract void m3(); 
} 
abstract class Super{} 
class Sub1 extends Super{} 
class Sub2 extends Super{} 
public class TestInterface{ 
	public static void main(String args[]){ 
		Super sup = new Sub1(); 
		Sub1 sub1 = (Sub1)sup; 
		//1 
	} 
} 
	在//1 处可以编译(不考虑运行时是否会产生异常)通过的代码为:A
	A. Sub2 sub2 = (Sub2) sup; 
	B. Sub2 sub2 = (Sub2) sub1; 
	C. IA ia = (IA) sup; 
	D. IA ia = (IA) sub1;

7.有下列代码:
interface ServiceInterface{
void doService1();
void doService2();
void doService3();
}
abstract class AbstractService implements ServiceInterface{
public void doService1(){}
public void doService2(){}
public void doService3(){}
}
需要一个实现 ServiceInterface 接口的类 MyService
第一种方式可以让 MyService 实现 ServiceInterface 接口,即:class MyService implements ServiceInterface
第二种方式可以让 MyService 继承 AbstractService 类,即 class MyService extends AbstractService
请问:这两种方式有什么区别?

MyService 直接实现 ServiceInterface 接口,重写该接口的方法
MyService 继承 AbstractService 类,不仅可以重写抽象方法,还可以继承AbstractService独有的方法和属性

8.写出下面代码的运行结果

interface IA{ 
	void ma(IB ib); 
} 
interface IB{ 
	void mb(); 
} 
class IAImpl implements IA{ 
	public void ma(IB ib){ 
		System.out.println(“ma in IAImpl); 
		ib.mb(); 
	} 
} 
class IBImpl implements IB{ 
	private IA ia; 
	public void setIa(IA ia){ 
		this.ia = ia; 
	} 
	public void mb(){ 
		System.out.println(“mb in IBImpl); 
	} 
	public void method(){ 
		ia.ma(this); 
	}	 
} 
public class TestMain{ 
	public static void main(String args[]){ 
		IA ia = new IAImpl(); 
		IBImpl ib = new IBImpl(); 
		ib.setIa(ia); 
		ib.method(); 
	} 
}:
接口回调:控制台输出   ma in IAImpl

二: 编程题

  1. 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
    举例:
    “Hello123World”

示:统计字符定义三个统计变量,分别代表大写字母字符,小写字母字符,数字字符

public class Demo01 {
	public static void main(String[] args) {
		String a = "Hello123World";
		char[] cs = a.toCharArray();
		int m=0;
		int M=0;
		int s=0;
		for (char c : cs) {
			int b = (int)c;
			if (b>48 && b<58) {
				s++;
			}else if (b>64 && b<91) {
				M++;
			}else if (b>96 && b<123) {
				m++;
			}
		}
		System.out.println("大写字母"+M+"次,小写字母"+m+"次,数字"+s+"次");
	}
}								

2)使用TreeSet存储以下个学生对象,要求如果对象属性一致,认为数据存在,不在存储
学生对象1:张三 21 男
学生对象2:李四 26 男
学生对象3:小红 19 女
学生对象4:马六 22 男
学生对象5:小白 18 女
学生对象6:张三 21 男

public class Test {
	public static void main(String[] args) {
		Student s1 = new Student("李四", 26, "男");
		Student s2 = new Student("小红", 19, "女");
		Student s3 = new Student("张三", 21, "男");
		Student s4 = new Student("马六", 22, "男");
		Student s5 = new Student("小白", 18, "男");
		Student s6 = new Student("张三", 21, "男");
		TreeSet<Student> set = new TreeSet<Student>();
		set.add(s1);
		set.add(s2);
		set.add(s3);
		set.add(s4);
		set.add(s5);
		set.add(s6);
		System.out.println(set);
	}
}
public class Student implements Comparable<Student>{
	private String name;
	private int age ;
	private String sex;
	
	public Student() {
		super();
	}

	public Student(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}

	@Override
	public int compareTo(Student o) {
		if (o.age < this.age) {
			return -1;
		}else if ( o.age == this.age && o.name.equals(this.name) && o.sex.equals(this.sex)) {
			return 0;
		}
		return 1;
	}
}

​ 3)有5个学生,请使用集合将5个学生进行存储,并按照年龄进行排序.遍历将5个学生信息一一输出
​ 学生对象1:张三 21 男
​ 学生对象2:李四 26 男
​ 学生对象3:小红 19 女
​ 学生对象4:马六 22 男
​ 学生对象5:小白 18 女

public class Test02 {
	public static void main(String[] args) {
		Student s1 = new Student("李四", 26, "男");
		Student s2 = new Student("小红", 19, "女");
		Student s3 = new Student("张三", 21, "男");
		Student s4 = new Student("马六", 22, "男");
		Student s5 = new Student("小白", 18, "男");
		TreeSet<Student> set = new TreeSet<Student>();
		set.add(s1);
		set.add(s2);
		set.add(s3);
		set.add(s4);
		set.add(s5);
		for (Student student : set) {
			System.out.println(student.toString());
		}
	}
}
public class Student implements Comparable<Student>{
	private String name;
	private int age ;
	private String sex;
	
	public Student() {
		super();
	}

	public Student(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	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 String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}

	@Override
	public int compareTo(Student o) {
		if (o.age < this.age) {
			return -1;
		}else {
			return 1;
		}	
	}
}

4)设计程序
定义一个人类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉,工作。
1)根据人类,编写子类-学生类,
增加属性:学校、学号;重写工作方法(学生的工作是学习)
2)根据人类,编写子类-工人类,增加属性:单位、工龄;重写工作方法(工人的工作是搬砖)
3)根据学生类,编写子类-学生干部类,增加属性:职务;增加方法:开会
4)编写主函数分别对上述3类具体人物进行测试

public class Person {

	private String name;
	private String sex;
	private int age ;
	private String guoJi;
	
	public Person() {
		super();
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", sex=" + sex + ", age=" + age + ", guoJi=" + guoJi + "]";
	}
	public Person(String name, String sex, int age, String guoJi) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.guoJi = guoJi;
	}
	public void eat() {
		System.out.println("吃");
	}
	public void sleep() {
		System.out.println("睡");
	}
	public void work() {
		System.out.println("人工作");
	}

}
//学生类
public class Students extends Person{
	private String school;
	private String id;
	
	public Students() {
		super();
	}
	
	public Students(String school, String id) {
		super();
		this.school = school;
		this.id = id;
	}
	
	public String getSchool() {
		return school;
	}
	
	public void setSchool(String school) {
		this.school = school;
	}
	
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	@Override
	public void work() {
		// TODO Auto-generated method stub
		System.out.println("xuexi");
	}
}
//工人类
public class Woreker extends Person{
	private String danWei;
	private int gongLing;
	
	public Woreker() {
		super();
	}
	public Woreker(String danWei, int gongLing) {
		super();
		this.danWei = danWei;
		this.gongLing = gongLing;
	}
	
	public String getDanWei() {
		return danWei;
	}
	public void setDanWei(String danWei) {
		this.danWei = danWei;
	}
	public int getGongLing() {
		return gongLing;
	}
	public void setGongLing(int gongLing) {
		this.gongLing = gongLing;
	}
	@Override
	public void work() {
		System.out.println("搬砖");
	}
}
//学生干部类
public class StudentPlus extends Students{
	private String zhiWu;
	public StudentPlus(String zhiWu) {
		super();
		this.zhiWu = zhiWu;
	}
	
	public String getZhiWu() {
		return zhiWu;
	}
	
	public void setZhiWu(String zhiWu) {
		this.zhiWu = zhiWu;
	}
	
	public void kaiHui() {
		System.out.println("开会");
	}

}
public class Test03 {
	public static void main(String[] args) {
		StudentPlus sPlus = new StudentPlus("班长");
		sPlus.setSchool("小学");
		sPlus.setId("12313");
		Woreker woreker = new Woreker("单位",3);
	}
}

5)模拟用户登录(3次机会),当用户输入用户名和密码和已知的用户名密码一致,判断用户登录是否成功,
如果登录成功,进入猜数字游戏;
如果登录失败,给用户一种其他提示信息
猜数游戏:
系统随机获取一个0~10之间的数
提示用户输入一个数
判断用户输入的数与系统随机获取的数是否一致,如果一致显示中奖,否则谢谢惠顾

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Login {
  	public static void main(String[] args) {
   		User u1 = new User("123", "123");
   		User u2 = new User("123", "123");
   		User u3 = new User();
   		ArrayList<User> list = new ArrayList<User>();
   		list.add(u1);
   		list.add(u2);
   		Scanner scanner = new Scanner(System.in);
   		System.out.println("欢迎你,请输入你的账号");
   		String m = scanner.next();
   		u3.setName(m);
   		System.out.println("欢迎你,请输入你的密码");
   		String s = scanner.next();
   		u3.setId(s);
   		for (User user : list) {
   			if (u3.getId().equals(user.getId()) && u3.getName().equals(user.getName())) {
   				System.out.println("登录成功进入猜字游戏");
   				Random random = new Random();
   				int nn = random.nextInt(11);
   				System.out.println("请输入你的数字(0~11)");
   				int mm = scanner.nextInt();
   				if (nn == mm ) {
   					System.out.println("恭喜你中奖");
   				}else {
   					System.out.println("谢谢惠顾");
   				}	
   			}else {
   				System.out.println("密码错误退出");
   			}
   		}
   	}
}
   
public class User {
 	private String name;
 	private String id;

   	public User() {
   		super();
	}
   	public User(String name, String id) {
   		super();
   		this.name = name;
   		this.id = id;
   	}
  	public String getName() {
   		return name;
   	}
   	public void setName(String name) {
   		this.name = name;
   	}
   	public String getId() {
   		return id;
   	}
   	public void setId(String id) {
   		this.id = id;
   	}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值