/Practice-Problems

Problems I have assigned to beginner CS students

Primary LanguageJava

Create a new Java Project inside Eclipse, called “Practice.” For each problem, create a separate class (with a main method) named accordingly, like problem 1 would be Problem1.java, etc.

  1. Write a for loop that counts by 2’s up until 20. Tip: You can use the for loop you wrote that counts by 1’s to 10 for reference. Here is the expected console output: 2 4 6 8 10 12 14 16 18 20

  2. Write a method called “add” that takes in two integers, and returns the sum of those integers. Hint: The method add takes in two parameters – an integer called a and an integer called b (though you can name them whatever you want) – and returns an integer which is the sum a and b. Then, call this method in main, and print the result.

  3. (Challenge) Write code in your main method that creates the following pattern, using whatever computer science concepts you want – while loops, for loops, if statements, etc. Here is the expected output: 5 10 15 20 26 32 38 44

  4. Copy and paste the code from problem 2 into your problem 4 class, and change the name accordingly. Then, alter the program so that the add methods multiply, instead. Rename all add methods “multiply”.

  5. Write a method that takes in a string, and prints out “Hello, {NAME}!” The method does not return anything. If “Calla” was passed into the method, the program would print out, “Hello, Calla!”

  6. Write a method that takes in a string, and returns that string after extracting all of its vowels. You may create a separate isVowel method if you please.

  7. Write a function that takes in a sentence as a string, and returns the number of times the word "noodle." Do not count words that contain "noodle" like "noodles" or "canoodle." "It is difficult to pick up a single noodle with chopsticks." should return 1. "Noodle. Noodle! NOODLE! Noodle is my dog." should return 4. "Noodles are great." should return 0.

Patronus Search: Write a program that asks a user to input a patronus, and then outputs the name of a witch or wizard who produces it and their Hogwarts house. Example: The user inputs "doe" and the program outputs "Snape, Severus" and "Slytherin." Use the file "data.txt" for the patronus/witch/wizard/house data. You may not alter the data, or hardcode it into your program. Rather, your program must read data from the file. There are many ways to solve this problem. You may use console logging and input, or you may use pop ups. You may use classes and objects to store the data, data structures like hashmaps or arrays, and more. Some may be better than others, and you should think about why certain approaches are better in some ways, and less advantageous in others.