laboratory study october, 17 2003. the very first example, traditional "hello world!"...

11
Laboratory Study October, 17 2003

Upload: cori-welch

Post on 26-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

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

Laboratory Study

October, 17 2003

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

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

1exp is a phrase (known as an expression) that denotes a value.

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

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 JavaProgrammingLanguage 4.) Write a variant of System.out.println as differentnamed System.out.print that displays thegiven message, but does not go to the next line for message:Java Programming Language (not JavaProgrammingLanguage)

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

A Fibonacci Programclass 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.

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

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.02 + 3 = 59.0 / 4 = 2.259 / 4 = 2 Test the results of different arithmetic operations on your computer

console

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

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); } }

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

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!

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

Overloading of + operatorAn 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 is42 + 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

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

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); }

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

Series Summation Program

An arithmetic series is a series of n integers i1, i 2 ,….…in where each element is greater than theprevious element by the same value d.

For instance, here are some examples of arithmetic series:

Series n i1 in d6;7; 8; 9; 10; 11; 12 7 6 12 12; 4; 6; 8 4 2 8 26; 11; 16; 21; 26; 31; 36; 41 8 6 41 5

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

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 (i1 + 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 : ( (in - i1) / 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.