题目一 类型与自定义异常
题目描述
代码实现
//三角形长度异常
public class IllegalSideLengthException extends Exception{
public String mess = "三角形的三边长度不合适";
public boolean SideException(double a,double b,double c) {
if(a<=0||b<=0||c<=0) {
return false;
}
return true;
}
}
//三角形长度关系判断
public class IllegalArgumentException extends Exception{
public String mess = "三边关系不符合三角形";
public boolean ArgumentException(double a,double b,double c) {
if(a+b<c||a+c<b||b+c<a||Math.abs(a-b)>c||Math.abs(a-c)>b||Math.abs(c-b)>a) {
return false;
}
return true;
}
}
//三角形类构造
public class Triangle {
double a;
double b;
double c;
public Triangle(double aa,double bb,double cc)throws IllegalSideLengthException,IllegalArgumentException {
this.a = aa;
this.b =bb;
this.c =cc;
if(new IllegalSideLengthException().SideException(aa, bb, cc)==false) {
System.out.println("三边长度不符合三角形");
}else if(new IllegalArgumentException().ArgumentException(aa, bb, cc)==false) {
System.out.println("三边关系不符合三角形");
}else {
System.out.println("三角形合法,面积是:"+this.Area());
}
}
//计算面积
public double Area() {
double p = (a+b+c)/2.0;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}
//测试类
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double result=0;
Triangle t = null;
try { //对异常进行处理
t = new Triangle(a,b,c);
}catch(IllegalSideLengthException e1) {
System.out.println(e1.mess);
}catch(IllegalArgumentException e2){
System.out.println(e2.mess);
}
}
}
题目二 文件读写
题目描述
因为是读写对象(引用类型),所以要对读写的对象进行序列化,才能实现对对象在文件中的读写。
代码实现
//Circle类型 -- 实现Serializable接口
public class Circle implements Serializable{
double r;
double area;
public Circle(double r) {
//注意:如果这里不用this 后边输出r会是0
//因为参数r和成员变量r是同一个变量名
this.r = r;
area = Math.PI*r*r;
}
public String toString() {
return "圆的半径是"+r+" 面积是:"+area+"\n";
}
}
//测试类
public class Test {
public static void main(String args[]) throws FileNotFoundException {
Circle c1;
Circle c2;
Circle c3;
c1 = new Circle(6);
c2 = new Circle(4);
c3 = new Circle(9);
File file = new File("objects.txt");
FileOutputStream fout = new FileOutputStream(file);
try {
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(c1);
oout.writeObject(c2);
oout.writeObject(c3);
oout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileInputStream fin = new FileInputStream(file);
ObjectInputStream oin = new ObjectInputStream(fin);
//readObject返回的是Object类型
Circle cc1 = (Circle)oin.readObject();
Circle cc2 = (Circle)oin.readObject();
Circle cc3 = (Circle)oin.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
题目三 数据库
题目描述
代码实现
public class DataTest {
// TODO Auto-generated method stub
Connection con = null;
Statement sql = null;
ResultSet rs1 = null;
// 1.加载驱动
public DataTest() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("加载成功!");
} catch (ClassNotFoundException e) {
System.out.println("加载失败!");
}
// 2. 连接
String url = "jdbc:mysql://localhost:3306/test?&useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "20031030";
try {
con = DriverManager.getConnection(url, user, password);
System.out.println("连接成功!");
} catch (SQLException e) {
System.out.println("连接失败!");
}
// 3.查询1002学生
try {
sql = con.createStatement();
String statement = "select * from Student where ID = '" + 1002 + "'";
rs1 = sql.executeQuery(statement);
/*很重要*/
rs1.next();
System.out.println("姓名:"+rs1.getNString(2)+" 成绩:"+rs1.getInt(3));
//插入学生信息 -- 插入操作只能正确执行一次
String insert1 = "insert into Student(id,name,score) values (1007,'Chris',95)";
// sql.executeUpdate(insert1);
//更新1004成绩
String update1 = "update Student set score = 89 where id=1004";
// String update1 = "update Student set score= '" +89+ "'where id = '"+1004+"'";
sql.executeUpdate(update1);
//删除1005学生信息
String dele = "delete from Student where id = 1005";
sql.executeUpdate(dele);
//记得最后关闭con
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
题目四 服务器和客户端连接以及界面的实现
题目描述
至于代码的具体实现下次测试完再补充,先把题目存下。
另外,今天是23年离校日,最后一天只有自己在学校,从早晨的小冰雹到现在的雪!今天在图书馆呆得,在窗户边的摇椅坐了好久看雪、剪视频,除了没啥能吃的,其他的都有点独特哎!!!