We will again be working with ../demos/W01N02.java (remember, ../demos/TextIO.java must also be in the directory where you saved W01N02.java)
Variable names should:
· start with a lower case letter,
· may not contain spaces,
· if is a two-word name, e.g., userName, should use the lowerCamelCase format.
1. reserve a location in memory and give this reserved location a name:
double radius;
reserves a memory location that holds a floating point number and gives this location the name radius
Remember: to declare a variable, you write the name of a type, e.g., double, followed by a name you create that says something about the purpose of the data stored in the variable, e.g., radius will hold a radius.
2. reserve a location in memory named s to hold a String value:
String s;
1. Assignment statement to store the value 5.0 in the variable named radius:
radius = 5.0;
Remember: variables must be declared before they can be assigned a value.
2. Store a String value in the variable named s:
s = "Hello World!"
1. Call to System.out.println() method to print a String in the run I/O window:
System.out.println("Hello World!");
2. Call the readDouble() method to print a prompt then read a value from the user:
TextIO.getlnDouble("Your Value> ");
At this point, the user must type a numeric value in the Run I/O window then type the Enter key in order for our program to continue.
Both variable declarations and statements must end with a semicolon!
Any expression containing a . is usually a call to some method, but here is an exception:
Math.PI refers to the value 3.14159 that is used to calculate values related to circles
Arithmetic:
circumference = 2*Math.PI*radius;
All multiplication operations must use the * character:
circumference = 2Math.PI*radius;
results in a compiler error:
Hint: when working with values of type double, always include a decimal point, even if you are typing a round number, e.g., type 3.0, not 3
Means arithmetic addition
4.0 + 3.0
also means concatenation when one (or both) of the operands is a String
System.out.println("The answer is: " + volume);
Compiler errors are shown in the run I/O JGrasp window.
Common errors:
Cannot find symbol.
W01N03.java:7:
cannot find symbol
symbol :
variable radius
location: class W01N03
radius = scan.nextDouble();
^
Compiler tells you what symbol it cant find and what it thinks the
symbol should be, in this case, a variable named radius.
The ^ sometimes, but not always points at the position where the problem is.
probable cause for Cannot find symbol errors:
A variable has not been declared. All variables must be declared before they are used as part of an expression or assignment statement.
Illegal character \8220
W01N03.java:5:
illegal character: \8220
__System.out.println(Hello
World!");
probable cause
Copied and pasted code from an internet browser into JGrasp
Unclosed String literal
W01N03.java:5:
unclosed string literal
System.out.println("Hello
World!);
probable cause
A String literal has an open double quote, but no close double quote the example above has an \8220 character following the ! instead of a real double quote.
; expected
W01N03.java:7:
';' expected
radius = scan.nextDouble();
probable cause
statement above the one given in the error message is missing its semicolon
Other errors can and WILL happen. Read the entire error message and try to deduce the cause. Also remember that the error is often on, but sometimes above the line the compiler identifies.
4/3*Math.PI*r*r*r produced 3.14159 when r was 1
In the first division, the operands are 4 and 3. These are both integers (whole numbers) if both operands are integers, java does integer division, which calculates a quotient and a remainder like you did in the 4th grade.
Remember to include a .0 after values of type double, e.g., 4.0/3.0
View | Line Numbers
Comments are characters in the source code that are NOT executed by the computer. You put them in to make your code more understandable to a human reader, like yourself.
First write comments that are notes on what you want to do, then translate your notes into java statements. We often call these notes pseudo-code.
Leave these comments in your code after you have finished, and they document what you have done for other readers.
/* This is a
multi-line comment.
*/
All characters between and including the opening /* and the closing */ are skipped by the compiler.
// This is a single line comment.
// Any text to the right of, and including, the two slashes
// up to and including the next linefeed <lf> is not
// considered by the compiler to be executable java code.
Write a program named TrapezoidArea that calculates and displays the area of a general trapezoid. The formula (expressed as you would see it in math, but not in programming) is
area = ½(a+b)h
Start by modifying W01N02.java:
1. Name the class TrapezoidArea
2. Save to a file named TrapezoidArea.java
You need to retrieve the values for a, b, and h from the user.