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); }}
阅读全文
0 0
- 2017-11-3周测试题
- 2017-10-27周测试题
- 2017-10-27 周测试题
- 2017-10-27 周测试题
- 2017-10-27 周测试题
- ISTQB测试题3
- 测试题3
- [测试]测试题
- 2017阿里巴巴在线测试题
- 阿里巴巴2017测试题--抽奖
- 2017阿里编程测试题
- 第3周小学生测试
- 面试3道测试题
- 测试11
- 测试11
- 测试11
- 测试11
- 测试11
- #Thinking in Java阅读笔记# 第八章 多态
- getchar 和putchar的注意事项
- 字符串反转的两种方式
- CSS三大特性
- Java读取Excel文件,生成SQL语句
- 2017-11-3周测试题
- css去除ios中input和textarea的阴影
- ubuntu设置pptp-vpn访问
- 多线程入门
- Css样式
- svn提交更新还原报错
- 阿里云机器安装Tensorflow和Paddlepaddle
- easyui + webuploader 爬坑集合不定期更新中
- 蓝牙协议分析(5)_BLE广播通信相关的技术分析