从方法返回对象数据类型(Returning object data type from method)

编程入门 行业动态 更新时间:2024-10-24 12:33:47
从方法返回对象数据类型(Returning object data type from method)

我有一些java书,我正在做练习。 但是现在我仍然坚持与数组相关的练习。 我有以下练习:

创建一个CollegeCourse课程。 该类包含课程ID的字段(例如,“CIS 210”),学分(例如,3)和字母等级(例如,“A”)。

为每个字段包含get()和set()方法。 创建一个包含ID号和五个CollegeCourse对象数组的Student类。 为学生ID号创建get()和set()方法。 还要创建一个返回Student's CollegeCourses之一的get()方法; 该方法采用整数参数并返回该位置(0到4)的CollegeCourse。 接下来,创建一个set()方法,用于设置学生大学课程之一的值; 该方法有两个参数 - 一个CollegeCourse和一个表示CollegeCourse位置(0到4)的整数。

我已经完成了学生班的数据领域和id和getter。 但是现在我对此有点困惑:

还要创建一个返回Student's CollegeCourses之一的get()方法; 该方法采用整数参数并返回该位置(0到4)的CollegeCourse。 接下来,创建一个set()方法,用于设置学生大学课程之一的值; 该方法有两个参数 - 一个CollegeCourse和一个表示CollegeCourse位置(0到4)的整数。

任何人都可以指出我正确的方向如何解决这个问题。 因为它的数组章节我认为必须解决数组? 任何帮助,将不胜感激。

编辑:好的,这是我的CollegeCourse课程

公立课程CollegeCourse {

String courseID; int creditHours; char grade; public void setCourseId(String id) { this.courseID = id; } public String getCourse() { return courseID; } public void setHours(int hours) { this.creditHours = hours; } public int getHours() { return creditHours; } public void setGrade(char grade) { this.grade = grade; }

}

这是我的学生班(我被困在这里):

公立班学生{

int id; CollegeCourse[] cc = new CollegeCourse[5]; public void setId(int id) { this.id = id; } public int getId() { return id; } public void setCollegeCourse(CollegeCourse course, int position) { // i'm stuck here }

}

I have some java book and i'm doing exercises. But right now i'm stuck on exercises associated with arrays. i have following exercise:

Create a CollegeCourse class. The class contains fields for the course ID (for example, “CIS 210”), credit hours (for example, 3), and a letter grade (for example, ‘A’).

Include get() and set()methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get() and set() method for the Student ID number. Also create a get() method that returns one of the Student’s CollegeCourses; the method takes an integer argument and returns the CollegeCourse in that position (0 through 4). Next, create a set() method that sets the value of one of the Student’s CollegeCourses; the method takes two arguments—a CollegeCourse and an integer representing the CollegeCourse’s position (0 through 4).

I already did data fields and id's and getters from student class. But right now i'm a little confused with this:

Also create a get() method that returns one of the Student’s CollegeCourses; the method takes an integer argument and returns the CollegeCourse in that position (0 through 4). Next, create a set() method that sets the value of one of the Student’s CollegeCourses; the method takes two arguments—a CollegeCourse and an integer representing the CollegeCourse’s position (0 through 4).

Can anyone point me to the right direction how to resolve this. Because it's array chapter i think it must be resolved doing arrays? Any help would be appreciated.

EDIT: ok here is my CollegeCourse Class

public class CollegeCourse {

String courseID; int creditHours; char grade; public void setCourseId(String id) { this.courseID = id; } public String getCourse() { return courseID; } public void setHours(int hours) { this.creditHours = hours; } public int getHours() { return creditHours; } public void setGrade(char grade) { this.grade = grade; }

}

and here is my student class ( im stuck here):

public class Student {

int id; CollegeCourse[] cc = new CollegeCourse[5]; public void setId(int id) { this.id = id; } public int getId() { return id; } public void setCollegeCourse(CollegeCourse course, int position) { // i'm stuck here }

}

最满意答案

基本上它要求你为数组的内容创建一个getter / setter;

CollegeCourse[] courses... ... public CollegeCourse getCourse(int index) { return courses[index]; } public void setCourse(int index, CollegeCourse course) { courses[index] = course; }

Basically its asking you to create a getter/setter for contents of the array;

CollegeCourse[] courses... ... public CollegeCourse getCourse(int index) { return courses[index]; } public void setCourse(int index, CollegeCourse course) { courses[index] = course; }

更多推荐

本文发布于:2023-07-05 07:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   对象   方法   Returning   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!