JAVA AND PYTHON CODING PRACTICE EXERCISES Coding for Beginners (Jj Tam [Tam, Jj]) (Z-Library)
Java
No Description
234
Views
0
Downloads
0.00
Total Donations
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
JAVA AND PYTHON CODING PRACTICE EXERCISES CODING FOR BEGINNERS JJ TAM
Page
3
JAVA STRING EXERCISES GET THE CHARACTER AT THE GIVEN INDEX WITHIN THE STRING TEST IF A GIVEN STRING CONTAINS THE SPECIFIED SEQUENCE OF CHAR VALUES COMPARE A GIVEN STRING TO THE SPECIFIED STRING BUFFER CHECK WHETHER A GIVEN STRING ENDS WITH THE CONTENTS OF ANOTHER STRING GET THE CONTENTS OF A GIVEN STRING AS A BYTE ARRAY JAVA ARRAY EXERCISES SUM VALUES OF AN ARRAY CALCULATE THE AVERAGE VALUE OF ARRAY ELEMENTS TEST IF AN ARRAY CONTAINS A SPECIFIC VALUE REMOVE A SPECIFIC ELEMENT FROM AN ARRAY INSERT AN ELEMENT INTO AN ARRAY JAVA BASIC EXERCISES SWAP TWO VARIABLES PRINT A FACE FAHRENHEIT TO CELSIUS DEGREE INCHES TO METERS ADDS ALL THE DIGITS PRINT THE NUMBER OF YEARS AND DAYS COMPUTE (BMI) PRINT THE SUM FIND THE GREATEST OF THREE NUMBERS DISPLAY THE MULTIPLICATION TABLE DISPLAY THE PATTERN PRINT A PATTERN LIKE A PYRAMID PRINT THE SUM DIVIDE TWO NUMBERS PYTHON CODING PRACTICE EXERCISES PYTHON STRING – EXERCISES
Page
4
PYTHON: REVERSE A STRING REMOVE A NEWLINE IN PYTHON FIND THE COMMON VALUES PYTHON DATA TYPE: LIST – EXERCISES PYTHON: SUM ALL THE ITEMS IN A LIST GET THE LARGEST NUMBER FROM A LIST REMOVE DUPLICATES FROM A LIST PYTHON DATA TYPES: DICTIONARY – EXERCISES ADD A KEY TO A DICTIONARY ITERATE OVER DICTIONARIES USING FOR LOOPS MERGE TWO PYTHON DICTIONARIES MULTIPLY ALL THE ITEMS IN A DICTIONARY REMOVE A KEY FROM A DICTIONARY GET A DICTIONARY FROM AN OBJECT'S FIELDS COMBINE TWO DICTIONARY ADDING VALUES FOR COMMON KEYS FIND THE HIGHEST 3 VALUES PYTHON BASICS – EXERCISES DISPLAY CURRENT DATE AND TIME PRINT THE CALENDAR COMPUTES THE VALUE OF N+NN+NNN CALCULATE NUMBER OF DAYS VOLUME OF A SPHERE IN PYTHON COMPUTE THE AREA OF TRIANGLE COMPUTE THE GCD CALCULATE THE LCM CONVERT FEET AND INCHES TO CENTIMETERS CONVERT TIME – SECONDS CONVERT SECONDS TO DAY PROGRAM TO SOLVE FUTURE VALUE OF AMOUNT CHECK WHETHER A FILE EXISTS
Page
5
CONVERT THE DISTANCE SUM ALL THE ITEMS MULTIPLIES ALL THE ITEMS GET THE LARGEST NUMBER GET THE SMALLEST NUMBER REMOVE DUPLICATES CLONE OR COPY A LIST DIFFERENCE BETWEEN THE TWO LISTS GENERATE ALL PERMUTATIONS FIND THE SECOND SMALLEST GET UNIQUE VALUES GET THE FREQUENCY OF THE ELEMENTS GENERATE ALL SUBLISTS FIND COMMON ITEMS CREATE A LIST
Page
6
JAVA CODING PRACTICE EXERCISES CODING FOR BEGINNERS JJ TAM
Page
7
Java String Exercises Get the character at the given index within the String JAVA CODE public class Exercise1 { public static void main(String[] args) { String str = "Java Exercises!"; System.out.println("Original String = " + str); // Get the character at positions 0 and 10. int index1 = str.charAt(0); int index2 = str.charAt(10);
Page
8
// Print out the results. System.out.println("The character at position 0 is " + (char)index1); System.out.println("The character at position 10 is " + (char)index2); } } OUTPUT Original String = Java Exercises! The character at position 0 is J The character at position 10 is i
Page
9
Test if a given string contains the specified sequence of char values JAVA CODE public class Exercise8 { public static void main(String[] args) { String str1 = "PHP Exercises and Python Exercises"; String str2 = "and"; System.out.println("Original String: " + str1); System.out.println("Specified sequence of char values: " + str2); System.out.println(str1.contains(str2)); } }
Page
10
OUTPUT Original String: PHP Exercises and Python Exercises Specified sequence of char values: and true
Page
11
Compare a given string to the specified string buffer JAVA CODE public class Exercise10 { public static void main(String[] args) { String str1 = "example.com", str2 = "Example.com"; StringBuffer strbuf = new StringBuffer(str1); System.out.println("Comparing "+str1+" and "+strbuf+": " + str1.contentEquals(strbuf)); System.out.println("Comparing "+str2+" and "+strbuf+": " + str2.contentEquals(strbuf)); } } OUTPUT Comparing example.com and example.com: true Comparing Example.com and example.com: false
Page
12
Check whether a given string ends with the contents of another string JAVA CODE public class Exercise12 { public static void main(String[] args) { String str1 = "Python Exercises"; String str2 = "Python Exercise"; // The String to check the above two Strings to see // if they end with this value (se). String end_str = "se"; // Check first two Strings end with end_str boolean ends1 = str1.endsWith(end_str); boolean ends2 = str2.endsWith(end_str);
Page
13
// Display the results of the endsWith calls. System.out.println("\"" + str1 + "\" ends with " + "\"" + end_str + "\"? " + ends1); System.out.println("\"" + str2 + "\" ends with " + "\"" + end_str + "\"? " + ends2); } } OUTPUT "Python Exercises" ends with "se"? false "Python Exercise" ends with "se"? true
Page
14
Get the contents of a given string as a byte array JAVA CODE import java.util.Calendar; public class Exercise16 { public static void main(String[] args) { String str = "This is a sample String."; // Copy the contents of the String to a byte array. byte[] byte_arr = str.getBytes(); // Create a new String using the contents of the byte array. String new_str = new String(byte_arr); // Display the contents of the byte array. System.out.println("\nThe new String equals " + new_str + "\n"); } } OUTPUT The new String equals This is a sample String.
Page
15
Java Array Exercises Sum values of an array JAVA CODE public class Exercise2 { public static void main(String[] args) { int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = 0; for (int i : my_array) sum += i; System.out.println("The sum is " + sum); } } OUTPUT
Page
16
The sum is 55
Page
17
Calculate the average value of array elements JAVA CODE public class Exercise4 { public static void main(String[] args) { int[] numbers = new int[]{20, 30, 25, 35, -16, 60, -100}; //calculate sum of all array elements int sum = 0; for(int i=0; i < numbers.length ; i++) sum = sum + numbers[i]; //calculate average value double average = sum / numbers.length;
Page
18
System.out.println("Average value of the array elements is : " + average); } } OUTPUT Average value of the array elements is : 7.0
Page
19
Test if an array contains a specific value JAVA CODE public class Exercise5 { public static boolean contains(int[] arr, int item) { for (int n : arr) { if (item == n) { return true; } } return false; }
Page
20
public static void main(String[] args) { int[] my_array1 = { 1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2265, 1457, 2456}; System.out.println(contains(my_array1, 2013)); System.out.println(contains(my_array1, 2015)); } } OUTPUT true false
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A hands-on exercise book for absolute beginners who want to build confidence by typing and running small, practical programs in both Java and Python, covering strings, arrays, lists, dictionaries, and basic math problems.
【Book Arc】
- **Opening (~0%–11%)**: The book opens with Java string exercises, teaching core methods like `charAt`, `contains`, `contentEquals`, and `endsWith`. Each exercise follows a consistent pattern: a short problem statement, complete Java code, and the expected output, so beginners can immediately see how each method behaves.
- **Early (~11%–33%)**: Moves into Java array exercises, starting with simple tasks like summing values and calculating averages, then progressing to trickier operations like removing and inserting elements. The code reveals an important caveat: arrays have fixed sizes, so removal leaves duplicate trailing values—a key insight for newcomers.
- **Middle (~33%–56%)**: Shifts to Java basic exercises, covering user input with `Scanner`, variable swapping, unit conversions (Fahrenheit to Celsius, inches to meters), digit summation, BMI calculation, and arithmetic operations on two integers. These problems focus on translating everyday formulas into code.
- **Late (~56%–78%)**: Introduces conditional logic and loops through exercises like finding the greatest of three numbers, printing multiplication tables, and generating number patterns and pyramids. The book then transitions to Python, starting with string reversal, finding common characters, and list operations like summing items and removing duplicates.
- **Ending (~78%–100%)**: The Python section deepens into dictionaries (merging, removing keys, combining values with `Counter`), then covers basics like date/time display, calendar printing, LCM/GCD calculations, unit conversions, and future value computation. The final exercises explore list manipulation: finding differences, generating permutations, counting element frequency, and creating sublists.
【Key Takeaways】
- **String methods are the gateway to text processing** (Early): Exercises like `charAt`, `endsWith`, and `contentEquals` show how to inspect and compare strings—skills needed for parsing user input and validating data in real projects.
- **Arrays require manual management** (Early): Removing an element doesn't shrink the array; the last value duplicates. Understanding this fixed-size limitation prepares beginners for why dynamic structures like `ArrayList` or Python lists exist.
- **`Scanner` is the standard Java input tool** (Middle): Repeatedly using `nextInt()` and `nextDouble()` across exercises builds muscle memory for reading user input, a prerequisite for interactive programs.
- **Formulas translate directly into arithmetic expressions** (Middle): BMI, temperature conversion, and digit summation show that coding is often just expressing math with variables and operators—no complex logic required.
- **Loops and conditionals turn logic into patterns** (Late): Multiplication tables, pyramids, and finding the maximum value introduce `for` loops and `if` statements, the building blocks for controlling program flow.
- **Python dictionaries are flexible key-value stores** (Late): Merging with `update()`, deleting with `del`, and combining values with `Counter` demonstrate idiomatic Python that's more concise than equivalent Java code.
- **Python's standard library does heavy lifting** (Ending): Modules like `datetime`, `calendar`, `itertools`, and `collections` solve date handling, permutations, and frequency counting in a few lines—showing beginners to leverage built-in tools.
- **Set operations simplify list comparisons** (Ending): Using `set()` difference and intersection to find unique or common items is a clean pattern that avoids verbose loops.
【Reading Tips】
- **Type every example, don't just read**: The book's value is in practice. Copy the code, run it, then modify values or inputs to see how output changes—this builds intuition faster than passive reading.
- **Skim the Java array section if you're Python-focused**: The array removal/insertion exercises highlight Java's fixed-size limitation, but Python learners can jump ahead to lists, which are more flexible and forgiving.
- **Deep-read the Python dictionary and list sections**: These are the most transferable skills. Pay special attention to `Counter` for merging and `itertools.permutations` for combinatorial tasks—they're elegant and reusable.
- **Watch for output formatting details**: Java uses `printf` with format specifiers (`%d`, `%.2f`), while Python uses `%` formatting or `round()`. These small differences matter when you want clean, readable output.
- **Use the expected outputs as a debugging checklist**: If your result differs, trace your logic step-by-step. The book's consistent structure (problem → code → output) makes it easy to isolate where you went wrong.
【Coverage Limits】
This guide covers the book's exercise structure and key techniques across Java and Python basics, but the excerpts do not include explanations of programming concepts, setup instructions, or advanced topics like object-oriented programming or file I/O beyond simple existence checks.
Passage locations
Excerpt 1
书名: JAVA AND PYTHON CODING PRACTICE EXERCISES Coding for Beginners (Jj Tam [Tam, Jj]) (Z-Library) 作者: Jj Tam [Tam, Jj] JAVA STRING EXERCISES GET THE CHARACTE...
View in text
Page 15
s Exercise2 { public static void main(String[] args) { int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = 0; for (int i : my_array) sum +...
View in text
Excerpt 3
Main { public static void main(String[] Strings) { Scanner input = new Scanner(System.in); System.out.print("Input an integer betwe...
View in text
Excerpt 4
stem.out.printf("Product of two integers: %d%n", firstInt * secondInt); System.out.printf("Average of two integers: %.2f%n", (double) (firstInt + sec...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay