题目:
//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
{
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 ts;
void init()
{
Scanner sc = new Scanner(System.in);
ts = new TreeSet();
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 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
{
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 ts;
void init()
{
Scanner sc = new Scanner(System.in);
ts = new TreeSet();
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 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 map = new TreeMap();
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 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("}");
}
}
心得体会(编程中碰到的问题及解决方案)
查阅资料整理(都是超链接,可直接点击)
以下是心得体会:
1. 回去检查优化时,突然发现三角形的边长,其实不一定是整型啊!我怎么能直接定义成int型呢?应该用double才对…然后就把所有涉及到,相关数据类型的地方,都回去改了改/(ㄒoㄒ)/~~
万幸,这题不用判断什么边长相等…不然还得定义常量EPS,因double型的精度损失,对double数据判等时要做特殊处理….呼,还好只是需要判断小于而已,没有需要判断小于等于的地方
还有身高其实也未必是整形,所以我也同样改为double了,虽然题目好像给的是int
2. 做的过程中,觉得自己好多都不懂啊!遇到了很多不会的地方,或者错误的地方,感觉自己泛型学的还是不到位,实验做得磕磕碰碰的,课下应该好好复习。
不过好在,一个个bug的一句句提示,我都逐个百度了一次,这样下次不懂时,就能直接定位超链接,直接看解决问题的方法了~
-------------------------------------------------------------------------------------
最后一点点小体会就是...唉,这周要复习两门数学...
等这周过了,才能继续做ACM了 T^T