hey guys, my code requires a toString method and an equals method. My question is, did i do them correctly? im a bit confused on this topic, Any help, im greatful for, and the toString and equals methods are at the bottom, Thank you so much.

import java.util.*;

class Scores
{
	public double quiz, quizPercentage, totalPoints, total;
	public double midTerm, midTermPercent;
	public double finalExam, finalPercent, friendFinalExam;
	public String grade, gradeM, gradeF, classGrade, schoolName, friendSchool;
	
	Scanner keyboard = new Scanner(System.in);
	
	public double setQuizes(int number)
	{
		System.out.println("Enter your grade for quiz "+number);
		
		quiz = keyboard.nextDouble();
		return quiz;
	}
	
	public double setmidTermGrade(int number)
	{
		System.out.println("Enter the grade of your mid term exam ");
		midTerm = keyboard.nextDouble();
		return midTerm;
	}
	
	public double setfinalExamGrade(int number)
	{
		System.out.println("Enter the grade of your the final exam ");
		finalExam = keyboard.nextDouble();
		return finalExam;
	}
	
	public String setnameSchool()
	{
		System.out.println("Enter the name of your school");
		schoolName = keyboard.nextLine();
		return schoolName;
	}
	
	public String setfriendSchool()
	{
		System.out.println("Enter the name of your friend's school");
		friendSchool = keyboard.nextLine();
		return friendSchool;
	}
	
	public double setfriendFinal()
	{
		System.out.println("Enter your friends final exam score");
		friendFinalExam = keyboard.nextDouble();
		return friendFinalExam;
	}
	
    public String getquizGrade(double quiz)
    {
    	if (quiz>=0 && quiz<=10)
		{	
		
			
				if (quiz<=10 && quiz>=9)
				{
					grade = "A";
				}
				else if (quiz<9 && quiz>=8)
				{
					grade = "B";
				}
				else if (quiz<8 && quiz>=7)
				{
					grade = "C";
				}
				else if (quiz<7 && quiz>=6)
				{
					grade = "D";
				}
				else if (quiz<6 && quiz>= 0)
				{
					grade = "F";
			    }
          
		 }
	           return grade;		
	}
	
	public String getmidTerm(double midTerm)
	{
		if (midTerm>=0 && midTerm<=100)
		{	
			
				if (midTerm<=100 && midTerm>=90)
				{
					gradeM = "A";
				}
				else if (midTerm<90 && midTerm>=80)
				{
					gradeM = "B";
				}
				else if (midTerm<80 && midTerm>=70)
				{
					gradeM = "C";
				}
				else if (midTerm<70 && midTerm>=60)
				{
					gradeM = "D";
				}
				else if (midTerm<60 && midTerm>=0)
				{
					gradeM = "F";
			    }

		}
		        return gradeM;		
	}
	
   	public String getfinalExam(double finalExam)
	{
		if (finalExam>=0 && finalExam<=100)
		{	
			
				if (finalExam<=100 && finalExam>=90)
				{
					gradeF = "A";
				}
				else if (finalExam<90 && finalExam>=80)
				{
					gradeF = "B";
				}
				else if (finalExam<80 && finalExam>=70)
				{
					gradeF = "C";
				}
				else if (finalExam<70 && finalExam>=60)
				{
					gradeF = "D";
				}
				else if (finalExam<60 && finalExam>=0)
				{
					gradeF = "F";
			    }

		}
		        return gradeF;	
	
	}
	
	public double percentQuizes(double number1, double number2, double number3)
	{
		total = number1 + number2 + number3;
		quizPercentage = ((total*25)/30);
		return  quizPercentage;
		
	}
	
	public double percentMidTerm()
	{
		midTermPercent = ((midTerm*35)/100);
		return midTermPercent;
	}
	
	public double percentFinal()
	{
		finalPercent = ((finalExam*40)/100);
		return finalPercent;
	}
	
	public double gradePercentage()
	{
		totalPoints = quizPercentage + midTermPercent + finalPercent;
        return totalPoints;
	}
	
	public String getclassGrade(double totalPoints)
	{
		if (totalPoints>=0 && totalPoints<=100)
		{	
			
				if (totalPoints<=100 && totalPoints>=90)
				{
					classGrade = "A";
				}
				else if (totalPoints<90 && totalPoints>=80)
				{
					classGrade = "B";
				}
				else if (totalPoints<80 && totalPoints>=70)
				{
					classGrade = "C";
				}
				else if (totalPoints<70 && totalPoints>=60)
				{
					classGrade = "D";
				}
				else if (totalPoints<60 && totalPoints>=0)
				{
					classGrade = "F";
			    }

		}
		        return classGrade;	
	
	}
	
	public String toString()
	{
		String str = "School name is: " + schoolName
		             + "\nFinal exam is: " + finalExam;
		                      
		             
		             
		return str;
	}
	

	public boolean equals(Scores object2)
	{
		boolean status;
		
		if (schoolName.equals(object2.friendSchool) &&
		   friendFinalExam == object2.finalExam)
		  status = true;
		else
		  status = false;
		  
		return status;
	}
	

}

Recommended Answers

All 5 Replies

i think that you didn't declear this
public static void main(String ary[])

Oh i don't need the main args because this is my class. I've already created my tester but i just need help on creating or developing the toString and equals methods. Thank you so much

Your toString looks fine. You could add more of the data items if you want.
The equals method seems valid - but not what I would expect. It seems to say that two scores are "equal" if one of them has the same data as the other's "friend" data.
Normally equals means that two objects are equivalent - have the same data, so I would expect to see something more like (eg)
schoolName.equals(other.schoolName) && finalExam == other.finalExam

ps Your logic for the equals is rather long. You can directly return the boolean result of a logical test without creating and setting a boolean, eg
return (schoolName.equals(other.schoolName) && finalExam == other.finalExam);

Ohh okay. Now im comprehending a bit more. Thanks alot. I worked all day and night trying to figure this out. Reading the book didn't really help on these two topics. But here i get much more. Thank you, so all you suggest is exchanging object2 to otherObject?

Not quite. It doesn't matter what you call the parameter.
I was worried that you were comparing the main values in one object with the "friend" values in the other object,which is not what I would think of as "equals".
Normally you would compare the same variables in the two objects to see if they had the same values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.