Exceptions in JAVA - Exercise

/*
 * Requirement: Teachers use a computer for class
 *
 * Problems in class
 * For example: computer blue screen
 * Computer smoking
 *
 * Describe the problem and encapsulate it as an object
 *
 * But when the smoke occurs, the progress of the lecture cannot be continued
 * There is a problem with the instructor and the lesson plan cannot be completed
 *
 * */

class LanPingException extends Exception {
	LanPingException(String message) {
		super(message);
	}
}

class MaoYanException extends Exception {
	MaoYanException(String message) {
		super(message);
	}
}

class NoPlanException extends Exception{
	public NoPlanException(String message) {
		// TODO Auto-generated constructor stub
		super(message);
	}
}

class Teacher {
	private String name;
	private Computer computer;

	Teacher(String name) {
		this.name = name;
		computer = new Computer();
	}

	public void perlect() throws NoPlanException{
		try {
			computer.run();

		} catch (LanPingException e) {
			// TODO: handle exception
			computer.reboot();
		}

		catch (MaoYanException e) {
			// TODO: handle exception
			test();
			throw new NoPlanException("Unable to teach"+e.getMessage());
		}

		System.out.println("lecture");
	}
	public void test(){
		System.out.println("Exercise");
	}
}

class Computer {
	private int state = 3;

	public void run() throws LanPingException, MaoYanException {
		if (state == 2)
			throw new LanPingException("Blue screen please restart");
		if (state == 3)
			throw new MaoYanException("Smoke please repair");
		System.out.println("Computer is running");
	}

	public void reboot() {
		state = 1;
		System.out.println("computer restart");
	}
}

public class ExceptionTest {
	public static void main(String args[]) {
		Teacher teacher = new Teacher("老师");
		try{
			teacher.perlect();
		}
		catch (NoPlanException e) {
			// TODO: handle exception
			System.out.println(e.toString());
			System.out.println("Change teacher or change computer");
	
		}
		
	}
}

one more

/*
 * There is a circle and rectangle
 * The area can be obtained. If there is an illegal value for the area, it is regarded as a problem in obtaining the area.
 * Problems are represented by exceptions
 *
 * programming
 *
 *
 * */

class IllegalNumberException extends RuntimeException{
	public IllegalNumberException(String mString) {
		// TODO Auto-generated constructor stub
		super(mString);
	}
	
	
}

abstract interface Area{
	int area() throws IllegalNumberException;
}

class Circle implements Area{
	private int radius;
	final static public int π = 3;
	public Circle(int radius) {
		// TODO Auto-generated constructor stub
		if(0>=radius)
			throw new IllegalNumberException("Incorrect data occurred");
		this.radius=radius;
	}
	public int area() throws IllegalNumberException{
		int area=π*this.radius*this.radius;
		return area;
	}
}

class Rectangle implements Area{
	private int height,weight;
	public Rectangle(int height,int weight) {
		// TODO Auto-generated constructor stub
		if(0>=height||0>=weight)
			throw new IllegalNumberException("Incorrect data occurred");
		this.height=height;
		this.weight=weight;
	}
	public int area(){
		int area=this.height*this.weight;
		return area;
	}
}

public class ExceptionTest2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Rectangle rectangle=new Rectangle(4, 3);
		System.out.println("Rectangle area: "+rectangle.area());
		Circle circle=new Circle(3);
		System.out.println("Circle area: "+circle.area());
	}

}



                                                                                                -------------------------By chick

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325593072&siteId=291194637