Presentation is loading. Please wait.

Presentation is loading. Please wait.

Laboratory Study October, 17 2003. The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]

Similar presentations


Presentation on theme: "Laboratory Study October, 17 2003. The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]"— Presentation transcript:

1 Laboratory Study October, 17 2003

2 The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ] args) { System.out.println("Hello World!"); }  1.) Which line is the core of this phrase (program)? Answer : This is an example of a statement, a phrase whose execution causes an action to happen. We could treat System.out.println (exp )1; as a magical line that prints out the value of expression 1 exp is a phrase (known as an expression) that denotes a value.

3  2.) To write the simple Java program named first, which lines are required? Answer:public class first { public static void main (String [] args) { : : : } }  3.) Write different screen outputs as Java Programming Language  4.) Write a variant of System.out.println as different named System.out.print that displays the given message, but does not go to the next line for message: Java Programming Language (not JavaProgrammingLanguage)

4 A Fibonacci Program class Fibonacci { public static void main (String args[ ]) { int lo = 1; int hi = 1; System.out.println(lo); while ( hi < 50 ) { System.out.println(hi); hi = lo + hi; lo = hi - lo; } } }  Type this code into the proper filename.  Compile and run the program.  Discuss the results.

5 Numerical Calculations public class Numerical Examples { public static void main (String [] args) { System.out.print ("2 + 3.0 = "); System.out.println (2 + 3.0); System.out.print ("2 + 3 = "); System.out.println (2 + 3); System.out.print ("9.0 / 4 = "); System.out.println (9.0 / 4); System.out.print ("9 / 4 = "); System.out.println (9 / 4); } } The result is displayed as: 2 + 3.0 = 5.0 2 + 3 = 5 9.0 / 4 = 2.25 9 / 4 = 2  Test the results of different arithmetic operations on your computer console

6 String Concatenation  Java's + operator is not only used to add numbers. It is also used to concatenate two strings together. For example, the expression: "Java" + "line" denotes the 8-character string "Javaline".  Using + for string concatenation can simplify programs by reducing the number of calls to System.out.print and System.out.println. For example: public class NumericalExamples { public static void main (String [] args) { System.out.print ("2 + 3.0 = " + (2 + 3.0)); System.out.print ("2 + 3 = " + (2 + 3)); System.out.print ("9.0 / 4 = " + (9.0 / 4)); System.out.print ("9 / 4 = " + (9 / 4); } }

7  In previous example the parenthesis around numerical expressions like (2 + 3.0) are required, not optional.  If we instead wrote "2 + 3.0 = " + 2 + 3.0, this would be parenthesized by Java as ("2 + 3.0 = " + 2) + 3.0  By the coercion rules from above this would be equivalent to ("2 + 3.0 = " + "2") + "3.0".  So the displayed result would be 2 + 3.0 = 23.0 which is not at all what was intended!

8 Overloading of + operator An operator like + that has different meanings in different contexts (e.g., add two integers vs. add two floating point numbers vs. concatenate two strings) is said to be overloaded. Expression is treated as whose value is 42 + 3.141 42.0 + 3.141 45.141 "42" + 3.141 "42" + "3.141“ "423.141" 42 + "3.141" "42" + "3.141" “423.141" 63 + -273.15 63.0 + -273.15 -210.15 "63" + -273.15 "63" + "-273.15" "63-273.15" 63 + “-273.15" "63" + "-273.15" "63-273.15  Show this representations on your computer console

9 Naming Values Via Variables An example program indicating variable operations: public static void main (String [ ] args) { double a = 10.27; double b = 8.56; double a_squared = a*a; double b_squared = b*b; System.out.println ((a_squared + b_squared) / (a_squared - b_squared)); } Another version of the above program can be written as follows:: public static void main (String [] args) { double a = 10.27; double b = 8.56; double a_squared = a*a; double b_squared = b*b; double sum = a_squared + b_squared double diff = a_squared - b_squared double quot = sum/diff System.out.println(quot); }

10 Series Summation Program An arithmetic series is a series of n integers i 1, i 2,….…i n where each element is greater than the previous element by the same value d. For instance, here are some examples of arithmetic series: Series n i 1 i n d 6;7; 8; 9; 10; 11; 12 7 6 12 1 2; 4; 6; 8 4 2 8 2 6; 11; 16; 21; 26; 31; 36; 41 8 6 41 5

11  The sum of such a series is the number of pairs of elements multiplied by the sum of the first and last elements: n /2 (i 1 + i n ).  Suppose that we are not given n, but are given the first and last elements of the series and the difference d between consecutive elements.  Then we can calculate n as : ( (i n - i 1 ) / d ) + 1  For instance, for series with first element 6, last element 41 and difference 5, n = (41-6) / 5 +1 = 8.  Write a Java program for calculating the sum of the more general form of an arithmetic series.


Download ppt "Laboratory Study October, 17 2003. The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]"

Similar presentations


Ads by Google