0% found this document useful (0 votes)
5 views4 pages

Java Programming Practice Exercises

The document contains a series of Java practice exercises organized into modules covering variables, loops, strings, arrays, conditionals, and small projects. Each exercise includes a brief description and sample code to illustrate the concepts being taught. The exercises aim to reinforce Java programming skills through practical application.

Uploaded by

aamandaruiz20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java Programming Practice Exercises

The document contains a series of Java practice exercises organized into modules covering variables, loops, strings, arrays, conditionals, and small projects. Each exercise includes a brief description and sample code to illustrate the concepts being taught. The exercises aim to reinforce Java programming skills through practical application.

Uploaded by

aamandaruiz20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Practice Exercises – Based on Class Notes

Module 1: Variables, Data Types, and Conversion


Exercise 1: Integer vs Double - Create two variables, one integer and one double. - Try to assign the sum of
these two to an integer variable and observe what happens. - Fix it using explicit casting if necessary.

public class IntDoubleTest {


public static void main(String[] args) {
int a = 5;
double b = 3.2;
// int sum = a + b; // This will cause an error
int sum = (int)(a + b);
[Link]("Sum: " + sum);
}
}

Exercise 2: Automatic Conversion - Create a double variable. - Assign an integer value to it and print the
result.

public class AutoConversion {


public static void main(String[] args) {
int num = 10;
double d = num; // Automatic conversion
[Link]("Double value: " + d);
}
}

Module 2: Loops and Patterns


Exercise 3: Stars in a Loop - Using a while loop and a for loop (nested), print 30 stars in one line.

Exercise 4: Reverse Counting Loop - Write a for loop that prints numbers from 10 down to 0. - Print a
message when the loop ends.

Module 3: Strings and Methods


Exercise 5: Reverse a String

1
public static String reverseString(String input) {
String result = "";
for(int i = [Link]() - 1; i >= 0; i--) {
result += [Link](i);
}
return result;
}

Exercise 6: Substring Extraction

public static String getSubString(String str, int start, int end) {


String result = "";
for(int i = start; i <= end; i++) {
result += [Link](i);
}
return result;
}

Exercise 7: Count a Character

public static int countCharacter(String str, char c) {


int count = 0;
for(int i = 0; i < [Link](); i++) {
if([Link](i) == c) {
count++;
}
}
return count;
}

Exercise 8: Check for Vowels

public static boolean isVowel(char c) {


return "AEIOU".indexOf(c) != -1;
}

Module 4: Arrays
Exercise 9: Max Value in Array

2
int[] numbers = {5, 10, 2, 8};
int max = numbers[0];
for(int i = 1; i < [Link]; i++) {
if(numbers[i] > max) max = numbers[i];
}
[Link]("Max value: " + max);

Exercise 10: Reverse Array

int[] arr = {1, 2, 3, 4, 5};


for(int i = [Link] - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}

Module 5: Conditionals and Switch


Exercise 11: Character Category

public static void checkCharacter(char c) {


switch(c) {
case 'A': case 'E': case 'I': case 'O': case 'U':
[Link]("Vowel");
break;
default:
if([Link](c)) [Link]("Consonant");
else [Link]("Other");
}
}

Exercise 12: Palindrome Check

public static boolean isPalindrome(String str) {


String reversed = reverseString(str);
return [Link](reversed);
}

3
Module 6: Small Projects / Fun Exercises
Exercise 13: Print Pattern - Print a star pattern:

*
**
***
****
*****

Exercise 14: User Input and Loops - Ask the user for a string. - Count vowels and consonants using loops
and conditionals.

Scanner sc = new Scanner([Link]);


[Link]("Enter a string: ");
String input = [Link]();
int vowels = 0, consonants = 0;
for(int i = 0; i < [Link](); i++) {
char c = [Link]([Link](i));
if("AEIOU".indexOf(c) != -1) vowels++;
else if([Link](c)) consonants++;
}
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);

End of Exercises

You might also like