美文网首页
2017-java期末考试

2017-java期末考试

作者: V0W | 来源:发表于2018-11-08 17:08 被阅读0次

    175 - 逆序输出整数

    Description

    编写程序将整数逆序输出。如输入为9876输出为6789
    Main函数中读入n个整数,输出n个整数的逆序数
    

    Input

    整数个数n
    n个整数
    

    Output

    n个整数的逆序数
    

    Sample Input

    3
    1234
    2323
    1112
    

    Sample Output

    4321
    3232
    2111
    

    Answer

    import java.util.*;
    public class Main{
    
        public static void main(String[] args){
    
            Scanner scanner = new Scanner(System.in);
    
            int numcount = scanner.nextInt();
    
            for (int i=0;i<numcount;i++)
    
            {
    
                String temp = scanner.next();
    
                StringBuffer sb=new StringBuffer(temp);
    
                System.out.println(sb.reverse());
    
            }
    
        }
    
    }
    

    176 - 汽车类

    Description

    2. 编写汽车类,其功能有启动(start),停止(stop),加速(speedup)和减速(slowDown),启动和停止可以改变汽车的状态(on/off),初始时状态为off,速度为0,speedUp和slowDown可以调整汽车的速度,每调用一次汽车速度改变10(加速增10,减速减10),汽车启动后才能加减速,加速上限为160,减速下限为0,汽车速度减为0后才能停止,给出汽车类的定义。
    Main函数中构造一个汽车对象,并对该对象进行操作,各个操作的编号为:
    1. start
    2. stop
    3. speedup
    4. slowdown
    操作完成后打印出汽车的状态和速度。
    

    Input

    操作
    

    Output

    汽车的状态和速度
    

    Sample Input

    8
    1 3 3 4 3 4 4 2
    

    Sample Output

    off 0
    

    Pre Append Code

    import java.util.Scanner;
    public class Main{
        
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int n = s.nextInt();
            Car c = new Car();
            for (int i=0;i<n;i++) {
                int a = s.nextInt();
                switch (a) {
                case 1: c.start(); break;
                case 2: c.stop(); break;
                case 3: c.speedUp(); break;
                case 4: c.slowDown(); break;
                }
            }
            System.out.print(c.status + " ");
            System.out.println(c.speed);
        }
    
    }
    
    

    Answer

    
    
    class Car {
        int speed = 0;
    
        String status = "off";
    
        void start() {
    
                status = "on";
    
        }
    
        void stop() {
    
            if(speed == 0)
    
            status = "off";
    
        }
    
        void speedUp() {
    
            if(status == "on")
    
            {
    
                speed +=10;
    
            }
    
            if (speed>=160)
    
            {
    
                speed = 160;
    
            }
    
        }
    
        void slowDown() {
    
            if (status == "on") {
    
                speed -= 10;
    
                if (speed <= 0) {
    
                    speed = 0;
    
                }
    
            }
    
        }
    
    }
    

    181 - 图书类

    Description

    构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition),提供该类的toString()和equals()方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。
    Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。
    

    Input

    两本书信息
    

    Output

    两本书的打印信息
    两本书是否相等
    

    Sample Input

    ThinkingInJava
    86
    BruceEckel
    4
    CoreJava
    95
    CayS.Horstmann
    10
    

    Sample Output

    name: ThinkingInJava, price: 86, author: BruceEckel, edition: 4
    name: CoreJava, price: 95, author: CayS.Horstmann, edition: 10
    false
    

    Pre Append Code

    import java.util.Scanner;
    public class Main {
        
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            Book b1 = new Book(s.next(),
                    s.nextInt(),
                    s.next(),
                    s.nextInt());
            Book b2 = new Book(s.next(),s.nextInt(),s.next(),s.nextInt());
            
            System.out.println(b1);
            System.out.println(b2);
            System.out.println(b1.equals(b2));
                
        }
    
    }
    
    

    Answer

    
    class Book{
        String name;
    
        int price;
    
        String auther;
    
        int no;
    
        Book(String name,int price,String auther,int no)
    
        {
    
            this.name= name;
    
            this.price= price;
    
            this.auther = auther;
    
            this.no = no;
    
        }
    
        @Override
    
        public String toString() {
    
            String result = "name: "+this.name+", price: "+this.price+", author: "+this.auther+", edition: "+this.no;
    
            return result;
    
        }
    
        @Override
    
        public boolean equals(Object obj) {
    
            Book another = (Book)obj;
    
            if(another.no==this.no&&another.name.equalsIgnoreCase(this.name)&&another.auther.equalsIgnoreCase(this.auther))
    
                return true;
    
            else
    
                return false;
    
        }
    
    }
    

    184 - 图书列表

    Description

    在上题的基础上构建一个书单类BookList,该类中用一个列表类对象存放书单,提供添加图书(addBook)、查找图书(searchBook)的函数
    main函数从键盘输入多个Book添加到书单中,(添加时,提供书的名称、价格、作者、版本号),而后从键盘读入一本书,查找该列表对象中是否包含该书,若包含,输出”found: 该书在列表中的序号”,若不包含,输出“not found”,查找时,提供书的名称、作者、版本号。
    

    Input

    添加书的个数
    添加的书
    查找的书
    

    Output

    查找结果
    

    Sample Input

    2
    ThinkingInJava
    86
    BruceEckel
    4
    CoreJava
    95
    CayS.Horstmann
    10
    CoreJava
    CayS.Horstmann
    10
    

    Sample Output

    found: 1 
    

    HINT

    
    

    Pre Append Code

    import java.util.Scanner;
    

    Post Append Code

    public class Main{
        
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            BookList bl = new BookList();
            int n = s.nextInt();
            for (int i=0; i<n;i++) {
                bl.addBook(new Book(s.next(),
                        s.nextInt(),
                        s.next(),
                        s.nextInt()));
            }
            bl.searchBook(new Book(s.next(),
                        0,
                        s.next(),s.nextInt()));
        }
    }
    
    

    Answer

    
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import java.util.List;
    
    class Book{
    
        String name;
    
        int price;
    
        String auther;
    
        int no;
    
        Book(String name,int price,String auther,int no)
    
        {
    
            this.name= name;
    
            this.price= price;
    
            this.auther = auther;
    
            this.no = no;
    
        }
    
        @Override
    
        public String toString() {
    
            String result = "name: "+this.name+", price: "+this.price+", author: "+this.auther+", edition: "+this.no;
    
            return result;
    
        }
    
        @Override
    
        public boolean equals(Object obj) {
    
            Book another = (Book)obj;
    
            if(another.no==this.no&&another.name.equalsIgnoreCase(this.name)&&another.auther.equalsIgnoreCase(this.auther))
    
                return true;
    
            else
    
                return false;
    
        }
    
    }
    
    class BookList{
    
        List<Book> booklist = new ArrayList<Book>();
    
        void addBook(Book abook){
    
            booklist.add(abook);
    
        }
    
        void searchBook(Book abook){
    
            int count = -1;
    
            for(int i=0;i<booklist.size();i++)
    
            {
    
                if(booklist.get(i).equals(abook))
    
                {
    
                    count = i;
    
                    break;
    
                }
    
            }
    
            if(count == -1){
    
                System.out.print("not found");
    
            }
    
            else
    
            {
    
                System.out.print("found: ");
    
            System.out.print(count);}
    
        }
    
    }
    

    185 - 动物体系

    Description

    基于继承关系编写一个动物体系,具体的动物包含小狗和小猫。每只动物都有名字和颜色,都能够做自我介绍(introduce)。此外,小狗有智商属性(整数),能接飞盘(catchFrisbee(),方法体内输出一行“catch frisbee”即可),小猫有眼睛颜色属性,能抓老鼠(catchMouse(),方法体内输出一行“catch mouse”即可)。各种小动物自我介绍时均介绍自己的姓名和颜色,此外,小狗应介绍自己的智商,小猫应介绍自己的眼睛颜色。小狗介绍时输出”My name is xxx, my color is xxx, my IQ is xxx”, 小猫介绍时输出“My name is xxx, my color is xxx, my eyecolor is xxx”
    构造类TestAnimal,提供静态函数introduce(Animal),对参数动物自我介绍。提供静态函数action(Animal),根据参数对象的实际类型进行活动,如果是小狗,则让其接飞盘,如果是小猫,则让其抓老鼠。
    Main函数中,根据动物类型构造动物,并调用TestAnimal中的方法进行自我介绍(introduce)和活动(action)
    

    Input

    动物类型 动物名称 动物颜色 动物其他属性 如
    1 猫名称 猫颜色 猫眼睛颜色
    2 狗名称 狗颜色 狗的智商
    

    Output

    自我介绍
    活动
    

    Sample Input

    1 Mikey white blue
    

    Sample Output

    My name is Mikey, my color is white, my eyecolor is blue
    catch mouse
    

    HINT

    
    

    Pre Append Code

    import java.util.Scanner;
    

    Post Append Code

    public class Main{
        
        public static void main(String args[]) {
            
            Scanner s = new Scanner (System.in);
            int i = s.nextInt();
            Animal a = null;
            if (i==1) {
                a = new Cat(s.next(), s.next(), s.next());
            } else if (i==2) {
                a = new Dog(s.next(), s.next(), s.nextInt());
            }
            TestAnimal.introduce(a);
            TestAnimal.action(a);
            
        }
        
    
    }
    

    Answer

    
    abstract class Animal {
    
        String name;
    
        String color;
    
        public Animal(String name, String color) {
    
            this.name = name;
    
            this.color = color;
    
        }
    
        abstract public void introduce();
    
        abstract public void action();
    
    }
    
    class Cat extends Animal {
    
        String eyecolor;
    
        public Cat(String name, String color, String eyecolor) {
    
            super(name, color);
    
            this.eyecolor = eyecolor;
    
        }
    
        public void introduce() {
    
            System.out.println("My name is " + name + ", my color is " + color + ", my eyecolor is " + eyecolor);
    
        }
    
        public void action() {
    
            System.out.println("catch mouse");
    
        }
    
    }
    
    class Dog extends Animal {
    
        int cel;
    
        public Dog(String name, String color, int cel) {
    
            super(name, color);
    
            this.cel = cel;
    
        }
    
        public void introduce() {
    
            System.out.println("My name is " + name + ", my color is " + color + ", my IQ is " + cel);
    
        }
    
        public void action() {
    
            System.out.println("catch frisbee");
    
        }
    
    }
    
    class TestAnimal {
    
        public static void introduce(Animal a) {
    
            a.introduce();
    
        }
    
        public static void action(Animal a) {
    
            a.action();
    
        }
    
    }
    

    188 - 单词在句子中的位置

    Description

    给定英文句子,编写方法void wordPositions(String sentence),该方法中计算sentence中的每个单词在句子中的起始位置和单词长度并输出。假设句子中只包含英文字母和空格,且单词不重复。
    

    Input

    句子
    

    Output

    每个单词在句子中的起始位置和单词长度
    

    Sample Input

    Why are you so crazy about java
    

    Sample Output

    Why: 0, 3
    are: 4, 3
    you: 8, 3
    so: 12, 2
    crazy: 15, 5
    about: 21, 5
    java: 27, 4
    

    Answer

    
    import java.util.Scanner;
    
    
     public class Main{
    
        public static void main(String args[]) {
    
            Scanner s = new Scanner (System.in);
    
            int pos = 0;
    
            while(true)
    
            {
    
                String temp = s.next();
    
                if(temp.length()!=0)
    
                {
    
                    System.out.print(temp);
    
                    System.out.print(": ");
    
                    System.out.print(pos);
    
                    System.out.print(", ");
    
                    System.out.println(temp.length());
    
                    pos += temp.length()+1;
    
                }
    
                else{
    
                    break;
    
                }
    
            }
    
        }
    
    }
    

    相关文章

      网友评论

          本文标题:2017-java期末考试

          本文链接:https://www.haomeiwen.com/subject/spgwxqtx.html