Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: write a service class called Course that implements a concept of a course

ID: 3593839 • Letter: J

Question

Java:

write a service class called Course that implements a concept of a course and has the following defined:

three fields: a code (which must be one of "CS1", "CS2", "CS3" or "CS4"), a description (String), and a number of credits as int (for example 3).

default constructor

secondary constructor

accessor methods for each field

mutator methods for each field

toString method

equals method

a "business" method called level, that returns either 1,2,3, or 4 based on the value of code

write a client class called CourseClient. The client class has a main method that should call all the methods defined in the Course class to make sure that they work properly. Please remember to use thiskeyword where appropriate. Submit both classes for grading.

Explanation / Answer

CourseClient.java

public class CourseClient {

public static void main(String[] args) {

Course c1 = new Course("CSS2","AAAAA", 3);

Course c2 = new Course("CSS3","BBBBB", 5);

System.out.println(c1.toString());

System.out.println(c2.toString());

System.out.println(c1.equals(c2));

Course c3 = new Course();

c3.setCode("CS1");

c3.setDescription("CCCCC");

c3.setNumberOfCredits(7);

System.out.println(c3.toString());

}

}

Course.java

public class Course {

private String code, description;

private int numberOfCredits;

public Course() {

}

public Course(String code, String description, int numberOfCredits) {

this.code = code;

this.description =description;

this.numberOfCredits = numberOfCredits;

}

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public int getNumberOfCredits() {

return numberOfCredits;

}

public void setNumberOfCredits(int numberOfCredits) {

this.numberOfCredits = numberOfCredits;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Course other = (Course) obj;

if (code == null) {

if (other.code != null)

return false;

} else if (!code.equals(other.code))

return false;

if (description == null) {

if (other.description != null)

return false;

} else if (!description.equals(other.description))

return false;

if (numberOfCredits != other.numberOfCredits)

return false;

return true;

}

@Override

public String toString() {

return "Course [code=" + code + ", description=" + description

+ ", numberOfCredits=" + numberOfCredits + "]";

}

public int business() {

if(code.equalsIgnoreCase("CS1")) {

return 1;

} else if(code.equalsIgnoreCase("CS2")) {

return 2;

} else if(code.equalsIgnoreCase("CS3")) {

return 3;

} else {

return 4;

}

}

}

Output:

Course [code=CSS2, description=AAAAA, numberOfCredits=3]
Course [code=CSS3, description=BBBBB, numberOfCredits=5]
false
Course [code=CS1, description=CCCCC, numberOfCredits=7]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote