Problem: Develop a class encapsulating the concept of a college course, assuming that a course has followingattributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include aconstructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test thebehavior of the class and its methods.The outline of the class is given as follows:public class Course {private String course;private String description;private int credits;private int maxStudents;public Course( ) { }public Course (String course, String desc, int credits, int maxNum ) { }public String getCourse ( ) { }public void setCourse ( String c ) { }public String getDescription ( ) { }public void setDescription ( String desc ) { }public int getCredits ( ) { }public void setCredits ( int cred ) { }public int getStudents ( ) { }public void setStudents ( int students ) { }public String toString ( ) { }public boolean equals ( Object obj ) { }public void finalize ( ) { }}ALL METHODS MUST BE IMPLEMENTED!!!Write a client program(test class) to THOROUGHLY test the class Course. In the client program, create two Courseobjects. Assign any values to the two objects. Exercise the methods to verify their functionalityApproach to Testing:• Create TWO class objectso One object with the default constructor Test the set methods initializing the instance variables to the desired values. Use the get methods to verify that the ‘set’s worked properly.o One object with the overloaded constructor – setting unique values in instance variables• Test the ‘set’ methods with the default object• Test the ‘get’ methods – System.out.println ( “course = “ + obj.getCourse() ).• Test the ‘toString’ method – never EXPLICITLY call the ‘toString’ method.Two separate files:• Course.java• CourseTest.java (client program) ( or other name )Approach to Testing:•Create an object with the default constructoro Verify and print to insure default values were assigned. Use the ‘get’ methods coupled with outputstatements.o Initialize all of the instance variables using the ‘set’methods.o Verify and print the values of the instance variables usingthe ‘toString’ method.•Create an object with the overloaded constructor.o Verify and print the values of the instance variables usingthe ‘toString’ method.•Verify each of the other methods –o Equals method – needs three objects, 2 equal, 2 unequal Compare equals Compare unequal’so ‘toString’ methodo Finalize method.o Others.

Question
Asked Mar 25, 2019
243 views

Problem: Develop a class encapsulating the concept of a college course, assuming that a course has following
attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a
constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the
behavior of the class and its methods.


The outline of the class is given as follows:


public class Course {
private String course;
private String description;
private int credits;
private int maxStudents;
public Course( ) { }
public Course (String course, String desc, int credits, int maxNum ) { }
public String getCourse ( ) { }
public void setCourse ( String c ) { }
public String getDescription ( ) { }
public void setDescription ( String desc ) { }
public int getCredits ( ) { }
public void setCredits ( int cred ) { }
public int getStudents ( ) { }
public void setStudents ( int students ) { }
public String toString ( ) { }
public boolean equals ( Object obj ) { }
public void finalize ( ) { }
}


ALL METHODS MUST BE IMPLEMENTED!!!
Write a client program(test class) to THOROUGHLY test the class Course. In the client program, create two Course
objects. Assign any values to the two objects. Exercise the methods to verify their functionality

Approach to Testing:
• Create TWO class objects
o One object with the default constructor
 Test the set methods initializing the instance variables to the desired values.
 Use the get methods to verify that the ‘set’s worked properly.
o One object with the overloaded constructor – setting unique values in instance variables
• Test the ‘set’ methods with the default object
• Test the ‘get’ methods – System.out.println ( “course = “ + obj.getCourse() ).
• Test the ‘toString’ method – never EXPLICITLY call the ‘toString’ method.
Two separate files:
• Course.java
• CourseTest.java (client program) ( or other name )
Approach to Testing:
•Create an object with the default constructor
o Verify and print to insure default values were assigned.
 Use the ‘get’ methods coupled with output
statements.
o Initialize all of the instance variables using the ‘set’
methods.
o Verify and print the values of the instance variables using
the ‘toString’ method.
•Create an object with the overloaded constructor.
o Verify and print the values of the instance variables using
the ‘toString’ method.
•Verify each of the other methods –
o Equals method – needs three objects, 2 equal, 2 unequal
 Compare equals
 Compare unequal’s
o ‘toString’ method
o Finalize method.
o Others.

check_circle

Expert Answer

Step 1

The program has the following algorithm:

 

Course.java

  • Create default constructors.
  • Test the “set” methods initializing the instance variables to the desired values.
  • Use the “get” methods to verify that the “set” methods.
  • Use “hashcode()” method to calculate and return the “result”.
  • Use “equals()” methods to check whether the object is equals to “true” or “false”.
  • Use “toString()” methods to return the values of the instance variables.
  • Finally, verify each of the other methods.

CourseTest.java

  • Create object with the default constructor.
  • Test all the “setter” functions using the object of the default constructor.
  • Test all the “getter” functions using the object of the default constructor.
  • Create an object with the overload constructor.
  • Verify and print the values of the instance variables using the “toString()” methods.
  • Finally, verify each of the other methods using the object of an overload constructor.
Step 2

The java program is:

Course.java

//Define the class
class Course
{
//Declare the necessary variables
private String course;
private String description;
private int credits;
private int maxStudents;

//Define the empty constructor
public Course()
{
}

//Define the parameterized constructor
public Course(String course, String desc, int credits, int maxNum)
{
//Assign values into the variables
this.course = course;
description = desc;
this.credits = credits;
maxStudents = maxNum;
}

//Define the function getCourse()
public String getCourse()
{
//Return the value
return course;
}

//Define the function setCourse()
public void setCourse(String c)
{
//Return the value
course = c;
}

//Define the function getDescription()
public String getDescription()
{
//Return the value
return description;
}

//Define the function setDescription()
public void setDescription(String desc)
{
//Return the value
description = desc;
}

//Define the function getCredits()
public int getCredits()
{
//Return the value
return credits;
}

//Define the function setCredits()
public void setCredits(int cred)
{
//Assign the value into the varoable "credits"
credits = cred;
}

//Define the function getStudents()
public int getStudents()
{
//Return the value
return maxStudents;
}

//Define the function setStudents()
public void setStudents(int students)
{
//Assign the value into the varoable "maxStudents"
maxStudents = students;

}

//Define the function hashCode()
public int hashCode()
{
//Initialize the variables
final int prime = 31;
int result = 1;

//Calculate the result
result = prime * result + ((course == null) ? 0 : course.hashCode());
result = prime * result + credits;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + maxStudents;

//Return the result
return result;
}

//Define the function equals()
public boolean equals(Object obj)
{
//Check the condition
if (this == obj)

//Return "true"
return true;

//Check the condition
if (obj == null)

//Return "false"
return false;

//Check the condition
if (getClass() != obj.getClass())

//Return "false"
return false;

//Create object for the class Course
Course other = (Course) obj;

//Check the condition
if (course == null)
{
//Check the condition
if (other.course != null)

//Return "false"
return false;

}

//Check the condition
else if (!course.equals(other.course))

//Return "false"
return false;

//Check the condition
if (credits != other.credits)

//Return "false"
return false;

//Check the condition
if (description == null)
{

//Check the condition
if (other.description != null)

//Return "false"
return false;
}

//Check the condition
else if (!description.equals(other.description))

//Return "false"
return false;

//Check the condition
if (maxStudents != other.maxStudents)

//Return "false"
return false;

//Return "true"
return true;
}

//Define the function toString()
public String toString()
{

//Return the value
return "Course [course=" + course + ", description=" + description + ", credits=" + credits + ", maxStudents="
+ maxStudents + "]";
}

//Define the function finalize()
public void finalize()
{
//Print the statement
System.out.println(this);
}
}

Step 3

CourseTest.java

//Define the class
public class CourseTest
{
//Define the main() function
public static void main(String[] args)
{
//Testing default constructor
//Create object for the class Course
Course c1 = new Course();

//Testing setters
//Call the method setCourse()
c1.setCourse("Java");

//Call the method setCredits()
c1.setCredits(10);

//Call the method setDescription()
c1.setDescription("Java Programming");

//Call the method setStudents()
c1.setStudents(50);

//Testing gettters

//Print the statement
System.out.println("Course : " + c1.getCourse());

//Print the statement
System.out.println("Credits : " + c1.getCredits());

//Print the statement
System.out.println("Description: " + c1.getDescription());

//Print the statement
System.out.println("Max Students : " + ...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: I need guidance on this, more so what direction do I take to append to a string and make sure it is ...

A: To append the character "D" and "A" into string you can use append(); function of string class.  As ...

question_answer

Q: Write a grammar for the language consisting of strings built only of theletters aand b. The strings ...

A: The given language L contains the strings that have infinite number of letters but the strings must ...

question_answer

Q: How do I read a sequence of values in a file into a arraylist in a method? It's in java progarmming.

A: We need to use Scanner calss. In which java.util package used for obtaining the input of the primiti...

question_answer

Q: 6. Write a class with a constructor that accepts a String object as its argument. The class should h...

A: Two java.util classes are used – Scanner and Arrays. Scanner is used to read input string and Arrays...

question_answer

Q: This code is for Python

A: Note: The below program has been complied and executed in "repl.it" online complierThe screenshot an...

question_answer

Q: Transform the grammar S → aSaaA|A, A → abA|bb into Chomsky normal form.

A:  For solving your problem , first of all we will discuss about  algorithm to convert grammar into  C...

question_answer

Q: Develop the ‘Shape’ application such that: Implement an array of objects of various types (all SIX ...

A:  Program to develop a ‘Shape’ application, by implementing an array of objects of the given six clas...

question_answer

Q: Note: Numbers greater then 3,999 require symbols with lines over letters. We can't use these, so use...

A: Note: The given python program has been compiled and executed in "repl.it" online complier.Screensho...

question_answer

Q: 4.23 (Financial application: payroll) Write a program that reads the following information and print...

A: C code#include<stdio.h>#include<string.h> int main(){char employeeName[20]="";double hou...