-
-
Notifications
You must be signed in to change notification settings - Fork 36
Adds a cheatsheet for Java fixes issue #42 #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Tristan-Raz
wants to merge
6
commits into
RunestoneInteractive:master
Choose a base branch
from
Tristan-Raz:adding-cheat-sheet-issue42
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3a5ce80
Adds a cheatsheet for Java fixes issue #42
Tristan-Raz 5f70b7e
Moves the cheat sheet to the appendix
Tristan-Raz b36e935
Merge branch 'master' into adding-cheat-sheet-issue42
Tristan-Raz 59186ee
Fixes merge conflicts and issue #42
Tristan-Raz 5711d75
Fixes syntax errors and explains a bit more. Still keeps one line loo…
Tristan-Raz 0e6b375
Adds code blocks, formats, and fixes issue #42
Tristan-Raz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<section xml:id="ap-java-cheatsheet" xmlns:xi="http://www.w3.org/2001/XInclude"> | ||
<title>Java Cheat Sheet</title> | ||
<paragraphs> | ||
<title>Purpose of this Cheat Sheet</title> | ||
<p> | ||
</p> | ||
<p> | ||
The following is intended to be useful in better understanding Java functions coming from a Python background. | ||
</p> | ||
</paragraphs> | ||
<paragraphs> | ||
<table> | ||
<title>Function/Method Equivalents: Python to Java</title> | ||
<tabular> | ||
<row header="yes"> | ||
<cell> Python Function </cell> | ||
<cell> Java Equivalent </cell> | ||
<cell> Description </cell> | ||
</row> | ||
<row> | ||
<cell><c>print()</c></cell> | ||
<cell><c>System.out.println()</c></cell> | ||
<cell> Prints output to the console. </cell> | ||
</row> | ||
<row> | ||
<cell><c>len()</c></cell> | ||
<cell><c>array.length</c> or <c>list.size()</c></cell> | ||
<cell> Returns the length of an array or size of a list. </cell> | ||
</row> | ||
<row> | ||
<cell><c>range()</c></cell> | ||
<cell><c>for (int i = 0; i < n; i++)</c></cell> | ||
<cell> Used in loops to iterate a specific number of times. </cell> | ||
</row> | ||
<row> | ||
<cell><c>str()</c></cell> | ||
<cell><c>String.valueOf()</c></cell> | ||
<cell> Converts an object to a string. </cell> | ||
</row> | ||
<row> | ||
<cell><c>int()</c></cell> | ||
<cell><c>Integer.parseInt()</c></cell> | ||
<cell> Converts a string to an integer. </cell> | ||
</row> | ||
<row> | ||
<cell><c>float()</c></cell> | ||
<cell><c>Float.parseFloat()</c></cell> | ||
<cell> Converts a string to a float. </cell> | ||
</row> | ||
<row> | ||
<cell><c>list.append()</c></cell> | ||
<cell><c>ArrayList.add()</c></cell> | ||
<cell> Adds an element to the end of a list. </cell> | ||
</row> | ||
<row> | ||
<cell><c>list.pop()</c></cell> | ||
<cell><c>ArrayList.remove(index)</c></cell> | ||
<cell> Removes and assign the return value to use it.</cell> | ||
</row> | ||
<row> | ||
<cell><c>list.sort()</c></cell> | ||
<cell><c>Collections.sort(list)</c></cell> | ||
<cell> Sorts a list in ascending order. </cell> | ||
</row> | ||
<row> | ||
<cell><c>list.reverse()</c></cell> | ||
<cell><c>Collections.reverse(list)</c></cell> | ||
<cell> Reverses the order of elements in a list. </cell> | ||
</row> | ||
<row> | ||
<cell><c>dict.get()</c></cell> | ||
<cell><c>Map.get(key)</c></cell> | ||
<cell> Retrieves the value associated with a key in a map. </cell> | ||
</row> | ||
<row> | ||
<cell><c>dict.keys()</c></cell> | ||
<cell><c>Map.keySet()</c></cell> | ||
<cell> Returns a set of keys in a map. </cell> | ||
</row> | ||
<row> | ||
<cell><c>dict.values()</c></cell> | ||
<cell><c>Map.values()</c></cell> | ||
<cell> Returns a collection of values in a map. </cell> | ||
</row> | ||
<row> | ||
<cell><c>dict.items()</c></cell> | ||
<cell><c>Map.entrySet()</c></cell> | ||
<cell> Returns a set of key-value pairs in a map. </cell> | ||
</row> | ||
<row> | ||
<cell><c>input()</c></cell> | ||
<cell><c>Scanner.nextLine()</c></cell> | ||
<cell> Reads a line of input from the console. </cell> | ||
</row> | ||
<row> | ||
<cell><c>open()</c></cell> | ||
<cell><c>FileReader</c>, <c>BufferedReader</c></cell> | ||
<cell> Used to read from files. </cell> | ||
</row> | ||
<row> | ||
<cell><c>enumerate()</c></cell> | ||
<cell><c>for (int i = 0; i < list.size(); i++) { ... }</c></cell> | ||
<cell> Used to iterate over a list with an index. </cell> | ||
</row> | ||
</tabular> | ||
</table> | ||
<table> | ||
<title>Operator Equivalents and Usage</title> | ||
<tabular> | ||
<row header="yes"> | ||
<cell> Operator Type </cell> | ||
<cell> Operator </cell> | ||
<cell> Description </cell> | ||
<cell> Example </cell> | ||
</row> | ||
<row> | ||
<cell> Arithmetic </cell> | ||
<cell><c>+</c>, <c>-</c>, <c>*</c>, <c>/</c></cell> | ||
<cell> Addition, Subtraction, Multiplication, Division </cell> | ||
<cell><c>5 + 2</c></cell> | ||
</row> | ||
<row> | ||
<cell> Arithmetic </cell> | ||
<cell><c>/</c></cell> | ||
<cell> Integer Division (truncates toward zero) </cell> | ||
<cell><c>7 / 2</c> → 3 </cell> | ||
</row> | ||
<row> | ||
<cell> Arithmetic </cell> | ||
<cell><c>%</c></cell> | ||
<cell> Modulus (remainder) </cell> | ||
<cell><c>7 % 2</c> → 1 </cell> | ||
</row> | ||
<row> | ||
<cell> Arithmetic </cell> | ||
<cell><c>Math.pow()</c></cell> | ||
<cell> Exponent </cell> | ||
<cell><c>Math.pow(2, 3)</c> → 8.0 </cell> | ||
</row> | ||
<row> | ||
<cell> Comparison </cell> | ||
<cell><c>==</c>, <c>!=</c></cell> | ||
<cell> Equal to, Not equal to (use <c>.equals()</c> for objects) </cell> | ||
<cell><c>x == y</c></cell> | ||
</row> | ||
<row> | ||
<cell> Comparison </cell> | ||
<cell><c>></c>, <c><</c>, <c>>=</c>, <c><=</c></cell> | ||
<cell> Greater/Less than, or equal to </cell> | ||
<cell><c>x > 5</c></cell> | ||
</row> | ||
<row> | ||
<cell> Logical </cell> | ||
<cell><c>&&</c>, <c>||</c>, <c>!</c></cell> | ||
<cell> Logical AND, OR, NOT </cell> | ||
<cell><c>x > 1 && y < 10</c></cell> | ||
</row> | ||
<row> | ||
<cell> Assignment </cell> | ||
<cell><c>+=</c>, <c>-=</c>, <c>*=</c>, <c>/=</c></cell> | ||
<cell> Adds, subtracts, multiplies, or divides and assigns </cell> | ||
<cell><c>x += 1</c></cell> | ||
</row> | ||
</tabular> | ||
</table> | ||
|
||
<p> | ||
<ul> | ||
<li> | ||
<p> | ||
Ternary Operator: Provides a compact, one-line if-else statement. For instance, <c>String result = (score >= 60) ? "Pass" : "Fail";</c> is much shorter than a full if-else block. | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
No Chained Comparisons: Java does not support chained comparisons. Range checks must use logical operators, such as <c>if (age >= 18 && age < 65)</c>. In Python, this could be written as <c>if 18 <= age < 65:</c>. | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
String Formatting: Java uses methods like <c>String.format()</c> or <c>System.out.printf()</c> for embedding expressions in strings, similar to Python's F-Strings. For example, <c>String message = String.format("Hello, %s!", name);</c> is cleaner than traditional string concatenation. | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
No Tuple or List Unpacking: Java does not have a direct equivalent to Python's tuple and list unpacking. Assignment must be done one variable at a time, such as <c>String name = "Alice"; int age = 30;</c>. | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
Short-Circuiting: The logical operators <c>&&</c> (AND) and <c>||</c> (OR) are efficient. They stop evaluating as soon as the outcome is known. For example, in <c>if (user != null && user.isAdmin())</c>, the code will not attempt to call <c>.isAdmin()</c> if <c>user</c> is null, preventing an error. | ||
</p> | ||
</li> | ||
<li> | ||
<p> | ||
Streams API: Java's Stream API is the idiomatic alternative to Python's List Comprehensions. It can be used to filter, map, and reduce data in a sequence of steps. For example, to generate a list of squares, instead of a multi-line loop, you can write <c>List<Integer> squares = IntStream.range(0, 10).map(i -> i * i).boxed().collect(Collectors.toList());</c>. | ||
</p> | ||
</li> | ||
</ul> | ||
</p> | ||
|
||
</paragraphs> | ||
</section> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems odd to include a section AFTER the backmatter. This seems like an
appendix
to me and should be xi:included inside the backmatter. Therefore it should also have anappendix
tag instead of asection
PreTeXt tries to help you semantically structure your book. Its worth following its lead!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your help! I've now made the change, converting the section to an appendix and including it inside the backmatter tag. I will make sure to follow PreTeXt's lead on structure in the future.