Java vs. Python challenges

Challenge 1

Write an application that creates a word object with the following methods:

  • a method to count vowels
  • a method to count consonants

Challenge 1 – Python solution

class Word:
    def __init__(self, word: str):
        self.word = word

    def vowel_count(self):
        count = 0
        for x in self.word:
            if x in "aeiou": 
                count += 1
        return count
    
    def consonant_count(self):
        count = 0
        for x in self.word:
            if x not in "aeiou": 
                count += 1
        return count

if __name__ == "__main__":
    w1 = Word("fantastic")
    vowels = w1.vowel_count()
    print(vowels)

    consonants = w1.consonant_count()
    print(consonants)
3
6

Challenge 1 – Java solution

public class Word {
    private String word;

    public Word(String word) {
        this.word = word;
    }

    public boolean isVowel(char c) {
        return "AEIOUaeiou".indexOf(c) != -1;
    }

    public int countVowels() {
        int count = 0;
        for (char c : word.toCharArray()) {
            if (isVowel(c)) count += 1;
        }
        return count;
    }

    public int countConsonants() {
        int count = 0;
        for (char c : word.toCharArray()) {
            if (!isVowel(c)) count += 1;
        }
        return count;
    }
}

Challenge 1 – Java solution main method

public static void main(String[] args) {
  Word w1 = new Word("fantastic");
  
  int vowels = w1.countVowels();
  System.out.println(vowels);
  
  int consonants = w1.countConsonants();
  System.out.println(consonants);
}

Challenge 2

Write an application that creates an object that has a sorted array of integers. Write the following methods:

  • a method to return the lowest number in the array
  • a method to return the highest number in the array
  • a method to remove duplicates from the array

Name your java file Numbers.java and your python file numbers.py

package com.gradescope.numbers;

Challenge 2

Example of use for Java:

public static void main(String[] args) {
        Numbers myNumbers = new Numbers();
        myNumbers.addNumber(10);
        myNumbers.addNumber(1);
        myNumbers.addNumber(5);
        myNumbers.addNumber(10);
        myNumbers.addNumber(25);
        myNumbers.addNumber(10);
        myNumbers.addNumber(-25);
        System.out.println(myNumbers.getMax()) // 25
        System.out.println(myNumbers.getMin()) // -25
        System.out.println(myNumbers); // -25 1 5 10 10 10 25
        myNumbers.removeDuplicates();
        System.out.println(myNumbers); // -25 1 5 10 25
    }

Challenge 2

Example of use for Python:

if __name__ == "__main__":
    my_numbers = Numbers()
    my_numbers.add_number(10)
    my_numbers.add_number(1)
    my_numbers.add_number(5)
    my_numbers.add_number(10)
    my_numbers.add_number(25)
    my_numbers.add_number(10)
    my_numbers.add_number(-25)
    print(my_numbers) # -25 1 5 10 10 10 25
    print(my_numbers.get_max()) # 25
    print(my_numbers.get_min()) # -25
    my_numbers.remove_duplicates()
    print(my_numbers) # -25 1 5 10 25