Week 15 - Monday.  What did we talk about last time?  File I/O.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Week 1 - Wednesday.  What did we talk about last time?  Syllabus  Computers.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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;
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Week 3 - Wednesday.  What did we talk about last time?  Math methods  Lab 2.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Introduction to Computer Systems and the Java Programming Language.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
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.
Week 2 - Wednesday.  What did we talk about last time?  C compilation model  Lab 1.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Java Java Translation Program Structure
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
Chapter 2 Variables.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Information and Computer Sciences University of Hawaii, Manoa
Week 2 - Wednesday CS 121.
Chapter 2 Variables.
Week 4 - Friday CS 121.
Week 3 - Wednesday CS 121.
Chapter 2 Basic Computation
Week 3: Basic Operations
Chapter 2 Basic Computation
Introduction to C++ Programming
Introduction to Java, and DrJava part 1
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Week 1 - Friday COMP 1600.
Presentation transcript:

Week 15 - Monday

 What did we talk about last time?  File I/O

 20% multiple choice questions  20% short answer questions  60% programming  Designed to take 2 hours (50% longer than the previous exams)  But, you will have the full 3 hour time period  More focus will be on the second half of the semester

 History of computers  Hardware  Software development  Basic Java syntax  Output with System.out.print()

Mechanical Calculation Devices (2400BC onward) Aid to human calculation No stored program Mechanical Computers (1725 onward) Punch card programming Serious limitations Early Electronic Computers (1941 onward) General purpose, stored program computers Electronic, using vacuum tubes Microprocessors (1970's onward) Succeeded transistors Now billions of computations per second at a nanometer scale

Cache Actually on the CPU Fast and expensive RAM Primary memory for a desktop computer Pretty fast and relatively expensive Flash Drive Faster than hard drives Most commonly on USB keychains Hard Drive Secondary memory for a desktop computer Slow and cheap Optical Drive Secondary memory that can usually only be written once Very slow and very cheap

 Storage for all the data and instructions on your computer  Modern computers store everything as binary digits (bits) which have a value of 0 or 1.

Monitor Common visual output device Speakers Common audio output device Mouse Common input device Keyboard Common input device

Computer! Solve a problem; Compile Execute Source Code Machine Code Hardware

class A { Problem p; p.solve(); } Compile JVM Execute Java Source Code Machine Code Hardware Java Bytecode

Often goes through phases similar to the following: 1. Understand the problem 2. Plan a solution to the problem 3. Implement the solution in a programming language 4. Test the solution 5. Maintain the solution and do bug fixes Factor of 10 rule!

 The absolute smallest program possible, with a print statement public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); }

 For example, instead of one print statement, we can have several:  Each statement is an instruction to the computer  They are printed in order, one by one System.out.println("Hello, world!"); System.out.println("Hello, galaxy!"); System.out.println("Goodbye, world!"); System.out.println("Hello, world!"); System.out.println("Hello, galaxy!"); System.out.println("Goodbye, world!");

 Java is a case sensitive language  Class is not the same as class  System.out.println("Word!"); prints correctly  system.Out.Println("Word!"); does not compile

 Java generally ignores whitespace (tabs, newlines, and spaces) is the same as:  You should use whitespace effectively to make your code readable System.out.println("Hello, world!"); System.out. println( "Hello, world!"); System.out. println( "Hello, world!");

 There are two kinds of comments (actually 3)  Single line comments use //  Multi-line comments start with a /* and end with a */ System.out.println("Hi!"); // this is a comment System.out.println("Hi!"); /* this is a multi-line comment */ System.out.println("Hi!"); /* this is a multi-line comment */

 Binary representation  Basic data types  Using Scanner for input

 The binary number system is base 2  This means that its digits are: 0 and 1  Base 2 means that you need 2 digits to represent two, namely 1 and 0  Each place in the number as you move left corresponds to an increase by a factor of 2 instead of 10

Ones1024’s Sixteens Thirty twos Eights Sixty fours Twos Fours 256’s 128’s 512’s

 We are going to focus on five basic types of data in Java  These are:  int For whole numbers  double For rational numbers  boolean For true or false values  char For single characters  String For words  As you now know, String is a little different from the rest

TypeKind of valuesSample Literals int Integers double Floating-point Numbers e23 boolean Boolean values true false char Single characters 'A' 'Z' '&' String Sequences of characters "If you dis Dr. Dre" "10 Sequipedalians"

 The int type is used to store integers (positive and negative whole numbers and zero)  Examples:  54  00  Inside the computer, an int takes up 4 bytes of space, which is 32 bits (1's and 0's)

 You will use the int type very often  Sometimes, however, you need to represent numbers with a fractional part  The double type is well suited to this purpose  Declaration of a double variable is just like an int variable: double x = 1.28;

 Numbers are great  But, sometimes you only need to keep track of whether or not something is true or false  This is what the boolean type is for  Hopefully you have more appreciation for boolean s now  Declaration of a boolean variable is like so: boolean value = false;

 Sometimes you need to deal with characters  This is what the char type is for  The char type only allows you to store a single character like '$' or 'q'  Declaration of a char variable is like so: char c = 'L';

 The String type is different from the other types in several ways  The important thing to focus on is that it can hold a large number of char values  A String literal is what we used in the Hello, World program  A String variable is declared much like any other type String word = "Terrifying!";

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  Hopefully this code makes more sense now 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();

 Numerical operations  Advanced math operations  boolean operations  char operations  String operations  Wrapper classes

 + adds  - subtracts  * multiplies  / divides (integer division for int type and fractional parts for double type)  % finds the remainder

 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 is 2)

 The sin() method allows you to find the sine of an angle (in radians)  This method is inside the Math class  The answer that it gives back is of type double  To use it, you might type the following: double value = Math.sin( 2.4 );

 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

Return typeNameJob doublesin(double theta) Find the sine of angle theta doublecos(double theta) Find the cosine of angle theta doubletan(double theta) Find the tangent of angle theta doubleexp(double a) Raise e to the power of a (e a ) doublelog(double a) Find the natural log of a doublepow(double a, double b) Raise a to the power of b (a b ) longround(double a) Round a to the nearest integer doublerandom() Create a random number in [0, 1) doublesqrt(double a) Find the square root of a

 ! NOT  Flips value of operand from true to false or vice versa  && AND  true if both operands are true  || OR  true if either operand is true  ^ XOR  true if operands are different

 char values can be treated like an int  It can be more useful to get the offset from a starting point int number; number = 'a';// number contains 97 int number; number = 'a';// number contains 97 char letter = 'r'; int number; number = letter – 'a' + 1; //number is 18 char letter = 'r'; int number; number = letter – 'a' + 1; //number is 18

 Remember that we use single quotes to designate a char literal: 'z'  What if you want to use the apostrophe character ( ' )?  apostrophe: '\''  What if you want to use characters that can't be printed, like tab or newline?  tab: '\t'  newline: '\n'  The backslash is a message that a special command called an escape sequence is coming

 The only operator that we will use directly with String s is the + (concatenation) operator  This operator creates a new String that is the concatenation of the two source String s  Concatenation can be used to insert the values of other types into String s as well String word; word = "tick" + "tock"; // word is "ticktock" String word; word = "tick" + "tock"; // word is "ticktock"

 equals()  Tests two String s to see if they are the same  compareTo()  Returns a negative number if the first String comes earlier in the alphabet, a positive number if the first String comes later in the alphabet, and 0 if they are the same  length()  Returns the length of the String  charAt()  Returns the character at a particular index inside the String  substring()  Returns a new String made up of the characters that start at the first index and go up to but do not include the second index

 Each primitive data type in Java has a wrapper class  Integer ▪ Allows String representations of integer values to be converted into int s  Double ▪ Allows String representations of floating point values to be converted into double s  Character ▪ Provides methods to test if a char value is a digit, is a letter, is lower case, is upper case ▪ Provides methods to change a char value to upper case or lower case

 Making choices with if statements  Basics  Having if bodies with more than one line  Using else blocks  Nesting if statements  Using switch statements

The if part Any boolean expression Any single executable statement if( condition ) statement;

Two different outcomes if( condition ) statement1; else statement2;

if( condition ) { statement1; statement2; … statementn; } A whole bunch of statements

if( condition1 ) { statement1; if( condition2 ) { if( condition3 ) statement2; … }

 The most common condition you will find in an if is a comparison between two things  In Java, that comparison can be:  == equals  != does not equal  < less than  <= less than or equal to  > greater than  >= greater than or equal to

switch( data ) { case value1: statements 1; case value2: statements 2; … case valuen: statements n; default: default statements; }

int data = 3; switch( data ) { case 3: System.out.println("Three"); case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); } Both "Three" and "Four" are printed The break is optional The default is optional too

1. The data that you are performing your switch on must be either an int, a char, or a String 2. The value for each case must be a literal 3. Execution will jump to the case that matches 4. If no case matches, it will go to default 5. If there is no default, it will skip the whole switch block 6. Execution will continue until it hits a break

 Review up to Exam 2

 Bring a question to class Wednesday!  Any question about any material in the course  Keep working on Project 5  Due on Friday before midnight  Study for Final Exam  2:30 - 5:30pm, Thursday, 12/10/2015 (CS121B)  11:00am - 2:00pm, Monday, 12/07/2015 (CS121C)