Basic Java Syntax. Program Structure



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

Introduction to Object-Oriented Programming

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)

Introduction to Java

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

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

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

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Java Programming Fundamentals

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Example of a Java program

Java Crash Course Part I

Installing Java (Windows) and Writing your First Program

Chapter 2 Introduction to Java programming

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Sample CSE8A midterm Multiple Choice (circle one)

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Programmierpraktikum

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

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.

CSE 308. Coding Conventions. Reference

AP Computer Science Java Subset

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

LAB4 Making Classes and Objects

PHP Tutorial From beginner to master

CSC 551: Web Programming. Fall 2001

Java Interview Questions and Answers

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

Fundamentals of Java Programming

CS170 Lab 11 Abstract Data Types & Objects

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

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

Python for Rookies. Example Examination Paper

Moving from CS 61A Scheme to CS 61B Java

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

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

Java Basics: Data Types, Variables, and Loops

CSC 551: Web Programming. Spring 2004

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

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

CMPT 183 Foundations of Computer Science I

Object Oriented Software Design

Introduction to Java Lecture Notes. Ryan Dougherty

Contents. 9-1 Copyright (c) N. Afshartous

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

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

Building Java Programs

ECE 122. Engineering Problem Solving with Java

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

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

Chapter 1 Java Program Design and Development

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Software documentation systems

Building Java Programs

Lecture 5: Java Fundamentals III

Lecture 2 Notes: Flow of Control

Unit 6. Loop statements

Pemrograman Dasar. Basic Elements Of Java

Section 6 Spring 2013

Fundamentals of Programming and Software Development Lesson Objectives

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Software Development (CS2500)

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

C++ INTERVIEW QUESTIONS

Java from a C perspective. Plan

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

An Incomplete C++ Primer. University of Wyoming MA 5310

public static void main(string[] args) { System.out.println("hello, world"); } }

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

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Statements and Control Flow

Java with Eclipse: Setup & Getting Started

JAVA Program For Processing SMS Messages

Crash Course in Java

The C Programming Language course syllabus associate level

Chapter 2: Elements of Java

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Explain the relationship between a class and an object. Which is general and which is specific?

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

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Quick Introduction to Java

C Compiler Targeting the Java Virtual Machine

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Python Programming: An Introduction to Computer Science

An Overview of Java. overview-1

Java Application Developer Certificate Program Competencies

Using Files as Input/Output in Java 5.0 Applications

How to use the Eclipse IDE for Java Application Development

Basic Programming and PC Skills: Basic Programming and PC Skills:

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

Install Java Development Kit (JDK) 1.8

Reading and Understanding Java s API Documentation

IBM Operational Decision Manager Version 8 Release 5. Getting Started with Business Rules

Konzepte objektorientierter Programmierung

Transcription:

Basic Java Syntax The java language will be described by working through its features: Variable types and expressions. Selection and iteration. Classes. Exceptions. Small sample programs will be provided to illustrate how each feature is used. 9/10/2003 Statements 1 Program Structure A program in java consists of one or more class definitions. One of these classes must define a method main(), which is where the program starts running // A Java Hello World Program public class HelloWorld { public static void main( String args[] ) { System.out.println( "Hello World" ); 9/10/2003 Statements 2 1

Comments Comments come in three forms: // single line comments /* multi line comment */ /** a * Javadoc * comment */ 9/10/2003 Statements 3 Javadoc A tool that comes with the JDK that produces html-based documentation from java source code. Within a Javadoc comment, various tags can appear which allow additional information to be processed. Each tag is marked by an @ symbol and should start on a new line. 9/10/2003 Statements 4 2

Javadoc Tags Tag @author Description Name the author(s) of the code: @author Paul Tymann @author Paul Tymann, Jim Gosling @deprecated @exception @param Indicates that the following method will be removed in future versions Information on exceptions thrown by a method Provide information about method and constructor parameters. The tag is followed by a parameter name and a comment @param count number of elements @return @see Return value for non-void methods Provide cross reference to another class, interface, method, variable or URL. @see java.lang.integer @since When a particular feature was included (i.e. since when it has been available) @since JDK 1.0 @version Version information about the current revision of the code being documentated 9/10/2003 Statements 5 Example /** * A class that manages a circle given the radius * @see java.lang.math * @version 1.0 * @author Paul Tymann */ public class Circle { private double radius; /** * Constructor for a circle. * * @param radius radius of the circle being created. Must be * positive and greater than 0. * */ public Circle( double radius ) { this.radius = radius; 9/10/2003 Statements 6 3

The Result The result is a set of HTML pages. The documentation that is produced is meant to be part of the overall documentation that comes with the JDK. The 1.1 version of Javadoc did not support local modifications to the java documentation well. A much improved version of Javadoc is provided with java2. 9/10/2003 Statements 7 Statement The statement is the main building block from which code sequences are constructed. Statements are executed in the order listed and are always terminated by a semicolon. expr; or { expr1; expr2; exprn; 9/10/2003 Statements 8 4

Syntax: The if Statement if ( booleanexpression ) statement or if ( booleanexpression ) statement else statement Note you can layout code in any way you want. 9/10/2003 Statements 9 Syntax: The switch statement switch ( expression ) { case char/byte/short/int constant : statementsequence default: statementsequence As in C, break statements are needed to jump out of a switch statement. The default case is optional. 9/10/2003 Statements 10 5

Example int z; switch ( i ) { case 1: z = 1; break; case 2: z = 2; case 3: z = 3; break; default: z = 0; 9/10/2003 Statements 11 Syntax: The while Loop while ( booleanexpression ) statement 9/10/2003 Statements 12 6

The do Loop Syntax: do statement while ( booleanexpression ); 9/10/2003 Statements 13 Syntax: The for Loop for ( initexpr; booleanexpr; updateexpr ) statement Each of the expressions is optional, the semicolons are not. A for loop is basically a while loop with initialization and updating thrown in. 9/10/2003 Statements 14 7

Transfer Statements The break statement can occur anywhere within a switch, for, while or do statement and causes execution to jump to the next statement. The continue statement can occur anywhere within a for, while or do statement and causes execution to jump to the end of the loop body. The return statement causes the execution of the current method, with control returning to the caller. 9/10/2003 Statements 15 Objects An object is a structure that represents a state and knows methods to manipulate it. The structure components are called instance variables. Given a class, one normally creates objects. Objects are created dynamically with operator new which in turn calls a constructor method to initialize the instance variables. Methods mostly access the instance variables of the receiver. 9/10/2003 Statements 16 8

Java Classes The Java system comes with an extensive set of classes from which you may create objects. Lets start with a familiar class String. To find out what you can do to java strings you need to refer to the documentation that comes with the JDK. 9/10/2003 Statements 17 Name.java // A simple program that exercises some basic methods // in the String class. Note: Strings are constant. public class Name { public static void main( String args[] ) { String name; int midloc; name = "Paul"; name = name.concat( " Tymann" ); midloc = name.indexof( " " ); name = name.substring( 0, midloc ) + " Thomas" + name.substring( midloc ); System.out.println( name ); for (int i=0; i<name.length() && name.charat(i)!= ' '; i++ ) System.out.println( name.charat(i) ); 9/10/2003 Statements 18 9

Reverse.java // This program reverses a given string public class Reverse { public static void main( String args[] ) { String orig = "Hello World"; String reverse = ""; for (int i=0; i<orig.length(); i++) reverse = orig.charat( i ) + reverse; System.out.println( reverse ); 9/10/2003 Statements 19 StringBuffer The String class provides string objects that cannot be changed. The StringBuffer class provides mutable string objects. 9/10/2003 Statements 20 10

Reverse2 // Another way to reverse a string public class Reverse2 { public static void main( String args[] ) { StringBuffer rev = new StringBuffer ( Hello World ); char tmp; for (int i=0,j=rev.length()-1; i<j; i++,j-- ) { tmp = rev.charat( i ); rev.setcharat(i, rev.charat(j) ); rev.setcharat(j, tmp ); System.out.println( rev ); 9/10/2003 Statements 21 Palin // This program checks a given string to see if it is a palindrome public class Palin { public static void main( String args[] ) { String orig = "mom, reverse = ""; // Reverse it for (int i=0; i<orig.length(); i++) reverse = orig.charat( i ) + reverse; // Now check it ( note that orig == reverse does not work ) if (orig.equalsignorecase(reverse)) System.out.println( "Palindrome" ); else System.out.println( "Not a palindrome" ); 9/10/2003 Statements 22 11