面向对象技术上机6

上机6

  1. 对上机5中的第1题,修改圆的半径的输入方式为从键盘上输入,为防止输入负数,请通过throws和throw设计出一旦出现负数时的异常处理方法,并通过本题的测试类进行测试。
    2.设计实现一个小型学生成绩管理系统,要求具有以下功能:
    从键盘上录入多个同学信息(包括学号 姓名 年龄 成绩)、输出所有学生信息、查找学生信息()、能对学生按成绩排序(),并能进行数据输入错误等的异常处理。(后面会随着知识的增加功能及实现技术层层递进)
    3、利用题2中定义的学生类,创建10个相应的对象,分别用ArrayList、 LinkedList、HashSet、HashMap、TreeMap类的对象来存储这10个对象,检索其中的某2个对象,遍历输出所有对象。
    4(*)、修改题3中的Student类实现Comparable接口,该接口中对学号进行排序,创建10个相应的对象,用HashSet类的对象来存储这10个对象,输出存储的这10个对象(排序后)。

**

第一题:

**

import java.util.Scanner;

//对上机5中的第1题,修改圆的半径的输入方式为从键盘上输入,
// 为防止输入负数,请通过throws和throw设计出一旦出现负数时的异常处理方法,
// 并通过本题的测试类进行测试。
public class CircleTest {
   
    public static void main(String[] args) {
   
        Scanner scanner = new Scanner(System.in);
        //输入半径
        int radius = scanner.nextInt();
        //创建圆对象
        try {
   
            Circle circle = new Circle(radius);
            System.out.println(circle.toString());
        } catch (NumException e) {
   
            e.printStackTrace();
        }
    }
}
/*Circle类:具有以下属性和方法:
      属性radius:double类型,表示圆的半径。
      方法:Circle(double r):构造函数
            toString(): 输出圆的相关信息(半径等)。*/
public class Circle implements Shape {
   
    //圆的半径
    private double radius;

    //构造方法


    public Circle() {
   
    }

    public Circle(double radius) throws NumException {
   
        //如果半径小于零,抛异常
        if (radius < 0){
   
            throw new NumException("圆的半径不能小于零");
        }
        this.radius = radius;
    }

    @Override
    public String toString() {
   
        return "Circle{" +
                "radius=" + radius + ",Area =" + getArea() + ",Perimeter=" +  getPerimeter()+
                '}';
    }

    @Override
    public double getArea() {
   
        return Math.PI*Math.pow(radius,2);
    }

    @Override
    public double getPerimeter() {
   
        return 2*Math.PI*radius;
    }

    public double getRadius() {
   
        return radius;
    }

    public void setRadius(double radius) {
   
        this.radius = radius;
    }
}

/*1)接口Shape:包含方法
       double getArea():求一个形状的面积
       double getPerimeter():求一个形状的周长
       */
public interface Shape {
   
    //求一个形状的面积
    double getArea();
    //求一个形状的周长
    double getPerimeter();
}

//第一步:编写一个类继承Exception或者RunException
//第二步:提供两个构造方法,一个无参的,一个有参的
//设计出一旦出现负数时的异常处理方法
public class NumException extends Exception{
   
    //无参构造
    public NumException() {
   

    }
    //有参构造
    public NumException(String message){
   
        super(message);
    }
}

第二,三题:


import java.util.*;

//利用题2中定义的学生类,创建10个相应的对象,
// 分别用ArrayList、 LinkedList、HashSet、HashMap、TreeMap类
// 的对象来存储这10个对象,检索其中的某2个对象,遍历输出所有对象。
public class StudentTest {
   
    public static void main(String[] args) {
   
        Student student1 = null;
        Student student2 = null;
        Student student3 = null;
        Student student4 = null;
        Student student5 = null;
        Student student6 = null;
        Student student7 = null;
        Student student8 = null;
        Student student9 = null;
        Student student10 = null;
        try {
   
            //创建十个对象
            student1 = new Student(116,"钢铁侠",30,98);
            student2 = new Student(120,"蜘蛛侠",20,80);
            student3 = new Student(111,"美国队长",60,95);
            student4 = new Student(112,"雷神",999,85);
            student5 = new Student(115,"绿巨人",20,60);
            student6 = new Student(114
  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值