韩顺平JAVA-D24

0244 对象创建流程详解

面试题

0245 引出this

info里面输出的age和name是属性而不是局部变量,构造器里面相当于把局部变量自己给自己赋值了两遍。

既希望关键字和属性一样,有希望可以给属性赋值。

0246 this入门

0247 this本质

0248 this小结

0249 this使用细节

传统方法使用就近原则,方法里有name就不输出属性了,使用this.xx输出的一定是属性。

0250 this课堂练习

public class TestPerson{
	public static void main(String[] args){
		Person p1 = new Person("李佳诺", 23);
		Person p2 = new Person("李佳诺", 23);
		System.out.println("p1和p2身份信息比较结果:" + p1.compareTo(p2));
	}
}

class Person{
	String name;
	int age;
	public Person(String name, int age){
		this.name = name;
		this.age = age;
	}
	public boolean compareTo(Person p){
		/*if(this.name.equals(p.name) && this.age.equals(p.age)){
			return true;
		}else{
			return false;
		}*/
		return this.name.equals(p.name) && this.age == p.age; 

	}
}

0251 - 0261 本章作业

第一题:

public class Homework01{
	public static void main(String[] args){
		A01 a1 = new A01();
		double arr[] = {2.0, 3.0, 4.0, 5.0};
		//double m = a1.max({2.0, 3.0, 4.0, 5.0});
		//System.out.println("数组的最大值为:" + m);
		//System.out.println("数组的最大值为:" + a1.max(arr[]));
		System.out.println("数组的最大值为:" + a1.max(arr));
	}
}
//类A01,方法max,求某个double数组的最大值,并返回
class A01{
	/*double arr[];
	public A01(double arr[]){
		this.arr = arr;
	}
*/
	public double max(double arr[]){
		double max = 0;
		for(int i = 0; i < arr.length; i++){
			if(arr[i] > max){
				max = arr[i];
			}

		}
		return max;
	}
}

第二题:

public class Homework02{
	public static void main(String[] args){
		String[] a = {"豆豆","咪咪","爸爸","妈妈"};
		String findStr = "豆豆";
		A02 a2 = new A02();
		int index = a2.find(a, findStr);
		if(index != -1){
			System.out.println("已找到" + findStr + "下标为" + index);
		}else{
			System.out.println("未找到");
		}
	}
}

//类A02,实现方法find,查找某字符串数组中的元素,返回索引,找不到返回-1
class A02{
	public int find(String[] a, String findStr){
		for (int i = 0; i < a.length; i++ ) {
			 if(a[i].equals(findStr)){
			 	return i;
			 }
		}
		return -1;
	}
}

第三题:

public class Homework03{
	public static void main(String[] args){
		Book book = new Book();
		int upprice = book.updatePrice(155);
		if(upprice == -1){
			System.out.println("价格输入有误");
		}else{
			System.out.println("修改后的价格为:" + upprice);
		}
	}
}
//编写类Book,方法updatePrice,实现更改某本书的价格
//若价格>150,则改为150;若价格>100,则改为100;否则不变

class Book{
	public int updatePrice(int price){
		if(price > 150){
			return 150;
		}else if(price <=150 && price > 100){
			return 100;
		}else if(price < 0){
			return -1;
		}else{
			return price;
		}
	}
}

public class Homework03{
	public static void main(String[] args){
		Book book = new Book("三国演义",160);
		book.info();
		book.updatePrice();
		book.info();
		//upprice = book.updatePrice(155);
		/*if(upprice == -1){
			System.out.println("价格输入有误");
		}else{
			System.out.println("修改后的价格为:" + upprice);
		}*/
	}
}
//编写类Book,方法updatePrice,实现更改某本书的价格
//若价格>150,则改为150;若价格>100,则改为100;否则不变

class Book{
	/*public int updatePrice(int price){
		if(price > 150){
			return 150;
		}else if(price <=150 && price > 100){
			return 100;
		}else if(price < 0){
			return -1;
		}else{
			return price;
		}
	}*/
	String name;
	double price;

	public Book(String name, double price){
		this.name = name;
		this.price = price;
	}

	public void updatePrice(){
		if(this.price > 150){
			this.price = 150;
		}else if(this.price > 100){
			this.price = 100;
		}
	}

	public void info(){
		System.out.println("书名为:" + this.name + "价格为:" + this.price);
	}
}

第四题:

public class Homework04{
	public static void main(String[] args){
		String[] arr = {"豆豆","咪咪","妈妈","爸爸"};
		A03 a1 = new A03(arr);
		String[] b1 = a1.copyArr();
		for(int i = 0; i < b1.length; i++){
			System.out.print(b1[i] + " ");
		}
	}
}

//编写类A03,实现数组复制功能copyArr,输入旧数组返回新数组,元素和旧数组一样
class A03{
	String[] arr;
	public A03(String[] arr){
		this.arr = arr; 
	}
	public String[] copyArr(){
		String[] b = new String[arr.length];
		for(int i = 0; i < arr.length; i++){
			b[i] = arr[i];
		}
		return b;
	}
}

第五题:

//5.定义圆类,属性半径,方法显示周长,面积
public class Homework05{
	public static void main(String[] args){
		Circle c1 = new Circle(6.6);
		c1.cir();
		c1.area();
		//double c = c1.cir();
		//ouble s = c1.area();
		//System.out.println("半径为" + r + "的圆周长为" + c);
		//System.out.println("半径为" + r + "的圆面积为" + s);
	}
}

class Circle{
	double r;
	public Circle(double r){
		this.r = r;
	}

	public void cir(){
		double c = Math.PI * r * 2;
		System.out.println("半径为" + r + "的圆周长为" + c);
	}

	public void area(){
		double s = Math.PI * r * r;
		System.out.println("半径为" + r + "的圆面积为" + s);
	}
}

第六题:

public class Homework06{
	public static void main(String[] args){
		Cale c = new Cale(8.8, 8);
		System.out.println("两数之和为:" + c.add());
		System.out.println("两数之差为:" + c.sub());
		System.out.println("两数之积为:" + c.mul());
		if(c.div() == null){
			System.out.println("除数为0");
		}else{
			System.out.println("两数之商为:" + c.div());
		}
	}
}

//6.Cale计算类,定义两个变量表示两个操作
//定义四个方法实现和差乘商(除数为零的话要提示)
//创建两个对象测试
class Cale{
	double num1;
	double num2;

	public Cale(double num1, double num2){
		this.num1= num1;
		this.num2 = num2;
	}

	public double add(){
		return (num1 + num2);
	}

	public double sub(){
		return (num1 - num2);
	}

	public double mul(){
		return (num1 * num2);
	}

	public Double div(){
		if(num2 != 0){
			return (num1 / num2);
		}else{
			return null;
		}
	}
}

第七题:

public class Homework07{
	public static void main(String[] args){
		Dog dog = new Dog("追风","黄色",6);
		dog.show();
	}
}

//设计一个Dog类,有名字,颜色和年龄属性
//定义输出方法show()显示其信息
//创建对象进行测试[this.属性]
class Dog{
	String name;
	String color;
	int age;

	public Dog(String name,String color,int age){
		this.name = name;
		this.color = color;
		this.age = age;
	}

	public void show(){
		System.out.println("小狗的名字为" + this.name);
		System.out.println("小狗的颜色为" + this.color);
		System.out.println("小狗的年龄为" + this.age);
	}
}

第八题:

10

9

10

第九题:

public class Homework09{
	public static void main(String[] args){
		Music m = new Music("月亮之上", 66);
		m.play();
		m.getInfo();
	}
}
//9.定义Music类,有音乐名name,音乐时常times属性
//播放play功能,返回本身信息功能getInfo功能
class Music{
	String name;
	double time;
	public Music(String name, double time){
		this.name = name;
		this.time = time;
	}
	public void play(){
		System.out.println("歌曲" + name + "正在播放中... 时常" + time + "秒");
	}
	public String getInfo(){
		return "歌曲" + name + "时常" + time + "秒";
	}

}

第十题:

i = 101

j = 100

i = 101

第十一题:

public double method(double n1, double n2){...}

第十二题:

public class Homework012{
	public static void main(String[] args){
		
	}
}

class employee{
	String name;
	char gender;
	int age;
	String job;
	double salary;

	public employee(String name,char gender,int age){
		this.name = name;
		this.gender = gender;
		this.age = age;
	}

	public employee(String job,double salary){
		this.job = job;
		this.salary = salary;
	}
	public employee(String name,char gender,int age,String job,double salary){
		this(name,gender,age);//构造器中用this去调用令一个构造器
		this.job = job;
		this.salary = salary;
	}
}

第十三题:

public class Homework013{
	public static void main(String[] args){
		PassObject p = new PassObject();
		Circle c = new Circle(0);
		/*for(int i = 1; i <= times; i++){
 			Circle c = new Circle(i);
 			p.printAreas(c,6);
 		}*/
		p.printAreas(c, 6);
		//System.out.println("当前的半径为:" + i);
	}
}
 class Circle{
 	double radius;
 	public Circle(double radius){
 		this.radius = radius;
 	}
 	public double findArea(){
 		double area = Math.PI * radius * radius;
 		return area;
 	}
 	public void setRadius(double radius){
 		this.radius = radius;
 	}
 }

 class PassObject{
 	public void printAreas(Circle c, int times){
 		System.out.println("radius\tarea");
 		for(int i = 1; i <= times; i++){
 			c.setRadius(i);
 			System.out.println((double)i + "\t" + c.findArea());
 		}
 	}
 }

第十四题:

import java.util.Scanner;
public class Homework014{
	public static void main(String[] args){
		int i = 0;
		int j = 0;
		int k = 0;
		char answer;
		do{
			System.out.println("============================");
			System.out.println("请输入(0-2):0-石头,1-剪刀,2-布");
			Scanner myScanner = new Scanner(System.in);
			int t = myScanner.nextInt();
			switch(t){
				case 0:
					System.out.println("Tom出了石头");
					break;
				case 1:
					System.out.println("Tom出了剪刀");
					break;
				case 2:
					System.out.println("Tom出了布");
					break;
				//case 3:
					//System.out.println("游戏结束");
					//return t;
				default:
					System.out.println("输入有误");
			}
			Game game = new Game();
			int tmp = game.stone(t);
			if(tmp == 0){
				i++;
				System.out.println("恭喜Tom赢了");
			}else if(tmp == 1){
				j++;
				System.out.println("很遗憾Tom输了");
			}else{
				k++;
				System.out.println("Tom和电脑平局");
			}
			System.out.println("Tom共玩了"+(i+j+k)+"局 "+"赢了"+ i + "局 "+ "输了"+j + "局");
			System.out.println("是否继续?(y/n)");
			answer = myScanner.next().charAt(0);
		}while(answer == 'y');
		
	}
}

class Game{
	public int stone(int numt){
		//Random random = new Random();
		if(numt == 0 || numt == 1 || numt == 2){
			int numc = (int)(Math.random()*3);
			switch(numc){
				case 0:
					System.out.println("电脑出了石头");
					break;
				case 1:
					System.out.println("电脑出了剪刀");
					break;
				case 2:
					System.out.println("电脑出了布");
					break;
				}
			if(numt - numc == -1 || numt - numc == 2){
				return 0;//赢了
			}else if(numt - numc == 1 || numt - numc == -2){
				return 1;//输了
			}else{
				return 2;//不输不赢	
			}
		}
		return 2;//不输不赢		
	}
}

​​​​​​​

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值