Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get.

Slides:



Advertisements
Similar presentations
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Advertisements

Computer Programming Mr. José A. Ortiz Morris. Computer Language  Languages that the computer understands.  They are low level languages. (BINARY 1.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Programming TBE 540 Farah Fisher. Objectives After viewing this presentation, the learner will be able to… Given a task, create pseudocode Given pseudocode,
Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program.
How to Debug VB .NET Code.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
General Programming Introduction to Computing Science and Programming I.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Computer Science 101 Introduction to Programming.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Java Data Types Data types, variable declaration, and initialization.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
The Java Programming Language
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
COMP Exception Handling Yi Hong June 10, 2015.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
C Hints and Tips The preprocessor and other fun toys.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
Mathematical Calculations in Java Mrs. G. Chapman.
Java Data Types Assignment and Simple Arithmetic.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Research Topics in Computational Science. Agenda Commenting Our Code Variable Review Button Program – Add Textbox Getting text from textbox Converting.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
Programming Errors. Errors of different types Syntax errors – easiest to fix, found by compiler or interpreter Semantic errors – logic errors, found by.
Error Handling Tonga Institute of Higher Education.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
Programming. Exam Validation 1-10 Technical words such as Method of validation Do not use “Something”.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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.
COMPUTER PROGRAMMING I SUMMER Understand Different Types of Programming Errors.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
Introduction to Exceptions in Java CS201, SW Development Methods.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Few More Math Operators
A variable is a name for a value stored in memory.
5.01 Understand Different Types of Programming Errors
Chapter 4 Assignment Statement
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
5.01 Understand Different Types of Programming Errors
Java Programming Arrays
Software Development Process
Presentation transcript:

Variables in Java Part 2

ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get an integer as an answer The answer always rounds DOWN (it ignores the remainder) int x=27; int y=7; int answer=x/y; The answer is 3 and 6/7 - but it is calculated as 3

ICS-3M1 - Mr. Martens - Variables Part 2 Sometimes You Want the Remainder On the last slide, you saw that 27/7 = 3-R6 There are 2 parts to the answer the “3” and the remainder of “6” Most languages have the ability to extract only the remainder part – this is called the “modulus” – and in Java the “%” is used. So... 27/7=3... and 27%7=6

ICS-3M1 - Mr. Martens - Variables Part 2 More Modulus Examples xyx / yx % yx * (x % y)

ICS-3M1 - Mr. Martens - Variables Part 2 Using Constants in Java A constant is a value that never changes during the life of a program (opposite of variable) In Java, we use the keyword “final” in front of a declaration to change it from a variable into a constant: final double PI = 3.14; final double PST = 0.08;

ICS-3M1 - Mr. Martens - Variables Part 2 A Constant Example It is also a customary practice to CAPITALIZE constants so that they stand out from the variables

ICS-3M1 - Mr. Martens - Variables Part 2 Formatting Numbers To format numbers you have to “import” a part of Java that deals with formatting To get this one going, we need some numbers to format

ICS-3M1 - Mr. Martens - Variables Part 2 Number Format, Continued Next, we will declare 3 NumberFormat “objects”, each with a slightly different job The 3 objects have been named according to the kind of format they will perform This isn’t mandatory, but it is just good programming practice.

ICS-3M1 - Mr. Martens - Variables Part 2 The Rest of the Program Each NumberFormat object has a “format” method that actually does the formatting: Here is the output of the program

ICS-3M1 - Mr. Martens - Variables Part 2 Here is the Entire Program Import the NumberFormat class Declare some variables with numbers Use the format method to format Create 3 NumberFormat objects

ICS-3M1 - Mr. Martens - Variables Part 2 The 3 Types of Programming Errors Syntax error. This type of error is detected when you try to compile your program. It is a typing error made by the programmer:

ICS-3M1 - Mr. Martens - Variables Part 2 The 3 Types of Programming Errors Logic Error. These are harder to catch. There is nothing wrong with the way you TYPED your code so it is not detected by the compiler. But, when you run the program, you seem to get unexpected results

ICS-3M1 - Mr. Martens - Variables Part 2 The 3 Types of Programming Errors Runtime Error. The code is typed correctly, but then something happens during the running of the program that causes it to crash Nothing seems wrong with this program when you run it..

ICS-3M1 - Mr. Martens - Variables Part 2 The 3 Types of Programming Errors But, we could cause the program to crash while its running if we do some ‘funny stuff’: Java doesn’t like it when we try to divide by 0

ICS-3M1 - Mr. Martens - Variables Part 2 Runtime Errors are called “Exceptions” Like everything else in Java, Exceptions are also grouped into different “objects” Java contains a whole collection of the various types of errors / exceptions that can occur during the running of a program. When a runtime error occurs, Java will stop the program and report to you what exception has happened

ICS-3M1 - Mr. Martens - Variables Part 2 Case Study – The Birthday Puzzle Write down the month you were born in (as a number) i.e. Jan=1, Feb = 2, etc Multiply that number by 5 Add another 6 to your total Multiply the new number by 4 Now add another 9 (almost done) Next, multiply the new number by 5 Lastly add your birth DATE (3 rd, 4 th, 21 st ) to your final total

ICS-3M1 - Mr. Martens - Variables Part 2 Now – tell me your number Give me the number you ended up with and I can tell when your birthday is... Who dares to challenge the great, psychic, Mr. Martens?!?!?!? Well??????????

ICS-3M1 - Mr. Martens - Variables Part 2 A Good Magician Never Reveals His Secrets Lucky for you, I’m not a good magician. Here is a flowchart that breaks down my process to get your birthday START Show Rules to User Subtract 165 from number Birth Month=number / 100 Birth Day=number % 100 Tell user their birthday STOP Ask for number from user Ex – Your birthday is 4/17

ICS-3M1 - Mr. Martens - Variables Part 2 Now, Write the Java Program Based on the flowchart on the previous slide, write the program Be sure to include several print statements at the beginning to explain to the user how they are to calculate the number they are to “punch in” to the program Use comments -> // within your code to explain what the code is doing.