CS177 Week2: Recitation Primitive data types and Strings with code examples.

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

Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Primitive Variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter One Lesson Three DATA TYPES ©
A: A: double “4” A: “34” 4.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
User Interaction and Variables
Building Java Programs
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
An Introduction to Java – Part I
Chapter 2.
Review Operation Bingo
Examples of Primitive Values
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Building Java Programs
Building Java Programs
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Building Java Programs
Building Java Programs Chapter 2
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Just Enough Java 17-May-19.
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

CS177 Week2: Recitation Primitive data types and Strings with code examples

Questions?

Data Types

int  int x; //declaration of variable x  x = 23; //assigns the number 23 to x  Stored as 1’s and 0’s. 23d = 10111b.  d stands for decimal, b for binary

Binary -> Decimal Conversion  What does b equal in decimal?

222

Decimal -> Binary Conversion  Do the following steps:  Start with a decimal number x  Set binary number b = 0  Until x = 0  Is x odd?  If yes, put a 1 at the end of b  If no, put a 0 at the end of b  Divide x by 2, rounding down (truncation)  Reverse the digits of b

Ex: x = 9

Ex: x = b = 222d, just like before

Positive and Negative Numbers  Positive numbers start with a 0 bit  Negative numbers start with a 1 bit  2’s complement – way that a negative number is represented

Doubles  Rational numbers (can still be an integer, but it is stored differently)  64 bits  double y = 1.25; //declares double y and assigns 1.25 to y  double z = 3;  double a = 2.0;  double b = -8.8;

Loosely typed or strongly typed?  Java is:  Strongly typed  double x;  x = 3;  What is the type of variable x?  A double!

Strings  Many letters in one type  String word1 = “purdue”;  String word2 = “boilermakers”;  String word3 = “p”;  String word4 = “”; //This is legal – called empty String

Chars  char – one character. Could be any ASCII character  String – many characters together  char w = ‘a’;  char x = w;  char y = ‘w’;  String z = “w”;  Are the variables w and x equal?  Are the variables x and y equal?  Are the variables y and z equal?

String - String concatenation /************************************************************************* * Compilation: javac Ruler.java * Execution: java Ruler * * Prints the relative lengths of the subdivisions on a ruler. * * % java Ruler * 1 * * * * * *************************************************************************/

public class Ruler { public static void main(String[] args) { String ruler1 = "1 "; String ruler2 = ruler1 + "2 " + ruler1; String ruler3 = ruler2 + "3 " + ruler2; String ruler4 = ruler3 + "4 " + ruler3; String ruler5 = ruler4 + "5 " + ruler4; System.out.println(ruler1); System.out.println(ruler2); System.out.println(ruler3); System.out.println(ruler4); System.out.println(ruler5); }

Double Operation - Quadratic formula /************************************************************************* * Compilation: javac Quadratic.java * Execution: java Quadatic b c * Given b and c, solves for the roots of x*x + b*x + c. * Assumes both roots are real valued. * % java Quadratic * 2.0 * 1.0 * % java Quadratic * * * * Remark: is the golden ratio. * * % java Quadratic * NaN *************************************************************************/

public class Quadratic { public static void main(String[] args) { double b = Double.parseDouble(args[0]); double c = Double.parseDouble(args[1]); double discriminant = b*b - 4.0*c; double sqroot = Math.sqrt(discriminant); double root1 = (-b + sqroot) / 2.0; double root2 = (-b - sqroot) / 2.0; System.out.println(root1); System.out.println(root2); }

Char Operation /************************************************************************* * Compilation: javac Charactertest.java * Execution: java Charactertest Is this letter lowercase? true Is this letter uppercase? false Is this a letter? true /************************************************************************* public class Charactertest { public static void main(String args[]) { char letter = 'd'; boolean lowerCase = Character.isLowerCase(letter); boolean upperCase = Character.isUpperCase(letter); boolean letterOrdigit = Character.isLetter(letter); System.out.println("Is this letter lowercase? " + lowerCase); System.out.println("Is this letter uppercase? " + upperCase); System.out.println("Is this a letter? " + letterOrdigit); }

Questions?