Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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 4: Mathematical Operators New York University.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Computer Programming Lab(4).
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CSC Programming I Lecture 5 August 30, 2002.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Chapter 2 Elementary Programming
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Programming Principles Operators and Expressions.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CS 201 Lecture 2: Elementary Programming Tarik Booker CS 201 California State University, Los Angeles.
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.
Week 4 - Friday.  What did we talk about last time?  Examples  switch statements.
Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.
CompSci 230 S Programming Techniques
Chapter 2 More on Math More on Input
Lecture 3 Java Operators.
Building Java Programs
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Week 3: Basic Operations
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
IFS410 Advanced Analysis and Design
Building Java Programs Chapter 2
Building Java Programs
CS2011 Introduction to Programming I Elementary Programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Week 2 - Friday

 What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String

 To output stuff, we just use System.out.println()  What about input?  Input is a little trickier  We need to create a new object of type Scanner System.out.println("Flip mode is the squad!"); System.out.println(35); System.out.println("Flip mode is the squad!"); System.out.println(35);

There are three parts to using Scanner for input 1. Include the appropriate import statement so that your program knows what a Scanner object is 2. Create a specific Scanner object with a name you choose 3. Use the object you create to read in data

 Lots of people have written all kinds of useful Java code  By importing that code, we can use it to help solve our problems  To import code, you type import and then the name of the package or class  To import Scanner, type the following at the top of your program (before the class !) import java.util.Scanner;

 Once you have imported the Scanner class, you have to create a Scanner object  To do so, declare a reference of type Scanner, and use the new keyword to create a new Scanner with System.in as a parameter like so:  You can call it whatever you want, I chose to call it in  Doesn't make any sense? For now, that's okay. Scanner in = new Scanner(System.in);

 Now that you've got a Scanner object, you can use it to read some data  It has a method that will read in the next piece of data that user types in, but you have to know if that data is going to be an int, a double, or a String  Let's say the user is going to input her age (an int ) and you want to store it in an int variable called years  We'll use the nextInt() method to do so: int years; years = in.nextInt(); int years; years = in.nextInt();

 Scanner has a lot of methods (ways to accomplish some tasks)  For now, we're only interested in three  These allow us to read the next int, the next double, and the next String, respectively: Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next(); Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next();

import java.util.Scanner; public class Age { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("What is your age?"); int years; years = in.nextInt(); years = years * 2; System.out.print("Your age doubled is "); System.out.println(years); } import java.util.Scanner; public class Age { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("What is your age?"); int years; years = in.nextInt(); years = years * 2; System.out.print("Your age doubled is "); System.out.println(years); } }

 In Java, each data type has a set of basic operations you are allowed to perform  It’s not possible to define new operations or change how the operations behave  Some programming languages allow this, but not Java

 Today we are going to consider the basic operations for numerical types:  int  double

 Use the + operator to add two int s together int a; int b; a = 5 + 6;// a contains 11 b = a + 3;// b contains 14 a + b; // not allowed, does nothing a = a + 1;// a contains 12, and b? int a; int b; a = 5 + 6;// a contains 11 b = a + 3;// b contains 14 a + b; // not allowed, does nothing a = a + 1;// a contains 12, and b?

 Some expressions are used so often, Java gives us a short cut  x = x + y; can be written x += y;  x = x + 1; can be written x++; int x; x = 6;// x contains 6 x += 4;// x contains 10 x++; // x contains 11 int x; x = 6;// x contains 6 x += 4;// x contains 10 x++; // x contains 11

 Exactly like + except performs subtraction int a; int b; a = 5 - 6;// a contains -1 b = 3 - a;// b contains 4 a -= 10; // shortcut for a = a – 10; a--; // shortcut for a = a – 1; int a; int b; a = 5 - 6;// a contains -1 b = 3 - a;// b contains 4 a -= 10; // shortcut for a = a – 10; a--; // shortcut for a = a – 1;

 The * operator performs multiplication int a; int b; a = 5 * 6;// a contains 30 b = a * 3;// b contains 90 a *= 2; // shortcut for a = a * 2; int a; int b; a = 5 * 6;// a contains 30 b = a * 3;// b contains 90 a *= 2; // shortcut for a = a * 2;

 The / operator performs integer division  Not the same as regular division  The factional part is dropped, not rounded int a; int b; a = 3;// a contains 3 b = a / 2;// b contains 1 a /= 2; // shortcut for a = a / 2; int a; int b; a = 3;// a contains 3 b = a / 2;// b contains 1 a /= 2; // shortcut for a = a / 2;

 The % operator is the mod operator  It finds the remainder after division  This operator is a good way to find out if a number is even or odd int a; int b; a = 8;// a contains 8 b = a % 5;// b contains 3 a %= 2; // shortcut for a = a % 2; int a; int b; a = 8;// a contains 8 b = a % 5;// b contains 3 a %= 2; // shortcut for a = a % 2;

 Compute the area of a rectangle  Area = length ∙ width length widthArea

 Exactly the same as + for int, except now you can have fractional parts double a; double b; a = ;// a contains b = a + 2.1;// b contains a += 1.6; // shortcut for a = a + 1.6; a++; // shortcut for a = a + 1.0; double a; double b; a = ;// a contains b = a + 2.1;// b contains a += 1.6; // shortcut for a = a + 1.6; a++; // shortcut for a = a + 1.0;

 No surprises here  They do subtraction and multiplication double a; double b; a = ;// a contains b = a - 2.1;// b contains a = b * 0.5;// a contains double a; double b; a = ;// a contains b = a - 2.1;// b contains a = b * 0.5;// a contains

 Unlike int, this division does have fractional parts  Can you explain this mystery? double a; double b; a = 3;// a contains 3.0 b = a / 2;// b contains 1.5 b = 3 / 2;// b contains 1.0 double a; double b; a = 3;// a contains 3.0 b = a / 2;// b contains 1.5 b = 3 / 2;// b contains 1.0

 Yes, there is a % operator for double s, but no one uses it  So, don’t worry about it

 Given a temperature in Celsius, what is the equivalent in Fahrenheit?  T F = (9/5)T C + 32

 How complex can expressions get?  What’s the value of a ?  18! int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d; int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d;

 Order of operations holds like in math  You can use parentheses to clarify or change the precedence  Now a is 16 int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d; int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d;

 You cannot directly store a double value into an int variable  However, you can cast the double value to convert it into an int  Casting tells the compiler that you want the loss of precision to happen  You can always store an int into a double int a = 2.6;// fails! int a = (int)2.6;// succeeds! (a = 2)

 In Java, the conversion of a double into an int does not use rounding  As in the case of integer division, the value is always rounded down  You can think of this as using the floor function from math  If you want to round normally, you can simply add 0.5 before the cast double x = 2.6; int a = (int)(x + 0.5);// rounds double x = 2.6; int a = (int)(x + 0.5);// rounds

 Examples  Advanced math operations  Operations on boolean and char types

 Keep reading Chapter 3 of the textbook  Get an early start on Project 1 (as soon as it is assigned)