JAVA实验六

JAVA实验六


实验六一共四题,附上题目及完整代码。

8702
题目內容:
建立Person类,成员变量为姓名和年龄,具有构造方法、get/set方法。创建NoAgesException类,当年龄为负数或大于200岁抛出异常IllegalArgumentException,正常输出“姓名年…龄从”,键盘输入姓名和年龄建立Person对象,测试该对象。
输入输出说明:
张三 300
年龄数值非法
李四 77
李四…77
代码编辑:

import java.util.Scanner;
class NoAgesException {
    NoAgesException(){
    }
    static void triangle(Person p) throws IllegalArgumentException{
        //start
        if(p.age > 200 || p.age < 0) {
            throw new IllegalArgumentException();
        }
        //end
    }
}
class Person {
    //start
    String name;
    int age;
    public Person(String name,int age){
        this.age=age;
        this.name=name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    //end
}
public class Main{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        Person p=new Person(s.next(),s.nextInt());;
        try {
            //start
            new NoAgesException().triangle(p);
            System.out.println(p.name+"..."+p.age);
            //end
        }catch (IllegalArgumentException e) {
            System.out.println("年龄数值非法");
        }
    }
}

8701
题目內容:
写一个方法void triangle(int a,int b,int c),判断三个参数是否能构成一个三角形。如果不能则抛出异常IllegalArgumentException,显示异常信息:“a,b,c不能构成三角形”;如果可以构成则显示三角形三个边长。在主方法中得到命令行输入的三个整数,调用此方法,并捕获异常。
输入输出说明:
2 4 3
三角形的三边长为:2,4,3
1 2 3
1,2,3不能构成三角形
1 2.0 3
请输入整数作为三角形的边长!
代码编辑:

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
class TestTriangle {
    static void triangle(int a, int b,int c) throws IllegalArgumentException,InputMismatchException{
        //starts
        if(a+b<=c||a+c<=b||b+c<=a) {
            throw new IllegalArgumentException();
        }
        if(a%1!=0||b%1!=0||c%1!=0){
            throw new InputMismatchException();
        }
        //end
    }
}
public class Main {
    public static void main(String[] args) {
        int a=0, b=0, c=0;
        Scanner in = new Scanner(System.in);
        a = in.nextInt();
        b = in.nextInt();
        c = in.nextInt();
        try{
            //starts
            new TestTriangle().triangle(a,b,c);
            System.out.println("三角形的三边长为:"+a+","+b+","+c);
            //end
        }
        catch (IllegalArgumentException e){
            System.out.println(a+","+b+","+c+"不能构成三角形");
        }
        catch (InputMismatchException e){
            System.out.println("请输入整数作为三角形的边长!");
        }
    }
}

8700
题目內容:
模拟向货船上装载集装箱,每个集装箱有一定重量,该重量(整数)大于100小于1000,货船总重为1000,装载若干集装箱后,如果货船超重,那么货船认为这是一个异常,将拒绝装载集装箱,但无论是否发生异常,货船都需要正点启航。
输入输出说明:
199
目前装载了199吨货物
400
目前装载了599吨货物
300
目前装载了899吨货物
300
超重
无法再装载重量是300吨的集装箱
货船将正点启航
本题为代码拼接题请注意以下说明:
你只能在代码输入框中:"//start(或#start)“行的下面,”//end(或#end)“行的上面输入你的代码,
而不能改变”//start(或#start)“以及其上所有行的代码,包括添加空格与空行,
也不能改变”//end(或#end)"以及其下所有行的代码,包括添加空格与空行.

import java.util.Scanner;
class DangerException extends Exception {
    //starts
    public void print(int m){
        System.out.println("超重");
        System.out.println("无法再装载重量是"+ m+"吨的集装箱");
    }
    //end
}
class CargoBoat {
    int realContent;  //装载的重量
    int maxContent;   //最大装载量
    public void setMaxContent(int c) {
        maxContent = c;
    }
    public void loading(int m) throws DangerException {
        //starts
        if(realContent+m>maxContent){
            throw new DangerException();
        }
        realContent+=m;
        //end
    }
}
public class Main {
    public static void main(String args[]) {
        CargoBoat ship = new CargoBoat();
        Scanner s=new Scanner(System.in);
        ship.setMaxContent(1000);
        int m=0;
        try{
            //starts
            while(true) {
                m = s.nextInt();
                ship.loading(m);
                System.out.println("目前装载了" + ship.realContent + "吨货物");
            }
            //end
        }
        catch(DangerException e) {
            //starts
            e.print(m);
            //end
        }
        finally {
            System.out.printf("货船将正点启航");
        }
    }
}

8691
题目內容:
从键盘输入一个整形数n,如果输入正确的话,输出10-n后的值,如果输入错误的话输出“not int”
最后输出end
输入输出说明:
输入:
asd
输出:
not int
end
代码编辑:

import java.util.Scanner;
class InputException extends Exception{
    public void warnMess (){
        System.out.println("not int");
    }
}
public class Main {
    public static void main(String[] args){
        //write code here
        Scanner reader = new Scanner(System.in);
        try {
            if(!reader.hasNextInt()){
                throw new InputException();
            }
            else{
                System.out.println(10-reader.nextInt());
            }
        }
        catch (InputException e) {
            e.warnMess();
        }
        finally {
            System.out.println("end");
        }
    }
}
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Write a class called Person with the following attributes: title (Mr., Mrs., Ms., etc.) first name last name nickname age in years sex (boolean - true/false to indicated either male or female) Write a constructor that takes no parameters and performs no initializations. Write a constructor that takes a parameter for each of the attributes listed above and sets them within the objects by calling the setter methods listed below. The Person class should have a setter method and a getter method with public access for each attribute. In the setter methods, get rid of any leading or trailing spaces (String trim() method). For a Person with the following attributes: title = "Mr." first name = "Michael" last name = "Zheng" nickname = "Mike" age = 22 sex = true (true is male, false is female) The Person class should have the following public access methods that return Strings as follows: standardName() concatenation of the first and last names (i.e., "Michael Zheng") formalName() concatenation of the title, first name, lastname (i.e., "Mr. Michael Zheng") casualName() return the nickname if it is not null, otherwise return the first name (i.e., "Mike") Be realistic when generating names. If a particular attribute does not exist for a given person, don't try to concatenate it. If necessary, add appropriate spacing and punctuation, but do not leave any leading or trailing spaces in the String that is returned. MakePerson Write a class called MakePerson with a main() method that instantiates 2 Person objects. Initialize the attributes of one of the Person objects by supplying parameters to it's constructor. Instantiate the other Person object with the default constructor (that does not accept any parameters), then set it's attributes via the appropriate setter methods. For each of the Person objects, execute and print (System.out.println()) the results of all of the getter methods and of the standardName(), formalName(), and casualName() methods.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值