Terms: Software  Set of instructions  aka programs  Operating System:  Special software whose job it is to translateinstructions into hardware instructions.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Programming G51PRG University of Nottingham Revision 1
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
Three types of computer languages
Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
String Escape Sequences
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Why Java? Free! Lots of support and use – Google, Android, Oracle – Java has been around for over 20 years And is only getting more popular! Platform Independent.
CIS Computer Programming Logic
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DAT602 Database Application Development Lecture 5 JAVA Review.
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2 Elementary Programming
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Introduction to Java Java Translation Program Structure
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
By Mr. Muhammad Pervez Akhtar
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Lecture1 Instructor: Amal Hussain ALshardy. Introduce students to the basics of writing software programs including variables, types, arrays, control.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Lecture 5: Some more Java!
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Computer Programming Methodology Introduction to Java
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Unit 3: Variables in Java
Building Java Programs Chapter 2
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

Terms: Software  Set of instructions  aka programs  Operating System:  Special software whose job it is to translateinstructions into hardware instructions Programs are written using programming languages. 1

Programming Languages Machine Language Assembly Language High-Level Language 2 Machine language a set of primitive instructions built into every computer binary code programming with native machine language is: tedious, error prone, highly difficult to read and modify For example, to add two numbers, you might write an instruction in binary like this:

Programming Languages Machine Language Assembly Language High-Level Language 3 Assembly Language designed to make programming “easier” “readable” text representation of machine language an assembler converts assembly language to machine language For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3

Programming Languages Machine Language Assembly Language High-Level Language 4 High-Level Language English-like and much easier to program “readable” text representation of machine language We must convert the high-level language to a language closer to machine language –Compilers Java uses a compiler –Interpreters

Programming Languages Machine Language Assembly Language High-Level Language 5 Compiler a compiler translates high-level language to an intermediate level –Could be: “object code” created to allow linking to libraries –Could be: assembly language Java compilers translate to a cross-platform assembly language. Each computer then has a custom Java Virtual Machine program that interprets the code at run-time and issues machine instructions.

Why Java?  Free!  Lots of support and use  Google, Android, Oracle  Java has been around for over 20 years  And is only getting more popular!  Platform Independent  OOP  One of the few almost completely oop languages  Powerful development environment  Eclipse “James Gosling invented Java, Bjarne Stroustroup invented C++, Dennis Ritchie invented C, and Bill Gates invented nothing. Yet, who’s the richest of the bunch?”

Start writing Java Code: EVERYTHING must be in a class To start: 1.Create a new file with the same name as the class you are going to create (and the.java extension) (e.g., NewClass.java) 2.Inside the new file, create the class: public class NewClass { } 3.Inside the class, you can place a method called main that will run automatically when you run the file: public class NewClass { public static void main(String[] args) { …Code for main function goes here } NOTE! With Java, every program must have a main function (and only one main function). This is where your code starts running from!

Two big syntax differences  Java uses the C/C++ code block syntax  { and } around code that goes together, e.g., if (x == 3) { Code that happens when x is equal to 3 goes here }  Java has type declaration as part of the syntax  You must tell the compiler what type your variables and parameters are  The compiler sets aside space in memory ahead of time. It must know how much space to set aside.

9 Blocks braces must go around blocks of code A semicolon separates each line of code. Python the colon (:) and indentation create blocks newlines separate components Racket parens () define blocks. Special functions over lists define a sequence of program components, (begin...), (local...)

Types are included on variable and parameter declaration public class Test { public static void main(String[] args) { int a = ; System.out.println(a); } Types type

11 Numerical Data Types

12 Numeric Operators

A simple program Write a function that computes the area of a circle:

14 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } no value radius allocate memory for radius

15 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } 20 radius no value area assign 20 to radius

16 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } 20 radius memory area compute area and assign it to variable area

17 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * ; //alternative: Math.pow(radius,2) * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } 20 radius memory area print a message to the console animation

Types  Java compiler tries to be efficient.  It sets aside space in memory ahead of time for parameters, return values, variables.  For every variable, parameter, and return value, you MUST specify the type!  E.g., public static int functionname(double x, String y) { … }

19 Character Data Type char letter = 'A'; // a new type – for a single character // note the single quotes!!! Also (we rarely use…): char numChar = '4'; char uletter = '\u0041'; (Unicode) //4 hex numbers make a char. char unumChar = '\u0034'; (Unicode) Description Escape Sequence Unicode Tab \t\u0009 Linefeed \n\u000A Carriage return \r\u000D Backslash \\\u005C Single Quote \ ' \u0027 Double Quote \ " \u0022

20 The String Type String message = "Welcome to Java";  Strings are just an array of chars  We’ll talk about arrays later,  +  “operator overloaded”  Numbers get added  Strings get concatenated  “adding” non-String types to an existing String will invoke the toString method that converts the non-String type to a String String s = “5” + 6; // s now has the String value “56” String r = “five” + “six”; // r now has the String value “fivesix”

Branching

22 The boolean Type Boolean Values are: true and false boolean b = (1 > 2); // b is the value false boolean even = (number%2 == 0);boolean even; if (number%2 == 0) { even = true; } else { even = false; }

23 TIP if (even == true) { System.out.println(“It is even.”); } Same as: if (even) { System.out.println(“It is even.”); }

24 Logical Operators Java Name ! not && and || or ^ exclusive or

What do we get? boolean b = 3 == 2*4; boolean d = !b; if (d) { System.out.println("snow day"); } else { System.out.println("Just rain"); }

26 Conditional Operator if (x > 0) { y = 1; } else { y = -1; } is equivalent to int y = (x > 0) ? 1 : -1; (boolean-expression) ? expression1 : expression2 y = (true)? then y becomes expression1 y = (false)? then y becomes expression 2

int k = 7; int m = 5; String y= ((21%k!=0) || (21%m!=0)) ? "Echidnas":"Wombats"; System.out.println(y);

Types in Java: Assume that int a = 1 and double d = 1.0. What is the result of the following expression? a = 46 / 9; A.46 B.6 C.5 D.0

Java: Assume that int a = 1 and double d = 1.0. What is the result of the following expression? a = 46 % * 4 - 2; A.8 B.0 C.19 D.15

Java: Assume that int a = 1 and double d = 1.0. What is the result of the following expression? a = 46 / * 4 - 2; A.8 B.0 C.19 D.15

Types in Java: Assume that int a = 1 and double d = 1.0. What is the result of the following expression? d = 1.5 * 3 + a; A.5.5 B.5 C.6 D.6.0

Java Methods Methods – like functions but inside of classes. Because everything must be in a class in Java, everything is technically a method E.g., public static int max(int x, int y) { int result; if (x > y) { result = x; } else { result = y; } return result; } //Call method public static void main(String[] args) { System.out.println(max(3,7)); }

public static int max(int x, int y) Method header:  public – available to classes other than the one it’s in.  static – don’t need to create an instance of the class to access the method (static- sticks around)  we’ll discuss these more later when we start using classes  int max – this function returns a value that is of the type int  If a function doesn’t return a value, then the type returned is void, e.g., public static void NoReturn(int x) { }  The function’s name is max  (int x, int y) – this method has 2 parameters, x, and y, and both x and y are of type int.

int result – creating a variable called result, and it is of type int. (setting aside an int’s worth of memory space for the variable result) return result – the value that is inside of the variable result is what comes out of the function and is what can be used by what called the function. public static int max(int x, int y) { int result; if (x > y) { result = x; } else { result = y; } return result; } Or public static int max2(int x, int y){ int result = (x > y) ? x : y; return result; }

 max(3,7) – 3 and 7 are the parameter values – 3 goes into x and 7 goes into y  System.out.println(max(3,7)); – prints out the value that is returned from the method max, called with the values 3 and 7.  int retvalue;  retvalue = max(3,7); - we know the value being returned from the method max is an int (because that’s how we defined the max method). So we can create a variable that is of type int to hold the value returned from max. //Call method public static void main(String[] args) { System.out.println(max(3,7)); } – Or, as an alternative: //Call method public static void main(String[] args) { int retvalue; retvalue = max(3,7); System.out.println(retvalue); }

Looping (Iteration)  repeating a process  usually toward a stopping condition  In a program this is typically accomplished by a  loop counting variable,  stopping condition  and then the loop code  Code that happens again and again

Sum numbers from 1 to 10 (Python) def sum_accum(i, limit): total = 0 while (i < limit): total = total + i i = i + 1 return total print (sum_accum(1, 10)) Loop code Loop counting variable stopping condition

Sum numbers from 1 to 10 (Racket) (define (sum-accum i limit total) (cond [(>= i limit) total] [else (sum-accum (+ i 1) limit (+ total i))])) (display (sum-accum )) loop Loop counting variable stopping condition

Sum numbers from 1 to 10 (Java) public static int sumAccum(int i, int limit) { int total = 0; while (i < limit) { total = total + i; i = i + 1; } return total; } public static void main(String[] args){ System.out.println(sumAccum(1,10)); } Loop code Loop counting variable stopping condition

Indefinite Loops  A repetition structure that executes code while a condition is true  We don’t know ahead of time how many times the loop will happen import java.util.Random; // at top of file... int guess = 1; Random r = new Random(); //must make an object of type random while (guess % 7 != 0) { guess = r.nextInt(100); //gets a random int between 0 and 100 } System.out.println(guess);