6.092: Intro to Java. 2: More types, Methods, Conditionals

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

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

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

Java Basics: Data Types, Variables, and Loops

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

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

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

Object Oriented Software Design

AP Computer Science Static Methods, Strings, User Input

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

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

Java Crash Course Part I

CS 106 Introduction to Computer Science I

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

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

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

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

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

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

Example of a Java program

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

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

Introduction to Programming

1.1 Your First Program

CS170 Lab 11 Abstract Data Types & Objects

Sample CSE8A midterm Multiple Choice (circle one)

Moving from CS 61A Scheme to CS 61B Java

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Introduction to Java

Introduction to Java. CS 3: Computer Programming in Java

Inheritance, overloading and overriding

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Interactive Applications (CLI) and Math

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Building Java Programs

AP Computer Science Java Subset

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Building Java Programs

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

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

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

java Features Version April 19, 2013 by Thorsten Kracht

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Building Java Programs

Chapter 2: Elements of Java

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Basic Object-Oriented Programming in Java

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Java Programming Fundamentals

A Simple Introduction to Game Programming With C# and XNA 3.1

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

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

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

Chapter 2 Introduction to Java programming

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Order of Operations More Essential Practice

Crash Course in Java

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.

Block IQ. Marko Boon Jacques Resing

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

(Eng. Hayam Reda Seireg) Sheet Java

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

Install Java Development Kit (JDK) 1.8

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

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Introduction to Python

Section 6 Spring 2013

1. Writing Simple Classes:

Chapter 2 Elementary Programming

Introduction to Python

3 length + 23 size = 2

In this Chapter you ll learn:

Computer Programming I

Habanero Extreme Scale Software Research Project

Introduction to Object-Oriented Programming

Topic 11 Scanner object, conditional execution

LAB4 Making Classes and Objects

Java Interview Questions and Answers

Unit 6. Loop statements

ECE 122. Engineering Problem Solving with Java

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Programming Fundamentals I CS 110, Central Washington University. November 2015

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

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

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Software Engineering Techniques

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

Object-Oriented Programming in Java

Building Java Programs

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

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

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

6.087 Lecture 2 January 12, 2010

Content of this lecture. Regular Expressions in Java. Hello, world! In Java. Programming in Java

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

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

Transcription:

6.092: Intro to Java 2: More types, Methods, Conditionals

Outline Lecture 1 Review More types Methods Conditionals

Types Kinds of values that can be stored and manipulated. boolean: Truth value (true or false). int: Integer (0, 1, -47). double: Real number (3.14, 1.0, -2.1). String: Text ( hello, example ).

Variables Named location that stores a value Example: String a = a ; String b = letter b ; a = letter a ; String c = a + and + b;

Operators Symbols that perform simple computations Assignment: = Addition: + Subtraction: Multiplication: * Division: /

class GravityCalculator { public static void main(string[] args) { double gravity = -9.81; double initialvelocity = 0.0; double fallingtime = 10.0; double initialposition = 0.0; double finalposition =.5 * gravity * fallingtime * fallingtime; finalposition = finalposition + initialvelocity * fallingtime; finalposition = finalposition + initialposition; System.out.println("An object's position after " + fallingtime + " seconds is " + finalposition + m.");

finalposition = finalposition + initialvelocity * fallingtime; finalposition = finalposition + initialposition; OR finalposition += initialvelocity * fallingtime; finalposition += initialposition;

Questions from last lecture?

Outline Lecture 1 Review More types Methods Conditionals

Division Division ( / ) operates differently on integers and on doubles! Example: double a = 5.0/2.0; // a = 2.5 int b = 4/2; // b = 2 int c = 5/2; // c = 2 double d = 5/2; // d = 2.0

Order of Operations Precedence like math, left to right Right hand side of = evaluated first Parenthesis increase precedence double x = 3 / 2 + 1; // x = 2.0 double y = 3 / (2 + 1); // y = 1.0

Mismatched Types Java verifies that types always match: String five = 5; // ERROR! test.java.2: incompatible types found: int required: java.lang.string String five = 5;

Conversion by casting int a = 2; // a = 2 double a = 2; // a = 2.0 (Implicit) int a = 18.7; // ERROR int a = (int)18.7; // a = 18 double a = 2/3; // a = 0.0 double a = (double)2/3; // a = 0.6666

Outline Lecture 1 Review More types Methods Conditionals

Methods public static void main(string[] arguments) { System.out.println( hi );

Adding Methods public static void NAME() { STATEMENTS To call a method: NAME();

class NewLine { public static void newline() { System.out.println(""); public static void threelines() { newline(); newline(); newline(); public static void main(string[] arguments) { System.out.println("Line 1"); threelines(); System.out.println("Line 2");

class NewLine { public static void newline() { System.out.println(""); public static void threelines() { newline(); newline(); newline(); public static void main(string[] arguments) { System.out.println("Line 1"); threelines(); System.out.println("Line 2");

class NewLine { public static void newline() { System.out.println( ""); public static void threelines() { newline(); newline(); newline(); public static void main(string[] arguments) { System.out.println("Line 1"); threelines(); System.out.println("Line 2");

Parameters public static void NAME(TYPE NAME) { STATEMENTS To call: NAME(EXPRESSION);

class Square { public static void printsquare(int x) { System.out.println(x*x); public static void main(string[] arguments) { int value = 2; printsquare(value); printsquare(3); printsquare(value*2);

class Square2 { public static void printsquare(int x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare("hello"); printsquare(5.5); What s wrong here?

class Square3 { public static void printsquare(double x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare(5); What s wrong here?

Multiple Parameters [ ] NAME(TYPE NAME, TYPE NAME) { STATEMENTS To call: NAME(arg1, arg2);

class Multiply { public static void times (double a, double b) { System.out.println(a * b); public static void main(string[] arguments) { times (2, 2); times (3, 4);

Return Values public static TYPE NAME() { STATEMENTS return EXPRESSION; void means no type

class Square3 { public static void printsquare(double x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare(5);

class Square4 { public static double square(double x) { return x*x; public static void main(string[] arguments) { System.out.println(square(5)); System.out.println(square(2));

Variable Scope Variables live in the block ({) where they are defined (scope) Method parameters are like defining a new variable in the method

class SquareChange { public static void printsquare(int x) { System.out.println("printSquare x = " + x); x = x * x; System.out.println("printSquare x = " + x); public static void main(string[] arguments) { int x = 5; System.out.println("main x = " + x); printsquare(x); System.out.println("main x = " + x);

class Scope { public static void main(string[] arguments) { int x = 5; if (x == 5) { int x = 6; int y = 72; System.out.println("x = " + x + " y = " + y); System.out.println("x = " + x + " y = " + y);

Methods: Building Blocks Big programs are built out of small methods Methods can be individually developed, tested and reused User of method does not need to know how it works In Computer Science, this is called abstraction

Mathematical Functions Math.sin(x) Math.cos(Math.PI / 2) Math.pow(2, 3) Math.log(Math.log(x + y))

Outline Lecture 1 Review More types Methods Conditionals

if statement if (CONDITION) { STATEMENTS

public static void test(int x) { if (x > 5) { System.out.println(x + " is > 5"); public static void main(string[] arguments) { test(6); test(5); test(4);

Comparison operators x > y: x is greater than y x < y: x is less than y x >= y: x is greater than or equal to x x <= y: x is less than or equal to y x == y: x equals y ( equality: ==, assignment: = )

Boolean operators &&: logical AND : logical OR if (x > 6) { if (x < 9) { if ( x > 6 && x < 9) {

else if (CONDITION) { STATEMENTS else { STATEMENTS

public static void test(int x) { if (x > 5) { System.out.println(x + " is > 5"); else { System.out.println(x + " is not > 5"); public static void main(string[] arguments) { test(6); test(5); test(4);

if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else { STATEMENTS else if

public static void test(int x) { if (x > 5) { System.out.println(x + " is > 5"); else if (x == 5) { System.out.println(x + " equals 5"); else { System.out.println(x + " is < 5"); public static void main(string[] arguments) { test(6); test(5); test(4);

Questions?

Assignment: FooCorporation Method to print pay based on base pay and hours worked Overtime: More than 40 hours, paid 1.5 times base pay Minimum Wage: $8.00/hour Maximum Work: 60 hours a week

Reminder Write your own code Homework due tomorrow (Wednesday) 3pm on Stellar.

Conversion by method int to String: String five = 5; // ERROR! String five = Integer.toString (5); String five = + 5; // five = 5 String to int: int foo = 18 ; // ERROR! int foo = Integer.parseInt ( 18 );

Comparison operators Do NOT call == on doubles! EVER. double a = Math.cos (Math.PI / 2); double b = 0.0; a = 6.123233995736766E-17 a == b will return FALSE!

MIT OpenCourseWare http://ocw.mit.edu 6.092 Introduction to Programming in Java January (IAP) 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.