带数组的类和测试类 [英] Class and test class with arrays

查看:84
本文介绍了带数组的类和测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求创建一个类



属性



name,String

job,String

roles [],用于标识角色的字符串数组

of people



构造函数:

•公共办公室(字符串作业)

•公共办公室(字符串作业,字符串

角色)


所有属性的所有获取者和设置者



以及;



public void addRoles(String roleName)



public void deleteRoles(String roleName)



然后我被要求创建一个测试类以确保其正确性



我尝试过:



我不知道从哪里开始,尤其是阵列。我设法获得getter和setter方法,但坚持使用整个数组并添加/删除方法

I have been asked to create a class with

Attributes

name, String
job, String
roles[], Array of Strings that identifies the roles
of people

Constructors:
• public Office(String job)
• public Office(String job, String
roles)

All Getters and setters for all the attributes

as well as;

public void addRoles(String roleName)

public void deleteRoles(String roleName)

Then I have been asked to create a test class to ensure its correctness

What I have tried:

I dont know where to start, especially with the arrays. I managed to get getters and setters methods, but stuck with the whole arrays and adding/removing methods

推荐答案

我们不这样做;为你做功课 - 如果有的话,你不会学到很多东西,如果我们做的话!



所以......时间开始阅读:Arrays(The Java™Tutorials) [ ^ ]

因为java数组是固定大小的,所以你需要为您的角色数组维护一个下一个自由元素的索引。您可以通过将项目放入该数组索引中来添加项目,并递增索引。

删除更复杂,您需要找到角色及其索引,然后将所有元素洗牌在减少自由元素指数之前,将指数下调一个。



听起来很复杂?这不是真的。在纸上试一试,或者用阵列元素的部分玩牌,你会看到它真的很容易!
We dont; do your homework for you - you wouldn;t learn much, if anythign, if we did!

So ... time to start reading: Arrays (The Java™ Tutorials)[^]
Because java arrays are a fixed size, you will need to maintain an index of teh "next free element" for your roles array. You can add an item by putting it in that array index, and incrementing the index.
Deleting is more complex, you will need to find the role and it's index, then "shuffle" all the elements with a higher index down one, before reducing the "free element" index.

Sounds complicated? It isn't really. try it on paper, or with playing cards acting out the part of array elements, and you'll see how easy it really is!


参见 Java™教程 [ ^ ]。


我们不做你的HomeWork。

HomeWork不会测试你的技能乞求其他人做你的工作,它会让你思考并帮助你的老师检查你对所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


这篇关于带数组的类和测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆