CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.

Slides:



Advertisements
Similar presentations
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
CS110 Programming Language I
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
03 Data types1June Data types CE : Fundamental Programming Techniques.
CS150 Introduction to Computer Science 1
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
CSC 204 Programming I Loop I The while statement.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 02 (Part III) Introduction to C++ Programming.
Chapter 5 Loops.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 4: Control Structures II
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Sourceshttp://
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
COMP Loop Statements Yi Hong May 21, 2015.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Midterm preview.
CSC111 Quick Revision.
Elementary Programming
Introduction to Computer Science / Procedural – 67130
Chapter 6 More Conditionals and Loops
Repetition-Sentinel,Flag Loop/Do_While
CSS 161: Fundamentals of Computing
Java Programming: From Problem Analysis to Program Design, 4e
TK1114 Computer Programming
Building Java Programs
Conditional Branching
Chapter 2: Basic Elements of Java
SELECTION STATEMENTS (2)
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Building Java Programs
Fundamental Programming
Unit 3: Variables in Java
Chapter 3 Debugging Section 3.4
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
CSS161: Fundamentals of Computing
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Programming Fundamental
Presentation transcript:

CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions

CSE 201 – Elementary Computer Programming2 Checking if string has digit   Given a string variable, inputName, check to see if it contains any numbers. If it contains any numbers, output an error message.   The boolean Character.isDigit(char) method can be used to check a character variable to see if it is a digit.

CSE 201 – Elementary Computer Programming3 Reverse Guessing Game   Write a program which plays the random number game,   the user chooses the secret number between 1 and 10, the computer will make "guesses" by choosing random numbers. User will enter ‘b’ is secret number is bigger than guess, and ‘s’ if secret number is smaller than guess, and ‘c’ when correct.   Sample: 99 ss 11 bb 88 ss 33 cc   Computer guessed the secret number in 4 guesses The code int randomNumber = (int)(10 * Math.random ()) + 1; will generate a random number between 1 and 10 and assign the value to the variable randomNumber.

CSE 201 – Elementary Computer Programming4 Value of expressions   Given the following variable declarations int i = 10, j = 3, k = -1; boolean b = true; String str = "Elephant"; char c = 'e'; (A) evaluate expressions (a)-(f): a. (i < j) || b b. str.charAt(j) c. str.length() == 7 d. str.substring(0,4) e. str.indexOf(c) f. str.indexOf('c')

CSE 201 – Elementary Computer Programming5 Errors and Types   Given the following variable declarations int i = 10, j = 3, k = -1; boolean b = true; String str = "Elephant"; char c = 'e'; determine which of the following statements (g)-(j) will produce an error. If a statement does produce an error, what kind? Briefly explain in one or two sentences why it produces an error. a. (i < j) || b b. str.charAt(j) c. str.length() == 7 d. str.substring(0,4) e. str.indexOf(c) f. str.indexOf('c')

CSE 201 – Elementary Computer Programming6 Code Writing   Fix the code so that it works according to the supplied documentation (in comments) /* Calculates sum of all of the even * numbers from 0 to limit inclusive. * Assumes limit is initialized above * to a positive integer. */ int i = 0; int sum = 0; while (i != limit) { sum = sum + i; i+=2;}

CSE 201 – Elementary Computer Programming7 Debug According to Description //This program counts the number of special characters and $) in a string input. Scanner in = new Scanner(System.in); String userInput; System.out.print("Please enter a string: "); userInput = in.nextLine(); int stringSize = userInput.length(); int counter = 0, numSpecialChars = 0; char letter; while (counter < stringSize) { letter = userInput.charAt(counter); letter = userInput.charAt(counter); if ((letter == '&') || (letter == || (letter == '$')) if ((letter == '&') || (letter == || (letter == '$')) { numSpecialChars = numSpecialChars + 1; numSpecialChars = numSpecialChars + 1; }} System.out.println("Number of special characters is " + numSpecialChars );

CSE 201 – Elementary Computer Programming8 If else  Are the two set of statements equivalent. Explain your answer in detail to get any credit. You can assume x and y are integers input by the user. if( x = = y)if( x = = y) x = 5;x = 5; x = 5;x = 5; elseif( x != y) x = 7; x = 7;