Chapter 4 Lab. Loops and Files. Objectives. Introduction
|
|
- Winifred York
- 6 years ago
- Views:
Transcription
1 Chapter 4 Lab Loops and Files Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write a do-while loop Be able to write a for loop Be able to use the Random class to generate random numbers Be able to implement an accumulator and a counter Introduction In this lab we create three different programs. The first is a simulation of a hamburger making program that uses loops to build a hamburger with multiple layers of lettuce and tomatoes. We will experiment with all three loops (while, do-while and for) in the course of building this program. The second is a program that chooses a secret random number and allows the user to guess the number, providing feedback such as too high or too low and continuing until the number is eventually guessed. Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 0 and 100. We will continue to use control structures that we have already learned, while exploring control structures used for repetition. We shall also continue our work with algorithms, translating a given algorithm to java in order to complete our program. For the third program, we will get to review logical operators and comparing Strings, in a program that acts like a grocery checkout cash register.
2 Task #1: Using while, do...while, and for loops In this exercise you will practice using while loops and do...while loops in order to create sections of source code that are repeated as long as some condition is met. At the end you will replace while loops with for loops. Dr. Kow's Hamburger Palace Make a new project called lab4 using BlueJ. Then create a new class called Sandwich, with the standard 3 comment lines at the top (file name, authors, and date). Delete everything inside the class and create a main method to put all of your code in. This program will make a sandwich. Since our laptops are lacking the robotic actuators and other materials to make a sandwich, we will print it out on the screen. Here's a preview of the final product: * Dr. Kow's Hamburger Palace * How many layers of lettuce do you want? 2 How many layers of tomatoes do you want? 1 Here's your sandwich: ( ) ~~~~~~~~~ ~~~~~~~~~ [ ] [ ] ********* ( ) As always, we start by outlining our plan. Copy these comments into your main method. //Print the Hamburger Palace sign. //Create a Scanner object for user input //Ask how much of the toppings the user wants (reject negative numbers) //Print out the result to the screen Writing the program Printing out the "Hamburger Palace" sign involves printing the same line a number of times. Rather than a whole bunch of System.out.println statements, we will use a while loop to do this. Note that in the following code, int line = 0; is shorthand for two lines: one that says int line; and another that says line = 0;
3 //Print the Hamburger Palace sign. System.out.println(""); int line = 0; while(line < 5) { System.out.println(""); line = line + 1; } System.out.println("* Dr. Kow's Hamburger Palace *"); //...or whatever title you prefer The source code above works by using a variable (line) to keep track of how many iterations through the loop we have done. After line becomes 5, Java finds that the boolean condition (line < 5) is false, so it exits the loop and continues with the println statement which prints out the restaurant name. Your program output should now look like this (feel free to change the name of the restaurant): * Dr. Kow's Hamburger Palace * Now write a second while loop, so that you get this result: * Dr. Kow's Hamburger Palace * Next, we will need to take some input from the program user, so create a Scanner object. Don't forget the import statement. //Create a Scanner object for user input Scanner keyboard = new Scanner(System.in); Now we will ask how many layers of lettuce they want. If they enter a negative int like -7 or -3, we will reject the input and make them enter something that makes sense. A "do...while" loop is well-suited to this sort of task, since the interior of the loop is guaranteed to execute at least once; the boolean condition is tested at the bottom of the loop rather than the top. Notice that in the following code, we don't need to initialize lettuce when we declare it. That's because the Java compiler realizes that it must get assigned some value in the do...while loop.
4 //Ask how much of the toppings the user wants (reject negative numbers) int lettuce; do { System.out.println("How many layers of lettuce do you want?"); lettuce = keyboard.nextint(); }while(lettuce < 0); Now that you know how much lettuce to print, it's time to print out the whole burger. Start by printing the symbols for your bun. Then use a while loop (not a do...while loop) to print all the layers of lettuce. You'll need to figure out what condition should be inserted in the while statement where it says /* TODO */ and convert the comments in the while loop into java statements. //print the bun //this while loop should repeat as many times // as the number stored in variable lettuce int line=0; while( /* TODO */ ) { // print a line of lettuce symbols like ~~~~~~~~~~~ // add one to variable line } Your program should work like this now: Try running it again and ask for 0 layers of lettuce. Notice how it gives you a sandwich without lettuce? This is because the boolean condition of the while loop was false the very first time, hence the interior of the while loop (the code block) didn't get executed at all. Make sure you understand this. Now modify your program so that it also asks for tomatoes before printing out the sandwich. It should keep asking until the user gives a number of tomato layers which is 0 or greater. Finally, make it print out the tomato layers. Here is the final program: * Dr. Kow's Hamburger Palace * How many layers of lettuce do you want? -1 How many layers of lettuce do you want? 2 How many layers of tomatoes do you want? -4 How many layers of tomatoes do you want? -3 How many layers of tomatoes do you want? 3
5 Here's your sandwich: ( ) ~~~~~~~~~ ~~~~~~~~~ [ ] [ ] [ ] [ ] [ ] [ ] ********* ( ) Experiment with for loops The last change to make is to go back and modify the while loops that printed the layers of lettuce and tomatoes so that they use a for loop instead of a while loop. Simply combine the three parts of the while loop--the counter initialization, the test condition, and the increment operation--into a single line, such as: for ( initialize counter ; test counter; increment counter) { } where the items in italics are replaced with your specific code solution.
6 Task 2: Nested control structures In this exercise you will get some exposure to nesting control structures. This means putting a control structure such as an if statement, while loop, or do..while inside the code block of another control structure. "Guess the number" game Create a new class called Guess inside your lab4 project folder. This program is a guessing game. The computer picks a random number and the user tries to guess it using as few guesses as possible. Here's a preview of the completed program: I've got a number 0 to 100. What's your guess? 50 Guess lower. What's your guess? 25 Guess higher. What's your guess? 37 Guess lower. What's your guess? 32 Guess lower. What's your guess? 29 You got it in 5 guesses! We will first have the computer secretly generate a random number and store it in some variable. You will also have to define another variable that holds the user's most recent guess, so we can compare the two and print out an appropriate message. We will use a do..while loop to repeat the process of asking for a guess and giving feedback. Here's the basic program structure, which assumes that you put the secret number in a variable called secret: //TODO: Declare a Scanner object called keyboard and initialize it to System.in //TODO: Declare a Random object called gen and initialize it to a new Random //TODO: Determine a secret random number int userguess; //stores the user's most recent guess do { //TODO: ask for a guess //TODO: retrieve a guess from your Scanner //TODO: say whether to guess lower, higher, or if it was correct. }while(userguess!= secret); //keep looping back as long as guess is wrong Filling in the details Copy the pseudocode algorithm above into your main program method. To determine the random secret number, use the nextint(int n) method of the Random object gen, in otherwords, secret = gen.nextint(101); (this gives a number from 0 to 100 inclusive)
7 You can use if statements inside the loop, just as you would anywhere else. When a control structure is nested inside another (such as an if inside a do..while), the computer still evaluates the line currently being executed by using the current status of the variables. It "thinks locally", evaluating one line at a time when it's running. One further detail deserves explanation. Notice how the variable userguess was declared before the main loop. We did this because of something called variable scope. In general, a variable can be used from the line where it was declared, all the way down to the end of the code block in which it was declared. Declaring userguess where we did allows us to use it for the rest of the main program block. If we had declared it inside the do..while loop, then we only would have been able to use it inside the loop (up to the closing }). Finally: Modify your program so it tells how many tries it took before the correct guess was entered. This will require creating a new counter variable that is initialized to 0 and incremented by 1 for every cycle of the loop.
8 Task 3: Logical operators and comparing Strings In this exercise you will get experience using "and", "or", and "not" in Java using the logical operators &&,, and!. You will also learn how to compare String objects, which operates differently than the comparison of primitives. Logical operators There are three logical operators in Java, summarized in the following table. logical operator meaning example in English && and a < 3 && b > 10 a is less than 3 and b is greater than 10 or a < 3 b > 10 a is less than 3 or b is greater than 10! not!(a < 3) it is not the case that a is less than 3 Both && and require some expression that evaluates to true or false on either side of the && or operator. The && operator evaluates to true if both expressions it is operating on evaluate to true. Otherwise it evaluates to false. The operator evaluates to true if either (or both) of the boolean expressions it is operating on evaluates to true. Otherwise it evaluates to false. Finally, the! operator reverses the truth value of the expression it is acting on; it is true if the rest of the expression is false and false if the rest of the expression is true. String comparison There are two categories of data types in Java: primitives (such as int, double, boolean, and long), and objects (such as Scanner and String). Comparing primitives for equality can be done with the ==operator. For objects, however, the == has a different meaning (it determines if two objects are the exact same object in memory). Usually, this is not what we want. Instead, we use methods to compare objects. These methods return boolean values, so we can use them in if statements and while loops just as we would when comparing primitives. Here is how to determine if two String objects contain exactly the same characters: String method meaning example in English equals equalsignorecase do the two Strings have exactly the same characters? (case sensitive) do the two Strings have exactly the same characters? (ignoring case) while(z.equals("hello")) { } while(z.equalsignorecase("hello")){ } Loop executes as long as z consists of exactly the characters Hello Loop executes as long as z consists of the characters Hello, in any combination of uppercase and lowercase letters
9 Shop smart. Shop KowMart. Create a new Java project Checkout in your lab 4 project folder. This will be a point-of-sale program for a self-checkout counter. Here's the final product in action: KowMart express self-checkout (10 item limit) Do you want a bag (costs.05 extra if you have 3 or less items)? yes Your total with 6% MI tax is The program first asks how many items there are, then adds up the cost of all the items, asks if they want a bag, and finally give the total amount due including tax. Start with the following code. //Create Scanner object and print greeting. Scanner in = new Scanner(System.in); System.out.println("KowMart express self-checkout (10 item limit)"); //Ask how many items int items; do { System.out.println(""); items = in.nextint(); }while( TODO ); //Add up the cost of each item (while loop, repeat items times) //Ask if a bag is needed //Add the cost of bag, if necessary. //Add in tax. //print out the total. I've left a TODO where a condition is needed for whether or not to loop back again. Using our new logical operators, fill in some code that makes it loop as long as they give a number that is less than 1 or greater than 10. Your program should now act like this: KowMart express self-checkout (10 item limit) Next, make a double variable called total, which will store the total cost of all the goods they are buying. Write a loop that asks for the cost of each item, and adds it into the total (don't worry about checking for negative dollar amounts - we trust our customers and maybe they are entering a coupon). Now you are this far:
10 KowMart express self-checkout (10 item limit) Now ask if they want a bag. Get the user's input and store it into a String variable. If they have 3 or less items and they said anything other than "no", then charge them an extra 5 cents (use one of the equality methods of the String class, from the table at the start of this lab). There isn't a String method called notequal, but we can make do with a logical operator. For example, if you have a String variable called s and you want an expression that tests to see if it is not equal to "hello", then the following code does it:!s.equals("hello") Finally, add sales tax. That's just a simple matter of multiplying the total by 1.06, and then printing it out. KowMart express self-checkout (10 item limit) Do you want a bag (costs.05 extra if you have 3 or less items)? yes Your total with 6% MI tax is
Introduction to Java Unit 1. Using BlueJ to Write Programs
Introduction to Java Unit 1. Using BlueJ to Write Programs 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationAssignment 2.4: Loops
Writing Programs that Use the Terminal 0. Writing to the Terminal Assignment 2.4: Loops In this project, we will be sending our answers to the terminal for the user to see. To write numbers and text to
More informationJava Coding 3. Over & over again!
Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java
More informationThe action of the program depends on the input We can create this program using an if statement
The program asks the user to enter a number If the user enters a number greater than zero, the program displays a message: You entered a number greater than zero Otherwise, the program does nothing The
More informationMITOCW watch?v=0jljzrnhwoi
MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To
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 informationWentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics
WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java
More informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationCOMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements
COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money
More informationCOMP 202 Java in one week
COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of
More informationCONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops
COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables
More informationMore about Loops and Decisions
More about Loops and Decisions 5 In this chapter, we continue to explore the topic of repetition structures. We will discuss how loops are used in conjunction with the other control structures sequence
More informationBEGINNER PHP Table of Contents
Table of Contents 4 5 6 7 8 9 0 Introduction Getting Setup Your first PHP webpage Working with text Talking to the user Comparison & If statements If & Else Cleaning up the game Remembering values Finishing
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationCOMP-202 Unit 4: Programming with Iterations
COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables
More informationObject-Oriented Programming
Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,
More informationCS112 Lecture: Loops
CS112 Lecture: Loops Objectives: Last revised 3/11/08 1. To introduce some while loop patterns 2. To introduce and motivate the java do.. while loop 3. To review the general form of the java for loop.
More informationThe name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.
Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that
More informationBasic Computation. Chapter 2
Basic Computation Chapter 2 Increment and Decrement Operators Used to increase (or decrease) the value of a variable by 1 Easy to use, important to recognize The increment operator count++ or ++count The
More informationChapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );
Chapter 5 Methods Sections Pages Review Questions Programming Exercises 5.1 5.11 142 166 1 18 2 22 (evens), 30 Method Example 1. This is of a main() method using a another method, f. public class FirstMethod
More informationDefinition: A data structure is a way of organizing data in a computer so that it can be used efficiently.
The Science of Computing I Lesson 4: Introduction to Data Structures Living with Cyber Pillar: Data Structures The need for data structures The algorithms we design to solve problems rarely do so without
More informationChapter 2 Part 2 Edited by JJ Shepherd, James O Reilly
Basic Computation Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly Parentheses and Precedence Parentheses can communicate the order in which arithmetic operations are performed examples: (cost +
More informationCOMP 202 Java in one week
CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language
More informationProblem Solving With Loops
To appreciate the value of loops, take a look at the following example. This program will calculate the average of 10 numbers input by the user. Without a loop, the three lines of code that prompt the
More informationCSIS 10A Assignment 4 SOLUTIONS
CSIS 10A Assignment 4 SOLUTIONS Read: Chapter 4 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,
More informationBasic Computation. Chapter 2
Walter Savitch Frank M. Carrano Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers
More informationSt. Edmund Preparatory High School Brooklyn, NY
AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think
More informationTOPIC 2 INTRODUCTION TO JAVA AND DR JAVA
1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)
More informationLecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18
Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical
More informationLECTURE 5 Control Structures Part 2
LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on
More informationCS1004: Intro to CS in Java, Spring 2005
CS1004: Intro to CS in Java, Spring 2005 Lecture #16: Java conditionals/loops, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Midterms returned now Weird distribution Mean: 35.4 ± 8.4 What
More informationCS112 Lecture: Repetition Statements
CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general
More informationBasic Computation. Chapter 2
Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers and letters. Think of them
More informationIntro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming
Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals
More information5) (4 points) What is the value of the boolean variable equals after the following statement?
For problems 1-5, give a short answer to the question. (15 points, ~8 minutes) 1) (4 points) Write four Java statements that declare and initialize the following variables: A) a long integer with the value
More informationPlease answer the following questions. Do not re-code the enclosed codes if you have already completed them.
Dec. 9 Loops Please answer the following questions. Do not re-code the enclosed codes if you have already completed them. What is a loop? What are the three loops in Java? What do control structures do?
More informationConditional Execution
Unit 3, Part 3 Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Simple Conditional Execution in Java if () { else {
More informationMr. Monroe s Guide to Mastering Java Syntax
Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.
More informationLoops (while and for)
Loops (while and for) CSE 1310 Introduction to Computers and Programming Alexandra Stefan 1 Motivation Was there any program we did (class or hw) where you wanted to repeat an action? 2 Motivation Name
More informationAP Computer Science Unit 1. Writing Programs Using BlueJ
AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save
More informationOver and Over Again GEEN163
Over and Over Again GEEN163 There is no harm in repeating a good thing. Plato Homework A programming assignment has been posted on Blackboard You have to convert three flowcharts into programs Upload the
More informationLaboratory 5: Implementing Loops and Loop Control Strategies
Laboratory 5: Implementing Loops and Loop Control Strategies Overview: Objectives: C++ has three control structures that are designed exclusively for iteration: the while, for and do statements. In today's
More informationFirst Java Program - Output to the Screen
First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the
More informationC++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures
More informationLoops. CSE 114, Computer Science 1 Stony Brook University
Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?
More informationASSIGNMENT 1 First Java Assignment
ASSIGNMENT 1 First Java Assignment COMP-202B, Winter 2012, All Sections Due: Sunday January 29th, 2012 (23:30) Please read the entire pdf before starting. You must do this assignment individually and,
More informationAP Computer Science Java Mr. Clausen Program 6A, 6B
AP Computer Science Java Mr. Clausen Program 6A, 6B Program 6A LastNameFirstNameP6A (Quadratic Formula: 50 points) (Including complex or irrational roots) Write a program that begins by explaining the
More informationCSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI
CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are
More informationLoops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017
Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The
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 informationCONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want
More informationCOMP 202. Java in one week
COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language
More informationAP Computer Science Unit 1. Writing Programs Using BlueJ
AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save
More information4. Java Project Design, Input Methods
4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java
More informationWhat did we talk about last time? Examples switch statements
Week 4 - Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation
More informationDarrell Bethea May 25, 2011
Darrell Bethea May 25, 2011 Yesterdays slides updated Midterm on tomorrow in SN014 Closed books, no notes, no computer Program 3 due Tuesday 2 3 A whirlwind tour of almost everything we have covered so
More informationThe for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming
The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:
More informationUnit 7. 'while' Loops
1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
More informationChapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6 Repetition Statements Animated Version required for reproduction or display. Chapter 6-1 Objectives After you have read and studied this chapter, you should be able to Implement repetition control
More informationCHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS
These are sample pages from Kari Laitinen s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html CHAPTER 5
More informationRepe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationComputational Expression
Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name
More informationIntroduction to Software Development (ISD) David Weston and Igor Razgon
Introduction to Software Development (ISD) David Weston and Igor Razgon Autumn term 2013 Course book The primary book supporting the ISD module is: Java for Everyone, by Cay Horstmann, 2nd Edition, Wiley,
More informationLab # 2. For today s lab:
1 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot 1 For today s lab: Go the course webpage Follow the links to the lab notes for Lab 2. Save all the java programs you
More informationCS 152: Data Structures with Java Hello World with the IntelliJ IDE
CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building
More informationWhy Is Repetition Needed?
Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable
More informationTo become familiar with array manipulation, searching, and sorting.
ELECTRICAL AND COMPUTER ENGINEERING 06-88-211: COMPUTER AIDED ANALYSIS LABORATORY EXPERIMENT #2: INTRODUCTION TO ARRAYS SID: OBJECTIVE: SECTIONS: Total Mark (out of 20): To become familiar with array manipulation,
More informationAssoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Dr. Marenglen Biba Before writing a program to solve a problem, have a thorough understanding of the problem and a carefully planned approach to solving it. Understand the types of building
More informationData and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:
Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions
More informationSchool of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn
CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...
More informationAP Computer Science Unit 1. Programs
AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated
More informationRepetition, Looping. While Loop
Repetition, Looping Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions
More informationDecision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)
Decision Structures if, if/ conditions Selection DECISION: determine which of 2 paths to follow (1+ statements in each path) CS1110 - Kaminski (ELSE path optional) 2 Selection options (in Java) Plain if
More informationFlow of Control. Chapter 3. Chapter 3 1
Flow of Control Chapter 3 Chapter 3 1 Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement chooses between
More informationA PROGRAM IS A SEQUENCE of instructions that a computer can execute to
A PROGRAM IS A SEQUENCE of instructions that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form
More informationLast Class. While loops Infinite loops Loop counters Iterations
Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();
More informationClaremont McKenna College Computer Science
Claremont McKenna College Computer Science CS 51 Handout 4: Problem Set 4 February 10, 2011 This problem set is due 11:50pm on Wednesday, February 16. As usual, you may hand in yours until I make my solutions
More informationGuessing Game with Objects
Objectives Lab1: Guessing Game with Objects Guessing Game with Objects 1. Practice designing and implementing an object-oriented program. 2. Use Console I/O in Java. Tasks 1. Design the program (problem
More informationLecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan
Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful
More informationLESSON 3 CONTROL STRUCTURES
LESSON CONTROL STRUCTURES PROF. JOHN P. BAUGH PROFJPBAUGH@GMAIL.COM PROFJPBAUGH.COM CONTENTS INTRODUCTION... Assumptions.... Logic, Logical Operators, AND Relational Operators..... - Logical AND (&&) Truth
More informationAP Computer Science A
AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive
More informationConditionals and Loops
Conditionals and Loops Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing steps in a loop Chapter 5 focuses on: boolean expressions conditional
More informationThe for Loop. Lesson 11
The for Loop Lesson 11 Have you ever played Tetris? You know that the game never truly ends. Blocks continue to fall one at a time, increasing in speed as you go up in levels, until the game breaks from
More informationWarm-Up: COMP Programming with Iterations 1
Warm-Up: Suppose I have a method with header: public static boolean foo(boolean a,int b) { if (a) return true; return b > 0; Which of the following are valid ways to call the method and what is the result?
More informationChapter 3. Iteration
Chapter 3 Iteration Iteration Iteration is the form of program control that allows us to repeat a section of code. For this reason this form of control is often also referred to as repetition. The programming
More informationcs1114 REVIEW of details test closed laptop period
python details DOES NOT COVER FUNCTIONS!!! This is a sample of some of the things that you are responsible for do not believe that if you know only the things on this test that they will get an A on any
More informationChapter Goals. Contents LOOPS
CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common
More informationSlide 1 CS 170 Java Programming 1 Testing Karel
CS 170 Java Programming 1 Testing Karel Introducing Unit Tests to Karel's World Slide 1 CS 170 Java Programming 1 Testing Karel Hi Everybody. This is the CS 170, Java Programming 1 lecture, Testing Karel.
More informationChapter 4 The If Then Statement
The If Then Statement Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression For example, the statement
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationCOMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1
COMP 202 Programming With Iterations CONTENT: The WHILE, DO and FOR Statements COMP 202 - Loops 1 Repetition Statements Repetition statements or iteration allow us to execute a statement multiple times
More informationDecisions in Java IF Statements
Boolean Values & Variables In order to make decisions, Java uses the concept of true and false, which are boolean values. Just as is the case with other primitive data types, we can create boolean variables
More informationLoops and Expression Types
Software and Programming I Loops and Expression Types Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline The while, for and do Loops Sections 4.1, 4.3 and 4.4 Variable Scope Section
More informationSPSS Tutorial - How to Perform an Offline License Activation on a Windows Computer
SPSS Tutorial - How to Perform an Offline License Activation on a Windows Computer Only Computers That DO NOT Have an Internet Connection Need to Do This If the computer you want to activate SPSS on does
More information5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution!
FINAL EXAM Introduction to Computer Science UA-CCI- ICSI 201--Fall13 This is a closed book and note examination, except for one 8 1/2 x 11 inch paper sheet of notes, both sides. There is no interpersonal
More information