2017-11-3周测试题

来源:互联网 发布:java常用接口 编辑:程序博客网 时间:2024/06/05 15:44

2017-11-3周测试题

1、实现在控制台输出九九乘法表。

package com.test;public class Test1 {    public static void main(String[] args) {        for (int i = 1; i <= 9; i++) {            for (int j = 1; j <= i; j++) {                //每行打印                System.out.print(j + "*" + i + "=" + i*j +"\t");            }            //换行            System.out.println();        }    }}

2、定义方法sum,要求实现两个数之和的运算,要求在main方法中调用。

package com.test;public class Test2 {    //定义相加的方法    public static int sum(int i,int j) {        return i + j;    }    public static void main(String[] args) {        int i = 10;        int j = 20;        System.out.println(i + " + " + j + " = " + Test2.sum(i,j));    }}

3、请写一个方法打印数组的内容,实现遍历数组,要求在main方法中调用。

提示:在main方法中定义一个数组,然后将数组作为参数传给方法,在方法中打印结果”[a,b,c,….]”

package com.test;public class Test3 {    //定义数组遍历的方法    public static String fun(String[] arr) {        String result = "";        //如果数组是null输出提示        if (arr == null) {            result = "您输入的数组为null";             } else {            //如果数组长度是0输出提示            if (arr.length == 0) {                result = "您输入的数组长度为0";            } else {                //遍历数组                //开始时添加前半个"["                result =result + "[";                for (int i = 0; i < arr.length; i++) {                              if (i == arr.length-1) {                        //最后一个不加“,”并且添加后半个"]"                        result += arr[i]+"]";                    } else {                        //每遍历出一个就在其后添加一个","并加一个" "                        result += arr[i] + ", ";                    }                }            }               }        //返回字符串        return result;    }    public static void main(String[] args) {        //定义测试数组        String[] array = {"a", "b", "c", "d"};        String[] array2 = {};        String[] array3 = null;        //调用方法,遍历数组并输出        System.out.println(Test3.fun(array));        System.out.println(Test3.fun(array2));        System.out.println(Test3.fun(array3));    }}

4、请将消费者在商城购物这个场景抽象出类,并编写一个客户端类,实现“小明在欧尚买了一件T恤”这样一个购物行为。

package com.test;//抽象出顾客类public class Customer {    //实例变量姓名    private String name;    //带参构造方法    public Customer(String name) {        super();        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }   }
package com.test;//抽象出商场类public class Shop {    //实例变量商场名称    private String name;    //带参构造方法    public Shop(String name) {        super();        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
package com.test;//抽象出货物类public class Goods {    //创建实例变量货名    private String name;    //带参构造方法    public Goods(String name) {        super();        this.name = name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
package com.test;//购物接口public interface ShoppingServiceInter {    public void shopping(Customer c, Shop s, Goods g);}
package com.test;//接口实现类public class ShoppingService implements ShoppingServiceInter {    public void shopping(Customer c, Shop s, Goods g) {        System.out.println(c.getName() + "在" + s.getName() + "买了一件" + g.getName() + "。");    }}
package com.test;public class Test4 {    public static void main(String[] args) {        //创建顾客对象        Customer customer = new Customer("小明");        //创建商场对象        Shop shop = new Shop("欧尚");        //创建货物对象        Goods good = new Goods("T恤");        ShoppingServiceInter shopServer = new ShoppingService();        shopServer.shopping(customer, shop, good);    }}
原创粉丝点击