EXERCISES SOFTWARE DEVELOPMENT I. 03 Control Flow & Branching Statements if/switch, while/do-while/for 2018W
|
|
- Roberta Manning
- 5 years ago
- Views:
Transcription
1 EXERCISES SOFTWARE DEVELOPMENT I 03 Control Flow & Branching Statements if/switch, while/do-while/for 2018W
2 1 st Test
3 I INFORMATION FOR THE 1ST TEST General information Date: Monday, November 12, 2018; 12:45 (- 13:05) Place: HS 1 & HS 16 HS 1: Exercise groups G1, G2, G3, G4 (instructors Haslgrübler / Jungwirth are present) HS 16: Exercise groups G5, G6 (instructors Grünberger / Trendafilov are present) Registration via KUSSS required and don t forget to bring your student id! Length: 20 minutes What/How? 1 short programming example (topic range: exercise lessons until Nov. 7/8, 2018) Allowed material/documents: 1 sheet of paper with notes, format A4, two-sided, e.g., loop constructs, type conversion table, switch/case construct) Not allowed: Books, Writing pad, Notebook, Smartphones, Calculators, or any other electronic equipment Software Development I // 2018W // 3
4 CHARACTER CONVERSION Complete the following program, which converts an upper case alphabetic character ("Buchstabe") to a lower case alphabetic character and vice versa, all other character types remain unchanged. e.g.: Input: 'b' => Output: 'B' Input: 'Y' => Output: 'y' Input: '9' => Output: '9' Input: '!' => Output: '! TODO Hints / Comments: You are not allowed to use any functions which operate on Strings or the Character class functions. Upper Case Characters have a lower decimal value in the ASCII table than lower case characters The distance between upper case and lower case characters is always the same, e.g. the distance between 'A' and 'a' is the same as between 'B' and 'b'. Software Development I // 2018W // 4
5 CHARACTER CONVERSION Complete the following program, which converts an upper case alphabetic character ("Buchstabe") to a lower case alphabetic character and vice versa, all other character types remain unchanged. e.g.: Input: 'b' => Output: 'B' Input: 'Y' => Output: 'y' Input: '9' => Output: '9' Input: '!' => Output: '! Hints / Comments: You are not allowed to use any functions which operate on Strings or the Character class functions. Upper Case Characters have a lower decimal value in the ASCII table than lower case characters The distance between upper case and lower case characters is always the same, e.g. the distance between 'A' and 'a' is the same as between 'B' and 'b'. Software Development I // 2018W // 5
6 DATA TYPES The figure on the right shows an electric circuit with two resistances. Resistance R1 is fixed with a value of 100 and resistance R2 can range from 5 to 15 and is entered by the user. Every correct entered value of R2 which is uneven should be decreased by 1. Afterwards, your program should calculate the total resistance of this circuit according to the following formula: Hints: Your program should calculate the result as floating-point value. Make sure that the user can only enter a correct number from 5 to 15. Use Input.readInt() to read in an integer value. TODO Software Development I // 2018W // 6
7 DATA TYPES The figure on the right shows an electric circuit with two resistances. Resistance R1 is fixed with a value of 100 and resistance R2 can range from 5 to 15 and is entered by the user. Every correct entered value of R2 which is uneven should be decreased by 1. Afterwards, your program should calculate the total resistance of this circuit according to the following formula: Hints: Your program should calculate the result as floating-point value. Make sure that the user can only enter a correct number from 5 to 15. Use Input.readInt() to read in an integer value. Software Development I // 2018W // 7
8 Control Flow & Branching Statements
9 CONTROL FLOW STATEMENTS :: DECISION MAKING Control flow statements break up the flow of execution by employing decision making (or looping and branching); this enables a program to conditionally execute particular statements or blocks of code. Java provides (as like most other modern programming languages) two types of decision-making statements for differentiation of cases: if statement (for binary branching) executes a certain section of code only if a particular test evaluates to true using the optional else statement, a secondary path of execution is provided when an if clause evaluates to false switch statement (for multiple branching) the switch statement can have any number of possible execution paths (if-else above is limited to 2) branching using switch works with the primitive data types byte, short, char, and int advanced: switch also works with enumerated types (enum types), the String-class, and a few special classes that wrap certain primitive types (Character, Byte, Short, Integer) Software Development I // 2018W // 9
10 CONTROL FLOW STATEMENTS :: BLOCKS Normally, every path of execution allows only a single statement; if you have more or less than one statement to execute, you have to group them using a block statement. A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed: EBNF: BlockStat = "" Stat Stat } "}". Example: public static void main (String[] arg) boolean condition = true; } if (condition) // begin block 1 System.out.println("Condition is true."); } // end block one else // begin block 2 ; } // end block 2 block with empty statement (";" can also be omitted) Software Development I // 2018W // 10
11 CONTROL FLOW STATEMENTS :: DECISION MAKING Binary branching EBNF: IfStat = "if" "(" BoolExpr ")" Stat1 [ "else" Stat2]. Interpretation when the expression BoolExpr evaluates to true, Stat1 will be executed, otherwise nothing or Stat2 BoolExpr needs to return a logical value (type boolean) by means of comparison operators: ==, <, >, <=, >=,!= using logical operations: &&,,! Conditional Operators: The && and operators perform conditional AND and conditional OR operations on two boolean expressions. These operators exhibit short-circuiting behavior, which means that the second operand is evaluated only if needed Software Development I // 2018W // 11
12 O CONTROL FLOW STATEMENTS :: EXAMPLES Calculation of postal charges Wanted: An algorithm that computes postal charges for packet delivery according to the following scheme: Cost table: first kilogram 13,00 2nd to 5th kilogram 06,00 (per kg) 6th to 10th kilogram 05,00 (per kg) from 11th kilogram 04,00 (per kg) Input: weight in kg Output: postal charges in Software Development I // 2018W // 12
13 CONTROL FLOW STATEMENTS :: EXAMPLES Calculation of postal charges Sample solution public class PostalCharges public static void main(string[] args) int weight = 0, costs = 0; System.out.print("Enter a weight (in kg): "); weight = Input.readInt(); if (weight > 10) costs = * * 5 + (weight - 10) * 4; } else if (weight > 5) costs = * 6 + (weight - 5) * 5; } else if (weight > 1) TODO costs = 13 + (weight - 1) * 6; } else costs = 13; // 1st kilogram } // end of if System.out.print("Postal charges (weight " + weight); System.out.println("kg): " + costs + " Euro"); } // end of main } // end of class UE03PostalCharges Software Development I // 2018W // 13
14 CONTROL FLOW STATEMENTS :: EXAMPLES Calculation of postal charges Sample solution public class PostalCharges public static void main(string[] args) int weight = 0, costs = 0; System.out.print("Enter a weight (in kg): "); weight = Input.readInt(); if (weight > 10) costs = * * 5 + (weight - 10) * 4; } else if (weight > 5) costs = * 6 + (weight - 5) * 5; } else if (weight > 1) costs = 13 + (weight - 1) * 6; } else costs = 13; // 1st kilogram } // end of if System.out.print("Postal charges (weight " + weight); System.out.println("kg): " + costs + " Euro"); } // end of main } // end of class UE03PostalCharges Software Development I // 2018W // 14
15 CONTROL FLOW STATEMENTS :: DECISION MAKING Multiple branches (Switchgroup) EBNF: SwitchStat = "switch" "(" Expr ")" "" SwitchGroup } "}". SwitchGroup = SwitchLabel SwitchLabel } Statement }. SwitchLabel = "case" ConstExpr ":" "default" ":". any number of statements; optionally closed by a break statement every constant expression (ConstExpr) only once! only one default label Interpretation Control expression Expr must be of data type byte, char, short or int (or enum, wrapper class) Data types of constant expressions SwitchLabel must be compliant with data type of control expression Expr Software Development I // 2018W // 15
16 CONTROL FLOW STATEMENTS :: DECISION MAKING Multiple branches (Switchgroup) switch (...) case...: stat1a; case...: stat1b; stat2b; stat3b; break; case...:... default:.. } // end of switch - arbitrary number of SwitchLabels - any number of statements per SwitchGroup - last statement in a SwitchGroup should be the break statement - default can be used to cover unexpected failures (if possible at all) Software Development I // 2018W // 16
17 I CONTROL FLOW STATEMENTS :: EXAMPLES Determination of the following date Wanted: An algorithm that determines based on a given date (day, month) the date of the next day (leap years not considered) Example Input: day (int), month (int) Output: date of the next day (format day. month.) Software Development I // 2018W // 17
18 CONTROL FLOW STATEMENTS :: EXAMPLES Determination of the following date Sample solution public class FollowDate public static void main(string[] args) int day; int month; int nrofdays = 0; day = Input.readInt(); month = Input.readInt(); System.out.print("Given date: " + day + "." + month + "., "); switch(month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: nrofdays = 31; break; case 2: TODO nrofdays = 28; break; case 4: case 6: case 9: case 11: nrofdays = 30; break; } // end of switch // Correct number of days for the given month is now stored in nrofdays Software Development I // 2018W // 18
19 CONTROL FLOW STATEMENTS :: EXAMPLES Determination of the following date Sample solution public class FollowDate public static void main(string[] args) int day; int month; int nrofdays = 0; day = Input.readInt(); month = Input.readInt(); System.out.print("Given date: " + day + "." + month + "., "); switch(month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: nrofdays = 31; break; case 2: nrofdays = 28; break; case 4: case 6: case 9: case 11: nrofdays = 30; break; } // end of switch //continued on next page Software Development I // 2018W // 19
20 CONTROL FLOW STATEMENTS :: EXAMPLES Determination of the following date Sample solution if (day < nrofdays) day = day + 1; else day = 1; month = month + 1; if (month == 13) month = 1; } System.out.println("date of the next day: " + day + "." + month + "."); } // end of main } // end of class UE03FollowDate I/O dialog 28 2 Given date: 28.2., date of the next day: Given date: , date of the next day: 1.1. Software Development I // 2018W // 20
21 I CONTROL FLOW STATEMENTS :: WHILE LOOP The while statement continually executes a block of statements (Stat) while a particular condition (Expr) is true Evaluation of the expression before first execution of Stat Stat will possibly not be executed at all EBNF: "while" "(" Expr ")" Stat. Example: public static void main (String[] arg) int count = 1; } while (count < 11) // repeated block System.out.println("Count is: " + count); count ++; } Expr==true Stat Yes No Software Development I // 2018W // 21
22 I CONTROL FLOW STATEMENTS :: DO-WHILE LOOP The do-while statement is similar to the while loop, however, do-while evaluates its expression (Expr) at the bottom of the loop instead of the top. Therefore, the statements (Stat) within the block are always executed at least once Evaluation of the expression after first execution of Stat Stat will be executed at least once EBNF: "do" Stat "while" "(" Expr ")". Stmt Example: public static void main (String[] arg) int count = 1; } do // repeated block System.out.println("Count is: " + count); count ++; } while (count < 11); Expr==true No Yes Software Development I // 2018W // 22
23 I CONTROL FLOW STATEMENTS :: FOR LOOP The for statement provides a compact way to iterate over a range of values. The general form is given in the following EBNF representation EBNF: "for" "(" Expr1 ";" Expr2 ";" Expr3 ")" Stat. Please keep in mind that The initialization expression (Expr1) initializes the loop; it is executed once at the start of the loop When the termination expression (Expr2) evaluates to false, the loop terminates The increment expression (Expr3) is invoked after each iteration through the loop Example: public static void main (String[] arg) for (int i=1; i<11; i++) // looping over i System.out.println("Count is: " + i); } } Expr1 Expr2==true Stat Expr3 Yes No Software Development I // 2018W // 23
24 I CONTROL FLOW STATEMENTS :: INFINITE LOOPS Using these control flow statements, it is very easy to implement an infinite loop Examples while (true) // any code can go here } i = 5; do // any code can go here } while (true); i = 5; Java recognizes such primitive endless loops and signals this with - "Unreachable Code" or - "Statement not reachable" (displayed in the line i = 5;) Eclipse screenshot for ( ; ; ) // any code can go here } i = 5; Software Development I // 2018W // 24
25 CONTROL FLOW STATEMENTS :: UNINTENDED INFINITE LOOPS Avoid to use floating point variables/expression for exact comparison! Examples double d = 1; while (d!= 0.0) d = d * 2 / 3; System.out.println("value: " + d); } Console output: value: value: value: value: value: value: value: value: 4.9E-324 value: 4.9E-324 value: 4.9E-324 Final result: prints 4.9E-324 forever (and never 0!!), i.e., results in an infinite loop - problem caused by imprecise representation of floating point variables (in termination condition) - even tough there is an exact representation of 0.0f/d in Java This problem cannot happen when using integer data types: int i = 4; while (i!= 0.0) i = i * 2 / 3; System.out.println("value: " + i); } Console output: value: 2 value: 1 value: 0 Software Development I // 2018W // 25
26 I CONTROL FLOW STATEMENTS :: WHICH LOOPS TERMINATES? int i, j, k; // declaration used for all code snippets Example 1 yes Example 3 j = 5; do j = j 1; } while (j!= 0); yes j = 5; do j = j 2; } while (j >= 0); Example 2 no Example 4 yes j = 5; do j = j 2; } while (j!= 0); j = -5; do j = j / 2; } while (j!= 0); Software Development I // 2018W // 26
27 I CONTROL FLOW STATEMENTS :: WHICH LOOPS TERMINATES? int i, j, k; // declaration used for all code snippets Example 5 no Example 7 i = 1; j = 1; while (i!= 100) k = i; i = j + i; j = k; } Example 6 yes i = 1; j = 1; while (i <= 100) k = i; i = j + i; j = k; } no i = 1; j = 1; while (i < 100) j = j + i; } Software Development I // 2018W // 27
28 I EXAMPLE :: FIBONACCI NUMBERS We are looking for a program that expects a number n (n > 1) as input and checks, whether the provided input is a Fibonacci number or not. If n is not a Fibonacci number, the program returns the next smaller Fibonacci number If n is a Fibonacci number, the number itself is returned Test your program by simulating it with a dry run test for the values n = 2, 5, and 6 The sequence F n of Fibonacci numbers is defined as follows: Fib(0) = 0 Fib(1) = 1 Fib(n) = Fib(n - 1) + Fib(n - 2) Software Development I // 2018W // 28
29 SAMPLE SOLUTION :: FIBONACCI NUMBERS public class Fibonacci public static void main(string[] arg) int n, fm0 = 0, fm1 = 1, f = 1; System.out.print("Enter a number: "); n = Input.readInt(); while (n <= 1) System.out.println("Number needs to be > 1!"); n = Input.readInt(); } while (n > f) f = fm0 + fm1; fm1 = fm0; fm0 = f; } if (n == f) System.out.println(n + " is a Fibonacci number!"); else System.out.println(n + " is not a Fibonacci number!"); System.out.println("Next smaller Fibonacci number: " + fm1); } } // end of main } // end of class UE03Fibonacci Software Development I // 2018W // 29
30 printf (for your Reference)
31 FORMATTED OUTPUT :: PRINTF Signature: printf(string format, Object... args) format is a text with placeholders Syntax see args is a comma separated list of objects to be used for the placeholders Example Placeholder Format for Numbers: %[argument_index$][flags][width][.precision]conversion int a = 65; //print an integer as a character and an integer System.out.printf("%c %1$d\n", a); //alternative way System.out.printf("%c %d\n", a, a); //print a float with 2 decimal places and without restriction System.out.printf("%.2f %1$f\n", (float)a); //Left Padding System.out.printf("%3d \n%3d ", a+a, a); Program Output: A 65 A Software Development I // 2018W // 31
32 Modulo Operation in Java (for your Reference)
33 JAVA OPERATORS :: MODULO (REMAINDER) OPERATOR Calculates the remainder of a division; operator is in Java (as compared to other programming languages such as C#, C++) also defined as floating point operator Operation mode Result is negative if the numerator is negative, otherwise positive (a % b) is always <= b (float), < b (int) ArithmeticException thrown if denominator equal to zero "Dividend" a... Numerator b... Denominator "Divisor" a mod I b = a (a / b) * b (Integer) a mod F b = a ((int)(a / b)) * b (Float) Software Development I // 2018W // 33
34 JAVA OPERATORS :: MODULO (REMAINDER) OPERATOR Definition of the Integer type int a, b; a mod I b (a % b) a - (a / b) * b; (a / b) * b + (a % b) = a Example 1: 5 % 3 ==? [ 5 / 3 = 1 ] (a / b) * b + (a % b) == a (5 / 3) * 3 + (5 % 3) == 5 (1) * 3 + (5 % 3) == 5-3 (5 % 3) == 2 Example 2: 5 % -3 ==? [ 5 / -3 = -1 ] Please note the higher precedence of unary operators! 5 % -3 5 % (-3) (5 / -3) * -3 + (5 % -3) == 5 (-1) * -3 + (5 % -3) == (5 % -3) == 5-3 (5 % -3) == 2 Software Development I // 2018W // 34
35 JAVA OPERATORS :: MODULO (REMAINDER) OPERATOR Definition of the Integer type int a, b; a mod I b (a % b) a - (a / b) * b; (a / b) * b + (a % b) = a Example 3: -5 % 3 ==? [ -5 / 3 = -1 ] (-5 / 3) * 3 + (-5 % 3) == -5 (-1) * 3 + (-5 % 3) == (-5 % 3) == -2 Example 4: -5 % -3 ==? [ -5 / -3 = 1 ] (-5 / -3) * -3 + (-5 % -3) == -5 (1) * -3 + (-5 % -3) == (-5 % -3) == -2 Software Development I // 2018W // 35
36 I JAVA OPERATORS :: MODULO (REMAINDER) OPERATOR Floating point type float a, b a modf b Operation mode If one operand is NaN, the result is also NaN (not a number) If the result is not NaN it has the same sign as the numerator If the numerator is infinite or the denominator == 0, the result is NaN If the numerator is finite and the denominator infinite, the result equals the value of the numerator If the numerator == 0 and the denominator is finite, the result equals the value of the numerator Else (regardless of NaN, and 0): No exceptions will be thrown and there is no overflow, underflow or loss of precision Software Development I // 2018W // 36
37 JAVA OPERATORS :: MODULO (REMAINDER) OPERATOR Definition of the Floating point type float a, b a mod F b a - b * (int)(a / b) == a mod b Example 1: 5.0 % 3.0 ==? * (int)(5.0 / 3.0) == 5.0 % * 1 == 5.0 % == 5.0 % 3.0 Example 2: 5.0 % -3.0 ==? * (int)(5.0 / -3.0) == 5.0 % * -1 == 5.0 % == 5.0 % == 5.0 % -3.0 Software Development I // 2018W // 37
38 JAVA OPERATORS :: MODULO (REMAINDER) OPERATOR Definition of the Floating point type float a, b a mod F b a - b * (int)(a / b) == a mod b Example 3: -5.0 % 3.0 ==? * (int)(-5.0 / 3.0) == -5.0 % * -1 == -5.0 % == -5.0 % == -5.0 % 3.0 Example 4: -5.0 % -3.0 ==? * (int)(-5.0 / -3.0) == -5.0 % * 1 == -5.0 % == -5.0 % == -5.0 % 3.0 Software Development I // 2018W // 38
39 JAVA OPERATORS :: MODULO (REMAINDER) OPERATOR Definition of the Floating point type float a, b a mod F b a - b * (int)(a / b) == a mod b Example 5: -5.2 % 2.67 ==? * (int)(-5.2 / 2.67) == -5.2 % * (int)(-1.94) == -5.2 % == -5.2 % == -5.2 % 2.67 Software Development I // 2018W // 39
40 EXERCISES SOFTWARE DEVELOPMENT I 03 Control Flow & Branching Statements if/switch, while/do-while/for 2018W
Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;
Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The
More informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
More informationOperators and Expressions
Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions
More informationChapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics
Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional
More informationAnnouncements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop
Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00
More informationGetting started with Java
Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving
More information13 th Windsor Regional Secondary School Computer Programming Competition
SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse
More informationIntroduction to the Java Basics: Control Flow Statements
Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared
More informationComputer Programming, I. Laboratory Manual. Experiment #3. Selections
Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #3
More informationSimple Java Reference
Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...
More informationCOMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal
COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal Chapter 5 5 Controlling the Flow of Your Program Control structures allow a programmer to define how and when certain statements will
More informationFull file at
Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional
More informationProgramming for Engineers Iteration
Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers
More informationObject oriented programming. Instructor: Masoud Asghari Web page: Ch: 3
Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More information3. Java - Language Constructs I
Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks
More informationDate: Dr. Essam Halim
Assignment (1) Date: 11-2-2013 Dr. Essam Halim Part 1: Chapter 2 Elementary Programming 1 Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting
More informationReview Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:
More informationBASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I
BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment
More informationData and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence
Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public
More informationCS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation
CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;
More informationBoolean Expressions. So, for example, here are the results of several simple Boolean expressions:
Boolean Expressions Now we have the ability to read in some information, calculate some formulas and display the information to the user in a nice format. However, the real power of computer programs lies
More informationFundamentals of Programming
Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables
More informationLanguage Reference Manual
ALACS Language Reference Manual Manager: Gabriel Lopez (gal2129) Language Guru: Gabriel Kramer-Garcia (glk2110) System Architect: Candace Johnson (crj2121) Tester: Terence Jacobs (tj2316) Table of Contents
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More informationConditional Statement
Conditional Statement 1 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition Also called Branching How do we specify conditions?
More informationCCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives
LIANMC03v3_0132221586.QXD 5/15/06 7:41 PM Page 67 CCHAPTER HAPTER 3 1 SELECTION STATEMENTS Objectives To declare boolean type and write Boolean expressions ( 3.2). To distinguish between conditional and
More informationMore Programming Constructs -- Introduction
More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and
More informationJAVA OPERATORS GENERAL
JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators
More informationFull file at
Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class
More informationCOMP Primitive and Class Types. Yi Hong May 14, 2015
COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to
More informationLOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee
LOON THE LANGUAGE OF OBJECT NOTATION Language Reference Manual Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee October 2017 1 Contents 1 Introduction 3 2 Types 4 2.1 JSON............................................
More informationControl Flow Statements
Control Flow Statements The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering
More informationDEPARTMENT OF MATHS, MJ COLLEGE
T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,
More informationSOFTWARE DEVELOPMENT 1. Control Structures 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)
SOFTWARE DEVELOPMENT 1 Control Structures 2018W (Institute of Pervasive Computing, JKU Linz) WHAT IS CONTROL FLOW? The control flow determines the order in which instructions are executed. Default: from
More informationMcGill University School of Computer Science COMP-202A Introduction to Computing 1
McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif
More informationEntry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process
Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will
More informationOutline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.
OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce
More information1. Introduction to Java for JAS
Introduction to Java and Agent-Based Economic Platforms (CF-904) 1. Introduction to Java for JAS Mr. Simone Giansante Email: sgians@essex.ac.uk Web: http://privatewww.essex.ac.uk/~sgians/ Office: 3A.531
More informationVisual C# Instructor s Manual Table of Contents
Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms
More informationOperators in java Operator operands.
Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,
More informationObject-Oriented Programming. Topic 2: Fundamental Programming Structures in Java
Electrical and Computer Engineering Object-Oriented Topic 2: Fundamental Structures in Java Maj Joel Young Joel.Young@afit.edu 8-Sep-03 Maj Joel Young Java Identifiers Identifiers Used to name local variables
More informationChapter 6 Primitive types
Chapter 6 Primitive types Lesson page 6-1. Primitive types Question 1. There are an infinite number of integers, so it would be too ineffient to have a type integer that would contain all of them. Question
More informationJava. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures
Chapter 5: Control Structures II Java Programming: Program Design Including Data Structures Chapter Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled,
More informationExample: Monte Carlo Simulation 1
Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149
More informationTester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time
Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester
More informationTime: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:
ECE 120 Midterm 1 HKN Review Session Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location: Your Room on Compass What to bring: icard, pens/pencils, Cheat sheet (Handwritten) Overview of Review Binary IEEE
More informationProgram Control Flow
Lecture slides for: Chapter 3 Program Control Flow Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2
More informationProgram Control Flow
Lecture slides for: Chapter 3 Program Control Flow Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2
More informationIntroduction to Java & Fundamental Data Types
Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains
More informationElementary Programming
Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal
More informationStructures, Operators
Structures Typedef Operators Type conversion Structures, Operators Basics of Programming 1 G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz 10 October, 2018 c based on slides by Zsóka, Fiala, Vitéz
More information1.00 Lecture 4. Promotion
1.00 Lecture 4 Data Types, Operators Reading for next time: Big Java: sections 6.1-6.4 Promotion increasing capacity Data Type Allowed Promotions double None float double long float,double int long,float,double
More informationJava is an objet-oriented programming language providing features that support
Java Essentials CSCI 136: Spring 2018 Handout 2 February 2 Language Basics Java is an objet-oriented programming language providing features that support Data abstraction Code reuse Modular development
More informationGOLD Language Reference Manual
GOLD Language Reference Manual Language Guru: Timothy E. Chung (tec2123) System Architect: Aidan Rivera (ar3441) Manager: Zeke Reyna (eer2138) Tester: Dennis Guzman (drg2156) October 16th, 2017 1 Introduction
More informationChapter 3: Operators, Expressions and Type Conversion
101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To
More informationLearning the Java Language. 2.1 Object-Oriented Programming
Learning the Java Language 2.1 Object-Oriented Programming What is an Object? Real world is composed by different kind of objects: buildings, men, women, dogs, cars, etc. Each object has its own states
More informationProf. Navrati Saxena TA: Rochak Sachan
JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.
More informationCS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University
CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can
More informationComputer System and programming in C
1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign
More informationMidterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.
Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total. Name: ID: Problem 1) (8 points) For the following code segment, what are the values of i, j, k, and d, after the segment
More informationControl Structures. Lecture 4 COP 3014 Fall September 18, 2017
Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls
More informationChapter 2: Data and Expressions
Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review
More informationChapter 2: Data and Expressions
Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data
More informationChapter 3 Selection Statements
Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification
More informationExpressions and Data Types CSC 121 Spring 2015 Howard Rosenthal
Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types
More informationCSC Java Programming, Fall Java Data Types and Control Constructs
CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions
More informationDecaf Language Reference Manual
Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented
More informationTail recursion. Decision. Assignment. Iteration
Computer Programming Tail recursion. Decision. Assignment. Iteration Marius Minea marius@cs.upt.ro 7 October 2014 Two ways of writing recursion unsigned max(unsigned a, unsigned b) { return a > b? a :
More informationLecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue
Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue General Loops in Java Look at other loop constructions Very common while loop: do a loop a fixed number of times (MAX in the example) int
More informationExpressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators
Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical
More informationBasic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types
More informationObject Oriented Programming with Java
Object Oriented Programming with Java What is Object Oriented Programming? Object Oriented Programming consists of creating outline structures that are easily reused over and over again. There are four
More informationCSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR
More informationLesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL
Lesson 02 Data Types and Statements MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Data Types Arithmetic
More informationBasic Java Syntax. COMP 401, Fall 2015 Lecture 2 1/13/2014
Basic Java Syntax COMP 401, Fall 2015 Lecture 2 1/13/2014 Program OrganizaDon and ExecuDon All code in Java is organized into classes and interfaces One public class or interface per file. Not strictly
More informationReview for Test 1 (Chapter 1-5)
Review for Test 1 (Chapter 1-5) 1. Introduction to Computers, Programs, and Java a) What is a computer? b) What is a computer program? c) A bit is a binary digit 0 or 1. A byte is a sequence of 8 bits.
More informationChapter 3 Structure of a C Program
Chapter 3 Structure of a C Program Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand
More informationConditional Programming
COMP-202 Conditional Programming Chapter Outline Control Flow of a Program The if statement The if - else statement Logical Operators The switch statement The conditional operator 2 Introduction So far,
More informationCharacter Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits
More informationJump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.
Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:
More informationCOMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler
More informationCOMP6700/2140 Operators, Expressions, Statements
COMP6700/2140 Operators, Expressions, Statements Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU 3 March 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Operators,
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a
More informationJump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.
Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:
More informationJAVA Programming Fundamentals
Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol
More informationExam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming
Exam 1 Prep Dr. Demetrios Glinos University of Central Florida COP3330 Object Oriented Programming Progress Exam 1 is a Timed Webcourses Quiz You can find it from the "Assignments" link on Webcourses choose
More informationLanguage Reference Manual
Espresso Language Reference Manual 10.06.2016 Rohit Gunurath, rg2997 Somdeep Dey, sd2988 Jianfeng Qian, jq2252 Oliver Willens, oyw2103 1 Table of Contents Table of Contents 1 Overview 3 Types 4 Primitive
More informationMidterm Examination (MTA)
M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and
More informationExercise: Using Numbers
Exercise: Using Numbers Problem: You are a spy going into an evil party to find the super-secret code phrase (made up of letters and spaces), which you will immediately send via text message to your team
More informationx = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0
6 Statements 43 6 Statements The statements of C# do not differ very much from those of other programming languages. In addition to assignments and method calls there are various sorts of selections and
More informationJava Programming Basics. COMP 401, Spring 2017 Lecture 2
Java Programming Basics COMP 401, Spring 2017 Lecture 2 AverageHeightApp take 2 Same as before, but with Eclipse. Eclipse Workspace CreaJng new project CreaJng a new package CreaJng new class Running a
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationChapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:
Java Programming, Eighth Edition 2-1 Chapter 2 Using Data A Guide to this Instructor s Manual: We have designed this Instructor s Manual to supplement and enhance your teaching experience through classroom
More information3. Java - Language Constructs I
Names and Identifiers A program (that is, a class) needs a name public class SudokuSolver {... 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations,
More information