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

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

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

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

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

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

COMP Computer Basics. Yi Hong May 13, 2015

Introduction to Computers and Java

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Chapter 1 Lab Algorithms, Errors, and Testing

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Fundamentals of Programming. By Budditha Hettige

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

St. Edmund Preparatory High School Brooklyn, NY

Introduction. Lecture 1 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

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

Input. Scanner keyboard = new Scanner(System.in); String name;

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

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

Anatomy of a Java Program: Comments

Midterm Examination (MTA)

Over and Over Again GEEN163

Lesson 01 Introduction

Introduction to Java Applications

Basic Computation. Chapter 2

Introduction to Computers and Java

Oct Decision Structures cont d

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

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Programming with Java

Java Coding 3. Over & over again!

CHAPTER 1 Introduction to Computers and Java

Controls Structure for Repetition

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

COMP 110 Project 1 Programming Project Warm-Up Exercise

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

Welcome to the Primitives and Expressions Lab!

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Computers and Java

Primitive Data, Variables, and Expressions; Simple Conditional Execution

CS 177 Recitation. Week 1 Intro to Java

CMSC131. Introduction to your Introduction to Java. Why Java?

Introduction to Java Unit 1. Using BlueJ to Write Programs

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

What did we talk about last time? Examples switch statements

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

Loops. CSE 114, Computer Science 1 Stony Brook University

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

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

Basic Computation. Chapter 2

ITERATION WEEK 4: EXMAPLES IN CLASS

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

CS 231 Data Structures and Algorithms, Fall 2016

Lecture Set 2: Starting Java

Introduction to Computer Science Unit 2. Notes

Lecture Set 2: Starting Java

Introduction to Computers and Java

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

CSE 1223: Exam II Autumn 2016

Introduction to Computer Programming

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

Java Classes: Math, Integer A C S L E C T U R E 8

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

Full file at

Darrell Bethea May 25, 2011

CHAPTER INTRODUCTION

AP Computer Science Unit 1. Programs

A+ Computer Science -

CSE 114 Computer Science I

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Java Basics Lecture: January 26, 2012 (On-line Lecture 1)

Week 2: Data and Output

CSIS 10A Assignment 4 SOLUTIONS

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer 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.

Lecture 8 " INPUT " Instructor: Craig Duckett

A Quick and Dirty Overview of Java and. Java Programming

Professor Yedidyah Langsam

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Chapter 5 Lab Methods

Objectives. Primitive Types, Strings, and Console I/O. Variables and Values. Outline. Naming and Declaring Variables. Variables and Values

INFO Object-Oriented Programming

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

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Full file at

II. Compiling and launching from Command-Line, IDE A simple JAVA program

Elementary Programming

Code Ninjas: Introduction to Computer Science. Macomb Science Olympiad Presented by Swati Dharia

CEN 414 Java Programming

CSE Module 1. A Programming Primer 1/23/19 CSE 1321 MODULE 1 1

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

Introduction to Java Applications; Input/Output and Operators

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Transcription:

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

Computer Basics Computer system: hardware + software Hardware: the physical components Software: the instructions that tell the hardware what to do 2

Common Hardware Components Processor (CPU) Central Processing Unit Interprets and executes the instructions Memory main & auxiliary holds data and instructions Input device(s) mouse, keyboard, etc. Output device(s) video display, printer, etc. CPU and memory are physically housed together Input Devices (such as mouse and keyboard) Standard Hardware Organization Memory (main & auxiliary) Processor (CPU) Output Devices (such as video display or printer) 3

Running a Program Program a set of instructions for a computer to follow Program Data (input for the program) Computer Output 4

Programming Languages Needed to write software High-level languages (e.g., Java) relatively easy for people to write and understand not directly understood by computers Low-level languages (machine language) directly understood by computer computer-dependent 5

The Compiler A program written in a high-level language (know as the source code) cannot be executed directly by the computer A compiler is a program that translates source code into machine code that does the same thing (known as the object code) 6

Java Program Translation and Execution Input to Java Program Java Source Code Java Compiler Java Byte-Code Java Virtual Machine Output of Java Program 7

Java Translation/Execution cont. Java byte-code is portable (hardwareindependent) The Java Virtual Machine (JVM) executes Java byte-code on a real machine The java compiler is called javac The JVM emulator is called java 8

Algorithmic Thinking Algorithm - a set of instructions (steps) for solving a problem. must be precise must be complete can be written in an arbitrary notation (e.g., natural language, programming language, diagram, mix, etc.) Algorithmic thinking is fundamental to computer science and programming 9

Example of an Algorithm Algorithm that determines the total cost of a list of items: 1. Write the number 0 on the blackboard. 2. Do the following for each item on the list: a. Add the cost of the item to the number on the blackboard. b. Replace the old number on the board by this sum. 3. Announce that the answer is the number written on the board. 10

First Java Program public class FirstProgram public static void main(string[] args) System.out.println("Hello out there."); System.out.println("How s it going?"); System.out.println("Good-bye."); 11

Language Syntax Syntax of a language is a set of (grammar) rules that describe the correct way to write sentences (programs) in the language. Programming languages have a very precise syntax: If you break the rules, you ll get one (or more) errors. 12

Structure of a Java Program // import needed libraries public class ProgramName public static void main(string[] args) // statements go here to describe // actions to be taken by the program 13

A Java Statement System.out.println( some message here ); Outputs the message in quotes to the screen (without the quotes) 14

What Does FirstProgram Do? Take a look at the program and see if you can figure out what the program does. How do we do this? We trace the program In other words go line by line through the program and ask: What does this line do? 15

First Java Program - Trace Program Line System.out.println("Hello out there."); System.out.println("How s it going?"); System.out.println("Good-bye."); What we see Hello out there. Hello out there. How's it going? Hello out there. How's it going? Goodbye. 16

What Does FirstProgram Do? So based on our trace, what does it do? It outputs the following: Hello out there. How s it going? Good-bye. 17

Second Java Program import java.util.scanner; public class EggBasket public static void main(string[] args) int numberofbaskets=2; int eggsperbasket=12; int totaleggs = numberofbaskets * eggsperbasket; System.out.println("Total number of eggs is "+ totaleggs); 18

What Does EggBasket Do? Take a look at the program and see if you can figure out what it does. Note that unlike our first program, not everything that EggBasket does is done by displaying things to the screen It does some math as well as displaying things 19

Tracing EggBasket To trace EggBasket we need to think about what is going on in the computer We call the internal configuration of the program the program state more on this later For now, let s see how we ll trace the program 20

EggBasket - Trace Program Line int numberofbaskets=2; Program state numberofbaskets = 2 int eggsperbasket=12; int totaleggs = numberofbaskets*eggsperbasket; numberofbaskets = 2 eggsperbasket = 12 numberofbaskets = 2 eggsperbasket = 12 totaleggs = 24 System.out.println("Total eggs: "+ totaleggs); numberofbaskets = 2 eggsperbasket = 12 totaleggs = 24 Outputs to screen: [Total eggs: 24] 21

Programming Errors Syntax errors violation of language s syntax rules, e.g., misspelling a word, forgetting a ;, etc. Caught by the compiler! Runtime errors execution errors, e.g., division by zero. Logical errors the program compiles and runs without runtime errors, but it does not do what it is supposed to. 22

Programming Errors import java.util.scanner; public class EggBasket public static void main(string[] args) int numberofbaskets=2; int eggsperbasket=12; nit totaleggs = numberofbaskets * eggsperbasket; System.out.println("Total number of eggs is "+ totaleggs); Syntax Error! Should be int 23

Programming Errors import java.util.scanner; public class EggBasket public static void main(string[] args) int numberofbaskets=2; int eggsperbasket=0; int totaleggs = numberofbaskets / eggsperbasket; System.out.println("Total number of eggs is "+ totaleggs); Runtime Error! Attempt to divide by zero 24

Programming Errors import java.util.scanner; public class EggBasket public static void main(string[] args) int numberofbaskets=2; int eggsperbasket=12; int totaleggs = numberofbaskets + eggsperbasket; System.out.println("Total number of eggs is "+ totaleggs); Logical Error! To find total number of eggs in all baskets, we need to multiply # of baskets by the eggs per basket, not add the two together! 25

Another Java Program import java.util.scanner; public class EggBasketEnhanced public static void main(string[] args) Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); String eggsstr = keyboard.nextline(); int eggsperbasket = Integer.parseInt(eggsStr); System.out.print("Enter the number of baskets: "); String basketstr = keyboard.nextline(); int numberofbaskets = Integer.parseInt(basketStr); int totaleggs = numberofbaskets * eggsperbasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totaleggs); 26

What Does EggBasketEnhanced Do? Take a look at the program and see if you can figure out what it does. There are some things in here we haven t talked about yet. Can you guess what they re doing based on their names and how we re using them? Can you trace it? 27