Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problems with Assignment in Java II

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with Assignment in Java II

    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 so far not sure how to test and finish the toString and equals method in this code ? Any help would be much appreciated.

    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 and letter grade//
    public void setName(String name,String letterGrade) {

    this.name = name;
    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 static void main(String[] args) {
    // TODO code application logic here
    }
    }


  2. #2
    Junior Member
    Join Date
    Jan 2014
    Location
    Saint-Petersburg, Russia
    Posts
    8
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Problems with Assignment in Java II

    equals may be something like this:
    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);
        }

    I'd add hashCode() function:
    public int hashCode(){
            int hash = 37;
            hash = hash*17 + this.name.hashCode();
            hash = hash*17 + this.letterGrade.hashCode();
            return hash;
        }

Similar Threads

  1. Java assignment- Need help please
    By JavaStudent100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 6th, 2013, 03:35 PM
  2. Replies: 8
    Last Post: February 12th, 2013, 04:45 AM
  3. hello im new to java please help me with my assignment!
    By New to Java in forum Java Theory & Questions
    Replies: 5
    Last Post: January 14th, 2012, 12:01 AM
  4. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM