BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Similar documents
MODULE 02: BASIC COMPUTATION IN JAVA

DATA TYPES AND EXPRESSIONS

Basic Computation. Chapter 2

Getting started with Java

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CIS133J. Working with Numbers in Java

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

Basic Computation. Chapter 2

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

Variables and data types

CS 106 Introduction to Computer Science I

Declaration and Memory

Basics of Java Programming

Datatypes, Variables, and Operations

Algorithms and Programming I. Lecture#12 Spring 2015

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Chapter 2: Data and Expressions

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Full file at

PROGRAMMING STYLE. Fundamentals of Computer Science I

CSE 142 Su 04 Computer Programming 1 - Java. Objects

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Object-Oriented Programming

VARIABLES AND TYPES CITS1001

Program Fundamentals

CEN 414 Java Programming

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

4 Programming Fundamentals. Introduction to Programming 1 1

Midterms Save the Dates!

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 2: Data and Expressions

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

3. Java - Language Constructs I

COMP 110 Introduction to Programming. What did we discuss?

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

Chapter 2 Elementary Programming

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CLASSES AND OBJECTS. Fundamentals of Computer Science I

What did we talk about last time? Examples switch statements

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Values and Variables 1 / 30

Information Science 1

Programming with Java

Course Outline. Introduction to java

AP Computer Science A

13 th Windsor Regional Secondary School Computer Programming Competition

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Chapter 2: Data and Expressions

CS313D: ADVANCED PROGRAMMING LANGUAGE

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CS112 Lecture: Primitive Types, Operators, Strings

Programming Lecture 3

Elementary Programming

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Topic 4 Expressions and variables

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

3. Java - Language Constructs I

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Full file at

Enumerations. Fundamentals of Computer Science

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Information Science 1

Expressions and Variables

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Zheng-Liang Lu Java Programming 45 / 79

Advanced Computer Programming

COMP Primitive and Class Types. Yi Hong May 14, 2015

Introduction Basic elements of Java

Lecture Set 4: More About Methods and More About Operators

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Introduction to Java Unit 1. Using BlueJ to Write Programs

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Building Java Programs

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

COMP-202: Foundations of Programming

Basics of Java Programming variables, assignment, and input

Chapter 2 ELEMENTARY PROGRAMMING

Values, Variables, Types & Arithmetic Expressions. Agenda

Lecture 3. Input, Output and Data Types

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Fundamental of Programming (C)

Programming Basics. Part 1, episode 1, chapter 1, passage 1

Transcription:

BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I

Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

Eclipse Eclipse IDE (Integrated Development Environment)

Comments A comment can begin with //. Everything after these symbols and to the end of the line is treated as a comment and is ignored by the compiler. double radius; //in centimeters A comment can begin with /* and end with */ Everything between these symbols is treated as a comment and is ignored by the compiler. A javadoc comment, begins with /** and ends with /** This program should only be used on alternate Thursdays, except during leap years, when it should only be used on alternate Tuesdays. */

Try It! Open Eclipse Browse to the directory you want to save your work in Flash drive if you have one, D:\YourName\Workspace if you don t Select File New Java Project, name it Activity01 Under Project Layout, select Use project folder as root then click Finish Right click on Activity01, select New, then Class, name it HelloWorld If you click the box public static void main Eclipse will create the main method for you Copy the HelloWorld program into the editor Type it in OR Copy and paste it from the slides (you ll need to edit the quote marks) OR Copy and paste it from the website NOTE: If you selected the public static void main in the previous step, you only need to add one line of code Save and Run your program, see if it prints out Hello world! Make sure your name is in the comment section at the top of the program (not my name) Open Moodle, go to CSCI 135, Section 01 Open the Activity01 dropbox for today Drag and drop your program file to the Moodle dropbox You can drag from the Eclipse file menu right into Moodle Wait until the blue line goes away and you see an icon for your file You get: 1 point if you turn in something, 2 points if you turn in something that is correct.

Variables Variables store data such as numbers and letters. Think of them as places to store data. They are implemented as memory locations. The data stored in a variable is called its value. The value is stored in the memory location. Its value can be changed.

Some Definitions int a; a = 10; int b; b = 7; int c = a + b; Declaration statement I'm going to need an integer and let's call it a REMEMBER: in Java you are required to declare a variable before using it! Variable name Whenever I say a, I mean the value stored in a Literal I want the value 10 Assignment statement Variable b gets the literal value 7 = in CS is not the same as = in math! Combined declaration and assignment Make me an integer variable called c and assign it the value obtained by adding together a and b 7

Assignment Evaluation The expression on the right-hand side of the assignment operator (=) is evaluated first. The result is used to set the value of the variable on the left-hand side of the assignment operator. score = numberofcards + handicap; eggsperbasket = eggsperbasket - 2;

Data Types A primitive type is used for simple, non-decomposable values such as an individual number or individual character. int, double, and char are primitive types. A class type is used for a class of objects and has both data and methods. "Java is fun" is a value of class type String DEFINITION A data type is a set of values and the legal operations on those values.

Variables and Data Types Variables Stores information your program needs Each has a unique name Each has a specific type Java built-in type what it stores example values operations int integer values 42 1234 double floating-point values 9.95 3.0e8 char characters 'a', 'b', '!' compare add, subtract, multiply, divide, remainder add, subtract, multiply, divide String sequence of characters boolean truth values true false "Hello world!" "I love this!" concatenate and, or, not 10

Primitive Type Sizes Primitive types Just a block of memory in your computer Size of block measured in bits (number of 0s or 1s) Integers: type bits example byte 8 0110 1110 short 16 0110 1110 1101 1101 int 32 0101 1001 0000 0001 0111 1101 0110 0010 long 64 1101 0011 1001 0001 1101 0101 1010 0101 0111 1010 0011 1010 1011 1100 1111 1111 11

Creating a Primitive Variable byte x; x x 011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000000000101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101010101010101010101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101 12

Initializing Variables A variable that has been declared, but not yet given a value is said to be uninitialized. Uninitialized class variables have the value null. Uninitialized primitive variables may have a default value. It's good practice not to rely on a default value. To protect against an uninitialized variable (and to keep the compiler happy), assign a value at the time the variable is declared. Examples: int count = 0; char grade = 'A';

Creating and Initializing a Primitive Variable byte x = 7; x primitive value x 011010101010100101010101110101010101010101010111010101 000101010101010111101010101010101010101001001101010101 010010101010110000001110101010101011101010100010101010 101011110101010101010101010100100110101010101001010101 011101010101010101010101110101010001010101010101111010 101010101010101010010011010101010100101010101110101010 101010101010111010101000101010101010111101010101010101 010101001001101010101010010101010111010101010101010101 14

Integers int data type An integer value between -2 31 and +2 31-1 Between -2,147,483,648 and 2,147,483,647 Operations: example result comment 10 + 7 17 10-7 3 10 * 7 70 add subtract multiply divide remainder + - * / % 10 / 7 1 integer division, no fractional part 10 % 7 3 remainder after dividing by 7 Watch out for this! / is integer division if both sides are integers! 10 / 0 runtime error, you can't divide an integer by 0! 15

Increment and Decrement Operators Used to increase (or decrease) the value of a variable by 1 Easy to use, important to recognize The increment operator count++ or ++count The decrement operator count-- or --count Equivalent operations: count++; ++count; count = count + 1; count--; --count; count = count - 1;

Floating-Point Numbers double data type Floating-point number (as specified by IEEE 754) Operations: add subtract multiply divide + - * / example result 9.95 + 2.99 12.94 1.0-2.0-1.0 1.0 / 2.0 0.5 1.0 / 3.0 0.3333333333333333 1.0 / 0.0 Infinity 0.0 / 123.45 0.0 0.0 / 0.0 NaN 17

e Notation e notation is also called scientific notation or floating-point notation. Examples 865000000.0 can be written as 8.65e8 0.000483 can be written as 4.83e-4 The number in front of the e does not need to contain a decimal point.

Imprecision in Floating-Point Numbers Floating-point numbers often are only approximations since they are stored with a finite number of bits. Hence 1.0/3.0 is slightly less than 1/3. 1.0/3.0 + 1.0/3.0 + 1.0/3.0 is less than 1.

Constants Literal expressions such as 2, 3.7, or 'y' are called constants. Integer constants can be preceded by a + or - sign, but cannot contain commas. Floating-point constants can be written With digits after a decimal point or Using e notation. Java provides mechanism to Define a variable Initialize it Fix the value so it cannot be changed public static final double PI = 3.14159;

Command line args in Eclipse

Summary Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment