异常的问题
http://expert.csdn.net/Expert/topic/1660/1660081.xml?temp=.185528
那也有编好的部分
今天老师要我加入异常处理
就出现问题了
后面的问题的最关键分数基本上没了才给那点分望原谅
那程序先多态
后异常老师出的
我想别人也做过
http://expert.csdn.net/Expert/topic/1660/1660081.xml?temp=.185528
那也有编好的部分
今天老师要我加入异常处理
就出现问题了
后面的问题的最关键分数基本上没了才给那点分望原谅
那程序先多态
后异常老师出的
我想别人也做过
* <p>Title: This follow is newman's writing</p>
* <p>Description: I want better writing ,instead of best one!</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: [email protected]</p>
* @author Newman
* @version 1.0
*/
import java.lang.Math.*;
import java.io.*;class MyException extends Exception{
Tangle tangle;
boolean flag;//true throw exception
public MyException(){
super("My Exception");
}
public MyException(Shape shape){
this();
if(shape instanceof Tangle){//check: shape is subclass of s(Tangle)
tangle=(Tangle)shape;
System.out.println("Tangle found.");
}
else{
System.out.println("Tangle not found.");
try {
throw new NotImplementException();
}
catch (NotImplementException ex) {
System.err.println("NotImplementException occured: "+ex.getMessage());
ex.printStackTrace();
}
}
}
public boolean check(){
if((tangle.m_dEdge1+tangle.m_dEdge2)<tangle.m_dEdge3)
this.flag =true;
if((tangle.m_dEdge2+tangle.m_dEdge3)<tangle.m_dEdge1)
this.flag =true;
if((tangle.m_dEdge1+tangle.m_dEdge3)<tangle.m_dEdge2)
this.flag =true;
return this.flag ;
} public void finalize() {
this.getchange();
} public void getchange(){
if((tangle.m_dEdge1+tangle.m_dEdge2)<tangle.m_dEdge3) tangle.m_dEdge1=+7;
if((tangle.m_dEdge2+tangle.m_dEdge3)<tangle.m_dEdge1) tangle.m_dEdge2=+7;
if((tangle.m_dEdge1+tangle.m_dEdge3)<tangle.m_dEdge2) tangle.m_dEdge3=+7;
}
} class NotImplementException extends Exception{
String msg="Not Implement Exception";
public NotImplementException(){
super("Not Implement Exception");
}
public String toString(){
return msg;
}
}interface Shape {
double PI=3.14;
/**
* 画图
*/
public void draw();
/**
* 面积
* @return
*/
public double area();
/**
* 周长
* @return
*/
public double length();
}class Circle implements Shape {
private int m_Radius;//半径
Circle(int radius){
this.m_Radius=radius;
} public void draw() {
System.out.println("Circle.draw()");
}
public double area(){
double ara=this.PI*this.m_Radius*this.m_Radius;
return ara;
}
public double length() {
double leng;
leng=2*this.PI*this.m_Radius;
return leng;
}
}/**
* 三角形
*/
class Triangle implements Shape {
private int m_nWeight;//底
private int m_nHeight;//高
Triangle(int weight,int height){
this.m_nWeight=weight;
this.m_nHeight=height;
}
public void draw() {
System.out.println("Triangle.draw()");
}
public double area() {
double ara;
ara=this.m_nHeight*this.m_nWeight/2;
return ara;
}
public double length() {
double leng;
double sider=Math.pow(this.m_nWeight/2,(double)2)+Math.pow(this.m_nHeight,(double)2);
sider=Math.sqrt(sider);
leng=sider*2+this.m_nWeight ;
return leng;
}
}/**
* 混合
*/
class Tangle implements Shape {
int m_dEdge1,m_dEdge2,m_dEdge3; Tangle(int edge1,int edge2,int edge3){
this.m_dEdge1=edge1;
this.m_dEdge2=edge2;
this.m_dEdge3=edge3;
} public void draw() {
System.out.println("Tangle.draw()");
} public double area() {
double ara;
int s=(this.m_dEdge1+this.m_dEdge2+this.m_dEdge3)/2;
ara=Math.sqrt((double)(s*(s-this.m_dEdge1)*(s-this.m_dEdge2)*(s-this.m_dEdge3)));
return ara;
} public double length() {
double leng;
leng=(double)(this.m_dEdge1+this.m_dEdge2+this.m_dEdge3);
return leng;
}
}
public class Shapes {
private int m_nType;
private int m_nNumOfInstance;
public Shapes(){
this.m_nType =0;
this.m_nNumOfInstance=3;
}
public static void main(String[] args) throws MyException{
Shapes shapes=new Shapes();
if(args.length==1){
shapes.m_nType= Integer.parseInt(args[0]);
}
else{
System.out.println("Invalid parameter number ,or NO parameter,\nUse default value (1-"+shapes.m_nNumOfInstance+").");
return ;
} double length=0;
double area=0;
Shape pt[] =new Shape[4]; for(int i=0;i<4;i++){
int type =((int)(Math.random()*10))%shapes.m_nNumOfInstance;
//type=2;
System.out.println("type: "+type);
switch (type) {
case 0:
int w = (int) (Math.random() * 8);
int h = (int) (Math.random() * 8); System.out.println("weight is:" + w);
System.out.println("height is:" + h);
pt[i] = new Triangle(w, h);
pt[i].draw();
length = pt[i].length();
area = pt[i].area();
break;
case 1:
int r = (int) (Math.random() * 8);
System.out.println("radius is:" + r);
pt[i] = new Circle(r);
pt[i].draw();
length = pt[i].length();
area = pt[i].area();
break;
case 2:
try{
int e1 = (int) (Math.random() * 8);
int e2 = (int) (Math.random() * 8);
int e3 = (int) (Math.random() * 8);
System.out.println("first edge is:" + e1);
System.out.println("second edge is:" + e2);
System.out.println("third edge is:" + e3);
pt[i] = new Tangle(e1, e2, e3);
MyException myex=new MyException(pt[i-1]);
if(myex.check()){
throw new MyException();
}
}
catch(MyException e){
//e.getchange();
System.err.println("MyException: "+e.getMessage());
e.printStackTrace() ;
}
pt[i].draw();
length = pt[i].length();
area = pt[i].area();
break;
default:
length=0;
area=0;
System.out.println("No this type: "+shapes.m_nType);
System.out.println("Use default value (1-"+shapes.m_nNumOfInstance+").");
break;
}
System.out.println("the total length is:" + length);
System.out.println("the total area is:" + area);
}
}
}
pt[i] = new Tangle(e1, e2, e3);
MyException myex=new MyException(pt[i-1]);
if(myex.check()){
throw new MyException();
}中的MyException myex=new MyException(pt[i-1]);
改为
MyException myex=new MyException(pt[i]);
import java.lang.Math.*;
import java.io.*;class MyException extends Exception{
Tangle tangle;
boolean flag;//true throw exception
public MyException(){
super("My Exception");
}
public MyException(Shape shape){
this();
if(shape instanceof Tangle){//check: shape is subclass of s(Tangle)
tangle=(Tangle)shape;
this.getchange() ;
System.out.println("Tangle found.");
}
else{
System.out.println("Tangle not found.");
try {
throw new NotImplementException();
}
catch (NotImplementException ex) {
System.err.println("NotImplementException occured: "+ex.getMessage());
ex.printStackTrace();
}
}
}
public boolean check(){
if((tangle.m_dEdge1+tangle.m_dEdge2)<tangle.m_dEdge3)
this.flag =true;
if((tangle.m_dEdge2+tangle.m_dEdge3)<tangle.m_dEdge1)
this.flag =true;
if((tangle.m_dEdge1+tangle.m_dEdge3)<tangle.m_dEdge2)
this.flag =true;
return this.flag ;
} public void getchange(){
if((tangle.m_dEdge1+tangle.m_dEdge2)<tangle.m_dEdge3) tangle.m_dEdge1=+7;
if((tangle.m_dEdge2+tangle.m_dEdge3)<tangle.m_dEdge1) tangle.m_dEdge2=+7;
if((tangle.m_dEdge1+tangle.m_dEdge3)<tangle.m_dEdge2) tangle.m_dEdge3=+7;
}
} class NotImplementException extends Exception{
String msg="Not Implement Exception";
public NotImplementException(){
super("Not Implement Exception");
}
public String toString(){
return msg;
}
}interface Shape {
double PI=3.14;
/**
* 画图
*/
public void draw();
/**
* 面积
* @return
*/
public double area();
/**
* 周长
* @return
*/
public double length();
}class Circle implements Shape {
private int m_Radius;//半径
Circle(int radius){
this.m_Radius=radius;
} public void draw() {
System.out.println("Circle.draw()");
}
public double area(){
double ara=this.PI*this.m_Radius*this.m_Radius;
return ara;
}
public double length() {
double leng;
leng=2*this.PI*this.m_Radius;
return leng;
}
}/**
* 三角形
*/
class Triangle implements Shape {
private int m_nWeight;//底
private int m_nHeight;//高
Triangle(int weight,int height){
this.m_nWeight=weight;
this.m_nHeight=height;
}
public void draw() {
System.out.println("Triangle.draw()");
}
public double area() {
double ara;
ara=this.m_nHeight*this.m_nWeight/2;
return ara;
}
public double length() {
double leng;
double sider=Math.pow(this.m_nWeight/2,(double)2)+Math.pow(this.m_nHeight,(double)2);
sider=Math.sqrt(sider);
leng=sider*2+this.m_nWeight ;
return leng;
}
}/**
* 混合
*/
class Tangle implements Shape {
int m_dEdge1,m_dEdge2,m_dEdge3; Tangle(int edge1,int edge2,int edge3){
this.m_dEdge1=edge1;
this.m_dEdge2=edge2;
this.m_dEdge3=edge3;
} public void draw() {
System.out.println("Tangle.draw()");
} public double area() {
double ara;
int s=(this.m_dEdge1+this.m_dEdge2+this.m_dEdge3)/2;
ara=Math.sqrt((double)(s*(s-this.m_dEdge1)*(s-this.m_dEdge2)*(s-this.m_dEdge3)));
return ara;
} public double length() {
double leng;
leng=(double)(this.m_dEdge1+this.m_dEdge2+this.m_dEdge3);
return leng;
}
public String toString(){
return "first edge is:" +this.m_dEdge1+",second edge is:" +this.m_dEdge2+",third edge is:"+this.m_dEdge3 ;
}
}
public class Shapes {
private int m_nType;
private int m_nNumOfInstance;
public Shapes(){
this.m_nType =0;
this.m_nNumOfInstance=3;
}
public static void main(String[] args) throws MyException{
Shapes shapes=new Shapes();
if(args.length==1){
shapes.m_nType= Integer.parseInt(args[0]);
}
else{
System.out.println("Invalid parameter number ,or NO parameter,\nUse default value (1-"+shapes.m_nNumOfInstance+").");
return ;
} double length=0;
double area=0;
Shape pt[] =new Shape[4]; for(int i=0;i<1;i++){
int type =((int)(Math.random()*10))%shapes.m_nNumOfInstance;
type=2;
System.out.println("type: "+type);
switch (type) {
case 0:
int w = (int) (Math.random() * 8);
int h = (int) (Math.random() * 8); System.out.println("weight is:" + w);
System.out.println("height is:" + h);
pt[i] = new Triangle(w, h);
pt[i].draw();
length = pt[i].length();
area = pt[i].area();
break;
case 1:
int r = (int) (Math.random() * 8);
System.out.println("radius is:" + r);
pt[i] = new Circle(r);
pt[i].draw();
length = pt[i].length();
area = pt[i].area();
break;
case 2:
try{
int e1 = (int) (Math.random() * 8);
int e2 = (int) (Math.random() * 8);
int e3 = (int) (Math.random() * 8);
e1=1;e2=4;e3=6;
System.out.println("first edge is:" + e1);
System.out.println("second edge is:" + e2);
System.out.println("third edge is:" + e3);
pt[i] = new Tangle(e1, e2, e3);
MyException myex=new MyException(pt[i]);
if(myex.check()){
throw new MyException();
}
}
catch(MyException e){
//e.getchange();
System.err.println("MyException: "+e.getMessage());
e.printStackTrace() ;
}
System.out.println("DEBUG: "+pt[i].toString());
pt[i].draw();
length = pt[i].length();
area = pt[i].area();
break;
default:
length=0;
area=0;
System.out.println("No this type: "+shapes.m_nType);
System.out.println("Use default value (1-"+shapes.m_nNumOfInstance+").");
break;
}
System.out.println("the total length is:" + length);
System.out.println("the total area is:" + area);
}
}
}
type=2;
和
e1=1;e2=4;e3=6;
是测试用的,你把它去掉,就可以用了。如果是测试Tangle类,你就放着吧!