Class & method Boolean

Reply

Join Date: Feb 2007
Posts: 20
Reputation: wavyaquaeyes is an unknown quantity at this point 
Solved Threads: 0
wavyaquaeyes wavyaquaeyes is offline Offline
Newbie Poster

Class & method Boolean

 
0
  #1
Apr 4th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 5
Reputation: eniacad is an unknown quantity at this point 
Solved Threads: 0
eniacad eniacad is offline Offline
Newbie Poster

Re: Class & method Boolean

 
0
  #2
Apr 4th, 2007
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,
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 20
Reputation: wavyaquaeyes is an unknown quantity at this point 
Solved Threads: 0
wavyaquaeyes wavyaquaeyes is offline Offline
Newbie Poster

Re: Class & method Boolean

 
0
  #3
Apr 4th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 3
Reputation: 5up3rJ is an unknown quantity at this point 
Solved Threads: 0
5up3rJ 5up3rJ is offline Offline
Newbie Poster

Re: Class & method Boolean

 
0
  #4
Apr 4th, 2007
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.

  1. import java.util.*;
  2. public class isVowel
  3. {
  4. static Scanner console = new Scanner(System.in); //creates the new object scanner
  5.  
  6. public static void main(String[] args)
  7. {
  8. boolean found = false;
  9.  
  10. String[] vowels = {"a","e","i","o","u","A","E","I","O","U"};
  11.  
  12. System.out.println("Please Enter a Letter: ");
  13. String input = console.next();
  14. System.out.println();
  15.  
  16. for (String s : vowels)
  17. if (input.equals(s))
  18. found = true;
  19.  
  20. String result = "The input character, " + input;
  21. if (found)
  22. result += " is a vowel.";
  23. else
  24. result += " is not a vowel!";
  25.  
  26. System.out.println(result);
  27. }
  28. }

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 5
Reputation: fwso is an unknown quantity at this point 
Solved Threads: 0
fwso's Avatar
fwso fwso is offline Offline
Newbie Poster

Re: Class & method Boolean

 
0
  #5
Apr 5th, 2007
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.");
}
}
}
{Learn, Communicate, Share & Discover}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC