CSE 131 Computer Science 1 Module 1: (basics of Java)

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
This is Java Jeopardy.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Some basic I/O.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
A Short Introduction to JAVA
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
CSE 131 Computer Science 1 Module 1: (basics of Java)
INTERNET APPLICATION DEVELOPMENT For More visit:
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Programming Fundamentals Enumerations and Functions.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Information and Computer Sciences University of Hawaii, Manoa
The eclipse IDE IDE = “Integrated Development Environment”
Java Primer 1: Types, Classes and Operators
Primitive Data, Variables, Loops (Maybe)
Lecture Note Set 1 Thursday 12-May-05
CompSci 230 Software Construction
Type Conversion, Constants, and the String Object
Chapter 2.
Type Conversion, Constants, and the String Object
User Defined Functions
Group Status Project Status.
MSIS 655 Advanced Business Applications Programming
Chapter 6 Methods.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Java’s Central Casting
Classes, Objects and Methods
IAT 800 Foundations of Computational Art and Design
Functions, Procedures, and Abstraction
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

CSE 131 Computer Science 1 Module 1: (basics of Java)

Welcome to the World of Java

Eclipse Integrated Development Environment (IDE) »“smart” editing of Java programs – continuous syntax checks »compiles program files automatically as necessary »integrated program testing and debugging tools »has plug-ins for version control system (Subversion) Can be a powerful tool for managing large projects »but, can also be a bit overwhelming and mysterious at first »watch the first few Eclipse videos on the web site before lab works the same way in Windows, Mac and Linux

Your First Program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } } »defines a class called HelloWorld (a class defines an object) »class contains a single method called main in Java, the main method is the starting point of a program the main method has an array of arguments (not used here) Any program has one and only one main() method »program “prints” a brief message and halts

Today: understand basic building blocks of JAVA You will be able to »Output a slogan on the screen »Tell if a year is a Leap Year »Calculate the area of a circle »Output anything you like »Do all kinds of calculation

Basic unit: variable A variable in Java is a piece of information stored in a computer’s memory: like a jar holding food Each variable has a type »Like different kinds of jars (different food, size, usage, etc.) Main types: int a; double b; String s; boolean x;

Basic Data Types in Java Type (keyword)MeaningExamplesPrimitive Operators intInteger3, 0, -5, 256+, -, *, / doubleDecimal number1.5, , 0.0+, -, *, / booleantrue or falsetrue, false&&, ||, ! (return boolean:, =,!=, ==) Stringa sequence of characters “hello world”+ (concatenation)

8 Basic Definitions Type. A category of values and a set of operations on such values. Variable. A name that refers to a value of a declared type. Assignment statement. Associates a value with a variable. type

The int type

Integers This surprises most students. Don’t feel bad: experts forget this too sometimes.

Key points Assignment statement Variable = Expression ; (For example: a = b+ 5/3; ) Expression’s type must match the variable’s type (will come back to this point) Sequential execution »The order of statements matters

The double type

Example: compute the area of a circle

String: a sequence of characters

String operation Meaning of characters depends on context. Example: report +, -, *, / of two integers

Boolean

17 Booleans boolean data type. Useful to control the logic of a program.

18 Comparisons Comparisons. Take two operands of one type ( int or double ) and produce a result of type boolean.

19 Leap Year Q. Is a given year a leap year? A. Yes if either (i) divisible by 400 or (ii) divisible by 4 but not 100. public class LeapYear { public static void main(String[] args) { int year = 2012; boolean isLeapYear; // divisible by 4 but not 100 isLeapYear = (year % 4 == 0) && (year % 100 != 0); // or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0); System.out.println(isLeapYear); }

Data Type Conversion Operations must be applied to same types, with the following exceptions Java automatically converts some types of values to make them compatible int i, j; double x, y; i = 5; j = i/2; y = 2; x = i/y; - As long as there is a double in an expression, the type of the expression is promoted to double Combining strings and numeric values int i = 5; String S = “two plus two =” + i;

Basic Expressions in Java ExpressionValueType 3+58int 7/2.03.5double 7/23int 3+5-2/4+210int /4+29.5double (3+5-2)/(4+2)1int “Hello”+”there”“Hellothere”String “I have “+5+5+” toes”“I have 55 toes”String true && falsefalseboolean false || truetrueboolean !truefalseboolean (true&&false)||(!false)trueboolean

Variable types A variable can only hold values of the proper type int x; (default 0) int x = 1; … x = false; (error! Type-checking problem) Any expression of type T can appear in an expression where a value of type T is required »E.g. int x = 3+5; int x = 7/2.0; (type error) int x= (int) (7/2.0) (cast, x has value 3)

Method What if we need to find areas of different circles? »Do we have write the code over and over again?

Procedural abstraction (method) Find areas of circles A method is like a blackbox (input  output) »Hide details of computation from outside A java method Use it: double area = circleArea(10.0); double circleArea(double radius) { return Math.PI * radius * radius; }

Method Call and Return Java methods used to decompose program into parts »allows us to solve a problem once and conveniently re-use the solution many times You can invoke the method by using it in an expression »Example: area = circleArea(2.0); »The number of input parameters should match the method definition »The types of input parameters should match the method definition

Method for Length of Hypotenuse We know C=√A 2 +B 2 Forms the basis of the Java method double hypotenuse(double A, double B) { return Math.sqrt(A * A + B * B); } » Math.sqrt(x) returns the square root of x part of a library of mathematical functions, including sin(x), cos(x), tan(x), log(x), exp(x) »Signature of a method: double hypotenuse(double, double) defines method name, return type, types of arguments »Invocation of a method must match its signature Example: double h = hypotenuse(3, 4); C B A