Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/denezt/java-coding-examples
Java Programming and Design Examples
https://github.com/denezt/java-coding-examples
Last synced: 4 days ago
JSON representation
Java Programming and Design Examples
- Host: GitHub
- URL: https://github.com/denezt/java-coding-examples
- Owner: denezt
- Created: 2016-02-22T01:24:15.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-30T08:16:36.000Z (11 months ago)
- Last Synced: 2023-12-30T09:23:16.767Z (11 months ago)
- Language: Java
- Homepage:
- Size: 50.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Java-Sessions
Java Programming and Computational Design
date: 2013-06-01 00:34:15
name: Computer Science 046 | factsheets
---# Factsheets
**Variables**
Variable_Declaration
Comment
Noteint age = 21;
This declares an integer variable and initializes it to 21.
Recommendedint nextAge = age + 1;
The initial value of a variable can be an expression (as long as age has been previously declared.)
RecommendedString course= "Udacity";
The variable has type String and is assigned an initial value of "Udacity".
Recommendedscore= 80;
ERROR: the type is required. This statement will not declare a variable. It is an assignment statement which assigns a new value to an existing variable.
NOT Recommendedint age= "42";
ERROR: You cannot initialize a number with a String. "42" is a String. See the quotation marks.
NOT Recommendedint age;
This declares an integer variable without initializing it. It is best to initialize variables when they are created: int age = 0; If you do not know what value you want yet
----Naming Rule
ExampleNames must consist of letters, numbers, an underscore, or a dollar sign only.
score_1Don't use single letter variable name as you do in mathematics. While it is legal in Java, it is usually not a good idea because it can make programs harder to understand. (you will encounter a couple of exceptions later)
aWARNING: Names are case sensitive. Note that by convention, variable names start with a lowercase letter
FinalGrade, finalGrade, and FINALGRADE are all different variablesERROR Names cannot start with a number.
7upERROR. You cannot use a reserved word as a name.
intERROR: You cannot use special characters such as * or & in names.
m&mERROR: Names cannot contain spaces.
final grade**Number Types**
Type
Range
Sizeint (integer)
–2,147,483,648 to 2,147,483,647(~2.14 billion)
4 bytesshort (integer)
-32,768 to 32,767
2 byteslong (integer)
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
8 bytesbyte
-128 to 127
1 bytedouble(double-precision floating point)
range of about + or - 10^308
8 bytes
float(single-precision floating point)
range of about + or - 10^38 & about 7 significant decimal places
4 bytes
char
represents a Unicode character
2 bytes
boolean
has only 2 possible values: true or false
1 bitNumber Literals
Description
Examplesint
An integer has no fractional part and can be positive, negative, or 0.
5,-100,0double
A number with fractional part
1.7, 1.0, 2.4E5, 3.47E-2ERROR
Do not use a comma to separate thousands
1,000,000ERROR
Do not use a fraction. Use a decimal instead.
3 1/4**Integer Arithmetic**
Expression
Value (when n =2497)
Descriptionn/10
249
Notice that the answer is an integer with no decimal part.n % 10
7
Always the last digit of nn /100
24
Again, decimal part is discarded. Removes the last 2 digits.n % 100
97
The last two digits.n % 2
1
If n % 2 is 0 the number is even. Otherwise it is odd.**Math Functions**
Method
Return ValueMath.sqrt (n)
Square root of n ( if n is > or = to 0)Math.pow(a,b)
a^b( if a = 0, b must be >0)Math.sin(n)
Sine of n where n is in radiansMath.cos(n)
Cosine of n where n is in radiansMath.tan(n)
Tangent of n where n is in radiansMath.round(n)
closest integer to n as a longMath.ceil(n)
smallest integer > or = to n as a doubleMath.floor(n)
largest integer < or = to n as a doubleMath.toRadians(n)
Converts n degrees to radiansMath.toDegrees(n)
Converts n radians to degreesMath.abs(n)
Absolute value of n |n|Math.max(a,b)
The larger of a and bMath.min(a,b)
The smaller of a and bMath.exp(n)
e^nMath.log(n)
natural log of nMath.log10(n)
Base 10 log of n**String Formatting**
Code
In an Example
Type
What It Printsd
"%4d"
Decimal integer
123x
"%x"
Hexadecimal integer
7Ao
"%o"
Octal integer
173f
"%5.2f"
Fixed floating-point
12.30e
"%e"
Exponential (very large or small) floating-point
1.23e+1g
"%3.2g"
General (medium sized) floating-point
12.3s
"%s"
String
Tax:n
"%n" or "\n"
Line end**Format Flags**
Flag
In an Example
Meaning
What It Prints-
"%-6d"
Align left
an integer that takes 6 spaces and starts in the first one0
"%07.2f"
Show leading zeroes
0001.23+
"%+7.2f"
Show a plus sign for positive numbers
+1.23(
"%(6.2f"
Enclose negative numbers in parentheses
-1.23 would look like (1.23),
"%,10d"
Show decimal separators
12,300^
"%^s"
convert letters to uppercase
"tax:" would print as "TAX:"**Strings**
Example_Code_For_String_Methods
Result
Other infoString str = "Java ";
str = str + "Programming"
str is assigned the value "Java Programming"
The + sign is used to concatenate StringsString answer = "Total: " + 42;
answer is set to "Total: 42"
Because "Total: " is a string 42 is converted to a string and then the concatenation takes placeString name = "Sara T";
int len = name.length();
len is set to 6
The number of characters in a string. A space counts as a characterString city = "San Jose";
String sub = city.substring(1, 3);
sub is set to "an"
Takes the substring starting at position 1 and ending before position 3String city = "San Jose";
String first = city.substring(0, 1);
first is set to "S"
Gets the first character. The substring has length 1String city = "San Jose";
String sub = city.substring(4);"
sub is set to "Jose"
If you only supply one parameter, the substring consists of all characters from that position until the end of the StringString city = "San Jose";
String last = city.substring(city.length() - 1);
returns the string containing the last letter in the string ("e") and assigns it to last
str.substring(str.length() - 1) will always give you the last character as a StringString city = "San Jose";
int index = city.indexOf("Jose")index is set to 4
returns the index where "Jose" startsString city = "Santa Barbara";
int index = city.lastIndexOf("a")index is set to 12
returns the index of the last "a" in the stringString cityWithTypo = "Son Jose";
String cityCorrected = cityWithTypo.replace("Son","San");Changes all ocurrences of "Son" to "San" in cityWithTypo and put the result in cityCorrected
Will also worked the following ("So","Sa");String sentence = "Joseph is in San Jose";
int index = sentence.indexOf("Jose", 2)
index is set to 17
indexOf returns the index where "Jose" starts. When an index position is supplied as the second argument (2 in this case), search will begin AFTER that indexCommon Loop Algorithms
======================Sum
-------
total = 0
for each item
total = total + inputCounting Matches
----------------
matches = 0
for each item
if the item matches
matches = matches + 1Finding the Location of the First Match
----------------
found = false
position = 0
while it's not found, and there are more items
if the item at position matches
found = true
else
position = position + 1
if the item was found
its location is positionMaximum
-----------
largest = the first item
for all the items except the first
if the current item is larger than largest
replace the value in largest with the current itemLast Update: 21.08.2021