JAVA-异常处理及toString的覆写

1、要求: 
1).Person类有name,age,salary属性,要求实现至少两个构造方法,并且属性私有,提供对应的getter、setter。 
2).覆写toString方法,要求在System.out.println()函数中传递Person对象能打印出三个属性值而不是对象地址。 

3).覆写equals方法,要求两个Person类对象的值相同时返回true。 

import javax.xml.transform.Source;
class Person{
	private String name;
	private int age;
	private double salary;
	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 double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	//构造方法
	private static Person instance = new Person();
	private Person() {
		System.out.println("1、Person的无参构造");
	}
	private Person(String name) {
		setName(name);
		System.out.println("2、"+this.name);
	}	
	public static Person getInstance() {
		return instance;
	}
	public String toString(){
		return "姓名:"+this.name+"年龄:"+this.age+"薪水:"+this.salary;
	}
	public boolean equals(Object obj){
		if(obj == null){
			return false;
		}
		if(this == obj){
			return true;
		}
		if(!(obj instanceof Person)){
			return false;
		}
		Person per=(Person) obj;
		return this.age==per.age&& this.name.equals(per.name)
		&& this.salary==per.salary ;
	}
}

class Test{
	public static void main(String []args){
		Person instance =null;
		instance=Person.getInstance();
		instance.setName("张三");
		instance.setAge(18);
		instance.setSalary(20000.0);
		Object obj = instance;
		Person per=(Person)obj;
		System.out.println(per.toString());
		
		Person instance2 =null;
		instance2=Person.getInstance();
		instance2.setName("张三");
		instance2.setAge(18);
		instance2.setSalary(20000.0);
		Object obj2 = instance2;
		Person per2=(Person)obj2;
		System.out.println(per2.equals(per));

	}
}
2、 说出下列程序的执行结果,并说明原因: 
Integer a = 55; 
Integer b = 55; 
System.out.println(a==b); 
System.out.println(a==new Integer(55)); 
System.out.println(a.equals(new Integer(55))); 
Integer c = 129; 
Integer d = 129; 

System.out.println(c==d);

答案是:true false true false
第一个true是a和b的值比较,是55==55 所以是true。在-128~127范围内。
第二个false是a和new所实例化的堆内存地址编码进行比较,自然是false
第三个true是使用equals命令,自动比较a和b所指向的数值。

第四个false 是因为c和d的数值已经超出了-128~127范围,不在常量池里面开辟空间,而在堆空间中开辟空间,就没有复用机制,随意这里比较的是c和d指向的堆空间的地址编码。

3、写出懒汉式单例模式:

class Singleton{
	private static Singleton instance;
	private Singleton(){
		System.out.println("空的构造方法");
	}
	public static Singleton getInstance(){
		if(instance == null){
			instance = new Singleton();
		}
		return instance;
	}
	public void fun(){
		System.out.println("Singleton里面的私有函数fun");
	}
}
class Test{
	public static void main(String[] args){
		Singleton instance =null;
		instance=Singleton.getInstance();
		instance.fun();
	}
}
4、编程: 
1).定义一个MulException类继承Exception类,要求两数相乘等于100报错,在主类中定义一个方法,在方法中抛出此异常,在主方法观察结果。 

2).定义一个DivException类继承RuntimeException类,要求两数相除等于2报错,在主类中定义一个方法,在方法中抛出此异常,在主方法观察结果

class MulException extends Exception {
	public MulException(String msg){
		super(msg);
	}
}
class DivException extends RuntimeException {
	public DivException(String msg){
		super(msg);
	}
}
class Test{
	public static void main(String[] args){
		try {
			Fun.mul();
			Fun.div();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	static class Fun{
		public static int mul()throws Exception{
			int x =3;
			int y =50;
			if(x*y == 100){
				throw new MulException(x+"*"+y+" 就不能等于100");
			}
			return x*y;
	}
		public static int div()throws Exception{
			int a=4;
			int b=2;
			if(a/b == 2){
				throw new MulException(a+"/"+b+" 就不能等于2");
			}
			return a/b;
		}
}
}

5、题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

class Test{
	public static void main(String []args){
		int day = Integer.parseInt(args[0]);
		int x=1;
		if(day<=0){
			System.out.println("输入天数错误");
			return;
		}
		for(;day>1;day--){
			x=(x+1)*2;
		}
		System.out.println("第一天总共摘了"+x+"个苹果");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值