1.编程题:
要求:
1).Person类有name,age,salary属性,要求实现至少两个构造方法,并且属性私有,提供对应的getter、setter。
2).覆写toString方法,要求在System.out.println()函数中传递Person对象能打印出三个属性值而不是对象地址。
3).覆写equals方法,要求两个Person类对象的值相同时返回true。
class Person{
private String name;
private int age;
private int salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public int getSalary() {
return salary;
}
public Person(){
super();
}
public Person(String name, int age, int salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString(){
return "Person [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this == obj)
return true;
if (obj instanceof Person) {
Person tmp = (Person) obj;
if (tmp.getAge() != this.age)
return false;
if (tmp.getName() == null) {
if (this.name != null) {
return false;
}
} else if (!tmp.getName().equals(this.name)) {
return false;
}
if (tmp.getSalary() == this.salary)
return true;
}
return true;
}
}
public class test{
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person("张三", 20, 200);
Person p4 = new Person("张三", 20, 20);
Person p5 = new Person("李四", 20, 2000);
System.out.println(p1.equals(p2));
System.out.println(p3.equals(p4));
System.out.println(p3.equals(p5));
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println(p4);
System.out.println(p5);
}
}
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);
1.true,在自动装箱时对于在-127-128值,它们被装箱为Integer对象,会存在内存中被重用,因为重用,所以Integer b = 55,实际上没有生成新的对象,而是引用了对象a,实际上参考的对象是同一个对象,内存地址一样,所以”a==b”结果是true。
2.false,new了一个对象后,产生一个新的Interger对象,而“==”比较的是对象的地址,所以是false。
3.true,equals比较的是对象的值。所以是true。
4.false,在自动装箱时如果值不在-128~127之间,则被装箱后Integer对象不会被重用,c与d的内存地址也就不一样,所以”c==d”结果为false。
3.写出懒汉式单例模式
class Singleton{
private final static Singleton INSTANCE=new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return INSTANCE;
}
public void print(){
System.out.println("恶汉式单例");
}
}
class SingletonH{
private static SingletonH instance;
private SingletonH(){}
public static SingletonH getInstance(){
if(instance==null){
instance=new SingletonH();
}
return instance;
}
public void print(){
System.out.println("懒汉式单例");
}
}
public class test1{
public static void main(String[] args){
Singleton singleton=null;
singleton=Singleton.getInstance();
singleton.print();
SingletonH singletonH=null;
singletonH=SingletonH.getInstance();
singletonH.print();
}
}
4.编程:
1).定义一个MulException类继承Exception类,要求两数相乘等于100报错,在主类中定义一个方法,在方法中抛出此异常,在主方法观察结果。
2).定义一个DivException类继承RuntimeException类,要求两数相除等于2报错,在主类中定义一个方法,在方法中抛出此异常,在主方法观察结果。
class MulException extends Exception {
public MulException(String str) {
super(str);
}
}
class DivException extends Exception {
public DivException (String str) {
super(str);
}
}
public class test2 {
public static void main(String[] args) {
try{
Mul(50,2);
}catch (Exception e) {
e.printStackTrace();
}
try {
Div(10,5);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int Mul(int a, int b) throws MulException {
int ret = a*b;
try {
if(ret == 100) {
throw new MulException("两数相乘不能为100");
}
}catch (Exception e) {
throw e;
}finally {
System.out.println("计算结果: "+ret);
}
return ret;
}
public static int Div(int a, int b) throws DivException {
int ret = a/b;
try {
if(ret == 2) {
throw new DivException("两数相除不能为2");
}
}catch (Exception e) {
throw e;
}finally {
System.out.println("计算结果: "+ret);
}
return ret;
}
}
5.(算法)
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
public class test3{
public static void main(String[] args) {
int ret=getPeach(1);
System.out.println(ret);
}
public static int getPeach(int days){
if(days==10){
return 1;
}
return getPeach(days+1)*2+2;
}
}