![]() |
| ||
| Class & method Boolean Hi wonderful programmers, I once again need help. First i need to create a class where if the input is a vowel , it returns true and if not it returns false. i am just learning java, but also just learning classes, methods etc. I know I have to call the method (I understand the method, class, formal param and actual param. But, I do not know where to start or begin. From my understanding, I need to first create the method, so it can be called. Is that correct? 2nd, I will need to create the program? I am confused at this point. Thanks |
| ||
| Re: Class & method Boolean You first have to create the Class file(t.e *.java) then inside you shoud define properties, constructor and methods after that yiuo should write the main method, in it to make an instance of a object of the class yiou just defined, and whit that object yoiu can call the methods.I hope this is what you wanted to know Regards, |
| ||
| Re: Class & method Boolean Hi enicad, thanks, but now I am even more confused. Here is my code and now I have numerous errors. I know I am probably turning something very simple into something very complicated and hard. In this code, I am creating the class..but I think I must be confusing the 2. Would you mind looking at it? / Program that tells if a letter is a vowel or constant import java.util.*; public class isVowel { static Scanner console = new Scanner(System.in); //creates the new object scanner public static void main(String[] args) { boolean found = true; boolean flag = false char true; true = vowel; vowel = a, e, i, o, u; System.out.println("Please Enter a Letter: "); char = console.nextChar(); System.out.println(); { if (vowel !found == a && e && i && o && u) System.out.println("The character is !found" + vowel (!found a, e, i, o, u)); else if (vowel != a, e, i, o, u) System.out.println("The character is not a !flag" + vowel (!flag a, e, i, o, u); } } } Thanks |
| ||
| Re: Class & method Boolean I moved your code around. I think it now does what you wanted. It actually needs some changes to be good OOP, but I wanted it to still be reconizable to you. import java.util.*; Convention says your classes should begin with a capitol letter, but IsVowel is not a great class name. Maybe the class should be called VowelValidator, or something important sounding like that. isVowel sounds like it should be a method name in your class. Also, you've got all your code in the main method. Your main should probably be very short. Something like: VowelValidator vowelValidator = new VowerValidator(); Then maybe take in your input in the constructor, or another method (perhaps in a loop even), calling the method isVowel each time. |
| ||
| Re: Class & method Boolean I think it would better to write the metod as a static one in a class, as following code shows: import java.util.*; public class Vowel{ private static final String[] vowels = {"a","e","i","o","u","A","E","I","O","U"}; public static boolean isVowel(String ch){ for(String v : vowels){ if(ch.equals(v)){ return true; } } //if not match return false; return false; } public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("Please enter a character:"); String ch = in.nextLine(); boolean isVowel = Vowel.isVowel(ch); if(isVowel){ System.out.println(ch + " is a vowel."); } else { System.out.println(ch + " is NOT a vowel."); } } } |
| All times are GMT -4. The time now is 6:28 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC