ITI1120 Introduction to Computing I Exercise Workbook Fall 2012

Similar documents
ITI1120 Introduction to Computing I Exercise Workbook Fall 2012

Section 2: Introduction to Java. Historical note

Programming with Java

Peer Instruction 1. Elementary Programming

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

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

Introduction to Programming Using Java (98-388)

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

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

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

1 Short Answer (10 Points Each)

Practice Midterm 1. Problem Points Score TOTAL 50

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Datatypes, Variables, and Operations

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS111: PROGRAMMING LANGUAGE II

CSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Simple Java Programming Constructs 4

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Course Outline. Introduction to java

Full file at

5) (4 points) What is the value of the boolean variable equals after the following statement?

Full file at

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Methods CSC 121 Spring 2017 Howard Rosenthal

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

c) And last but not least, there are javadoc comments. See Weiss.

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal

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

CSCI 355 Lab #2 Spring 2007

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

5. What is a block statement? A block statement is a segment of code between {}.

Computer Programming : C++

Introduction to Java Applications

Simple Java Reference

Chapter 2: Basic Elements of Java

I. Variables and Data Type week 3

Practice Midterm 1 Answer Key

1 Short Answer (5 Points Each)

Welcome to the Primitives and Expressions Lab!

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

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

Object-Oriented Programming

Software Practice 1 Basic Grammar

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Chapter 2: Data and Expressions

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

CSCI 355 LAB #2 Spring 2004

4. Java language basics: Function. Minhaeng Lee

Darrell Bethea May 25, 2011

CS111: PROGRAMMING LANGUAGE II

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Basic Programming Elements

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

Fundamentals of Programming Data Types & Methods

Computer Components. Software{ User Programs. Operating System. Hardware

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

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

5. What is a block statement? A block statement is a segment of code between {}.

Arithmetic and IO. 25 August 2017

Coding Standards for Java

Fundamentals of Programming CS-110. Lecture 2

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CSCE 145 Exam 1 Review. This exam totals to 100 points. Follow the instructions. Good luck!

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Chapter 9 Lab Text Processing and Wrapper Classes

Introduction to Computer Science Unit 2. Notes

Data Types and the while Statement

AP Computer Science A

CompSci 125 Lecture 11

3. Java - Language Constructs I

Chapter 3: Operators, Expressions and Type Conversion

Robots. Byron Weber Becker. chapter 6

Instructor: Eng.Omar Al-Nahal

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

CS 231 Data Structures and Algorithms, Fall 2016

A+ Computer Science -

The Warhol Language Reference Manual

STUDENT LESSON A12 Iterations

Fall 2017 CISC124 9/16/2017

What did we talk about last time? Examples switch statements

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Language Reference Manual

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

Introduction to Programming EC-105. Lecture 2

Basic Computation. Chapter 2

array Indexed same type

Transcription:

ITI1120 Introduction to Computing I Exercise Workbook Fall 2012 1

Table of Contents 1 Introduction 3 2 Computer Programming Model 3 3 Pre-defined Algorithm Models 6 4 Summary of Program Structure, Algorithm Concepts and Java Programming Concepts 7 5 Section 1 Exercises 8 6 Section 2 Exercises 12 7 Section 3 Exercises 14 8 Section 4 Exercises 17 9 Section 5 Exercises 26 10 Section 6 Exercises 35 11 Section 7 Exercises 55 12 Section 8 Exercises 63 13 Section 9 Exercises 73 14 Section 10 Exercises 83 15 Section 11 Exercises 87 2

1 Introduction This work book contains the exercises that shall be studied in class Please bring it to all classes The front matter (sections 1 to 4) of the workbook provides some background information that can be useful in completing the exercises as well as help in completing assignments It includes: Computer Programming Model The programming model is a representation of the main computer components that are used to execute programs The model is useful in understanding how a program is executed It is used with most of the exercises in the work book The model is briefly described in Section 2 This section also gives two blank programming model pages that can be used for your studying and assignments Pre-defined Algorithm Models A number of pre-defined standard algorithm models are available for developing algorithms They represent standard functions available in a computer system such as reading from the keyboard and writing to the terminal console window Program Structure, Algorithm and Java Programming Concepts This section provides a table that summarizes many of the programming structure and concepts studied in class and during the labs The table can serve as a quick reference for the important representation of algorithm concepts and Java programming concepts/details 2 Computer Programming Model The two main components used in executing a computer program are the computer s main memory and the central processing unit (CPU) The computer memory contains the program in the form of machine instructions these machine instructions are typically created from higher level languages such as C, Java (using compilers more on this in class) The machine instructions operate on variables, which are also located in the computers memory Essentially the CPU reads values from and writes values to the variables (ie the locations in memory where variables are located) Operations such as addition, incrementing, testing, are all typically done within the CPU Computer memory consist of a sequence of bytes (8 bit values; a bit is a digit that can assume the value 0 or 1 and a byte contains 8 bits) Thus a computer that contains 3 GigaBytes of memory contains 3 x 1024 x 1024 x 1024 bytes, that is, over 3 billion bytes Each of these bytes has an address An address is simply a number that points to each byte in memory Thus when the CPU wants to execute an instruction it reads the instruction using its address (the CPU actually keeps track of the address of instructions to execute them in sequence) Instructions contain the addresses of the variables to manipulate them Memory is divided into regions part of the memory will contain the program to execute (the machine instructions), part of the memory contains variables used for executing the sub-programs of a program (such memory is retrieved after the execution of a sub-program and re-used during the execution of other sub-programs), and part of the memory is used to store variables and other data structures that are available during the whole execution of a program (for example, for storing arrays and objects) The program model represents each of these memory parts as the program memory, the working memory, and the global memory respectively Note that the working memory is divided into pieces to illustrate how working memory is reserved for the execution of different sub-programs The following two pages provide blank working models so that you may use them for studying and completing assignments The first page does not include global memory and can be used during most of the first half of the course since global memory is used when arrays are introduced 3

Program Memory Working memory CPU 4

Program Memory Working memory Global Memory CPU 5

3 Pre-defined Algorithm Models The following algorithm models can be used for developing algorithms Most of the algorithms represent input and output for interacting with the user The last three algorithms represent other tasks supported by the computer (or language) printline(<argument list>) Prints on the terminal screen the contents of the argument list The argument list consists of a comma separated list of items that can be a string (eg a string ) or a variable name After the output, the cursor on the terminal is placed on the next line print(<argument list>) Prints on the terminal screen the contents of the argument list The argument list consists of a comma separated list of items that can be a string (eg a string ) or a variable name After the output, the cursor remains after the last character printed intvar readinteger() Reads an integer number from the keyboard numvar readreal() Reads a real number from the keyboard charvar readcharacter() Reads a single character from the keyboard boolvar readboolean() Reads or and stores TRUE or FALSE; it gives a Boolean value as a result (intarray, n) readintline Reads a line of input, extracts the integers from the line and adds them into a integer array (note that intarray is a reference to the array) The variable n represents the number of elements in the array (realarray, n) readrealline Reads a line of input, extracts the real values from the line and adds them into a real array (note that realarray is a reference to the array) The variable n represents the number of elements in the array (chararray, n) readcharline Reads a line of input, and adds the characters on the line into a character array (note that chararray is a reference to the array) The variable n represents the number of elements in the array strvar readline() Reads a line of input, ie a string from the keyboard strvar is a reference to a string numvar random(): Generates a random value (result numvar) greater or equal to 00 and less than 10 anarrayrefvar makenewarray( L ) Creates an array, referenced by anarrayrefvar, which has L positions with unknown values in them amatrixrefvar makenewmatrix(r, C ) Creates a matrix, referenced by amatrixrefvar, which has R rows and C columns of elements with unknown values in them The following conventions shall be used in both algorithm models and Java programs: All variable names shall start with a lower case character (examples: num, x, arrayref) All algorithm/method names shall start with a lower case character (examples: get, set, getvalue) When a name is composed of two or more words, capitals are used to start each word except the first word of the name (examples: amatrixrefvar, readreal) Class names shall start with an upper case character (examples: Student, Course) If you use constants, use all upper case characters (PI, CONST_VAL) The underscore can be useful to separate words 6

4 Summary of Program Structure, Algorithm Concepts and Java Programming Concepts Algorithm Model Java Instruction Block Primitive Types Result Name(<paramList>) ( ) (expression) [ ] (array subscript) (object member) + (positive / negative valeu) NOT (logical operator) * / MOD (multiply, divide, and modulo) + - (addition or subtraction of two values, concatenation) < > (comparison) = (comparison) AND (logical operator) OR (logical operator) (assignment variable) z x + y Subprogram Instructions public static <type> name(<paramlist>) Operations ( ) (expression) [ ] (array subscript) (object member) + (to indicate positive or negative values)! (not) * / % (multiply, divide, and modulo) + - (for addition or subtraction of two values, concatenation) < > >= <= (comparison) ==!= (comparison) && (logical AND) (logical OR) = (assignment to variable) Simple Expression z = x + y; int double char boolean integers real values single characters Boolean values Arrays / Matrices // Array Type [] varname; varname = new Type[size]; // Matrix Type [][] varname; varname = new Type[#Rows][#Cols]; // Type any variable type (int, // double, etc) or class name // varname legal variable name // size number of elements // #Rows number of rows // #Cols number of columns Classes / objects class ClassName // Attributes average markresults(score1, score2, score3) <expressioninit> Calls to subprograms average = markresults(score1, score2, score3); Branch Instruction if (<logical expression>) else Pre-Test Loop Instruction while(<logical expression>) Post-Test Loop Instruction do while(<logical expression>); Alternative Loop Instruction (Java for loop) for(<expressioninit>; <logical expression>; <expressionupdate>) // Constructor public ClassName(parameterList) // Methods // ClassName valid name // keywords used in // declaring attributes // and methods public accessible by all private accessible by class only static class variable or method // Reference variable ClassName varname; // Creating an object varname = new ClassName(); Available Class/Methods ITI1120 Methods: ITI1120readInt( ) ITI1120readDouble( ) ITI1120readChar( ) ITI1120readBoolean( ) ITI1120readDoubleLine( ) ITI1120readIntLine( ) ITI1120readCharLine( ) ITI1120readString( ) Scanner Methods: Scanner keyboard = new Scanner(Systemin); keyboardnextint( ) keyboardnextdouble( ) keyboardnextboolean( ) keyboardnextline( ) Systemout Methods: Systemoutprintln( ) Systemoutprint( ) Math Class Methods/Attributes Mathsin(rad) //cos, tan, asin, // acos, PI Mathexp(x) // also log(x) Mathlog(x) // also log10(x) Mathpow(x, a) // x to power a Mathsqrt(x) Mathrandom() Mathabs(x) <expressionupdate> 7