An intro to programming The basic nitty-gritty that’ll make you scratch your head and say, “This is programming?”

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
 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 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
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.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
Classes, methods, and conditional statements We’re past the basics. These are the roots.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
CS107 Introduction to Computer Science Java Basics.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Programming.
CS107 Introduction to Computer Science Java Basics.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Welcome to AP Computer Science A We use the Java language, but this class is much more rigorous than Intro to Java More programming, but also more theory.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Midterm preview.
Building Java Programs
Console Output, Variables, Literals, and Introduction to Type
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Computer Programming Methodology Introduction to Java
Chapter 2.
Building Java Programs
Building Java Programs
Chapter 3 – Introduction to C# Programming
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Building Java Programs
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

An intro to programming The basic nitty-gritty that’ll make you scratch your head and say, “This is programming?”

What is a program? A program is computer coding that: Takes input Performs some calculation on the input Displays output

A program is a function! Both take inputs and give outputs based on some “rule” Both make calculations Both take “arguments”

A program is a NOT function! In a function, there is exactly one output for every input Some computer programs will have different outputs for the same input Example: a virtual set of dice

The client The user of the program is sometimes called the client The client doesn’t have to know what’s going on inside the “black box” to use the program Do you know exactly how Microsoft Word works?

Using outside sources Many programs must consult outside sources to perform the correct calculations on their input

Writing programs You need: Knowledge of a coding language A compiler – translates your language into machine code (0s and 1s the computer can understand)

What is pseudocode? Pseudocode is programmers’ shorthand for actual code It mimics actual programming languages There are no “rules” of pseudocode…yet

A simple pseudocode program program DogYears; write “How old are you?”; let humanAge = INPUT; let dogAge = humanAge * 7; write humanAge; end program; INPUT  CALCULATION BASED ON INPUT  OUTPUT  MISCELLANEOUS OUTPUT  PROGRAM CONTROL STATEMENT  A program that 1) asks the user’s age in human years, 2) calculates the user’s age in dog years, and 3) tells the user what that age is.

A few good pseudocode “rules” The name of the program begins with a capital letter Each line ends in a semicolon; All variable names begin in lowercase We use an asterisk (*) to represent multiplication (also use +, -, /, ^) program DogYears; write “How old are you?”; let humanAge = INPUT; let dogAge = humanAge * 7; write humanAge; end program;

Write your own pseudocode program Write a program in pseudocode that: 1.Asks the client what grade they just finished 2.Calculates how many years of high school the client has remaining. 3.Outputs this answer.

A sample program program Graduation; write “What grade did you just complete?”; let lastGrade = INPUT; let yearsLeft = 12 – lastGrade; write lastGrade; end program;

Three parts of programs Variables, to store data Calculation statements, to handle input, calculations, and output A user interface, which allows the client to use the program

Variables – primitive types Different types depending on the kind of data In Java, some types include: boolean for true or false int for integers char for letters double for real numbers string for strings These are called primitive types because they are built into the language

Variables - objects In some languages, including Java, users can define their own variable types, called objects An object is a collection of primitive types Example: an object representing a cat might contain a double variable for its weight, a string variable for its name, and a boolean variable for if it’s been neutered or spayed

Variables - objects In some languages, including Java, users can define their own variable types, called objects An object is a collection of primitive types Example: a string is an object that holds many char variables, so a string is any set of letters: a word or a sentence

Java Java was released in 1995 by Sun Microsystems. It is based on the language C++. Java is platform-independent, object-oriented, and easy to use on the Internet.

import extra.*; public class Graduation { public static void main(String args[]) { Std.out.println( "What grade did you just complete?" ); int lastGrade; lastGrade = Std.in.readInt(); int yearsLeft; yearsLeft = 12 - lastGrade; Std.out.println( "You have " + yearsLeft + " years of high school left."); } Graduation in Java Java blather that’s meaningless for now Write this: Make a new variable of type int and call it “lastGrade” Assign to “lastGrade” the value that the user inputs…hope it’s an integer Make a new variable of type int and call it “yearsLeft” Use calculations to assign “lastGrade” a value based on “yearsLeft”

Declaring variables We create variables for our program to use by declaring them Declaring a variable is like saying, “Computer, save some space in RAM for this variable” Declaring is done ONCE before using the variable a variable does NOT assign a value to the variable

How to declare a variable Write the variable’s type Leave a space Write the variable’s name (start with a lowercase letter, it’s good style) Examples: char faveLetter; int days; double foodMass; string name;

How to assign a value to a variable Write the variable’s name Write a single equals sign (=) Use single quotes for char, double quotes for string Write the variable’s value and a semicolon Examples: faveLetter = ‘q’; days = 42; foodMass = 33.42; name = “Matthew”;

Declaring and assigning at the same time You can, if you choose, declare variables and assign values to them at the same time Write the variable’s type, then its name, and then its value Examples: char faveLetter = ‘q’; int days = 42; double foodMass = 33.42; string name = “Matthew”;

Input: the easy way Because input in Java is not easy, we use a package called extra that assists with input. To input using extra : Declare a variable Assign an input statement as its value The input statement should begin with Std.in. readInt() reads an integer readChar() reads a character readLine() reads a string etc…

Output: always easy To output, write Std.out.println(); In the parens, write a variable name, a string, or any combination of variables and strings connected with plus (+) signs

Input and output examples int days; Days = Std.in.readInt(); String name; Std.out.println(“What is your name?) Name = Std.in.readLine();

An exercise Write a program that: Calculates the number a miles has traveled during the last year Calculates the car’s average miles/gallon You should ask the client for the average number of miles per day s/he drives and the total gallons of gas s/he uses in one year

import extra.*; public class Miles { public static void main(String args[]) { Std.out.println( “How many miles per day do you drive?" ); double milesPerDay; milesPerDay = Std.in.readDouble(); Std.out.println( “How many gallons of gas did you use last year?” ); int gallonsUsed; gallonsUsed = Std.in.readDouble(); double totalMiles = milesPerDay * 365; double avgMPG = totalMiles / gallonsUsed; Std.out.println( "You drove “ + totalMiles + “ miles last year and averaged “ + “ avgMPG + “ miles per gallon.”); } A solution

Another exercise Write a program that: Calculates the amount of change you should receive when you go shopping and the type of bills you should receive it in You should ask the client for the price of the article and the amount of cash s/he actually paid