需要有关java对象的帮助&班? [英] Need help on java objects & classes?

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

问题描述

这里提出的问题是:构建一个名为Course.java的类。此类应具有4个属性:CourseId,CourseName,Description和credit hours。还要添加适当的set和get方法。添加一个display()方法,将这4个属性显示到DOS窗口。最后添加一个main()方法进行测试。在main()方法中实例化一个Course对象,使用set方法填充数据,然后调用display方法显示数据。



我只是想确保我的代码是正确的&如果我的代码中有错误,你可以建议如何修复它们。



这是我的代码:



我尝试了什么:



Here what the question ask: Build a class called Course.java. This class should have 4 properties: CourseId, CourseName, Description, and credit hours. Also add the appropriate set and get methods. Add a display() method to display these 4 properties out to the DOS window. Lastly add a main() method for testing. In the main() method instantiate a Course object, fill it with data using the set methods, then call the display method to display the data.

I just want to make sure is my code right & if there are mistake in my code can you suggestions of how to fix them.

Here is my code:

What I have tried:

public class Course {

//   ========================== Properties ===========================
private int courseid;
private String courseName;
private String description;
private String creditHours;

//   ==========================  Behaviors  ==========================
public void setCourseId(int c) { courseid = c; }
public int getCourseId() { return courseid;}

public void setCourseName(String cn) { courseName = cn; }
public String getCourseName() { return courseName;}

public void setDescription(String d) { description = d; }
public String getDescription() { return description;}

public void setCreditHours(int ch) { ch = 4; }
public int getCreditHours() { return ch;}

//Returning String
public String toString() {
    return courseName + ":" + description + ":" + creditHours;
}

public void display() {
    System.out.println("Course ID             = " + getCourseId());
    System.out.println("Course Name      = " + getCourseName());
    System.out.println("Description      = " + getDescription());
    System.out.println("Credit Hours           = " + getCreditHours());

} //end display()





public static void main(String args []) {

    Course c1;
    c1 = new Course();

    c1.setCourseId(109);
    c1.setCourseName("Intro to Python");
    c1.setDescription("This course intros the Python Prog Lang.");
    c1.setCreditHours(4);

    c1.display();


    //Test out toString() method
    System.out.println(c1);
} //end main

推荐答案

您的代码几乎没问题。

更改 creditHours 的类型:

Your code is almost OK.
Change the type of creditHours:
private int creditHours;



及其访问者:


and its accessors:

public void setCreditHours(int ch) { creditHours = ch; }
public int getCreditHours() { return creditHours;}


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

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