generated from fastai/fastpages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf97163
commit 6f18df7
Showing
1 changed file
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,255 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Lesson 3 Hacks\n", | ||
"\n", | ||
"- toc: true\n", | ||
"- branch: master\n", | ||
"- badges: true\n", | ||
"- comments: true\n", | ||
"- categories: [college board, java]" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Recursion" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "java" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"* * * * * * * * * * \n", | ||
"* * * * * * * * * \n", | ||
"* * * * * * * * \n", | ||
"* * * * * * * \n", | ||
"* * * * * * \n", | ||
"* * * * * \n", | ||
"* * * * \n", | ||
"* * * \n", | ||
"* * \n", | ||
"* \n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"public void drawLine(int n) {\n", | ||
" for (int i = 1; i <= n; i++) {\n", | ||
" System.out.print(\"* \");\n", | ||
" // System.out.print();\n", | ||
" // drawLine(n - 1);\n", | ||
" }\n", | ||
" System.out.println();\n", | ||
" if (n>1) {\n", | ||
" drawLine(n-1);\n", | ||
" }\n", | ||
"}\n", | ||
"drawLine(10);" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## ArrayList" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "java" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[a, b, c, d, e, f]" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"public ArrayList<Character> alphabet(ArrayList<Character> myArr) {\n", | ||
" int originalSize = myArr.size();\n", | ||
" myArr.clear();\n", | ||
"\n", | ||
" for (char c = 'a'; c < 'a' + originalSize; c++) {\n", | ||
" myArr.add(c);\n", | ||
" }\n", | ||
" return myArr;\n", | ||
"}\n", | ||
"ArrayList<Character> old = new ArrayList<Character>(Arrays.asList('j','l','k','a','s','d'));\n", | ||
"alphabet(old);" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "java" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[1, 3, 5, 7, 9]" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"public ArrayList<Integer> remove(ArrayList<Integer> myArr) {\n", | ||
" for (int i = 1; i < myArr.size(); i++) {\n", | ||
" myArr.remove(i);\n", | ||
" }\n", | ||
" return myArr;\n", | ||
"}\n", | ||
"ArrayList<Integer> newArr= new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));\n", | ||
"remove(newArr);" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Sorting" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "java" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Russia (17098246)\n", | ||
"Canada (9984670)\n", | ||
"China (9596961)\n", | ||
"United States (9372610)\n", | ||
"Brazil (8515767)\n", | ||
"Australia (7692024)\n", | ||
"India (3287263)\n", | ||
"Argentina (2780400)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import java.util.ArrayList;\n", | ||
"import java.util.Collections;\n", | ||
"import java.util.Comparator;\n", | ||
"\n", | ||
"public class Main {\n", | ||
" public static void main(String[] args) {\n", | ||
" ArrayList<Country> countries = new ArrayList<Country>();\n", | ||
" countries.add(new Country(\"Brazil\", 8515767));\n", | ||
" countries.add(new Country(\"India\", 3287263));\n", | ||
" countries.add(new Country(\"Australia\", 7692024));\n", | ||
" countries.add(new Country(\"China\", 9596961));\n", | ||
" countries.add(new Country(\"Canada\", 9984670));\n", | ||
" countries.add(new Country(\"Russia\", 17098246));\n", | ||
" countries.add(new Country(\"Argentina\", 2780400));\n", | ||
" countries.add(new Country(\"United States\", 9372610));\n", | ||
" \n", | ||
"\n", | ||
" // Sort the countries in decreasing order of size using a selection sort algorithm and a Comparator\n", | ||
" Comparator<Country> sizeComparator = new Comparator<Country>() {\n", | ||
" @Override\n", | ||
" public int compare(Country c1, Country c2) {\n", | ||
" return c1.getSize() - c2.getSize();\n", | ||
" }\n", | ||
" };\n", | ||
" for (int i = 0; i < countries.size() - 1; i++) {\n", | ||
" int maxIndex = i;\n", | ||
" for (int j = i + 1; j < countries.size(); j++) {\n", | ||
" if (sizeComparator.compare(countries.get(j), countries.get(maxIndex)) > 0) {\n", | ||
" maxIndex = j;\n", | ||
" }\n", | ||
" }\n", | ||
" if (maxIndex != i) {\n", | ||
" Collections.swap(countries, i, maxIndex);\n", | ||
" }\n", | ||
" }\n", | ||
"\n", | ||
" // Print the sorted countries\n", | ||
" for (Country country : countries) {\n", | ||
" System.out.println(country.getName() + \" (\" + country.getSize() + \")\");\n", | ||
" }\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"class Country {\n", | ||
" private String name;\n", | ||
" private int size;\n", | ||
"\n", | ||
" public Country(String name, int size) {\n", | ||
" this.name = name;\n", | ||
" this.size = size;\n", | ||
" }\n", | ||
"\n", | ||
" public String getName() {\n", | ||
" return name;\n", | ||
" }\n", | ||
"\n", | ||
" public int getSize() {\n", | ||
" return size;\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"Main.main(null);" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Java", | ||
"language": "java", | ||
"name": "java" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "java", | ||
"file_extension": ".jshell", | ||
"mimetype": "text/x-java-source", | ||
"name": "Java", | ||
"pygments_lexer": "java", | ||
"version": "11.0.16.1+1" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |