Chapters 1-4 Summary. Syntax - Java or C? Syntax - Java or C?

Similar documents
Program Fundamentals

CS 11 java track: lecture 1

Getting started with Java

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

CMSC131. Introduction to your Introduction to Java. Why Java?

Java Basic Programming Constructs

Java Bytecode (binary file)

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Java Identifiers, Data Types & Variables

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Introduction to Programming Using Java (98-388)

( &% class MyClass { }

Zheng-Liang Lu Java Programming 45 / 79

An overview of Java, Data types and variables

Programming Language Concepts: Lecture 2

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Introduction to Programming (Java) 2/12

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Introduction to Java

Selected Questions from by Nageshwara Rao

PROGRAMMING FUNDAMENTALS

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS313D: ADVANCED PROGRAMMING LANGUAGE

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

INFO Object-Oriented Programming

Programming with Java

Decisions in Java IF Statements

CS 177 Spring 2010 Exam I

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Software and Programming 1

Chapter 3: Operators, Expressions and Type Conversion

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

1 Short Answer (10 Points Each)

Give one example where you might wish to use a three dimensional array

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Section 2.2 Your First Program in Java: Printing a Line of Text

Programming. Syntax and Semantics

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Section 2.2 Your First Program in Java: Printing a Line of Text

13 th Windsor Regional Secondary School Computer Programming Competition

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Oct Decision Structures cont d

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CSC 1214: Object-Oriented Programming

Fundamentals of Programming Data Types & Methods

CS251L REVIEW Derek Trumbo UNM

Character Stream : It provides a convenient means for handling input and output of characters.

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Object-Oriented Programming

CS 231 Data Structures and Algorithms, Fall 2016

Kickstart Intro to Java Part I

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CMPT 125: Lecture 3 Data and Expressions

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Review Ch 5 static Multiple Choice Test Creating Class Methods

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Crash Course Review Only. Please use online Jasmit Singh 2

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

Conditional Execution

Software and Programming 1

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

Java Programming. Atul Prakash

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Review for Test 1 (Chapter 1-5)

Introduction Basic elements of Java

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Expressions & Flow Control

CS111: PROGRAMMING LANGUAGE II

Lecture Set 4: More About Methods and More About Operators

Computer Components. Software{ User Programs. Operating System. Hardware

Loops. CSE 114, Computer Science 1 Stony Brook University

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Lab 9: Creating a Reusable Class

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Getting started with Java

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Introduction to Java

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

A Foundation for Programming

CS11 Java. Fall Lecture 1

Building Java Programs

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Fall 2017 CISC124 9/16/2017

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

1.1 Your First Program

AP Computer Science Unit 1. Programs

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

EECS1710. Checklist from last lecture (Sept 9, 2014) " get an EECS account (if you don t have it already) " read sections

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Values and Variables 1 / 30

Transcription:

Chapters 1-4 Summary These slides are brief summary of chapters 1-4 for students already familiar with programming in C or C++. Syntax - Java or C? int x[]={1,2,3,4,5,6,7,8,9,10; int i; int sum=0; float average; for(i=0; i<10; i++){ sum = sum+x[i]; average = sum/10; Syntax - Java or C? void main(void){ int x[]={1,2,3,4,5,6,7,8,9,10; int i; int sum=0; float average; for(i=0; i<10; i++){ sum = sum+x[i]; average = sum/10; printf("%f\n",average); 1

class Average{ Syntax - Java or C? public static void main(string[] args){ int x[]={1,2,3,4,5,6,7,8,9,10; int i; int sum=0; float average; for(i=0; i<10; i++){ sum = sum+x[i]; average = sum/10; System.out.println(average); Compiling a program Source code - HelloWorld.java viewed with an editor understandable by a human Object code - HelloWorld.class for Java, this is machine independent byte code compilers for other languages produce machine code this is also called the binary form or executable Compiling Create HelloWorld.java with an editor Execute the command: javac HelloWorld.java HelloWorld.java (source) Java Compiler HelloWorld.class (bytecode) 2

Running your Java program Once it compiles with no errors, type: java HelloWorld Notice it is not HelloWorld.class. The name here must be the name found after the keyword class in your programs source file. In general it should be the same as the name of the file, minus the extension. // SimpleInput.java-reading numbers from the keyboard import tio.*; // use the package tio class SimpleInput { public static void main (String[] args) { int width, height, area; System.out.println("type two integers for" + " the width and height of a box"); width = Console.in.readInt(); height = Console.in.readInt(); area = width * height; System.out.print("The area is "); System.out.println(area); Using tio Download tio.jar onto your computer (http://www.cse.ucsc.edu/~charlie/java/tio/) Depending upon your OS you can either put tio.jar in JAVA_HOME\jre\lib\ext or modify your CLASSPATH environment variable to include somepath/tio.jar. Detailed instructions are at the link above. 3

Numbers standard lengths for byte, short, char, int, long, float, and double char is 16 bit unsigned - Unicode not ASCII Naming Conventions SomeClass somemethod() somevariable some_variable preferred by some for private/local variables CONSTANT boolean standard type has values true and false if (x = y) syntax error no conversion between int and boolean if/while/for all same as C except that the test expression is type boolean 4

logical operators short circuit like C && - and - or ^ - XOR! - not & - non-short circut and - non-short circut or methods = functions mimic C functions by adding keyword static always part of a class no & or * for output parameters of primtive types - more about pointers soon // Min2.java: return expression in a method class Min2 { public static void main(string[] args) { int j = 78, k = 3 * 30, m; System.out.println( "Minimum of two integers Test:"); m = min(j, k); System.out.println("The minimum of : " + j + ", " + k + " is " + m); static int min(int a, int b) { if (a < b) return a; else return b; 5

package sample class First Java Program Structure method aaa instructions local variables class Second method aaa instructions local variables method zzz instructions local variables class/instance variables method zzz instructions local variables class/instance variables Method Overloading 1st new concept there can be two methods with the same name, distinguished only by their signatures recall that a method/functions signature is specified by the name plus the parameter types the signature of min() give earlier is int min(int, int) System.out.println() is overloaded to accept a String or any of the primitive types. // method overloading class Min2 { public static void main(string[] args) {... static int min(int a, int b) { if (a < b) return a; else return b; static double min(double a, double b) { if (a < b) return a; else return b; 6

//AmbiguousOverload.java: won t compile class AmbiguousOverload { public static void main(string[] args) { int i = 1, j = 2; System.out.println(ambig(i,j)); static boolean ambig(float x, int y){ return x < y; static boolean ambig(int x, float y){ return x < y; // The problem is the method call not the method // definitions. Scope Variable declarations can be introduced at anytime. for(int i = 0; i < x; i++) {... // i is undefined here You cannot have overlapping local definitions. public class SquareRoots2 { // contains scope errors public static void main(string[] args) { int i = 99; double squareroot = Math.sqrt(i); System.out.println("the square root of " + i + " is " + squareroot); for (int i = 1; i <= 10; i++) { double squareroot = Math.sqrt(i); double square = squareroot * squareroot; System.out.println("the square root of " + i + " is " + squareroot); System.out.println("squaring that yields " + square); System.out.println("The final value of square" + " is " + square); 7

Non-primitive Types Non-primitive types: classes (and arrays) Their values: objects Create objects with: new TypeName(...) Except String which has syntactic support with "some string". Declare variables just like primitive types. Operations on Objects Except for String, there are no built in operators for operating on objects (as there are for numeric types, such as + and * for int). Instead, operations are performed on objects using methods. Example operations on objects Point p = new Point(10, 20); p.translate(100, 200); System.out.println(p); Prints: java.awt.point[x=110,y=220] String s = "testing"; int x = s.length(); int y = "even this works".length(); int z = ("this " + "or that").length(); System.out.println(x + ", " + y + ", " + z); Prints: 6, 15, 12 8