Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CS110 Programming Language I
Strings Testing for equality with strings.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
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.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
If Statement if (amount
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Complexity (Running Time)

Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CSC 204 Programming I Loop I The while statement.
Instructor - C. BoyleFall Semester
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Decision Making - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 10/27/20151.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
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.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
02 Variables1November Variables CE : Fundamental Programming Techniques.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Java Review if Online Time For loop Quiz on Thursday.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
1 Variables and Arithmetic Operators in JavaScript.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSE 142 Final Exam Review Problems. 2 Question Types expressions array mystery inheritance mystery file processing array programming Critters classes.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Midterm preview.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Line Continuation, Output Formatting, and Decision Structures
IF statements.
Variables and Arithmetic Operators in JavaScript
Midterm Review Problems
Solving Word Problems Underline important information (data, numbers, etc) Circle what you are trying to find Draw Picture Define your variable Write.
Repetition-Sentinel,Flag Loop/Do_While
Control Statement Examples
Building Java Programs
Line Continuation, Output Formatting, and Decision Structures
Truth tables: Ways to organize results of Boolean expressions.
Truth tables: Ways to organize results of Boolean expressions.
Building Java Programs
Presentation transcript:

Practice for Midterm 1

Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems for take-home practice The midterm will have five problems of similar difficulty A JavaFacts summary is posted on the course web page, and will be available during the midterm

Other problems The midterm will also have a number of t/f, multiple choice or short answer questions The programming problems will count for the majority of points on the exam

Key points How to create and initialize a variable How to call a method on an object How to design or evaluate an expression How to make decisions using if and if/else statements

In each programming problem, you are given a code framework similar to the following. Solve the problem by filling in the necessary statements in place of ‘..’ You may assume that the necessary ‘import’s have been made. public class ProblemX{ public static void main(String[] args) {.. }

Practice 1 Input three integers from a user (call them a, b, c). You may assume that the user input will be correct. Output the message that correctly describes the numbers: –"Three negative" –"Two negative and one non-negative" –"One negative and two non-negative" –"Three non-negative"

Practice 2 Input: You are given a Rectangle r. public class ProblemX{ public static void main(String[] args) { Rectangle r; r.setValues();.. }

Practice 2 Do: –Determine the dimensions of r. –If the height and width are not equal, set the larger one equal to the smaller one. Output: "r is a n by n square", where "n" is the actual height and width of r. (Hint: Refer to the JavaFacts sheet for the methods of the class Rectangle)

Practice 3 The statement Math.random() generates a random double greater than or equal to 0.0 and less than 1.0. Input: Use this statement to generate a roll of two dice (call them a and b). Output: –Print out the values of the two dice. –If they sum to 2, 3 or 12, print out "Craps. You lose." –If they sum to 7 or 11, print out, "You win." –Otherwise, print out, "Roll again."

Practice 4 The statement Math.random() generates a random double greater than or equal to 0.0 and less than 1.0. Input: Use this statement to generate four random numbers in the range Call them a, b, c, d. Output: –Print out the four numbers generated. –Print out, "The largest is" and then the largest of the four numbers.

Practice 5 Input three strings from the user. Call them s1, s2 and s3. Output: Print out the three strings ordered by length, shortest first. If two strings are of equal length, their order doesn't matter.

Practice 6 Input a string from the user. Output: –If the string is empty, print out, "Non empty string, please." –Otherwise, if the first character is an upper or lower case letter or underscore, print out, "Legal identifier." –Otherwise, print out, "Illegal identifier."

Take home practice 1 Input three integers from a user (call them a, b, c). (You may assume that the user input will be correct.) Output the message that correctly describes the numbers: –"Three even" –"Two even and one odd" –"One even and two odd" –"Three odd"

Take home practice 2 Input: You are given a Rectangle r. Output: –If the width of r is greater than the height, print out, "Landscape." –If the height of r is greater than the width, print out, "Portrait." –If the height and width of r are equal, print out, "Square."

Take home practice 3 The statement Math.random() generates a random double greater than or equal to 0.0 and less than 1.0. Input: Use this statement to generates a random lower case letter of the alphabet. Output: –Print out the letter generated. –If the letter is 'a', 'e', 'i', 'o' or 'u', print out, "Vowel." –Otherwise, print out, "Not a vowel."

Take home practice 4 Input three strings from the user. Call them s1, s2 and s3. Output: Print them out in lexicographic (dictionary) order.

Take home practice 5 Input a string from the user. Output: –If the string is empty, print out, "Non empty string, please." –Otherwise, if the first character is a digit, print out, "Starts with digit." –If the first character is a lower case letter, print out, "Starts with lower case letter." –If the first character is an upper case letter, print out, "Starts with upper case letter." –Otherwise, print out, "Starts with non-alphanumeric."

Take home practice 6 Input a string from the user. Output: –Print out the string. –If the string contains at least two instances of the substring 'yes', print out, "Very affirmative." –Otherwise, print out, "Not very affirmative."

Take home practice 7 Input two strings from the user; call them s1 and s2. Output: –If either is less than two characters in length, print out, "Too short." –Otherwise, if the first two characters of each is the same, print out, "Same prefix“ –If the last two characters of each is the same, print out, "Same suffix." Note that the prefix and suffix of a string may overlap.