2017-java期末考试

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;

            }

        }

    }

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 227,428评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,024评论 3 413
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 175,285评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,548评论 1 307
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,328评论 6 404
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,878评论 1 321
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,971评论 3 439
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,098评论 0 286
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,616评论 1 331
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,554评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,725评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,243评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,971评论 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,361评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,613评论 1 280
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,339评论 3 390
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,695评论 2 370

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,775评论 18 139
  • 116 - Person类 Description Input Output Sample Input Sampl...
    V0W阅读 1,678评论 0 1
  • 今天我们学校进行了一场默写和一场考试。其中我默写43分,考试72分。默写是中等,考试是中上。最近我的英语成绩...
    杰西卡武阅读 133评论 1 0
  • 你的儿女,其实不是你的儿女。 他们是生命对于自身渴望而诞生的孩子。 他们借助你来到这个世界,却非因你而来, 他们在...
    玘皓阅读 255评论 0 0
  • 七十六、 普献法师 这一篇故事是我看过一位法师所写出来,他在日志里写说民国三十七年在湖南省衡阳县某地方有一座寺庙,...
    谦与默阅读 433评论 0 0