Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm doing an online java course and I'm finding that some of the assignments given are very vague and not adequately described for a beginner..

I was given this with next to no instructions.. I know its a lot, but I really want to get this and I'm getting frustrated with this course. I figure its some kind of test, but I wouldn't have a clue as to how to create the methods! If anyone could explain how to make these methods i'd be so grateful!
Java
import static java.lang.System.out;

/**
 * This assignment has you practice writing functions.  Test cases are provided
 * in code, so when you run the program, it will tell you if the functions
 * are working right.
 * 
 * Instructions
 * 
 * 1. Read all of the code all the way through.
 * 2. Figure out what should be in the ___ areas.  Fix them for all the methods, so the code will compile.
 * 3. Compile the code to check your work on data types and names.
 * 4. Figure out what code belongs in the // TODO areas.  Write that code, one function at a time.
 * 5. Compile and run frequently as you work, noting your progress through the test cases.
 * 6. Keep going until they're all written and the tests cases all pass.
 * 7. Celebrate!
 * 
 */
public class ManyFunctions
{
    /**
     * Takes two integer numbers as input and returns whichever number is smaller.
     */
    public static ___ minimum(_, __)
    {
        // TODO
    }

    /**
     * Takes two integer numbers as input and returns whichever number is larger.
     */
    public static ____ maximum(_, ___)
    {
        // TODO
    }

    /**
     * Returns true if the integer input is an odd number (1, 3, 5, 7...)
     */
    public static _____ isOddNumber(_____)
    {
        // TODO
    }

    /**
     * Returns true if the integer input is an even number (0, 2, 4, 6, 8...)
     */
    public static _____ isEvenNumber(_____)
    {
        // TODO
    }

    /**
     * Returns true if the letter is a vowel, or false otherwise.
     * You can assume only letters are being sent as input (not
     * spaces or punctuation marks or anything else).
     */
    public static _____ isVowel(_______)
    {
        // TODO
    }

    /**
     * Returns true if the letter is a consonant, or false otherwise.
     * You can assume only letters are being sent as input (not
     * spaces or punctuation marks or anything else).
     * 
     * This can be written in 1 short line of code without typing out
     * all the consonants in the alphabet.  Can you figure out how?
     */
    public static _____ isConsonant(_____)
    {
        // TODO
    }

    /**
     * Runs a series of test cases against the above methods.
     * 
     * * DO NOT!!!!! CHANGE THE CODE INSIDE main() { }
     * 
     * Just use this as an example of how the methods will be called, so 
     * you can figure out what they are supposed to do.
     * 
     */
    public static void main(String[] args)
    {
        out.println("Testing your functions.");
        int testFailures = 0;

        if (minimum(4, 3) != 3) {
            out.println("Error 1: Minimum failed test.  Expected 3, got " + minimum(4, 3));
            testFailures++;
        }
        if (minimum(4, 4) != 4) {
            out.println("Error 2: Minimum failed test.  Expected 4, got " + minimum(4, 4));
            testFailures++; 
        }
        if (minimum(3, 4) != 3) {
            out.println("Error 3: Minimum failed test.  Expected 3, got " + minimum(3, 4));
            testFailures++; 
        }
        if (minimum(5, 4) != 4) {
            out.println("Error 4: Minimum failed test.  Expected 4, got " + minimum(5, 4));
            testFailures++; 
        }

        if (maximum(4, 3) != 4) {
            out.println("Error 5: Maxmimum failed test.  Expected 4, got " + maximum(4, 3));
            testFailures++; 
        }
        if (maximum(3, 4) != 4) {
            out.println("Error 6: Maxmimum failed test.  Expected 4, got " + maximum(3, 4));
            testFailures++; 
        }
        if (maximum(4, 4) != 4) {
            out.println("Error 7: Maxmimum failed test.  Expected 4, got " + maximum(4, 4));
            testFailures++; 
        }
        if (maximum(-2, 10) != 10) {
            out.println("Error 8: Maxmimum failed test.  Expected 10, got " + maximum(-2, 10));
            testFailures++; 
        }

        if (isEvenNumber(3)) {
            out.println("Error 9: isEven failed test.  Expected isEven(3) to be false, but got true.");
            testFailures++; 
        }
        if (!isEvenNumber(2)) {
            out.println("Error 10: isEven failed test.  Expected isEven(2) to be true, but got false.");
            testFailures++; 
        }
        if (isOddNumber(0)) {
            out.println("Error 11: isOdd failed test.  Expected isOdd(0) to be false, but got true.");
            testFailures++; 
        }
        if (!isOddNumber(5)) {
            out.println("Error 12: isOdd failed test.  Expected isOdd(5) to be true, but got false.");
            testFailures++; 
        }
        if (isOddNumber(18)) {
            out.println("Error 13: isOdd failed test.  Expected isOdd(18) to be false, but got true.");
            testFailures++; 
        }

        if (!isVowel('a')) {
            out.println("Error 14: isVowel failed test.  Expected isVowel('a') to be true, but got false.");
            testFailures++; 
        }
        if (isVowel('x')) {
            out.println("Error 15: isVowel failed test.  Expected isVowel('x') to be false, but got true.");
            testFailures++; 
        }
        if (!isConsonant('z')) {
            out.println("Error 16: isConsonant failed test.  Expected isConsonant('z') to be true, but got false.");
            testFailures++; 
        }
        if (isConsonant('i')) {
            out.println("Error 17: isCononant failed test.  Expect isConsonant('i') to be false, but got true.");
            testFailures++; 
        }
        if (!isConsonant('b')) {
            out.println("Error 18: isConsonant failed test.  Expected isConsonant('b') to be true, but got false.");
            testFailures++; 
        }        
        if (!isConsonant('c')) {
            out.println("Error 19: isConsonant failed test.  Expected isConsonant('c') to be true, but got false.");
            testFailures++; 
        }        
        if (!isConsonant('d')) {
            out.println("Error 20: isConsonant failed test.  Expected isConsonant('d') to be true, but got false.");
            testFailures++; 
        }        
        if (!isConsonant('f')) {
            out.println("Error 21: isConsonant failed test.  Expected isConsonant('f') to be true, but got false.");
            testFailures++; 
        }        
        if (!isConsonant('g')) {
            out.println("Error 22: isConsonant failed test.  Expected isConsonant('g') to be true, but got false.");
            testFailures++; 
        }
        if (!isVowel('A')) {
            out.println("Error 23: isVowel failed test.  Expected isVowel('A') to be true, but got false.");
            testFailures++; 
        }
        if (isVowel('X')) {
            out.println("Error 24: isVowel failed test.  Expected isVowel('X') to be false, but got true.");
            testFailures++; 
        }
        if (!isConsonant('Z')) {
            out.println("Error 25: isConsonant failed test.  Expected isConsonant('Z') to be true, but got false.");
            testFailures++; 
        }
        if (isConsonant('I')) {
            out.println("Error 26: isCononant failed test.  Expect isConsonant('I') to be false, but got true.");
            testFailures++; 
        }

        if (testFailures == 0) {
            out.println("Success!  All test cases passed.");
        } else {
            out.println(testFailures + " tests failed.  Keep trying!");
        }
    }
}


What I have tried:

Rocking back and forth in the foetal position
Posted
Updated 19-Nov-22 20:34pm
v2
Comments
Graeme_Grant 18-Sep-17 9:37am    
"Rocking back and forth in the foetal position"

Is programming really for you?

Spend some time with The Java™ Tutorials[^], you will learn much more.
 
Share this answer
 
Comments
wilma2202 19-Sep-17 2:26am    
Thanks, Will do!
From my point of view the instructions are clear. However, they require basic knowledge about functions (return value, arguments).

Let's have a look:
Java
/**
* Takes two integer numbers as input and returns whichever number is smaller.
*/
public static ___ minimum(_, __)
{
    // TODO
}
You just have to replace the __ so that the function is working as described. So the first must be replaced with the return type and the others with type and name of the arguments.

Then implement the function body using the argument variable names to perform the requested operation.

For the above, it might be
Java
/**
* Takes two integer numbers as input and returns whichever number is smaller.
*/
public static int minimum(int x, int y)
{
    if (x <= y)
        return x;
    else
        return y;
}

Do it similar for the other functions:
Deduce the return type and the type of arguments from the function decsription, select names for the arguments, and implement the function body so that it does what is expected. Compile and run to know if the body has been implemented correctly.
 
Share this answer
 
Comments
wilma2202 19-Sep-17 2:26am    
Ok, wow..

As usual I think I've assumed the answer was far more complicated! Thanks for that..

However, I'm still stuck on the isVowel and isConstanant methods..

I'm assuming they return booleans and accept 'char' but I can't work out how to properly construct the Syntax for such a method.
Jochen Arndt 19-Sep-17 3:10am    
Boolean return and char arguments are correct.

Read the comments. They give you hints. For the vowel it tells you that you can expect letters. That means that the arguments are in the ranges a-z and A-Z.

For the consonant there is another hint (1 short line of code).
My hint: What is the difference between vowels and consonants?

wilma2202 19-Sep-17 4:22am    
if (a!= 'a' || 'e' || 'i' || 'o' || 'u')

This is what logically makes sense to me, but this won't run because its mixing boolean and char
syntax
Jochen Arndt 19-Sep-17 4:48am    
You have to use multiple comparisons where each results in a boolean:
if ((a != 'a') || (a != 'e') || ...)
wilma2202 19-Sep-17 5:02am    
Thanks!

One last question.. I realised I didn't properly fill out the isOdd isEven parts..

I looked up how to do this and most forums suggest using a modulus operator, but I need to know how to do it without as I will need to use similar logic in the next assignment..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900