[ Java学习 ] 异常实验

题目:






//1.
import java.util.Arrays;
class MyException extends Exception
{
	MyException ( String s )
	{
		super(s);
	}
}


class Triangle
{
	double side[];
	
	Triangle()
	{
		
	}
	Triangle ( double a, double b, double c )
	{
		side = new double[3];
		side[0] = a; side[1] = b; side[2] = c;
		Arrays.sort(side);
	}
	
	double getArea() //Heron's formula
	{
		double p = 0.0, s;
		for (int i = 0; i < 3; i++) p += side[i];
		p /= 2;
		s = Math.sqrt(p * (p - side[0]) * (p - side[1]) * (p - side[2]) );
		return s;
	}
	void countArea()
	{
		boolean jud = true;
		try
		{
			for ( int i = 0; i < 3; i++ )
				if ( side[i] < 0)
					throw new MyException("Invalid input! You have input a negative !");
			
			if ( side[0] + side[1] <= side[2] )
				throw new MyException("Invalid input! The three sides can not form a triangle!");
		}
		catch (MyException e)
		{
			jud = false;
			System.out.println(e.toString());
		}
		
		if (jud)
		{
			System.out.println("The square for the triangle is " + getArea());
		}
		
	}
	
}
public class test1 
{
	
	public static void main(String[] args)
	{
		Triangle t[] = new Triangle[3];
		
		t[0] = new Triangle(-3, 4, 5);
		t[1] = new Triangle(1, 1, 3);
		t[2] = new Triangle(3, 4, 5);
		
		for (int i = 0; i < 3; i++)
		t[i].countArea();
	}

}

//2.(1)
import java.util.*;
class Student implements Comparable<Student>
{
	String name;
	double height;
	Student (String n, double h)
	{
		name = n;
		height = h;
	}
	Student ()
	{
		
	}
	
	public int compareTo(Student s)
	{
		if (this.height != s.height)
			return (this.height > s.height? 1 : -1); //身高不同按身高
		return this.name.compareTo(s.name); //身高相同按名字字典序
	}
	void display()
	{
		System.out.println(name + " " + height);
	}
}

class SetDemo
{
	TreeSet<Student> ts;
	
	void init()
	{
		Scanner sc = new Scanner(System.in);
		ts = new TreeSet<Student>();
		String []info = {"一", "二"};
		for (int i = 0; i < 2; i++)
		{
			System.out.println("请输入第" + info[i] + "个人的姓名");
			String n = "";
			double h = 0.0;
			n = sc.next();
			sc.nextLine(); // name
			
			boolean loop = true;
			while (loop)
			{
				System.out.println("请输入第" + info[i] + "个人的身高");
				try
				{
					h = sc.nextDouble();
					loop = false;
				}
				catch (InputMismatchException e)
				{
					System.out.println("身高数据有误,请重新输入!");
					sc.nextLine();
				}
			}
			Student s = new Student(n, h);
			ts.add(s);
		}
	}
	
	void solve()
	{
		System.out.println("----------------------------");
		System.out.println("按身高排序后的结果是:");
		Iterator<Student> it = ts.iterator();
		while (it.hasNext())
			it.next().display();
	}
}

public class test2 
{
	public static void main(String[] args)
	{
		SetDemo demo = new SetDemo();
		demo.init();
		demo.solve();
	}
}

//2.(2)用 Scanner 类的函数进行改写
//不用 try catch 来判断输入类型的合法性,而是改用 Scanner 类的函数来判断
import java.util.*;
class Student implements Comparable<Student>
{
	String name;
	double height;
	Student (String n, double h)
	{
		name = n;
		height = h;
	}
	Student ()
	{
		
	}
	
	public int compareTo(Student s)
	{
		if (this.height != s.height)
			return (this.height > s.height? 1 : -1); //身高不同按身高
		return this.name.compareTo(s.name); //身高相同按名字字典序
	}
	void display()
	{
		System.out.println(name + " " + height);
	}
}

class SetDemo
{
	TreeSet<Student> ts;
	
	void init()
	{
		Scanner sc = new Scanner(System.in);
		ts = new TreeSet<Student>();
		String []info = {"一", "二"};
		for (int i = 0; i < 2; i++)
		{
			System.out.println("请输入第" + info[i] + "个人的姓名");
			String n = "";
			double h = 0.0;
			n = sc.next();
			sc.nextLine(); // name
			
			boolean loop = true;
			while (loop)
			{
				System.out.println("请输入第" + info[i] + "个人的身高");
				if (sc.hasNextDouble())
				{
					h = sc.nextDouble();
					loop = false;
				}
				else
				{
					System.out.println("身高数据有误,请重新输入!");
					sc.nextLine();
				}
			}
			Student s = new Student(n, h);
			ts.add(s);
		}
	}
	
	void solve()
	{
		System.out.println("----------------------------");
		System.out.println("按身高排序后的结果是:");
		Iterator<Student> it = ts.iterator();
		while (it.hasNext())
			it.next().display();
	}
}


public class test2 
{
	public static void main(String[] args)
	{
		SetDemo demo = new SetDemo();
		demo.init();
		demo.solve();
	}
}

//3.

import java.util.*;
import java.util.TreeMap;

public class test3 
{
	public static void main(String []args)
	{
		TreeMap<String, Integer> map = new TreeMap<String, Integer>();
		String str = "aaa bbb ccc ccc ccc ccc bbb bbb aaa";
		Scanner sc = new Scanner(str);
		System.out.print("{");
		
		while (sc.hasNext())
		{
			String s = sc.next();
			if (!map.containsKey(s))
				map.put(s, new Integer(1));
			else
			{
				int val = map.get(s);
				map.put(s, new Integer(val + 1));
			}
		}
		
		Iterator<String> it = map.keySet().iterator();
		boolean first = true;
		while (it.hasNext())
		{
			if (first) first = false;
			else System.out.print(",");
			
			String s = it.next();
			System.out.print(s + "=" + map.get(s));
		}
		System.out.println("}");
	}
}

心得体会(编程中碰到的问题及解决方案)

查阅资料整理(都是超链接,可直接点击

java中异常抛出后代码还会继续执行吗

try-catch-finallyreturn的执行情况

关于Javatry-catch-finally-return的执行顺序

[eclipse]Syntax error on token ";",{ expected after this token

异常机制及throwthrows的区别

StackOverflow  Duplicate static field (Array vs String)

Java 如何抛出异常、自定义异常

java序列化问题:The serializable class does not declare a static final serialVersionUID field of type long

What does it mean: The serializable class does not declare a static final serialVersionUID field?

关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

Java 实例 - 自定义异常

如何编写和应用Java的自定义异常类

Java自定义异常与异常使用最佳实践

java 异常捕捉 ( try catch finally ) 你真的掌握了吗?

JAVA中定义宏

Java排序方法sort的使用详解

求算三角形面积的海伦公式

关于comparator接口和comparable接口以及它们各自的方法compare()compareTo()

学习笔记--如何使用Comparable接口里compareTo 方法进行排序

java 的排序框架的计较器问题

java简单的字符串大小比较——compareTo()方法

TreeSet的两种排序方式

使用treeSetcannot be cast to java.lang.Comparable

错误:in thread "main" java.lang.Error: Unresolved compilation problem [问题点数:100]

Java面试题:用java编写一个函数,统计一个字符串中每个字母出现的次数

HashMap,LinkedHashMap,TreeMap的区别

HashMapTreeMap的区别

Java 集合系列12TreeMap详细介绍(源码解析)和使用示例

JavaHashMapHashSetTreeMapTreeSet判断元素相同(代码整理)

JavaIntegerint互转

Java数据类型中StringIntegerint相互间的转换

集合详解之TreeSet集合--排序及API应用

Map四种获取keyvalue值的方法,以及对map中的元素排序

以下是心得体会:

1. 回去检查优化时,突然发现三角形的边长,其实不一定是整型啊!我怎么能直接定义成int型呢?应该用double才对然后就把所有涉及到相关数据类型的地方都回去改了改/(o)/~~

万幸,这题不用判断什么边长相等…不然还得定义常量EPS因double型的精度损失double数据要做特殊处理….呼,还好只是需要判断小于而已,没有需要判断小于等于地方

还有身高其实也未必是整形,所以我也同样改为double了,虽然题目好像给的是int

2. 做的过程中,觉得自己好多都不懂啊!遇到了很多不会的地方,或者错误的地方,感觉自己泛型学的还是不到位,实验做得磕磕碰碰的,课下应该好好复习。

不过好在,一个个bug的一句句提示,我都逐个百度了一次,这样下次不懂时,就能直接定位超链接,直接看解决问题的方法了~

-------------------------------------------------------------------------------------

最后一点点小体会就是...唉,这周要复习两门数学...

等这周过了,才能继续做ACM了 T^T

原文地址:https://www.cnblogs.com/mofushaohua/p/7789345.html