• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED Calling a Method within the same class

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

funguygordy

Member
Joined
Feb 15, 2011
Well I am stuck on the following java code:

Code:
import javax.swing.JOptionPane;

public class College {

	private static void accepted
		(double testGPA, int testScore){
		if (testGPA >= 0.0 && testGPA <= 4.0 && testScore >= 0 && testScore <= 100){
		if(testGPA >= 3.0 && testScore >= 60 || testScore >= 85){
		System.out.println("Student is ACCEPTED to university!");
		System.out.println("Students GPA is a " + testGPA + " and students test score is a " + testScore + "%");
}
		else {
		System.out.println("Student is NOT ACCEPTED to university!");
		System.out.println("Students GPA is a " + testGPA + " and students test score is a " + testScore + "%");
}
}
		else{
		System.out.println("Input INVALID please Check GPA and Test score input!");
		System.out.println("Your inputs were:");
		System.out.println(testGPA + " = GPA should be between 0.0 and 4.0.");
		System.out.println(testScore + " = Test Score should be between 0 and 100");
}
}

	public static void main(String[] args) {
		String gpaString, testString;

		gpaString = JOptionPane.showInputDialog(null,
		"Please enter your GPA.", "GPA",
		JOptionPane.PLAIN_MESSAGE);

		testString = JOptionPane.showInputDialog(null,
		"Please enter your entrance exam score.", "Test Score",
		JOptionPane.PLAIN_MESSAGE);

		double testGPA = Double.parseDouble(gpaString);
		int testScore = Integer.parseInt(testString);

}
}

The main method should be calling the "accepted" method and giving it the user input for testGPA and testScore. The class compiles and will accept the input but it does not apply the "if" commands. Any pointers would be appreciated!
 
your running into ambiguously defined logical conditions. You need to manually group them so the compiler knows what your asking it to check. Your code also never calls the method accepted. I would also note that you have no means of protecting this program from crashing IF someone put a non number into one of the JOptionPanes which you might want to address.

Code:
import javax.swing.JOptionPane;

public class College {

  private static void accepted(double testGPA, int testScore){
    if (testGPA >= 0.0 && testGPA <= 4.0 && testScore >= 0 && testScore <= 100){
      
      if((testGPA >= 3.0 && testScore >= 60) || testScore >= 85){
        System.out.println("Student is ACCEPTED to university!");
        System.out.println("Students GPA is a " + testGPA + " and students test score is a " + testScore + "%");
      }
      else {
        System.out.println("Student is NOT ACCEPTED to university!");
        System.out.println("Students GPA is a " + testGPA + " and students test score is a " + testScore + "%");
      }
      
    }
    else{
      System.out.println("Input INVALID please Check GPA and Test score input!");
      System.out.println("Your inputs were:");
      System.out.println(testGPA + " = GPA should be between 0.0 and 4.0.");
      System.out.println(testScore + " = Test Score should be between 0 and 100");
    }
    
  }

  public static void main(String[] args) {
    String gpaString, testString;

    gpaString = JOptionPane.showInputDialog(null,
        "Please enter your GPA.", "GPA",
        JOptionPane.PLAIN_MESSAGE);

    testString = JOptionPane.showInputDialog(null,
        "Please enter your entrance exam score.", "Test Score",
        JOptionPane.PLAIN_MESSAGE);

    double testGPA = Double.parseDouble(gpaString);
    int testScore = Integer.parseInt(testString);
    
    accepted(testGPA,testScore);

  }
}
 
Code:
   accepted(testGPA,testScore);

:bang head Exactly what I was trying to figure out. I was putting accepted.testGPA to no avail. Indeed something should be put into place to keep it from being broken with non numbers, but for the moment the rubric does not want me to go that far. Thanks!
 
The fully qualified name for the method would technically be College.accepted(double,int);
However, since your within the same class you only need to reference the methods name.
 
May i ask what book your class is using? I have a pdf thats always helpful if you have no internet or your book. Pm me if interested :D
 
Back