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

Slides:



Advertisements
Similar presentations
Java Planning our Programs Flowcharts Arithmetic Operators.
Advertisements

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.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
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;
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;
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.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
CSC Programming I Lecture 5 August 30, 2002.
Chapter 2 Elementary Programming
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
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.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
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:
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
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.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
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:
Week 6 - Friday.  What did we talk about last time?  Loop examples.
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.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Introduction to programming in java
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.
CompSci 230 S Programming Techniques
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
TemperatureConversion
Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e
TK1114 Computer Programming
Week 3: Basic Operations
Something about Java Introduction to Problem Solving and Programming 1.
An Introduction to Java – Part I, language basics
Chapter 2: Basic Elements of Java
IFS410 Advanced Analysis and Design
Fundamentals 2.
Building Java Programs
Input, Variables, and Mathematical Expressions
Presentation transcript:

Week 2 - Wednesday

 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

 Advanced mathematical operations  Operations on boolean values  Lab 2

 Keep reading Chapter 3 of the textbook  Get an early start on Project 1