Operators Laboratory 6 2018/11/16.

Slides:



Advertisements
Similar presentations
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
1 Repetition structures Overview while statement for statement do while statement.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Lecture 3 Java Basics Lecture3.ppt.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Reading Information From the User Making your Programs Interactive.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Variable Scope Brackets, brackets…. brackets. Variable Scope /** * HelloWorld --- program that prints something to the screen. Blanca Polo */
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
CS/IS 112 – Week 2 Logic Problem More Java background and basics Values Variables, and operations.
1 Course Lectures Available on line:
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CSC 204 Programming I Loop I The while statement.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
©2004 Brooks/Cole Assignment and Interactive Input Java Shorthand Statements Mathematical Methods Conversion Methods Interactive Keyboard Input Interactive.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPSC 233 Tutorial Xin Jan 24, Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on.
Java operatoriai sandbolts/operators.html
Java 1.5 The New Java Mike Orsega Central Carolina CC.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Computers and Programming Lecture 7:
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
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.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
CompSci 230 S Programming Techniques
Lecture 3 Java Operators.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Interactive Standard Input/output
Lecture Note Set 1 Thursday 12-May-05
Java operatoriai
Java Programming: From Problem Analysis to Program Design, 4e
TK1114 Computer Programming
Operators and Expressions
Lecture 5 from (Chapter 4, pages 73 to 96)
More about Numerical Computation
Operators August 6, 2009.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
Presentation transcript:

Operators Laboratory 6 2018/11/16

Arithmetic Operator Operator Description + addition - Subtraction * Multiplication / Division % Modules ++ Increment -- Decrement 2018/11/16

Special operator Expression Equivalent expression a = a + 2; a +=2; 2018/11/16

Increment and Decrement Expression Equivalent expression a = a + 1; ++a; a = a – 1; --a; result 2018/11/16

Bitwise Operators Operator Description ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift Right << Shift LEFT >>> Shift Right with zero fill 2018/11/16

Operation - examples Operator Expression AND 1 & 1 = 1; 1& 0 = 0 ~ 0 =~1; 1 =~0; ^ 0^ 0 = 0; 1^1 = 0; 1^0 =1; 0^1 = 1 >> 0x0010 = 0x0001 >>1 << 0x0001 = 0x0010 <<1 >>> 0x1001 = 0x0100 >>>1 2018/11/16

SHIFT >> (right) by one bit 1111 0010 (0xf2) >> 1 (shift right by one bit) --------------------- 0111 10001 (0x79) Example byte c = 0xf2; byte e = c >>1; //e is 0x79 2018/11/16

Program for Question 1 2018/11/16

sample 2018/11/16 /* demonstarting arithmetic operator */ public class lecture61 { public static void main(String[] args) { //a few numbers int x, y; x = 1 + 2 - 8/2*2; short j = 34; System.out.println("The value of x is ..."); System.out.println(" x = " + x); y = 5; x = y << 5; //using << System.out.println("demonstrating << "); x = 2; y = x++ + 2; //using ++ System.out.println("demonstrating ++ "); } 2018/11/16

Prime number 2018/11/16

Sample 2018/11/16 import java.io.*; public class lab62 { public static void main(String[] args) throws java.io.IOException InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s1; int num, loop; double sqr, num2; boolean prime = true; // read a number System.out.println("Please enter a positive integer:"); s1 = br.readLine(); num = Integer.parseInt(s1); if (num == 2) prime = true; else ........................ //please fill in if(prime) System.out.println(num + " is a prime number."); } 2018/11/16

String to Double/Integer/Float 2018/11/16

Quadratic Equation 2018/11/16

Sample 2018/11/16 import java.io.*; public class lab63 { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s1; double a, b, c; double x1, x2; // read a System.out.print("Please enter the value of a: "); s1 = br.readLine(); a = Double.valueOf(s1).doubleValue(); // convert string to double ..................... // please fill in System.out.println("When a = " + a + ", b = " + b + " and c = " + c + " X = " + x1 + " or " + x2); } 2018/11/16

Returning a value – from myarea.area() 2018/11/16

Sample 2018/11/16 import java.io.*; class Square { double width; double area() { return width*width; } class lab64 { public static void main(String[] argv) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr);double width, b; String s1; // read a number Square myarea = new Square(); //create an object System.out.print("Please enter the width: "); s1 = br.readLine(); b = Double.valueOf(s1).doubleValue(); // convert string to double myarea.width = b; //assign a value //compute the area double a = myarea.area(); //return the value System.out.print("area is " + a + "\n"); 2018/11/16

High-low 2018/11/16

Sample 2018/11/16 import java.io.*; public class lab65 { public static void main(String[] argv) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); int g, i; //guess and input int tries = 0; String s1; i = (int) (Math.random() * 100 + 1); // choose new random number between 1 and 100 // read a number System.out.print("Please enter the guess: "); s1 = br.readLine(); g = Integer.parseInt(s1); // convert string to integer while(g != i) { ......................... // please fill in g = Integer.parseInt(s1); } // Correct System.out.print("Correct!, You have tried " + tries +" times"); 2018/11/16