5.11java周测

1 实现在控制台输出九九乘法表。
public class Test {
	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("" + i + " * " + j + " = " + (i * j) + "\t");
			}
			System.out.println();
		}
	}
}

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

public class Test2 {

	public static int sum(int a, int b) {
		return a + b;
	}

	public static void main(String[] args) {
		int i = 10;
		int j = 20;

		System.out.println("" + i + " + " + j + " = " + Test2.sum(i, j) + "");
	}
}

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

public class Test3 {
	
	public static void main(String[] args) {
		String[] arr1 = {"a","b","c"};
		String[] arr2 = {};
		String[] arr3 = null;
		System.out.println(printArray(arr1));
		System.out.println(printArray(arr2));
		System.out.println(printArray(arr3));
		
	}
	
	public static String printArray(String[] arr) {
		// [a,b,c]
		String result = "";
		
		if (null == arr) {
			result = "数组为null";
		} else {
			if (arr.length == 0) {
				result = "数组长度为 0";
			} else {
				result = "[";
				
				for (int i = 0; i < arr.length; i++) {
					if (i == arr.length - 1) {
						result += arr[i];
					} else {
						result += arr[i] + ",";
					}
				}
				
				result += "]";
			}
		}
		
		return result;
	}
}

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

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;
	}
	
}

public class Market {
	private String name;
	
	public Market(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}

public class Product {
	private String name;

	public Product(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}

public interface ShoppingServiceInter {
	void shopping(Customer cus, Market market, Product product);
}

public class ShoppingService implements ShoppingServiceInter {

	public void shopping(Customer cus, Market market, Product product) {
		System.out.println("" + cus.getName() + "在" + market.getName() + "买了件" + product.getName() + "");
	}
}

public class Client {
	public static void main(String[] args) {
		Customer customer = new Customer("小明");
		Market market = new Market("欧尚");
		Product product = new Product("T恤");
		
		ShoppingService service = new ShoppingService();
		service.shopping(customer, market, product);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值