CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Similar documents
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

COMP-202 Unit 1: Introduction. CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics

COMP-202 Unit 1: Introduction. CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

COMP 102: Computers and Computing

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

COMP-202 Unit 0: Course Details

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

CSCI 1103: Introduction

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

St. Edmund Preparatory High School Brooklyn, NY

Computer Science is...

Programming with Java

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Lab # 2. For today s lab:

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Controls Structure for Repetition

Introduction to the course and basic programming concepts

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

CS 177 Recitation. Week 1 Intro to Java

CS 11 java track: lecture 1

COMP 202 Java in one week

COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

How to approach a computational problem

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Warmup : Name that tune!

Math Modeling in Java: An S-I Compartment Model

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Post Experiment Interview Questions

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Part II Composition of Functions

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

COMP 110 Project 1 Programming Project Warm-Up Exercise

Midterms Save the Dates!

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

COMP-202 More Complex OOP

Software and Programming 1

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Java Bytecode (binary file)

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

CS 177 Week 5 Recitation Slides. Loops

COMP-202 Unit 0: Course Details

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

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

! Learn how to think like a computer scientist. ! Learn problem solving. ! Read and write code. ! Understand object oriented programming

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Java Programming. Computer Science 112

Conditionals, Loops, and Style

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Basic Programming Language Syntax

Civil Engineering Computation

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

COMP-202 Unit 9: Exceptions

Chapter 2, Part I Introduction to C Programming

COMP-202: Foundations of Programming

Java Programming Constructs Java Programming 2 Lesson 1

Intro. Speed V Growth

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

How to make a "hello world" program in Java with Eclipse *

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Yup, left blank on purpose. You can use it to draw whatever you want :-)

Lecture 1 - Introduction (Class Notes)

Object-Oriented Programming

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

Arrays. myints = new int[15];

First Java Program - Output to the Screen

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

SESSION 3 Programming Languages for Objects

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Introduction to the Java Basics: Control Flow Statements

Problem One: A Quick Algebra Review

An overview of Java, Data types and variables

Transcription:

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction

Announcements Did you miss the first lecture? Come talk to me after class. If you want an exemption from this course, please send me email: dpomer@cs.mcgill.ca

Assignment 0 Assignment 0 is posted. Due Wednesday January 12th. Worth 2% The assignment is geared towards making sure you have everything set up properly and some basic programming. After today's class, you will have most of the knowledge you need to do the assignment. (A bit may be done on Monday)

Last Class Administrative stuff Modular programming A tiny bit about java

Today Understanding Input/Output How to count cards! How computers think A tiny bit more about java

After today, you should be able to... Understand the input/output of a problem, function, or method Explain the way we go from a problem to a computer program, and understand why it is useful to break problems into subproblems Know in English what a loop variable and if statement mean Be able to write output to the screen in Java Understand the idea of methods in Java

You should be able to answer questions like... What is the input and output of the function f(x)=sin(x)? What would a variable be used for? What is an example of the use of {,, and ; in java. What is the input and output of a method that looks like public static double MyMethod(int x, double y)

Part 1: What Is Programming?

Programming and Computers In order to understand what programming is, we need to know what a computer is A computer is a machine that executes lists of instructions We feed a list of instructions to the computer and the computer executes them The computer may apply the instructions on additional information fed to the computer (the input) The computer may produce information as a result of executing this list of instructions (the output) COMP-202 - Introduction 9

Programming and Computers Programming a computer involves two things: 1) Designing lists of instructions that will make the computer solve specific problems 2) Having the computer execute the instructions The purpose is to have the computer solve the problem for you instead of you solving the problem by hand COMP-202 - Introduction 10

Designing instruction list Designing an instruction list is largely about breaking problems down into smaller tasks. It is also about considering the input and output of a program COMP-202 - Introduction 11

Input vs Output : Input The input to a program is what goes into it. It is whatever is given to the program or problem. This is anything that is necessary to solve it. COMP-202 - Introduction 12

Input vs Output : Output The output from a program is what comes from it. This is anything that is produced as a result of the program running COMP-202 - Introduction 13

Example: f(x) = 2x The input to this mathematical function is a number The output to this function is a number Note: Input is similar to domain in math. Output is similar to range We have to judge by context whether this function is being used on integers, real numbers, or imaginary numbers. (Or something completely different!) In Java, we will explicitly write what the domain and range are. COMP-202 - Introduction 14

Example: f(x,y) = 2x + 3y What is the input and output? COMP-202 - Introduction 15

Example: f(x,y) = 2x + 3y The input to this mathematical function is two numbers -- x and y The output is 1 number. The sum of 2x and 3y COMP-202 - Introduction 16

Input/Output Input/Output is not necessarily restricted to math problems. For example, what is the input and output of the problem cooking scrambled eggs COMP-202 - Introduction 17

Cooking Scrambled Eggs (1) Input (things we need to follow the instructions): Two eggs A tablespoon of oil A pan A stove Output (things that result from following the instructions): Scrambled eggs COMP-202 - Introduction 18

Cooking Scrambled Eggs (2) Instructions: Add oil to pan Heat pan on stove Crack eggs into pan Mix until light and flaky COMP-202 - Introduction 19

Mmmmm... COMP-202 - Introduction 20

How to count cards! COMP-202 - Introduction 21

Counting Cards of a Given Suit Input: The deck The suit we are looking for COMP-202 - Introduction 22

Breaking the problem down How can we write this suit-counting problem down in simple to understand instructions? COMP-202 - Introduction 23

Counting Cards of a Given Suit 1)Take a blank piece of paper and the deck 2)If there are cards left, take the top card in the deck If there are not cards left, skip to step 2 3)Check if the card belongs to the suit you are looking for. If so, make a mark on the piece of paper. 4)Go back to step 2 5)Count the number of marks on your paper. That number is the answer we want. By the way, they will not let you do this at the casino! COMP-202 - Introduction 24

Counting Cards of a Given Suit 1)Take a blank piece of paper and the deck 2)If there are cards left, take the top card in the deck If there are not cards left, skip to step 2 3)Check if the card belongs to the suit you are looking for. If so, make a mark on the piece of paper. 4)Go back to step 2 5)Count the number of marks on your paper. That number is the answer we want. an if statement COMP-202 - Introduction 25

Counting Cards of a Given Suit 1)Take a blank piece of paper and the deck 2)If there are cards left, take the top card in the deck If there are not cards left, skip to step 2 3)Check if the card belongs to the suit you are looking for. If so, make a mark on the piece of paper. 4)Go back to step 2 5)Count the number of marks on your paper. That number is the answer we want. an if statement a loop COMP-202 - Introduction 26

Counting Cards of a Given Suit 1)Take a blank piece of paper and the deck 2)If there are cards left, take the top card in the deck If there are not cards left, skip to step 2 3)Check if the card belongs to the suit you are looking for. If so, make a mark on the piece of paper. 4)Go back to step 2 5)Count the number of marks on your paper. That number is the answer we want. an if statement a variable (it stores something) a loop COMP-202 - Introduction 27

Exercise:Determine Whether you have an even/odd # of pages of paper Suppose that your computer is a person who does not know how to multiply/divide/add/subtract and does not know what odd or even mean. Write down instructions for determining whether a stack of paper has an even or odd number of sheets. What is the input and output? Hint: You may find an additional 2-sided, such as a coin or another piece of paper useful. COMP-202 - Introduction 28

Amelia Bedilia COMP-202 - Introduction 29

Instructions and Precision The instructions for finding a card in a deck or counting the number of cards of a given suit in a deck are very precise and unambiguous Amelia would have no room for misunderstanding Writing lists of instructions like these is the very essence of programming a computer COMP-202 - Introduction 30

Programs (1) A computer program is essentially a list of instructions telling a computer what to do The computer is stupid in that it is just following the instructions without knowing what it is doing. Thus you must be very precise and omit no details. COMP-202 - Introduction 31

Programs (1) Programs have an input and an output as well. If you omit the proper instructions or include the wrong instructions, generally 4 things can happen: 1)You get incredibly lucky and on that particular input it works anyway 2)The program gives the incorrect output 3)The program crashes 4)The program goes on forever and ever COMP-202 - Introduction 32

Case 1 Sometimes you will give the computer the wrong instructions, but it will work anyway on a particular input. For example, in the card program, suppose we omitted the instruction make a mark on the piece of paper This means the count will always be 0, no matter what the input. When would this still lead to the right answer? COMP-202 - Introduction 33

Case 2 The program gives the wrong output This would happen in the previous example on any input where the pack of cards had at least one card of the suit in question. COMP-202 - Introduction 34

Case 3 Sometimes the program will crash. In the card example, this happens if the computer is unable to do a certain step. For example, at step 1 we have: Take a blank piece of paper and the deck Suppose you don't have any paper. Then this step is impossible. In a computer program, when something like this happens, the program will crash, exiting without output COMP-202 - Introduction 35

Case 4 The program goes on forever and ever What if after step 3, we inserted Put the card in your hand back on top of the deck Then the program would go on forever. We would continually pick up the top card, check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. Then we would pick up the top card (the same card!), check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. Then we would pick up the top card COMP-202 - Introduction 36

Case 4 check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. Then we would pick up the top card, check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. Then we would pick up the top card, check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. Then we would pick up the top card, check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. Then we would pick up the top card, check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. Then we would pick up the top card, check if it is the suit we are interested in, possibly mark it down on paper, then put it back on top. COMP-202 - Introduction 37

Having the computer execute the instructions This step consists of translating your human words into a language the computer understands Kind of like learning a foreign language. But there are some key differences. Sentences in human languages can often have many interpretations. COMP-202 - Introduction 38

Human and Computer Languages Compare to the following English sentence: "The lady hit the man with a baby" Does this mean 1)A lady hit a man who had a baby? (Dude, what a jerk!) 2)A lady used a baby to hit a man? (Good lord!) 3)A lady and a baby ganged up on a man and hit him. (Kids today!) Computer statements, on the other hand, always have only one possible interpretation. COMP-202 - Introduction 39

Human and Computer Languages One of the challenges is to learn the different interpretations the computer will give to commands. The computer will not normally tell you how it is interpreting things. It is up to you to figure it out, both by looking at your code and observing the output. COMP-202 - Introduction 40

Complex Tasks Sometimes, we want the computer to perform complex tasks Writing a detailed list of precise instructions for this complex task would be very difficult There are just too many instructions for humans to manage Solution: Break down the complex task into a series of simpler tasks; if the simpler tasks are still too complex, break them down into even simpler tasks Write a list of instructions for each of the simpler tasks The list of instructions for each of the simpler tasks can be used as a single instruction in the list of instructions that performs the original task COMP-202 - Introduction 41

Summary so far What are some of the things we know we have to do to solve a problem on a computer? COMP-202 - Introduction 42

Summary so far 1)Figure out the input and output of the problem 2)Write a very precise set of instructions for solving the problem. 3)Translate each instruction from human language to a computer language. If we have broken our steps in number two into very small pieces, then number three will be much easier. COMP-202 - Introduction 43

Summary so far Thinking about the card problem, what are some things that we would like the computer to be able to do? COMP-202 - Introduction 44

Summary so far 1)Store information (i.e. the piece of paper) 2)Output information (i.e. the final count) 3)Check if certain conditions are satisfied or not (i.e. does the card match and check if there remain any cards ) 4)Have a way to jump to step n when we want to (i.e. go back to step 2 5)Be able to make subproblems or subtasks One more thing is the program needs to know where to start! COMP-202 - Introduction 45

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); The first thing is that every program in java MUST be inside of a class. For now, do not worry about precisely what that means and just make sure that every program has the line public class NAMEOFFILE { in it. (Note: we'll see later that this isn't always on the first line though!) COMP-202 - Introduction 46

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); The red { marks the beginning of the class HelloWorld. {'s always mark the beginning of a block (or segment) of code. Each { always has a corresponding to mark the end. You can tell which corresponds to which because the first { opened, will correspond with the last COMP-202 - Introduction 47

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Which matches with the red {, and which would match with the blue {? COMP-202 - Introduction 48

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Try at home: What happens if you remove the final, so that there are not matching braces? What happens if you add an extra at the end so that there are 3 s? COMP-202 - Introduction 49

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); The next thing to notice is on the second line. This is known as the main method. The important thing to know is that the main method is where your program starts. (We will later see exactly what all the words before mean) COMP-202 - Introduction 50

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Without the highlighted line (or something very similar), the computer will not know where to start. It is possible to have a class that does not have a main method. However, then you can not run it as a program. We will see examples of this later. COMP-202 - Introduction 51

Returning to our list 1)Store information (i.e. the piece of paper) 2)Output information (i.e. the final count) 3)Check if certain conditions are satisfied or not (i.e. does the card match and check if there remain any cards ) 4)Have a way to jump to step n when we want to (i.e. go back to step 2 5)Be able to make subproblems or subtasks One more thing is the program needs to know where to start! COMP-202 - Introduction 52

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); The highlighted line is one of the ways you can output information in java. Anything between the quotations will be printed to the screen. The entire line is called a statement Note: The computer expects everything between the quotations to all be on one line. COMP-202 - Introduction 53

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); The computer expects everything between the quotations will all be one one line. If you want to write multiple lines, you could write multiple statements. COMP-202 - Introduction 54

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); System.out.println( I am on a second line ); Exercise to think about: What if we want to write a quotation mark to the screen? Hint: google string escape sequence or something similar COMP-202 - Introduction 55

Java Program: Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Every statement in java must end in a semicolon. Next week we will go into more detail on what exactly a statement is. COMP-202 - Introduction 56

Methods A method is like a sub-program or sub-routine. You can use it as part of your program. When you call a method, the computer will jump from the place it was before to the method. When it finishes the method, it will jump back. COMP-202 - Introduction 57

Using a method or subroutine 1 2 3 4 5 6 public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); PrintMoreStuff(); System.out.println( Goodbye world! ); 7 8 9 10 11 //the next line is a method public static void PrintMoreStuff() { System.out.println( More stuff here ); What is the order of steps here? COMP-202 - Introduction 58

Using a method or subroutine 1 2 3 4 5 6 public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); PrintMoreStuff(); System.out.println( Goodbye world! ); 7 8 9 10 11 //the next line is a method public static void PrintMoreStuff() { System.out.println( More stuff here ); 1)First the program starts at main COMP-202 - Introduction 59

Using a method or subroutine 1 2 3 4 5 6 public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); PrintMoreStuff(); System.out.println( Goodbye world! ); 7 8 9 10 11 //the next line is a method public static void PrintMoreStuff() { System.out.println( More stuff here ); 2)Next the program prints HelloWorld COMP-202 - Introduction 60

Using a method or subroutine 1 2 3 4 5 6 public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); PrintMoreStuff(); System.out.println( Goodbye world! ); 7 8 9 10 11 //the next line is a method public static void PrintMoreStuff() { System.out.println( More stuff here ); 3)The program calls the method PrintMoreStuff() so it jumps to the section PrintMoreStuff() COMP-202 - Introduction 61

Using a method or subroutine 1 2 3 4 5 6 public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); PrintMoreStuff(); System.out.println( Goodbye world! ); 7 8 9 10 11 //the next line is a method public static void PrintMoreStuff() { System.out.println( More stuff here ); 4)Now the program prints More stuff here COMP-202 - Introduction 62

Using a method or subroutine 1 2 3 4 5 6 public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); PrintMoreStuff(); System.out.println( Goodbye world! ); 7 8 9 10 11 //the next line is a method public static void PrintMoreStuff() { System.out.println( More stuff here ); 5)It then returns to where the method was called and prints Goodbye world! COMP-202 - Introduction 63

Comments You may have noticed the following line and thought, That's weird. It looks a lot like English to me //the next line is a method That's because it is a comment. Any time you write two /'s in a row, the rest of the line will be completely ignored by the compiler. It is a good idea to add explanations for code you write. That way either you or someone else looking at your code will understand what it does. If you want to write a longer comments, put the comments between /* which starts the comment and COMP-202 - Introduction 64

Returning a value Like programs, methods can also take input and give output. Many mathematical functions can be written this way. In the previous example, the method gives as output void (i.e. nothing) We know this because the first line of the method is: public static void PrintMoreStuff() COMP-202 - Introduction 65

Returning a value Methods can also give output. If you want the method to give an output, you write what you want it to output instead of the word void. For example public static Flamingo PrintMoreStuff() --if you want the method to output flamingos public static Pizza PrintMoreStuff() --if you want it to output pizza COMP-202 - Introduction 66

int, string,double A computer does not know what Flamingos or Pizza are unless you define them. However there are a few things a computer knows already. A few (but not all) of these are: int -- an integer (3,4,-10) string a sequence of letters, numbers, punctuation, etc ( hello,, foo ) double a real number (2.6,-55,0) public static int PrintMoreStuff() --if you want the method to output an int COMP-202 - Introduction 67

Outputting a value If you make your method output something instead of void, you can write what it should output using a return statement. We often call this returning a value /*The following function ReturnsANumber*/ public static int ReturnANumber() { return 0; This means the method outputs 0. COMP-202 - Introduction 68

Getting the output If you know a method has output, you use it by calling the method public class GetANumber { public static void main(string[] args) { System.out.println(ReturnANumber()); public static int ReturnANumber() { return 0 There is a syntax error here. What is it? COMP-202 - Introduction 69

Storing the output If you want to store a value, you can do what is known as declaring a variable. To do this, first write the kind or type of variable you want to store. This could be an int, double, string, or other things. This MUST match the type you are trying to store. For example, if I am trying to store the output from ReturnANumber(), I should put it into an int COMP-202 - Introduction 70

Storing the output Somewhere in your code, you could have: int x = ReturnANumber(); System.out.println(x); The first line does a lot of things. First it calls the method ReturnANumber(). It then stores the output from ReturnANumber() into the variable x. Because ReturnANumber() outputs or returns an int, we make x an int. The second line, takes the stored value and prints it to COMP-202 - Introduction 71

Giving Input to Methods Methods can also have input Everything between the ()'s in the method is input to the method. public static int ReturnANumber() { return 0; There is nothing between the (), so the method has no input. This is why it always outputs the same thing COMP-202 - Introduction 72

Giving Input to Methods public static int AddOne(int x) { x+1; This method takes as input one value. This value must be an int. We often refer to the input a method takes as the method's arguments What do you think this method does? COMP-202 - Introduction 73

Calling a method with arguments The previous method takes as input one int and outputs one int. If you try to call it without giving it an int, it won't know what to do. When you call it, you must give it an int, for example: System.out.println(AddOne(3)); will work but System.out.println(AddOne()); will not COMP-202 - Introduction 74

Methods with more than one argument Methods can also have more than one argument as input. For example public static int Add(int x, int y) { return????? What could we write instead of????? if we wanted the method to output the addition of x and y? COMP-202 - Introduction 75

Example: Experimenting with things Suppose we want to understand the order of operations in Java. We try googling it but have some trouble understanding the explanations. How could we try to figure it out? Well one way to do it is letting Java tell you. For example, write the line: System.out.println(3+4*3); Then look at the output. If it is 21, then it probably adds first. If it is 15, then it probably multiplies first. COMP-202 - Introduction 76

Exercises 1)Make sure you can compile HelloWorld.java 2)Take the program from the slides with the method PrintMoreStuff() in it. Make sure you can compile it 3)Try to change the method PrintMoreStuff so that it takes as a method an int and prints the value of the int instead of always printing the same thing. 4)Call this method 5 times from your main() method. Make sure one time is with the value stored in a variable 5)Now change PrintMoreStuff() so that it takes two ints as input and prints the sum of the 2 values COMP-202 - Introduction 77

Next Class How computers store all these values What memory in a computer is More on methods and variables COMP-202 - Introduction 78