Example of a Java program



Similar documents
Object Oriented Software Design

Moving from CS 61A Scheme to CS 61B Java

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction to Java

Object Oriented Software Design

Chapter 2 Introduction to Java programming

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Informatica e Sistemi in Tempo Reale

Chapter 2: Elements of Java

Statements and Control Flow

VB.NET Programming Fundamentals

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

J a v a Quiz (Unit 3, Test 0 Practice)

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Unit 6. Loop statements

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Variables, Constants, and Data Types

Pemrograman Dasar. Basic Elements Of Java

PHP Tutorial From beginner to master

Introduction to Java Lecture Notes. Ryan Dougherty

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Java. CS 3: Computer Programming in Java

Sample CSE8A midterm Multiple Choice (circle one)

Iteration CHAPTER 6. Topic Summary

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

In this Chapter you ll learn:

Lecture 2 Notes: Flow of Control

JavaScript: Control Statements I

Lecture 1 Introduction to Java

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Introduction to Python

Java Crash Course Part I

Introduction to Visual C++.NET Programming. Using.NET Environment

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

CS170 Lab 11 Abstract Data Types & Objects

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

AP Computer Science Java Subset

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Java CPD (I) Frans Coenen Department of Computer Science

Java Basics: Data Types, Variables, and Loops

Chapter 1 Java Program Design and Development

Basic Object-Oriented Programming in Java

TIP: To access the WinRunner help system at any time, press the F1 key.

The C Programming Language course syllabus associate level

Part I. Multiple Choice Questions (2 points each):

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Java Interview Questions and Answers

Lecture 5: Java Fundamentals III

Exercise 4 Learning Python language fundamentals

Moving from C++ to VBA

Fundamentals of Programming

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Sources: On the Web: Slides will be available on:

Chapter 3 Operators and Control Flow

13 Classes & Objects with Constructors/Destructors

6. Control Structures

Keil C51 Cross Compiler

Conditional Statements Summer 2010 Margaret Reid-Miller

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

CS 111 Classes I 1. Software Organization View to this point:

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Programming and Data Structures with Java and JUnit. Rick Mercer

LOOPS CHAPTER CHAPTER GOALS

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Topic 11 Scanner object, conditional execution

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

1.3 Conditionals and Loops

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

csce4313 Programming Languages Scanner (pass/fail)

Programming in Java Course Technology, a part of Cengage Learning.

Java Program Coding Standards Programming for Information Technology

Installing Java (Windows) and Writing your First Program

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Statements and Control Flow

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Selection Statements

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Arrays. Introduction. Chapter 7

Java Programming Fundamentals

Conditions & Boolean Expressions

Primitive data types in Java

Software Development (CS2500)

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Transcription:

Example of a Java program class SomeNumbers static int square (int x) return x*x; public static void main (String[] args) int n=20; if (args.length > 0) // change default n = Integer.parseInt(args[0]); for (int i=0; i <= n; i++) System.out.print("The square of " + i + " is "); System.out.println(square(i));

Compiling and running the Java program $ javac SomeNumbers.java $ ls Some* SomeNumbers.class SomeNumbers.java $ java SomeNumbers 10 The square of 0 is 0 The square of 1 is 1 The square of 2 is 4 The square of 3 is 9 The square of 4 is 16 The square of 5 is 25 The square of 6 is 36 The square of 7 is 49 The square of 8 is 64 The square of 9 is 81 The square of 10 is 100 $

Class definition [access_modifier] class class_name method definition 1 method definition 2 method definition 3 method definition 4 // etc. access_modifier: (optional except for main method, which is public): one of public private protected - accessible to every user of the class - accessible only inside class - accessible only to classes in same directory (default if nothing written) class_name: identifier, i.e. lower or Upper case letter followed by letters, digits, or underscore "_". Class names start in Upper case by convention.

Method definition [access_modifier] [static] return_type method_name (parameter_list) statement 1 ; statement 2 ; statement 3 ; statement 4 ; // etc. return expression ; // except if return type is void return_type (mandatory): any data type, e.g. one of int boolean double float long char - primitive types void - nothing to be returned (method is not a "function") String - a class, defined elsewhere method_name: identifier convention: starts in lower case for methods

Parameter list type identifier1, type identifier2, type identifier3 etc. Example: static int gcd (int a, int b) // greatest common divisor type (mandatory): any data type. Each parameter needs its own type, even if several have the same type. Notes: - Parameters separated by COMMAS - even if empty parameter list, () required. - no semicolon after (parameter_list) Example: static void moan () System.out.println("This shouldn't have happened!"); then use as: moan(); // not just: moan; parentheses required

Statements: declarations Note: - ANY statement terminated by SEMICOLON - several statements per line possible - statement may use more than one line of text Examples: type identifier ; type identifier1, identifier2 ; // same type: list with commas type identifier = expression; // initialization int a; double p, q1, r_0 = 1.0; String s1; char letter = '@' ; Note: - the type of any variable must be declared before the variable is used.

Statements that "do something" Assignments: identifier = expression; Examples: i=0; b = c+d; i = i+1; // same as i++; s = "Hello" + ", my dear"; t1 = s + ", good to see you"; if/else and while: if (condition) statement; if (condition) statement1; else statement2; better: indented if (condition) statement1; else statement2; while (condition) statement;

Expressions Expressions appear: - on the right hand side of an assigment - as an argument of a method, assigned to the method's parameter. Example: square(a+1); - as return value for the return statement. Arithmetic expression: - with operators + - * / % [ use of blanks optional ] - precedence: * / % over + -. Use parentheses () - warning: a/b is integer if a,b integer. 10/3 is 3 fractional part of division is a%b, so 10 % 3 is 1 Boolean expression (same as condition): - with operators < > == <= >= between arithmetic expressions - note: use == for "is equal to",!= for "not equal to" - conjunction && and disjunction of two boolean expressions warning: evaluation will stop when result known: (false && expr), (true expr) will leave expr unevaluated, since result known to be false resp. true

Two purposes: Grouping of statements - group several statements together, in particular for if ( ) else while ( ) for ( ; ; ) Note: no semicolon after - limit the scope of a variable (identifier): Example: class Test static int a = 10; // global variable of Test void abc() // some method int a = 20; // this is now a new local variable System.out.println(a); // prints "20"

The for ( ; ; ) statement (iterative loop) for (statement1; condition; statement2) statement3 ; is equivalent to statement1; while (condition ) // note: loop may never be executed // if condition false from the start statement3 ; statement2 ; Example: int s=0; for (int i=0; i<=20; i++) // sum the first 20 integers s += i ; // add i to s // note: i local to the for loop, now no longer available

Increment operator ++ : Special operators counter++ ; is equivalent to counter = counter + 1 ; Decrement operator -- : counter-- ; is equivalent to counter = counter - 1 ; Arithmetic assignment operators += -= /= *= (add to, subtract from, divide by, multiply with) Examples: bigsum += b; is equivalent to bigsum = bigsum + b; position /= 2; is equivalent to position = position / 2; Bitwise operators & take the conjunction / disjunction of the bits in the integers: 12 & 5 (binary 1100 & 0101) is 4, 12 5 is 13

Indenting program text Statements belonging to the same group start in the same column (= indented by same amount). Indent after if, for, while. When a group starts, opened with a brace "" start on a new line and indent by 4 blanks all subsequent statements and close in the same column as the opening brace "" the closing brace on a new line: Note: "Java in a Nutshell" moves the opening brace up so that it no longer matches the closing brace This saves 1 line of vertical space but makes programs harder to read. The vi editor will automatically indent for you. Set tab stops to 4, and convert tabs to spaces instantly. (Configured like that in your _vimrc file.)