Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),

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

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Introduction to Computers and Programming Lecture 7:
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 7:
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Java Syntax Primitive data types Operators Control statements.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Review Java.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
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.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
Object Oriented Programming Lecture 2: BallWorld.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 7 User-Defined Methods.
Strings, conditional statements, Math
Multiple variables can be created in one declaration
Chapter 3 Java Input/Output.
Message, Input, Confirm, and Specialized Dialogs
Chapter 2.
IDENTIFIERS CSC 111.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Unit-2 Objects and Classes
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 2: Java Fundamentals
Chapter 3 Input/Output.
Message, Input, and Confirm Dialogs
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Fundamental Programming
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32), long(64), float(32), double(64), boolean Numeric type conversion  float f = (float)10;  int I = (int)f;

Control structure Selection if( condition ) { statements; } else { statements; } Switch( integer) { Case 1: statements; Case 2: statements; … Default:.. } Repetition while( condition) { … } do { …. }while(condition); for ( x = 0; x < 10; x++) { statements; }

String type: String String is a predefined class in the Java library It is not primitive data type It is known as a reference type Declare a string  String message = “welcome!”; Concatenate strings  String s = “Chapter”+2;  message +=“ and Java is fun”; System.out.println(“i+j is” +(i+j)); // i=1, j = 2

Getting input from input Dialogs String input = JOptionPane.showInputDialog(null, “Enter an input”, “Input Dialog memo”, JOptionPane.QUESTION_MESSAGE); JOptionPane.showMessageDialog(null, input, “Example”, JOptionPane.INFORMATION_MESSAGE); Converting string to numbers  int val = Integer.parseInt(input);  double d = Double.parseDouble(input);

Methods Creating a method: modifier returntype methodname(list of parameters) { } public static int max( int int1, int int2) { if( int1 > int2) return int1; else retun int2; } primitive data are passed by value ( No Pointer!)

Calling a method int larger = max(3,4); System.out.println(max(3,4)); Static method can be called directly, it doesn’t need a class object to call it In the same class, it can be called by the method name In other class definition, it can be called by ClassName.max(3,4);

The Math class public static double sin(double x) public static double cos( double x) public static double exp( double a) public static double log( double a) public static double pow( double a, double b) public static double sqrt( double a) Min, max, abs and random methods

The random methed Math.random() generate a random double number 0<= Math.random <1.0 Generate random number between 0 and 9 int I = (int)(Math.random()*10); Generate number between 50 and 99 Int I = 50 + (int)(Math.random()*50); In genral, generate any random number between a and a+b, excluding a+b a + Math.random()*b

Generate random characters Every character has a unique Unicode between 0 and FFFF in hexadecimal (65535) in decimal Generate random integer between 0 – (int) (Math.random()*( ) Generate random lower case letter Unicode for a (int) ‘a’ Unicode for z is (int) ‘z’ Random letter between a-z (char) (‘a’ + Math.random()*(‘z’ =‘a’ +1))

Guess a number Write a program that generate a random number between Ask user to guess it, Print message that state either the number user guess is higher, lower Repeat until user enter the correct nmber.