CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2014
    Posts
    1

    Don't know how to write a client class to test methods for an assignment

    Write a class encapsualting the concept of a course grade, assuming a course grade
    * has the following attributes: a course name and a letter grade. Include a constructor,
    * the accessor and mutator, and methods toString and equals.Write a client class
    * to test all the methods in your class.

    Here is my code with all the methods except a client class to test all the methods in the class.
    Not sure how to writes the code for it not many tutorials on the subject. Any help is apperiated.

    package labmodule7num57;
    import java.util.*;

    public class LabModule7Num57 {
    // Constructors//
    private String name;
    private String letterGrade;
    public LabModule7Num57 (String name,String letterGrade) {

    this.name = name;
    this.name =letterGrade;
    }



    // Setting the name of course //
    public void setName(String name) {

    this.name = name;

    }
    //Setting the lettergrade of course//
    public void setletterGrade(String letterGrade){
    this.name=letterGrade;
    }
    // Getting the name of the course//
    public String getName() {

    return name;
    }
    // Getting the name of the letter grade//
    public String getletterGrade() {

    return letterGrade;
    }
    // Returning the name of the course and the letter grade//
    public String toString() {

    return name+":"+letterGrade;

    }
    public boolean equals(Object obj){
    if (!(obj instanceof LabModule7Num57))
    return false;
    LabModule7Num57 entry = (LabModule7Num57)obj;
    return this.name.equals(entry.name) &&
    this.letterGrade.equals(entry.letterGrade);
    }
    public int hashCode(){
    int hash = 37;
    hash = hash*17 + this.name.hashCode();
    hash = hash*17 + this.letterGrade.hashCode();
    return hash;
    }


    public static void main(String[] args) {

    }
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Don't know how to write a client class to test methods for an assignment

    In the testing class's main method, add code that creates an instance of the class whose methods are to be tested and then use the reference to that instance of the class to call its methods:
    Code:
    ClassToTest ctt = new ClassToTest(); // create instance of class / may require some args???
    ctt.methodToTest();  // call a method to test it. may require args
    <dataType> var = ctt.methodToTest2(); // call method that returns a value
    //  add code here to print the value of var
    Norm

  3. #3
    Join Date
    Dec 2013
    Posts
    14

    Re: Don't know how to write a client class to test methods for an assignment

    Quote Originally Posted by sdunn2 View Post
    Here is my code with all the methods except a client class to test all the methods in the class.
    Not sure how to writes the code for it not many tutorials on the subject. Any help is apperiated.
    I believe you just need to create two objects with different attributes, test that they are not equal.
    Then again create two objects with same attributes and test that they are equal.
    Also use their setters with your parameters and prompt their getters to show that all your methods work as intended.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured