Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

Java
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
Posted
Updated 13-Mar-18 0:47am
v2
Comments
ZurdoDev 12-Mar-18 16:27pm    
Does your code do all that it is supposed to do?
Mohibur Rashid 12-Mar-18 19:28pm    
Your setCreditHours is wrong. Your getCreditHours is wrong. Your code is not supposed to compile. What is the point of using member variable you you have to call the function?
Richard MacCutchan 13-Mar-18 5:42am    
Use the compiler and runtime to test it, and see what errors are produced. You will learn so much more by trying it yourself.

1 solution

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;}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900