Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Similar documents
Important Java terminology

Introduction to Computer Science Unit 2. Notes

AP CS Unit 3: Control Structures Notes

Introduction to Computer Science Unit 2. Notes

CIS133J. Working with Numbers in Java

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Chapter 02: Using Data

Course PJL. Arithmetic Operations

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

Chapter 2: Using Data

CS 106 Introduction to Computer Science I

Full file at

Chapter 2: Using Data

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

What did we talk about last time? Examples switch statements

4. Java Project Design, Input Methods

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

CS 302: Introduction to Programming

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

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Ex: If you use a program to record sales, you will want to remember data:

Chapter 2 ELEMENTARY PROGRAMMING

4 WORKING WITH DATA TYPES AND OPERATIONS

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Programming with Java

Course Outline. Introduction to java

CS 106 Introduction to Computer Science I

Lesson 3: Accepting User Input and Using Different Methods for Output

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Variables and data types

Lecture 6: While Loops and the Math Class

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

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

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

The Math Class. Using various math class methods. Formatting the values.

Introduction to Computer Science and Object-Oriented Programming

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Lecture 2: Intro to Java

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Methods CSC 121 Spring 2017 Howard Rosenthal

8. Decision-Making Statements. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

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

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Computer Programming I - Unit 2 Lecture 1 of 13

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Object-Oriented Programming

Program Fundamentals

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

CISC 110 Week 3. Expressions, Statements, Programming Style, and Test Review

Arithmetic and IO. 25 August 2017

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Eng. Mohammed S. Abdualal

Methods CSC 121 Fall 2016 Howard Rosenthal

CS110: PROGRAMMING LANGUAGE I

CEN 414 Java Programming

Mr. Monroe s Guide to Mastering Java Syntax

CS111: PROGRAMMING LANGUAGE II

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Chapter 4 Mathematical Functions, Characters, and Strings

AP Computer Science A. Return values

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

CS313D: ADVANCED PROGRAMMING LANGUAGE

AP Computer Science A

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Pace University. Fundamental Concepts of CS121 1

Getting started with Java

Lecture 2: Intro to Java

Introduction to Computer Programming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

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

Welcome to the Primitives and Expressions Lab!

AP Computer Science Java Mr. Clausen Program 6A, 6B

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Introduction to Programming Using Java (98-388)

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

JAVA Ch. 4. Variables and Constants Lawrenceville Press

CS 251 Intermediate Programming Java Basics

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

Introduction to Java Applications

CIS 110: Introduction to Computer Programming

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

4. Java language basics: Function. Minhaeng Lee

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Introduction to Java & Fundamental Data Types

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

Chapter 2. Elementary Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Java Classes: Math, Integer A C S L E C T U R E 8

Simple Java Programming Constructs 4

Transcription:

Programming 2

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent information Input can receive information from the user or listen for events Output can display information on a screen, write information to a file, etc. Math can process information (in particular perform math on numbers) Conditional Execution can perform operations under particular circumstances, depending on the value of variables Repetition perform a set of instructions multiple times (either a set number of times or until a condition is met)

In Java, before you set a variable to a particular value, you need to declare the variable You need to choose the appropriate data type the variable will be You need to notify Java what data type it will be Formula: DataType + VariableName; int age; String name; double weight = 175.50; Note: you can declare multiple variables in one line int age, weight, zipcode; In Java, Data types fall under two main categories: primitive data types abstract data types

Primitive data types are the basic building blocks in Java programming. byte integer with a range from -128 to 127 (8-bit) short integer with a range between -32,768 to 32,767 (16 bit) int integer with a range between -2,147,483,648 and 2,147,483,647 (32 bit) long an integer with a range between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 (64-bit) float single precision floating point number. Only use this when precise doubles are not crucial. These numbers are stored in 32-bit memory locations Do not use with precise values, such as currency or scientific calculations double double precision floating point number. They offer twice the precision as floats, and for most of our work, they are sufficient These numbers are stored in 64-bit memory locations Do not use with precise values, such as currency or scientific calculations boolean these have only 1 of two values: true or false char - The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (the question mark? ) That gives you a possible 65,535 different characters

There is a huge range of other data types that fall under the category of abstract data types abstract data types are more complex data types they are often used to represent real-life objects Some abstract data types are built into Java: strings arrays, etc. Some need to be imported: Date objects Labels Buttons TextFields Some are objects that you can imagine Card if you wanted to code a card game Ship if you wanted to code a video game Student if you wanted a program for a school application

With a Console Application System.out.print( Hello. ); This will position the cursor at the end of the line (great for input) System.out.println( Hello. ); This will output Hello. and then position the cursor on the next line. Note: with both output commands, you can place strings, variables, & expressions e.g. System.out.println( The answer is + (10 * 3) + (22 / 3)); Note: there are many ways to produce output, but this is just a start

Make sure you import Swing import javax.swing.*; Show a message dialogue box JOptionPane.showMessageDialog(null, We have a problem Houston", "alert", JOptionPane.ERROR_MESSAGE); About the arguments in the parentheses: 1. null means don t place it into another window or frame 2. We have a problem Houston is the message you want to display 3. alert is the title for the dialog box 4. JOptionPane.ERROR_MESSAGE displays a generic error message icon. Other icons are INFORMATION_MESSAGE WARNING_MESSAGE PLAIN_MESSAGE

First, import the Scanner class import java.util.scanner; Next, create a Scanner object where you declare your variables Scanner reader = new Scanner(System.in); Create a variable to capture the input double fahrenheit; using System.out.print(), produce a prompt System.out.print( Enter degrees: ); Get your input fahrenheit = reader.nextdouble(); What if we want to get another data type other than a double? int pounds = reader.nextint(); String name = reader.nextline();

To get input using a pop-up window, use the showinputdialog() method Import swing import javax.swing; Assign a variable to a showinputdialog method String answer = JOptionPane.showInputDialog(null, What s your name?, Input Dialog Box, JOptionPane.INFORMATION_MESSAGE);

Strings are another form of abstract data type they are considered objects you can concatenate strings (just like with Python) String output = firstname +, + lastname; you can call String methods output.length() returns the number of characters in the string You can print index position of strings output.charat(i); You can run a string through a for loop We ll cover that soon We ll deal with more abstract data types later

Math Operators: these are pretty much the same as in Python with a few exceptions: Operation Operator Symbol Example Multiplication * 15 * 3 45 Result Division / 20.0 / 3.0 6.6666667 for a float 6.666666666666667 for a double Integer Division Modular Division / 20/3 6 the result is truncated (the remainder is dropped) % 20%3 2 (the integer remainder only) Addition + 15 + 5 20 Subtraction - 15-5 10

Type Casting: no, this is not when Gary Busey gets the part of the crazy maniac or Michael Cera plays an awkward teenager It s when you change the data type of a number, (int) 20.6; would produce 20 (double) 5; would produce 5.000000 The Math Class: Method Description Example Code Result abs() Absolute value Math.abs(-75) 75 max() Higher of two numbers Math.max(99, 41) 99 min() Lower of the two numbers Math.min(99, 41) 41 pow() Exponentiation Math.pow(2, 5) 32 random() Random number generator Math.random() round() Round to an integer Math.round(27.59) 28 sqrt() Square root Math.sqrt(144) 12 Produces a random number between 0.0 and 1.0

There are 4 types of conditional execution in Java the if structure the if else structure the if else if else structure the case/switch structure we ll cover this when we cover the while loop Conditions: remember, with conditional execution, a condition is a statement that either evaluates to true or false: ex. (age > 20) either the age is greater than twenty (true) or it is not greater than twenty (false)

== is equal to!= is not equal to < less than > greater than <= less than or equal to >= greater than or equal to && AND (logical operator) OR (logical operator)! NOT (logical operator) Practice: does the expression evaluate to true or false? 4 <= 6 3 < 5 1 == 6 9 > 7 7 <= 6 (7 > 3) (1 < 0) 4!= 4 (7 > 3) && (1 < 0) 8 >= 10 (3 > 7) (1 < 0) 5 < 3 7!= 4 7 > 0 2 == 2 8 >= 8!(a == a) (7 > 3) && (1 > 0)!(5 == 4)

in Java you can do the if structure two ways if (age > 40) System.out.println( You re getting old! ); if (age > 55) { System.out.println( You re really getting old! ); oldman = true; Note: if the code that executes under the condition is extra long or requires more than 1 statement, you must surround your statements with curly braces

void applybrakes(){ if (ismoving) { currentspeed--; else { System.err.println("The bicycle has already stopped!"); if-else if (testscore >= 90) { grade = 'A'; else if (testscore >= 80) { grade = 'B'; else if (testscore >= 70) { grade = 'C'; else if (testscore >= 60) { grade = 'D'; else { grade = 'F'; if-else if-else

String comment// The generated insult. int which = (int)(math.random() * 3); switch (which) { case 0: comment = "You look so much better than usual."; break; case 1: comment = "Your work is up to its usual standards."; break; case 2: comment = "You're quite competent for so little experience."; break; default: comment = "Oops -- something is wrong with this code."; Sample Case-Switch Code Switch structures only work with integers which is a random number (0, 1, or 2) Use the switch keyword In the parentheses, include an integer (variable or literal) Each case is like an if statement End each case with a break, so it doesn t execute more than 1 line of code You can wrap multiple lines of code inside each case (use braces to surround the block) default is what gets run if none of the none of the cases apply Notes