Hello World Program. // HelloWorld // Author: Chris Wilcox // Date: 1/1/2015 // Class: CS160 // import java.lang.

Similar documents
Introduction to Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 106 Introduction to Computer Science I

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Java. CS 3: Computer Programming in Java

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Java Basics: Data Types, Variables, and Loops

AP Computer Science Java Subset

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Crash Course in Java

Example of a Java program

Chapter 2 Introduction to Java programming

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Sources: On the Web: Slides will be available on:

Sample CSE8A midterm Multiple Choice (circle one)

Java Crash Course Part I

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Object Oriented Software Design

Java Programming Fundamentals

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Object Oriented Software Design

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

AP Computer Science Static Methods, Strings, User Input

Chapter 2: Elements of Java

J a v a Quiz (Unit 3, Test 0 Practice)

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Moving from CS 61A Scheme to CS 61B Java

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

VB.NET Programming Fundamentals

Introduction to Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Pemrograman Dasar. Basic Elements Of Java

Variables, Constants, and Data Types

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

public static void main(string[] args) { System.out.println("hello, world"); } }

java Features Version April 19, 2013 by Thorsten Kracht

Part I. Multiple Choice Questions (2 points each):

Introduction to Programming

Introduction to Object-Oriented Programming

Java Interview Questions and Answers

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

1. Writing Simple Classes:

CMSC 202H. ArrayList, Multidimensional Arrays

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Basic Programming and PC Skills: Basic Programming and PC Skills:

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

The C Programming Language course syllabus associate level

Chapter 2 Elementary Programming

In this Chapter you ll learn:

Computer Programming I

Introduction to Java Lecture Notes. Ryan Dougherty

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

1.1 Your First Program

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Computer Programming Tutorial

Week 1: Review of Java Programming Basics

Java from a C perspective. Plan

CS 111 Classes I 1. Software Organization View to this point:

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Conditional Statements Summer 2010 Margaret Reid-Miller

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Chapter 1 Java Program Design and Development

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

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

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Contents. 9-1 Copyright (c) N. Afshartous

Programming Languages CIS 443

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Topic 11 Scanner object, conditional execution

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Quick Introduction to Java

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Using Files as Input/Output in Java 5.0 Applications

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Semantic Analysis: Types and Type Checking

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Arrays in Java. Working with Arrays

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Unit title: Computer Games: Programming (SCQF level 6)

The following program is aiming to extract from a simple text file an analysis of the content such as:

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

ECE 122. Engineering Problem Solving with Java

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

An Introduction to Computer Science

Chapter 6: Programming Languages

Lecture 5: Java Fundamentals III

Informatica e Sistemi in Tempo Reale

Primitive data types in Java

Tutorial on C Language Programming

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Transcription:

Java Review Slides

Hello World Program // HelloWorld // Author: Chris Wilcox // Date: 1/1/2015 // Class: CS160 // Email: wilcox@cs.colostate.edu import java.lang.*; public class HelloWorld { public static void main(string[] args) { System.out.println( "Hello World!" ); } } CS 160, Fall Semester 2015 2

Literals Literals are values that are directly recognized by Java: numbers 237, 10, 9, 1.5, 5.8, 99.999 characters a, Z, 0, $ strings hello, there CS 160, Fall Semester 2015 3

Java Identifiers An identifier is a name, such as the name of a variable. Identifiers may contain only Letters Digits (0 through 9) The underscore character (_) And the dollar sign symbol ($) which has a special meaning The first character cannot be a digit. CS 160, Fall Semester 2015 4

Java Types n In Java, there are two kinds of data types: n Primitive data types n Hold a single, indivisible piece of data n Pre-defined by the language n Examples: int, char, double, boolean n Classes n Hold complex combinations of data n Programs may define new classes n Examples: String, System CS 160, Fall Semester 2015 5

Primitive Types Integer types: byte, short, int, and long int is most common Floating-point types: float and double double is more common Character type: char Boolean type: boolean CS 160, Fall Semester 2015 6

Primitive Types CS 160, Fall Semester 2015 7

Sample Expressions CS 160, Fall Semester 2015 8

Assignment Compatibilities A value of each following type can be assigned to a variable of type to the right: byte à short à int à long à float à double but not to a variable of any type to the left. You can assign a value of type char to a variable of type int. except through type casting CS 160, Fall Semester 2015 9

if Statement with else An if statement may have an optional else clause that will only be executed when the condition is false Example: if ( wages <= 57600.0 ) tax = 0.124 * wages; else tax = 0.124 * 57600.0; Give an example of when BOTH statements will execute? Give an example of when NEITHER statement will execute? NONE! One or the other must execute CS 160, Fall Semester 2015 10

boolean Data Type boolean A primitive data type that can be set to: true false Example: boolean correct = true; Notice there are no quotation marks around trueand false! CS 160, Fall Semester 2015 11

Numeric Relational Operators Math Java description < < Less than > > Greater than <= Less than or equal to >= Greater than or equal to = == Equal to!= Not equal to CS 160, Fall Semester 2015 12

boolean Operators Logical and (conjunction) Java symbol && Math symbol true only when both expressions are true (MIN_WAGE <= wages) && (wages <= MAX_WAGE) Logical inclusive or (disjunction) Java symbol Math symbol true when either or both expressions are true (wages < MIN_WAGE ) (wages > MAX_WAGE ) CS 160, Fall Semester 2015 13

Java Logical and Arithmetic Operator Precedence Rules 1.! - (unary) 2. * / % 3. + - 4. < <= > >= 5. ==!= 6. ^ & 7. && 8. CS 160, Fall Semester 2015 14

Control Loops for loops for (int i=0; i<=9; i++... for (int i=100; i>0; i-=5)... for (char c= a ; c<= z ; c++)... while loops while (d < 1.2345)... do/while loops do {...} while (!done); CS 160, Fall Semester 2015 15

Temperature Conversion Program System.out.println( \tdegrees C\tDEGREES F ); for (int cent = 50; cent <= 100; cent++) { double fahr = (9.0 / 5.0) * cent + 32.0; System.out.print( \t + cent); System.out.println( \t + fahr); } CS 160, Fall Semester 2015 16

Program example: find divisors public class foo { publicstaticvoid main(string[] args) { int number = Integer.parseInt(args[0]); int divisor = 2; while (divisor < number ) { if ((number % divisor) == 0) { System.out.print(divisor + " "); } divisor = divisor + 1; } } } CS 160, Fall Semester 2015 17

Method Declarations public double sin(double angle) public double cos(double angle) public char charat(int index) public int indexof(char c) public int minimum(int i, int j) public String tolower(string s) public int[] getarray() CS 160, Fall Semester 2015 18

Method Example public static int[] truncate(double darray[]) { int iarray[] = new int[darray.length]; for (int i = 0; i < darray.length; i++) iarray[i] = (int) darray[i]; return iarray[]; } double doubles[] = {1.1, 2.2, 3.3, 4.4}; int integers[] = truncate(doubles); CS 160, Fall Semester 2015 19

public public and private Can access the class, method, or data by name outside defining class private Can access the class, method, or data by name only inside defining class Classes generally specified as public Instance variables usually are private Methods can be public or private CS 160, Fall Semester 2015 20

Caution: Pass by value What do you expect this to print? public class PassByValue { public static void main(string[] args) { int number = 100; increment(number); System.out.println( Number: + number); } public static void increment(int n) { n++; } } The value of the argument is copied, so no change to number! CS 160, Fall Semester 2015 21

Arrays An array is a set of variables of the same type accessed by their index int[] day = new int[4]; 31 28 31 30 day[0] day[1] day[2] day[3] CS 160, Fall Semester 2015 22

Creating and Accessing Arrays Figure 7.1 A common way to visualize an array Note class ArrayOfTemperatures CS 160, Fall Semester 2015 23

Java's Representation of Multidimensional Arrays Multidimensional array represented as several onedimensional arrays Given int [][] table = new int [10][6]; Array table is actually 1 dimensional of type int[] It is an array of arrays Important when sequencing through multidimensional array CS 160, Fall Semester 2015 24