| | |
Class & method Boolean
![]() |
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Mar 2007
Posts: 5
Reputation:
Solved Threads: 0
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,
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,
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jan 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
It actually needs some changes to be good OOP, but I wanted it to still be reconizable to you.
Java Syntax (Toggle Plain Text)
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 = false; String[] vowels = {"a","e","i","o","u","A","E","I","O","U"}; System.out.println("Please Enter a Letter: "); String input = console.next(); System.out.println(); for (String s : vowels) if (input.equals(s)) found = true; String result = "The input character, " + input; if (found) result += " is a vowel."; else result += " is not a vowel!"; System.out.println(result); } }
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.
Last edited by 5up3rJ; Apr 4th, 2007 at 11:54 pm.
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.");
}
}
}
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.");
}
}
}
{Learn, Communicate, Share & Discover}
![]() |
Similar Threads
- Class File, making directories... relative to (Java)
- How to get Array type members' value from a class (JSP)
- class rectangle (Java)
- calling a method from another class (Java)
- Class Passing and Returning Arrays (Java)
Other Threads in the Java Forum
- Previous Thread: Error in NetBeans
- Next Thread: Error running Java
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor





