First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Similar documents
First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Variables, Types, Operations on Numbers

Methods (Functions) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Decisions (If Statements) And Boolean Expressions

CS 302: Introduction to Programming

double float char In a method: final typename variablename = expression ;

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

Programming in Java

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

STUDENT LESSON A7 Simple I/O

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Introduction to Java Applications

Arithmetic and IO. 25 August 2017

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

Java Reference Card. 1. Classes. 2. Methods. 3. Conditionals. 4. Operators

COMP 202 Java in one week

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

What did we talk about last time? Examples switch statements

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

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

4. Java Project Design, Input Methods

First Java Program - Output to the Screen

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

FUNDAMENTAL DATA TYPES

COMP 202. Java in one week

Section 2.2 Your First Program in Java: Printing a Line of Text

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Basic Programming Language Syntax

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

Getting started with Java

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Programming with Java

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Introduction to Software Development (ISD) David Weston and Igor Razgon

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Full file at

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

2 nd Week Lecture Notes

Introduction to Computer Science Unit 2. Notes

Class 9: Static Methods and Data Members

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Course Outline. Introduction to java

Research Group. 2: More types, Methods, Conditionals

CS111: PROGRAMMING LANGUAGE II

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Expressions, Statements, Variables, Assignments, Types

I. Variables and Data Type week 3

Simple Java Reference

Topics. The Development Process

CEN 414 Java Programming

Visual Programming. Lecture 2: More types, Methods, Conditionals

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

2.8. Decision Making: Equality and Relational Operators

Big Java. Fifth Edition. Chapter 3 Fundamental Data Types. Cay Horstmann

Introduction to Java Applications

Data Structure and Programming Languages

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

Datatypes, Variables, and Operations

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Elementary Programming

Chapter 4: Conditionals and Recursion

Java Coding 3. Over & over again!

DM550 Introduction to Programming part 2. Jan Baumbach.

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

AP Computer Science Unit 1. Programs

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

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

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

Section 2.2 Your First Program in Java: Printing a Line of Text

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 2 ELEMENTARY PROGRAMMING

COMP 110/L Lecture 4. Kyle Dewey

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Expressions and Variables

CS 106 Introduction to Computer Science I

Programming with Java

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

CC112 Structured Programming

BASIC ELEMENTS OF A COMPUTER PROGRAM

Building Java Programs

Basic Programming Elements

COMP 202 Java in one week

Transcription:

First Programs CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

Output System.out.println( ) prints out something. System.out.println is the first piece of Java that we learn in this class. We will see in detail what kind of things can get printed. In the beginning, the things we care about printing are: Numbers. Strings (text). 2

Examples of System.out.println Program: public class hello1 { System.out.println("Have a nice day."); System.out.println(6.3 + 12/7); In Netbeans, the program output always starts with "run:" and ends with "BUILD SUCCESSFUL ". Output: run: Have a nice day. 7.3 BUILD SUCCESSFUL (total time: 0 seconds) 3

Examples of System.out.println Program: public class hello1 { System.out.println("Have a nice day."); System.out.println(6.3 + 12/7); Output: In Netbeans, the program output always starts with "run:" and ends with "BUILD SUCCESSFUL ". From now on, we will not be showing those lines. We will call "program output" what is between those lines. Have a nice day. 7.3 4

Syntax of System.out.println Program: public class hello1 { System.out.println("Have a nice day."); System.out.println(6.3 + 12/7); What you want to print is called the argument. To use System.out.println, you write a line like this: System.out.println(argument); In other words, you write System.out.println, followed by a left parenthesis, followed by an argument, followed by a right parenthesis, followed by a semicolon. 5

Syntax of System.out.println Program: public class hello1 { System.out.println("Have a nice day."); System.out.println(6.3 + 12/7); If the argument is text (also called a string), then it must be enclosed in double quotes. If the argument is a numerical expression, then System.out.println prints the result of that expression. 6

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println("hello"); System.out.println(hello); 7

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println("hello"); Correct, prints hello. System.out.println(hello); Incorrect, missing double quotes. Will not run. 8

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println("6.3 + 12/7"); System.out.println(6.3 + 12/7); 9

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println("6.3 + 12/7"); Correct, prints 6.3 + 12/7 Note that the argument here is text. System.out.println(6.3 + 12/7); Correct, prints 7.3 Note that the argument here is a numerical expression. 10

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println("hello") System.out.println(6.3 + 12/7) 11

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println("hello") Incorrect. Missing semicolon at the end. Will not run. System.out.println(6.3 + 12/7) Incorrect. Missing semicolon at the end. Will not run. 12

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println "hello"; System.out.println 6.3 + 12/7; 13

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println "hello"; Incorrect. Missing parentheses. Will not run. System.out.println 6.3 + 12/7; Incorrect. Missing parentheses. Will not run. 14

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println "hello" (); System.out.println 6.3 + 12/7 (); 15

Syntax of System.out.println Is each of these lines correct or not? If correct, what will it print? System.out.println "hello" (); Incorrect. Misplaced parentheses. Will not run. System.out.println 6.3 + 12/7 (); Incorrect. Misplaced parentheses. Will not run. 16

Syntax of System.out.println As we saw a few slides ago, to use System.out.println, you write a line like this: System.out.println(argument); Java (like any programming language) is very strict. If you do not follow the syntax EXACTLY, it will refuse to execute that line. This is true not only for System.out.println, but for any syntax rules that we will see in this course. 17

Java as a Calculator. public class hello1 { System.out.println((23*3) + 12/4.5); System.out.println(6.3 + 12/7-4); Output: 71.66666666666667 3.3 We can type in arbitrary numerical expressions, and Java evaluates them. This is still not that exciting. However, such calculations are a useful building block for real programs. 18

More Math Calculations public class hello1 { System.out.println(Math.pow(2, 10)); System.out.println(8 * Math.pow(2 + 3.5/7, 4)); System.out.println(Math.sqrt(3)); System.out.println(4 - Math.sqrt(3+5/7.2)); Powers: 2 10 becomes Math.pow(2, 10) Output: 1024.0 312.5 1.7320508075688772 2.077906234221534 8 2 + 3.5 7 Roots 4 becomes 8 * Math.pow(2 + 3.5/7, 4) 3 becomes Math.sqrt(3) 4 3 + 5 7.2 becomes 4 - Math.sqrt(3+5/7.2) 19

More Math Calculations public class hello1 { System.out.println(Math.PI); System.out.println(Math.sin(Math.PI / 2)); System.out.println(Math.cos(Math.PI / 2)); System.out.println(Math.tan(Math.PI / 2)); System.out.println(Math.log(12.5)); Output: 3.141592653589793 1.0 6.123233995736766E-17 1.633123935319537E16 2.5257286443082556 The pi constant: Math.PI The sine of x: Math.sin(x) The cosine of x: Math.cos(x) The tangent of x: Math.tan(x) The natural logarithm of x: Math.log(x) 20

Division: Floating Point and Integer public class hello1 { System.out.println(7.0 / 4.0); System.out.println(7 / 4.0); System.out.println(7.0 / 4); System.out.println(7 / 4); System.out.println(7 % 4); Output: 1.75 1.75 1.75 1 3 Floating point division: 7.0 / 4.0 7 / 4.0 7.0 / 4 They all evaluate to 1.75 Integer division: 7 / 4 evaluates to 1 7 % 4 produces the remainder of 7/4, so it evaluates to 3. 21

Circumference and Area of Circle We want to write a program to compute the circumference and area of a circle. What do the the circumference and area of a circle depend on? 22

Circumference and Area of Circle We want to write a program to compute the circumference and area of a circle. What do the the circumference and area of a circle depend on? The radius of the circule. circumference = 2 * pi * radius area = pi * radius 2 23

Circumference and Area of Circle Suppose we have a circle with radius = 20.231. Computing the circumference of the circle: Circumference = 2 * pi * radius Code? Computing the area of the circle: area = pi * radius 2 Code? 24

Circumference and Area of Circle Suppose we have a circle with radius = 20.231. Computing the circumference of the circle: Circumference = 2 * pi * radius System.out.println(2 * Math.PI * 20.231); Output: 127.11512194955021 Computing the area of the circle: area = pi * radius 2 System.out.println(Math.PI * Math.pow(20.231, 2)); Output: 1285.8330160806754 25

Circumference and Area of Circle Suppose we have a circle with radius = 20.231. Program: public class hello1 { System.out.println(2 * Math.PI * 20.231); System.out.println(Math.PI * Math.pow(20.231, 2)); Is this a good program to sell to a user? 26

Circumference and Area of Circle Suppose we have a circle with radius = 20.231. Program: public class hello1 { System.out.println(2 * Math.PI * 20.231); System.out.println(Math.PI * Math.pow(20.231, 2)); Is this a good program to sell to a user? No: the only way for the user to use this program is to modify the code every time, to specify the radius. That is bad. Users should not need to be programmers. 27

Circumference and Area of Circle Suppose we have a circle with radius = 20.231. Program: public class hello1 { System.out.println(2 * Math.PI * 20.231); System.out.println(Math.PI * Math.pow(20.231, 2)); Any other issues/problems with this program? 28

Circumference and Area of Circle Suppose we have a circle with radius = 20.231. Program: public class hello1 { System.out.println(2 * Math.PI * 20.231); System.out.println(Math.PI * Math.pow(20.231, 2)); Any other issues/problems with this program? The radius is specified TWICE. This is bad practice, introduces the risk of errors. Also, more painful to change the radius, we must change it in two places. 29

Circumference and Area of Circle Suppose we have a circle with radius = 20.231. Program: public class hello1 { System.out.println(2 * Math.PI * 20.231); System.out.println(Math.PI * Math.pow(20.231, 2)); Any other issues/problems with this program? The program is hard to read and understand. If you show it to a programmer, is it clear what the program is supposed to be doing? The output is just numbers, not very user-friendly. 30

Using Variables public class hello1 { double radius = 20.231; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); This code has the same output as the previous version. However: The radius is specified only once (better than specifying twice). If you show this program to any programmer, it is fairly obvious what it does (easy to read). 31

Declaring a Variable At any point, you can create a variable, by doing a variable declaration. Syntax for variable declaration: type variable_name = initial_value; For example: int x = 123; int number_of_fingers = 5; double radius = 20.231; 32

Types There are many different types in Java. However, initially, you just need to know these two: double int You need to think carefully, and use the correct type for your variable. For integers (positive and negative), use int. For floating point numbers, use double. 33

Variable Names The textbook describes the rules for variable names. Here is a simplified version: variable names should start with a letter (upper or lower case). variable names should only include letters, numbers, and underscores. variable names are case-sensitive. variable names cannot be equal to reserved words, such as double, class, int, public, 34

Using Variables After you declare a variable, you can use it in the rest of the code: You can use its value. You can change its value. This is called assignment. public class hello1 { int candies = 5; System.out.println(candies); candies = 7; System.out.println(candies); candies = candies + 10; System.out.println(candies); Output: 35

Using Variables After you declare a variable, you can use it in the rest of the code: You can use its value. You can change its value. This is called assignment. public class hello1 { int candies = 5; System.out.println(candies); candies = 7; System.out.println(candies); candies = candies + 10; System.out.println(candies); Output: 5 7 17 36

Declarations and Assignments In this program: Which lines of code are declarations? Which lines of code are assignments? public class hello1 { int candies = 5; System.out.println(candies); candies = 7; System.out.println(candies); candies = candies + 10; System.out.println(candies); Output: 5 7 17 37

Declarations and Assignments In this program: Which lines of code are declarations? int candies = 5; Which lines of code are assignments? candies = 7; candies = candies + 10; public class hello1 { int candies = 5; System.out.println(candies); candies = 7; System.out.println(candies); candies = candies + 10; System.out.println(candies); Output: 5 7 17 38

Returning to the Circles Program Version with variables: public class hello1 { double radius = 20.231; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); Which lines are declarations? Which lines are assignments? 39

Returning to the Circles Program Version with variables: public class hello1 { double radius = 20.231; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); Which lines are declarations? Shown in red. Which lines are assignments? None. 40

Returning to the Circles Program Version with variables: public class hello1 { double radius = 20.231; double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); Problem: the radius is hardcoded. Why is this a problem? 41

Problem: Radius is Hardcoded Why is this a problem? Biggest reason: the user needs to be a programmer. You cannot use this program without changing the program. 42

Solution Allow the user to enter the radius value as input. 43

import java.util.scanner; public class hello1 { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextdouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); Revised Program with User Input There are several new things here: the import line. The Scanner object. The System.out.printf method. 44

import java.util.scanner; public class hello1 { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextdouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); The Scanner object allows us to obtain user input. To create a Scanner object, we need to: Put the import statement at the top of the Java file. Create a Scanner object, as shown in the first line of the main method: Scanner in = new Scanner(System.in); 45

import java.util.scanner; public class hello1 { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextdouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); The System.out.printf method is a more powerful version of the System.out.println method. We will see more details in a few days. One difference is that System.out.println always prints a new line at the end, whereas System.out.printf does not. 46

println and printf public class hello1 { public static void main(string[] args) { System.out.println("hello"); System.out.printf("hello\n"); These two lines do the exact same thing: System.out.println("hello"); System.out.printf("hello\n"); 47

Another Example: Converting Weeks to Days import java.util.scanner; public class example1 { Scanner in = new Scanner(System.in); System.out.printf("Please enter number of weeks: "); int weeks = in.nextint(); int days = weeks * 7; System.out.printf("There are %d days in %d weeks\n", days, weeks); 48

Another Example: Converting Weeks to Days import java.util.scanner; public class example1 { Scanner in = new Scanner(System.in); System.out.printf("Please enter number of weeks: "); int weeks = in.nextint(); int days = weeks * 7; System.out.printf("There are %d days in %d weeks\n", days, weeks); 49

Another Example: Computing the Average of Three Numbers import java.util.scanner; public class example1 { Scanner in = new Scanner(System.in); System.out.printf("Please enter the first number: "); double n1 = in.nextdouble(); System.out.printf("Please enter the second number: "); double n2 = in.nextdouble(); System.out.printf("Please enter the third number: "); double n3 = in.nextdouble(); double average = (n1 + n2 + n3) / 3.0; System.out.printf("The average is %.2f\n", average); 50

Another Example: Computing Gravity import java.util.scanner; public class example1 { Scanner in = new Scanner(System.in); System.out.printf("Please enter the first mass: "); double m1 = in.nextdouble(); System.out.printf("Please enter the second mass: "); double m2 = in.nextdouble(); System.out.printf("Please enter the radius: "); double r = in.nextdouble(); double G = 6.694E-11; double gravity = G * m1 * m2 / (r * r); System.out.printf("The gravity force is %f\n", gravity); 51

Comments /* A program that converts weeks into days. Written on 7/15/2015. */ import java.util.scanner; public class example1 { Scanner in = new Scanner(System.in); System.out.printf("Enter number of weeks: "); int weeks = in.nextint(); // Here is where we convert weeks into days. int days = weeks * 7; System.out.printf("Result: %d days\n", days); Comments allow you to make notes on the program for yourself, and for other people reading your code. Comments are ignored by Java. Single line comments: they start with // (see line in green above) Multiple-line comments: they start with /*, end with */ (see lines in red) 52

import java.util.scanner; Comments public class example1 { Scanner in = new Scanner(System.in); // Create scanner object. System.out.printf("Enter number of weeks: "); int weeks = in.nextint(); // Get user input int days = weeks * 7; // Converting weeks into days. System.out.printf("Result: %d days\n", days); Comments starting with // can be placed at the end of a line (see code marked in red) 53

Some Guidelines To learn how to code, you need PRACTICE. What will usually not work: Listen to the lectures. Go and try to do the assignments. What will usually work: Listen to the lectures and KEEP NOTES. Actually run every piece of code that we do in class. Understand every line of every piece of code we do in class. Think of variations of what we do in class, and try them. Predict what the variation will do, and verify by running it. Then try the assignments. 54

Some Guidelines You need to understand the terminology: method, string, double, ints, main class name, numerical expression, variable, declaration, assignment, newline character You will encounter many terms in this course. YOU NEED TO LEARN EXACTLY WHAT THEY MEAN. DO NOT RELY ON ENGLISH. These terms have meanings in conversational English that are only vaguely related with their meaning in programming. 55