Help with methods!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Help with methods!

  1. #1
    Join Date
    Feb 2005
    Posts
    18

    Help with methods!

    I need help with the following java programming assignments...
    1. Write a value-returning method, isSVowel, that returns the value true if a given character is a vowel, and otherwise returns false.
    2. Write a program that prompts the user to input a sequence of characters and outputs
    the number of voweks. (Use the method isVowel written in exercise 1.)
    3. Consider the following program segment:
    public class Ch7Ex3
    {
    public static void main(String[] args)
    {
    int num;
    double dec;
    .
    .
    .
    }

    public static int one(int x, int y)
    {
    .
    .
    .
    }

    public static double two(int x, double a)
    {
    int first;
    double z;
    .
    .
    .
    }
    }
    a. Write the definition of method one so that
    it retunrs the sum of x and y if x is greater than y;
    otherwise, it should return x minus 2 time y.

    b. Write the definition of method two as follows:
    i. Read a number and store it in z.
    ii. Update the value of z by adding the value of a to its previous value.
    iii. Assign the variable first the value returned by method one with the
    parameters 6 and 8.
    iv. Update the value of first by adding the value of x to its previous value.
    v. If the value of z is more than twice the value of first, return z; otherwise, return
    2 times first minus z.
    c. Write a Java program that tests parts a and b. (Declare additional variables in the method main, if necessary.)
    Last edited by blueandconfused; 02-10-2005 at 06:40 PM.

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    Which parts are you having problems with, or don't understand - can you be more specific
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

  3. #3
    Join Date
    Feb 2005
    Posts
    18
    Here is what I have for the first one so far. Can you tell me what I am doing wrong and I what I need to do to make it work?
    import java.io.*;

    public class Vowel
    {
    static BufferedReader keyboard =
    new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException
    {
    char ch;

    ch = System.out.println("Enter a lowercase letter: ");
    System.flush();
    isVowel(ch);
    }


    public static boolean isVowel(String vow)
    {
    char a, e, i, o, u;
    a = 'a';
    e = 'e';
    i = 'i';
    o = 'o';
    u = 'u';

    if(vow.charAt(a))
    return true;
    if(vow.charAt(e))
    return true;
    if(vow.charAt(i))
    return true;
    if(vow.charAt(o))
    return true;
    if(vow.charAt(u))
    return true;
    else
    return false;

    }

    }

  4. #4
    Join Date
    Jun 2004
    Posts
    10
    The first thing to get working is your isVowel() method. From the problem, it sounds like isVowel() accepts a char value and determines if this char value is a vowel (i.e. 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). This could be implemented as a single if statement:

    Code:
    
    public static boolean isVowel(char character) {
        if(character == 'a' || character == 'A' ||
           character == 'e' || character == 'E' ||
           character == 'i' || character == 'I' ||
           character == 'o' || character == 'O' ||
           character == 'u' || character == 'U')
            return true;
            
        return false;
    }
    
    This should give you a jump start.

    The second part of the exercise uses BufferedReader instance (keyboard) that you created. You need to make the call: keyboard.readLine(); to get input from the user.

  5. #5
    Join Date
    Feb 2005
    Posts
    18
    Thank you for your help. How would I make it so that the method is used in the input/output statements?

  6. #6
    Join Date
    Feb 2005
    Posts
    55
    Do you want the user to enter input and then display the output? If this is so, you can ask the user to input information by the following code

    String input = JOptionPane.showInputDialog("Enter a lowercase letter"); this is quite simple to do.

    I don't know where you want to show the output, if u want it to be shown on the console it would look something like this
    System.out.println("Lowercase letter entered by the user " + input);
    if u wanted it to be shown in a GUI it could look something like this
    JTextField field1 = new JTextField(3);
    field1.setText(input);

    I hope that helps

  7. #7
    Join Date
    Feb 2005
    Posts
    18
    can you repair this for me please?

    public class Exercise1
    {
    static BufferedReader keyboard =
    new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args)
    {
    char in;
    String inputMessage;
    inputMessage = keyboard.ReadLine(in);
    inputMessage = System.out.println("Enter a sentence: ");
    System.out.flush();

    isVowel(in);
    }



    public static boolean isVowel(char character)
    {
    if(character == 'a' || character == 'A' ||
    character == 'e' || character == 'E' ||
    character == 'i' || character == 'I' ||
    character == 'o' || character == 'O' ||
    character == 'u' || character == 'U')
    return true;
    else
    return false;
    }
    }

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468

    Another way to handle input and to manipulate string

    Have you thought about using the Scanner class (new in Java 1.5) instead of buffered reader? I did some assignments using it last quarter and I thought it was easier to use - - more c++ like ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center