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

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Lecture 2 Basics of Java Programming Language Slides produced by Antonio Martinez Java Programming: From the Ground Up.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CMT Programming Software Applications
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
2.2 Information on Program Appearance and Printing.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Working with arrays (we will use an array of double as example)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Mixing integer and floating point numbers in an arithmetic operation.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
By Mr. Muhammad Pervez Akhtar
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
CompSci 230 S Programming Techniques
The eclipse IDE IDE = “Integrated Development Environment”
Exercise Java programming
Building Java Programs
Primitive Data, Variables, Loops (Maybe)
Intro to Java.
Writing Methods.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Building Java Programs
Building Java Programs
CS110D Programming Language I
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
CS 1054 Introduction to Programming in Java
Instructor: Alexander Stoytchev
Building Java Programs
Presentation transcript:

Laboratory Study October,

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.

 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)

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.

Numerical Calculations public class Numerical Examples { public static void main (String [] args) { System.out.print (" = "); System.out.println ( ); 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: = = / 4 = / 4 = 2  Test the results of different arithmetic operations on your computer console

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 (" = " + ( )); System.out.print ("2 + 3 = " + (2 + 3)); System.out.print ("9.0 / 4 = " + (9.0 / 4)); System.out.print ("9 / 4 = " + (9 / 4); } }

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

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" "42" + "3.141“ " " 42 + "3.141" "42" + "3.141" “ " "63" "63" + " " " " 63 + “ " "63" + " " "  Show this representations on your computer console

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

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; ; 4; 6; ; 11; 16; 21; 26; 31; 36;

 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.