Background
I just started taking a Java course this week. It is an introduction-level undergrad course to computer science from my university. It is taught online though.
To start, download and install Java 8 and Eclipse.
This course uses two online platforms: Piazza as a message board for Q&A, and Bitbucket as a git solution for teamwork.
Course page: http://www.cse.wustl.edu/~dshook/cse131/
Textbook online version: http://introcs.cs.princeton.edu/java/home/
I typed a lot of notes in Word documents, but here I am (obviously) not going to write down everything. Just some tips.
Table of Contents
Before we get started... Tip 1: How to undo typing (go back) in Eclipse? Tip 2: How to type System.out.println() fast? Module 1: Data Types Tip 3: Review built-in data types Tip 4: Do the math carefully / and % for integers / for doubles evaluation tree operator cannot be chained Tip 5: Useful functions and values Transcendental functions Return random numbers within the range [0,1) Constants π and e Scientific notation Tip 6: Rounding How to round numbers to two decimal places? Tip 7: Prompt the user for input values Tip 8: How to convert types?
Before we get started…
Tip 1: How to undo typing (go back) in Eclipse?
right click – choose “undo typing”
Or
press “Ctrl + Z”
(I did not realize that until the end of module 1. It’s not used very often, but good to know.)
Tip 2: How to type System.out.println() fast?
It’s pretty long! But you can type it fast by content assist.
Steps:
(1) Type “Sysout”
(2) Press “Ctrl+Space”
It’s done! System.out.println() will show up automatically.
Module 1: Data Types
Tip 3: Review built-in data types
// Below is what I learnt from this module:
char
String *Note that String starts with the capital letter S
int
long
double *Doubles are real numbers.
boolean
// Others data types (byte, short, float):
see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Tip4: Do the math carefully
/ and % for integers
3/2 = 1; 5/2 = 2. Everything after the decimal point will be dropped.
The % operator computes the remainder after division
3%2 = 1, 5%2 = 1
/ for doubles
However, the slash “/” works differently for doubles.
3.0/2.0 = 1.5
Evaluation tree
Note that the data type is converted before dividing by 2.
(Screenshot. Reference: course website http://www.cse.wustl.edu/~dshook/cse131/)
Q. Why doesn’t the statement if (a <= b <= c) work?
A. The <= operator cannot be chained. Instead, use if (a <= b && b <= c).
Tip 5: Useful functions and values
Transcendental functions
- Math.sin( ), Math.cos( ), Math.tan( )
- Math.log( )
- Math.exp( )
- Math.sqrt( )
Return random numbers within the range [0,1)
syntax:
double r = Math.random();
0 ≤ r < 1
Constants π and e
- Math.PI – close to π
- Math.E – close to e
Scientific notation
- We can use scientific notation
- The E character represents “times 10 to the”
- You can also specify the E as lower-case e
“double val = 3E-9” is the same with “double val = .000000003”
Tip 6: Rounding
Math.round ()
- round numbers to the nearest integer
How to round numbers to two decimal places?
Below is the answer from Jonik:
(Source: stackoverflow)
double time = 200.3456;
DecimalFormat df = new DecimalFormat("#.##");
time = Double.valueOf(df.format(time));
System.out.println(time); // 200.35
“Note that this will actually do the rounding for you, not just formatting.”
Below is the rationale behind this.
(Source: http://mathbits.com/MathBits/Java/DataBasics/money.htm)
“The pattern establishes how the output will look – “00.00” – the zeroes in front of the point will be the minimum number of digits displayed. The zeros after the point will be the maximum number of digits displayed.”
I noticed that some people use “#.##” while some use “0.00”. I tried both and both worked fine.
—-
Only one problem! I would get an output value such as 90.5 instead of 90.50.
How to keep exactly two decimal places? (That is, show “90.50”, for example)
I have asked my instructor. I will update this section when he answers.
Update: OK he said it’s fine because the assignment requires rounding to two decimal points and having 0s dropped off is fine. If I want to keep exactly two decimal points, I can use string manipulation.
—–
Update 2: I later realized that there’s an easier way to do this with just Math.round().
For example (source: http://stackoverflow.com/questions/13210491/math-round-java):
double inches = Math.round( (centimeters / 2.54) * 100.0 ) / 100.0;
Tip 7: Prompt the user for input values
// example
ArgsProcessor ap = new ArgsProcessor(args);
int aliceCarrots = ap.nextInt(“Starting number of carrots?”);
String name = ap.nextString(“What’s the name of the person?”);
double quiz = ap.nextDouble(“What’s the number of quiz points received?”);
Tip 8: How to convert types?
Type casting: There are two types – implicit and explicit
This website provides clear instructions about this:
http://www.studytonight.com/java/type-casting-in-java
Examples:
*Note that casting such as (int) will remove all decimals, e.g., turning 20.74 to 20. So it is different from rounding, which turns 20.74 to 21.
int i = 100; long l = i; //no explicit type casting required float f = l; //no explicit type casting required
double d = 100.04; long l = (long)d; //explicit type casting required int i = (int)l; //explicit type casting required
0 ≤ Math.random() < 1
I forgot that Math.random() doesn't include 1 today, causing some confusion for myself when programming. Important point.
LikeLike
thanks for the tips you share.I just started my career as a developer in a company as a fresher and collecting data for learning. These tips surely gonna help me in future.Thank You
LikeLike
You are welcome. Congrats on your new job. I am learning too.
LikeLike