帮助学生班 [英] help with Student class

查看:81
本文介绍了帮助学生班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是问题:


创建一个具有实例数据名,studentNumber,类的Student(其中class是包含以下内容之一的String:?Freshman?,?大二? ,?Junior?,?Senior?。

让类实现Comparable接口。包含一个toString方法。

编写一个驱动程序来演示你的工作。

实例化Student类的几个对象。

多次调用您的方法以显示该类已订购。


继承人我做了什么到目前为止:(我在编写compareTo方法时遇到问题)感谢您的帮助

展开 | 选择 | Wrap < span class =codeDivider> | 行号

解决方案

< blockquote> 1)请使用代码周围的[code]标签,以便于阅读。

2)你声明4个静态整数,但不要给它们任何值 - 也许这是你应该从哪里开始?

3)请记住,如果当前对象小于给定对象,compareTo方法将返回-1,如果当前对象大于给定对象,则返回1,如果当前对象大于给定对象,则返回0他们是平等的。因此,如果两个学生在同一年,则返回0.否则,您将不得不执行一些额外的检查。


或者,您可以添加一个实例变量,指示他们在哪一年 - 在构造函数中,如果给出的字符串是新生,那么学生在他/她的第一年,并且新变量将保持1.大二学生将有2,大三学生和大四。然后你可以比较这个整数,而不是试图比较字符串。


我仍然遇到麻烦,但尝试了你的东西......这是对的吗?


展开 | 选择 | Wrap | 行号


1)静态变量看起来不错,但现在我必须问你是否曾经使用它们?

2)你的问题规范说明studentYear必须是一个String。这意味着您必须将其保留为String,而不是int。我的建议是添加一个额外的变量 - 因为你有一个String studentYear,int yearNumber怎么样?


3)compareTo对象只接受1个对象参数。签名是:

展开 | 选择 | Wrap | 行号


Heres the problem:

Create a class Student with instance data name, studentNumber, class (where class is a String containing one of the following: ?Freshman?, ?Sophomore?, ?Junior?, ?Senior?.
Make the class implement the Comparable interface. Include a toString method.
Write a driver program to demonstrate your work.
Instantiate several objects of the Student class.
Call your methods several times to show that class is ordered.

Heres what i have done so far: (i am having trouble writing the compareTo method) Thanks for any help

Expand|Select|Wrap|Line Numbers

解决方案

1) Please use the [code] tags around your code to make it easier to read.
2) You declare 4 static integers, but don''t assign them any value - maybe this is where you should start?
3) Remember that the compareTo method will return -1 if the current object is less than the given object, 1 if the current object is greater than the given object, and 0 if they are equal. So if two students are in the same year, return 0. Otherwise, you will have to perform some additional checks.

Alternatively, you could add an instance variable indicating what year they are in - in the constructor, if the String given is Freshman, then the Student is in his/her 1st year, and the new variable would hold 1. A sophomore would have 2, a junior 3, and a senior 4. Then you can just compare this integer rather than trying to compare Strings.


i am still having trouble but tried what you sugested...is this right?


Expand|Select|Wrap|Line Numbers


1) The static variables look good, but now I must ask if you ever use them?
2) Your problem specification states that studentYear must be a String. This means you have to keep it as a String, not as an int. My suggestion was to add an additional variable - since you have a String studentYear, how about an int yearNumber?

3) The compareTo object takes only 1 object argument. The signature is:

Expand|Select|Wrap|Line Numbers


这篇关于帮助学生班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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