- 编程题
(1)定义一个MulException类继承Exception类,要求两数相乘等于100保错
在主类中定义一个方法,在方法中抛出此异常,在主方法观察结果。
(2)定义一个DivException类继承RuntimeException类,
要求两数两除等于2保错。在主类中定义一个方法,在方法中抛出此异常,在主方法观察结果
package com.wschase.threadlesson1;
/**
* Author:WSChase
* Created:2019/1/10
*/
class MulException extends Exception{
public MulException(String str){
super(str);
}
}
class DivException extends RuntimeException{
public DivException(String str){
super(str);
}
}
public class TestPractice {
public static void main(String[] args) {
MulTest();
DivTest();
}
public static void MulTest(){
try{
int x=10;
int y=10;
if(x*y==100){
throw new MulException("两数相乘不能等于100");
}
}catch (Exception e){
e.printStackTrace();
}
}
public static void DivTest(){
try{
int x=4;
int y=2;
if(x/y==2){
throw new DivException("两数相除不能等于2");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
2.题目:候子吃桃
候子一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个
第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
以后每天都吃了前一天剩下的一半零一个。
到第10天早上想再吃时,见只剩下一个桃子了。求第一天一共摘下了多少个桃子
package com.wschase.threadlesson1;
/**
* Author:WSChase
* Created:2019/1/10
*/
public class TestEate {
public static void main(String[] args) {
//循环
int result=1;
for(int i=1;i<10;i++){
result=(result+1)*2;
}
System.out.println("这个是循环方法的结果:"+result);
System.out.println("这个是递归方法的结果:"+getTotal(1));
}
//递归
public static int getTotal(int day){
if(day==10){
return 1;
}else {
return (getTotal(day+1)+1)*2;
}
}
}
3. 编程题:
要求:
(1)Person类有name,age,salary属性,要求实现至少两个构造方法,并且属性私有,提供对应的getter、setter。
(2)覆写toString方法
要求在System.out.println()函数中传递Person对象能打印出三个属性值而不是对象地址。
(3)覆写equals方法,要求两个Person类对象的值同时返回true。
package com.wschase.threadlesson1;
/**
* Author:WSChase
* Created:2019/1/10
*/
import java.util.DoubleSummaryStatistics;
class Person{
private String name;
private Integer age;
private Double salary;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public Double getSalary() {
return salary;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person(String name, Integer age, Double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "姓名为:"+this.name+",年龄为:"+this.age+",工资为:"+this.salary;
}
@Override
public boolean equals(Object obj) {
if(obj==null){
return false;
}else if(!(obj instanceof Person)){
return false;
}else if(obj==this){//指向同一块空间
return true;
}
Person per=(Person) obj;
return this.name.equals(per.name)&&this.age.equals(per.age)&&this.salary.equals(per.salary);
}
}
public class TestBasic {
public static void main(String[] args) {
Person p1=new Person("herry",18,1000.00001);
Person p2=new Person("jack",19,1722.00001);
Person p3=new Person("herry",18,1000.00001);
Person p4=new Person("herry",20,1000.00001);
//输出对象名,默认调用对象的toString()方法
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println(p4);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println(p1.equals(p4));
System.out.println(p3.equals(p4));
System.out.println(p2.equals(p3));
}
}