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

I need to write a class that encapsulates this program. It should supplement the

ID: 3773938 • Letter: I

Question

I need to write a class that encapsulates this program. It should supplement the constructor, accessors, and mutators. As well as methods like toString and equals.


public class CourseClient
{
public static void main( String [] args )
{
    Course c1 = new Course( "CS1", "Intro to Computer Science", 4 );
    Course c2 = new Course( "CS2", "Intro to Data Structures", 3 );
    System.out.println( "The code of course # 1 is " + c1.getCode( ) );
    System.out.println( "The description of course # 1 is " + c1.getDescription( ) );
    System.out.println( "The number of credits of course # 1 is " + c1.getCredits( ) );
    System.out.println( "Course # 2 is " + c2.toString( ) );

    if ( c1.equals( c2 ) )
      System.out.println( "Original courses #1 and #2 are identical" );
    else
      System.out.println( "Original courses #1 and #2 are different" );

    c2.setCode( "CS1" );
    c2.setDescription( "Intro to Computer Science" );
    c2.setCredits( 4 );

    if ( c1.equals( c2 ) )
      System.out.println( "Original course #1 and modified course #2 are identical" );
    else
      System.out.println( "Original course #1 and modified course #2 are different" );

}
}

Explanation / Answer

CourseClient.java


public class CourseClient
{
public static void main( String [] args )
{
Course c1 = new Course( "CS1", "Intro to Computer Science", 4 );
Course c2 = new Course( "CS2", "Intro to Data Structures", 3 );
System.out.println( "The code of course # 1 is " + c1.getCode( ) );
System.out.println( "The description of course # 1 is " + c1.getDescription( ) );
System.out.println( "The number of credits of course # 1 is " + c1.getCredits( ) );
System.out.println( "Course # 2 is " + c2.toString( ) );
if ( c1.equals( c2 ) )
System.out.println( "Original courses #1 and #2 are identical" );
else
System.out.println( "Original courses #1 and #2 are different" );
c2.setCode( "CS1" );
c2.setDescription( "Intro to Computer Science" );
c2.setCredits( 4 );
if ( c1.equals( c2 ) )
System.out.println( "Original course #1 and modified course #2 are identical" );
else
System.out.println( "Original course #1 and modified course #2 are different" );
}
}

Course.java


public class Course {
   private String code;
   private String description;
   private int credits;
   public Course(String c, String d, int cr){
       code = c;
       description = d;
       credits = cr;
   }
   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 getCredits() {
       return credits;
   }
   public void setCredits(int credits) {
       this.credits = credits;
   }
   public String toString(){
       return "code is "+code+" description is "+description+" credits is "+credits;
   }
   public boolean equals(Course c){
       if(this.code == c.code && this.description==c.description && this.credits==c.credits){
           return true;
       }
       else{
           return false;
       }
   }
}

Output:

The code of course # 1 is CS1
The description of course # 1 is Intro to Computer Science
The number of credits of course # 1 is 4
Course # 2 is code is CS2 description is Intro to Data Structures credits is 3
Original courses #1 and #2 are different
Original course #1 and modified course #2 are identical