第七章综合练习:
package com.project.demo;
import java.util.Scanner;
import org.apache.log4j.Logger;
/**
* 动物父类
* @author suixin
*
*/
public abstract class Animal {
private static Logger logger = Logger.getLogger(Animal.class.getName());
private String name = ""; //昵称
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Animal() {
super();
// TODO Auto-generated constructor stub
}
public Animal(String name) {
super();
this.name = name;
}
public abstract void shout() ; //动物叫
}
package com.project.demo;
/**
* 猫类
* @author suixin
*
*/
public class Cat extends Animal implements Terrestrial{
private int letNum = 0; //腿数量
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("喵喵喵......");
}
public Cat() {
super();
// TODO Auto-generated constructor stub
}
public Cat(String name,int letNum ) {
super(name);
this.letNum = letNum;
// TODO Auto-generated constructor stub
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return letNum;
}
}
package com.project.demo;
/**
* 海豚类
* @author suixin
*
*/
public class Dolphin extends Animal {
public Dolphin() {
super();
// TODO Auto-generated constructor stub
}
public Dolphin(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("海豚音......");
}
}
package com.project.demo;
/**
* 鸭类
* @author suixin
*
*/
public class Duck extends Animal implements Terrestrial{
private int letNum = 0; //腿数量
public Duck() {
super();
// TODO Auto-generated constructor stub
}
public Duck(String name,int letNum) {
super(name);
this.letNum = letNum;
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("嘎嘎嘎......");
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return letNum;
}
}
package com.project.demo;
/**
* 获取腿的数量的接口
* @author suixin
*
*/
public interface Terrestrial {
int getLegNum(); //腿的数量
}
package com.project.demo;
import java.util.Scanner;
/**
* 测试类
* @author suixin
*
*/
public class Test {
//创建动物类对象数组
static Animal animal [] = new Animal[3];
static Scanner scanner = new Scanner(System.in);
static int choose;
public static void main(String[] args) {
start();
while (choose != 0) {
try {
modifyAnimal();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
show();
choose = scanner.nextInt();
}
}
}
/**
* 打印信息方法
*/
public static void show() {
System.out.println("动物名字\t\t腿的条数\t\t动物叫");
for (int i = 0; i < animal.length; i++) {
if (animal[i] != null) {
if (animal[i] instanceof Terrestrial) {
String name = animal[i].getName();
int legNum = ((Terrestrial) animal[i]).getLegNum();
System.out.print(name + "\t\t" + legNum + "\t\t");
animal[i].shout();
}else {
String name = animal[i].getName();
System.out.print(name + "\t\t" + 0 + "\t\t");
animal[i].shout();
}
}
}
System.out.println("是否要继续修改数据:按0进行修改操作,其它任意数字键退出:");
}
/**
* 给对象数组初始化
*/
public static void start() {
animal[0] = new Cat("加菲猫", 4);
animal[1] = new Duck("唐小鸭", 2);
animal[2] = new Dolphin("海豚奇奇");
show();
choose = scanner.nextInt();
}
/**
* 更改信息方法
* @throws Exception
*/
public static void modifyAnimal() throws Exception{
//定义变量存储临时数据
String [] names = new String[3];
int [] number = new int [3];
System.out.print("请输入猫的名称:");
names[0] = scanner.next();
System.out.println("请输入猫腿的条数");
number[0] = scanner.nextInt();
if (number[0] != 4) {
throw new Exception("猫只能有4条腿");
}
System.out.println("请输入鸭子的名称");
names[1] = scanner.next();
System.out.println("请输入鸭子腿的条数");
number[1] = scanner.nextInt();
if (number[1] != 2) {
throw new Exception("鸭子只能有两条腿");
}
System.out.println("请输入海豚的名称");
names[2] = scanner.next();
animal[0] = new Cat(names[0], number[0]);
animal[1] = new Duck(names[1], number[1]);
animal[2] = new Dolphin(names[2]);
}
}
(1)编写能产生ArrayIndexOutOfBoundsException异常的代码,并将其捕获,在控制台上输出异常信息。
package com.homework.demo.test7;
public class ArraysIndex {
public static void main(String[] args) {
int arr [] = new int [3]; //长度为3的整型数组
try {
arr[3] = 2; //下标为3的元素赋值为2(数组越界)
} catch (ArrayIndexOutOfBoundsException e) { //数组下标越界
// TODO: handle exception
e.printStackTrace();
}
}
}
(2)修改第一题,使用log4j记录日志,在jbit.log文件中记录产生的异常信息。
package com.homework.demo.test7;
import org.apache.log4j.Logger;
public class ArraysIndexLog {
private static Logger logger = Logger.getLogger(ArraysIndexLog.class.getName());
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] = new int [3]; //长度为3的整型数组
try {
arr[3] = 2; //下标为3的元素赋值为2(数组越界)
} catch (ArrayIndexOutOfBoundsException e) { //数组下标越界
// TODO: handle exception
logger.error(e.getMessage());
e.printStackTrace();
}
}
}