java-练习2:MobilePhone

本文通过两个Java程序设计案例——Bank和MobilePhone,详细解析了面向对象编程的应用实践。包括如何设计类、方法及其实现逻辑,特别是MobilePhone案例中涉及的矩阵操作、输入处理与输出验证等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,MobilePhone.java

import java.util.*;
public class MobilePhone {
	public static void entrance() {
		Scanner sc=new Scanner(System.in);
		
		int act=sc.nextInt();   //输入的指令数字
		int s=sc.nextInt();		//矩阵大小s
		int a[][]=new int[s][s];
		for(int i=0;i<s;i++)  //初始化矩阵为0,不过好像java定义一个矩阵后自动赋值为0
		{
			for(int j=0;j<s;j++)
			{
				a[i][j]=0;
			}
		}
		
		while(sc.hasNextInt())
		{
			int act2=sc.nextInt();
			if(act2==1)   //act2=1,更新位置上的值
			{
				int x=sc.nextInt();
				int y=sc.nextInt();
				int A=sc.nextInt();
				a[x][y]+=A;
				
			}else if(act2==2)//act2=2,查询活跃手机 数之和,并输出结果
			{
				int L=sc.nextInt();
				int B=sc.nextInt();
				int R=sc.nextInt();
				int T=sc.nextInt();
				int sum=0;
				for(int i=L;i<=R;i++)
				{
					for(int j=B;j<=T;j++)
					{
						sum+=a[i][j];
					}
				}
				System.out.println(sum);
			}else if(act2==3)
			{
				break;
			}
		}
	}
}

2,MobilePhoneTest1.java

import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MobilePhoneTest1 {
	String sep;
	MobilePhone mp;
	PrintStream console = null;
	ByteArrayInputStream in = null;
	ByteArrayOutputStream out = null;
	InputStream input = null;

	@Before
	public void setUp() throws Exception {
		mp = new MobilePhone();
		out = new ByteArrayOutputStream();
		input = System.in;
		console = System.out;
		System.setOut(new PrintStream(out));
		sep = System.getProperty("line.separator");
	}

	@After
	public void tearDown() throws Exception {
		System.setIn(input);
		out.close();
		System.setOut(console);
	}
	
	@Test(timeout=4000)
	public void test1() {
		in = new ByteArrayInputStream(("0 4" + sep + "1 1 2 3" + sep +"2 0 0 2 2" + sep + "1 1 1 2" + sep + "1 1 2 -1" + sep + "2 1 1 2 3" + sep +"3").getBytes());
		System.setIn(in);
		mp.entrance();
		String ans = out.toString();
		assertEquals("3" + sep + "4"+sep, ans);
	}
	
	@Test(timeout=4000)
	public void test2() {
		in = new ByteArrayInputStream(("0 9" + sep + "1 2 5 55" + sep +"2 0 3 5 7"  + sep + "1 3 3 77" + sep + "2 0 0 6 5" + sep +"3").getBytes());
		System.setIn(in);
		mp.entrance();
		String ans = out.toString();
		assertEquals("55" + sep + "132"+sep, ans);
	}
	
	@Test(timeout=4000)
	public void test3() {
		in = new ByteArrayInputStream(("0 110" + sep + "1 77 66 233" + sep +"2 0 0 0 0" + sep + "1 11 22 112" + sep + "1 11 22 112" + sep + "1 3 5 -17" + sep + "2 21 34 5 9" + sep +"3").getBytes());
		System.setIn(in);
		mp.entrance();
		String ans = out.toString();
		assertEquals("0" + sep + "224"+sep, ans);
	}
	
	@Test(timeout=4000)
	public void test4() {
		in = new ByteArrayInputStream(("0 255" + sep + "1 128 64 196" + sep +"2 167 173 47 53" + sep + "2 133 247 150 232"  + sep + "2 152 167 201 37" + sep +"3").getBytes());
		System.setIn(in);
		mp.entrance();
		String ans = out.toString();
		assertEquals("196" + sep + "0" + sep+ "0" + sep, ans);
	}
	
	@Test(timeout=4000)
	public void test5() {
		in = new ByteArrayInputStream(("0 777" + sep + "1 7 7 77" + sep +"2 7 7 7 7" + sep + "1 6 6 -66" + sep + "1 111 111 111" + sep + "2 17 27 37 47" + sep + "2 57 67 77 87" + sep + "2 97 107 127 137" + sep +"3").getBytes());
		System.setIn(in);
		mp.entrance();
		String ans = out.toString();
		assertEquals("77" + sep + "0" + sep + "0" + sep + "111" + sep, ans);
	}
}

 

注:本来以为机试会和以上给的两道练习题(1.Bank, 2.MobilePhone)难度差不多,结果我错了,我以为的只是我以为。第一道面向对象题和给的系统练习题相比,复杂多了,细节也更多,例如判断小数点后面有没有超过两位数, 判断输入的是否为纯字母,如果输入的中文或者其他符号,需要抛出异常,使输出的数字为百分数的形式等等。

一共有四个类

1,商品Project.java,实现设置商品信息,设置商品价格、折扣、名称

2,订单子项OrderItem.java

2,订单Order.java,实现新增订单、打印全部销售记录、获取金额最大的订单

3,商店Shop.java,实现获取商品信息、上架、下架、出售、更新

4,商店老板ShopKeeper.java

Project.java—>Order.java(orders、items、orderitems)—>Shop.java—>ShopKeeper.java

其中有的测试样例一直不过,显示输出的name为null等,最后才明白问题出在哪,是因为在构造函数中为Product赋值商品名称、商品价格、商品折扣时(this.name=setName()),这个赋值用的是一个函数,所以在这个赋值函数setName里,最后一定要进行赋值,忘记加上则就是会一直提醒输出的结果为空,样例一直不会通过。。。。蠢啊蠢,做题时一定要一步步的仔细分析,一点都不能有遗漏。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39450145

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值