|
home—info—lects—labs—exams—hws
textbook—java.lang docs—java.util docs
We have already seen two types of values: String: "hello", "hi", "a", "" int: 43, -7, 2, 0 There is one more we'll see: double: 2.3, -7.0, 3.0e8, 6.022e23 ("double" means "double-precise *estimate* to the actual desired number"; we'll talk more on that later.) Examples: System.out.print( "hello" ) System.out.print( 7 ); System.out.print( "spy " + 7 ); // Note implicit conversion (Integer.toString(7)) Declaring a variable: examples A program: What's the volume of an ice-cream cone, 9cm high, 2cm radius? We are told by our math buddies that the formula is 1/3*base*height. The base will be pi*2*2 = ~12.56; 1/3 * ~12.56 * 9 = ~37.68
System.out.println( "The volume of an ice-cream-cone " ); System.out.println( "with height 9cm and radius 2cm is " ); System.out.println( 1/3 * (3.14*2*2) * 9 ); |
public class Lect01cTester { public static void main( String[] args ) { double r; r = 1.2; double h; h = 12.0; double base; base = 3.14 * r * r; System.out.println( "The volume of an ice-cream-cone " ); System.out.println( "with height " + h + "cm and radius " + r + "cm is " ); System.out.println( 1.0/3.0 * base * h ); } } |
home—info—lects—labs—exams—hws
textbook—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.Jan.27 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |