-
Boolean method help
Okay, I have this program:
Code:
import java.util.*;
public class somethingextra {
public static void main (String [] args) {
String name = readName();
int age = readAge();
char gender = readGender();
float mark = readMark();
char grade = getGrade(mark);
String desc = getDescription(grade);
boolean eligible = eligibleNs(age, gender);
System.out.println ("\n" + name + " is " + age + " years old. " +
"\nGender is " + gender + " with " + mark + " marks.");
System.out.println ("Grade is " + grade + " " + desc + "\n" + "\n" + eligible );
}
public static boolean eligibleNs (int age, char gender) {
if ((age >= 16) && (gender == 'M'))
return true;
else
return false;
}
public static char getGrade (float mark) {
char grade;
if (mark >=50)
grade = 'P';
else
grade = 'F';
return grade;
}
public static String getDescription(char grade) {
String desc = " ";
switch (grade) {
case 'P' : desc = "Pass";
break;
case 'F' : desc = "Fail";
break;
}
return desc;
}
public static String readName() {
String name;
Scanner sc = new Scanner(System.in);
System.out.print ("Enter name: ");
name = sc.nextLine();
return name;
}
public static int readAge() {
int age;
Scanner sc = new Scanner(System.in);
System.out.print("Enter age :");
age = sc.nextInt();
return age;
}
public static char readGender() {
String str;
char gender;
Scanner sc = new Scanner(System.in);
System.out.print("Enter gender :");
str = sc.nextLine();
gender = str.charAt(0);
return gender;
}
public static float readMark() {
float mark;
Scanner sc = new Scanner (System.in);
System.out.print ("Enter mark :");
mark = sc.nextFloat();
return mark;
}
} //end
When I run this. Everything works fine. However I'd like to know whether I am able to do this:
Instead of having the output of "true" and "false", I'd like it to be "Eligible" and "Not Eligible". I am not sure on how to start and go on doing that. I tried to create a new method, but I ended up just getting the output of "Eligible" only o__o''
I have never had experience with boolean stuff, so if anyone can point me out to some good links that could teach me more about it, I'd really appreciate it!
Thanks!
-
You could just use an if statement for that.
or you could go fancy and use
Code:
String eligibleAsString = eligible?"Eligible":"Not Eligible";
-
I can get it to work by replacing this:
Code:
public static boolean eligibleNs (int age, char gender) {
if ((age >= 16) && (gender == 'M'))
return true;
else
return false;
}
with this:
Code:
public static String nsTest (int age, char gender) {
String ns;
if ((age >=16) && (gender=='M'))
ns = "Eligible";
else
ns = "Not Eligible";
return ns;
}
and of course, changing the code for calling the method accordingly.
But I was wondering how can I get the same result using the boolean method. Its rather confusing, and I really don't understand it at all :(
-
A boolean method returns either true or false so you can't make it return strings.
It is the calling code that should be allowed to interpret that returned value as they wish. Changing your method to returning a string may not be a good workaround. You might need to know whether eligible or not without printing out the results (e.g to perform some action based on eligibility).
Better keep the method returning boolean and let the users of the method do the if-else bits to convert to String (if they want to) like I suggested above.
-
I tried what you suggested and it worked.
Thanks!
-
You'll realize with time (even as you have done now) that there are always many different ways of doing things in Java. Sometimes the options are so much that they appear to be too much. Which option you choose determines how good a developer you are.
-
if you prefer working with variables rather than true / false you can declare a variable:
private boolean ns=false; and then set / get it, as you can see, just saying true or false is a lot less effort, but can be/is poor practise :P
Code:
public static String nsTest (int age, char gender) {
String ns;
if ((age >=16) && (gender=='M')) {
ns = true;
}
else {
ns = false;
}
return ns;
}
making use of this variable is very easy, code such as:
Code:
if (ns)
{
runSomeMethod;
orAssignSome = variables;
}
else
{
declareSomeOtherThings;
}