1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Introduction to Computing Science and Programming I
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Objectives In this chapter, you will learn about:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 10.
Computer Science 1620 Loops.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Fundamentals of Python: From First Programs Through Data Structures
1 Project 6: Cats in Tree. 2 Project 3: Cats in Tree In this project you will Create a class definition for a Cat class. Populate a BST with Cat information.
1 Project 7: Huffman Code. 2 Extend the most recent version of the Huffman Code program to include decode information in the binary output file and use.
Towson University COSC 236
Fundamentals of Python: First Programs
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
1 Project 4: Vehicle Inventory. 2 Write a program to read a text file containing information about vehicles and output the information First in the order.
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
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.
1 Project 5: New Venue. 2 New Venue Based on the posted New Venue scenario 077_Ticket_Booth_Scenarios.pdf.
CPS120: Introduction to Computer Science Decision Making in Programs.
30/10/ Iteration Loops Do While (condition is true) … Loop.
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.
11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
1 Project 5: Printing Address Labels. 2 Assignment Write a Windows forms program to display and print a set of address labels. Input from a csv file.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 Project 3 The Kumquat Society Conference. 2 Conference Registration In this project you will write a program to handle a conference registration. The.
Project 3: Ticket Printer
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
1 Project 3 String Methods. Project 3: String Methods Write a program to do the following string manipulations: Prompt the user to enter a phrase and.
1 Project 1: Tickets. 2 Class Ticket Write a definition for class Ticket. A Ticket object corresponds to a physical ticket for a stage show. Attributes.
1 Project 2: Sorting Cats. Write a C++ console application to read a text file containing information about cats and output the information to the screen.
1 Project 4 Address Lookup. Project 4 Write an ASP.NET app that permits users to retrieve addresses from a potentially large list of addresses. There.
1 Project 3: Farey Sequences. 2 Farey Sequences Further details about Farey sequences, and more examples, can be found in the Wikipedia article:
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
11 Project 2 Temperature Conversion. 22 Project 2: Temperature Conversion Write an ASP.NET Web Forms app to convert temperatures from Fahrenheit to Celsius.
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
1 Project 4: Computing Distance. 222 Computing Distance Write a program to compute the distance between two points. Recall that the distance between the.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
1 Project 4: Palindrome Detector. 2 Assignment Write a C++ program that reads a line of text from the keyboard and reports to the user whether the text.
1 Project 4: Venue Sort. 2 This project is an exercise in Using C++ strings. Operator overloading. Text File Input.
1 Project 15: Animals Using Derived Classes and Inheritance.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
1 Project 6: New Venue. 2 New Venue Based on the posted New Venue scenario
1 Project 8: Tic Tac Toe. 2 Assignment Write an HTML and JavaScript page that pemits users to play Tic Tac Toe. Both players use a single browser. Alternating.
1 Project 7: Ticket_Booth_0 Using XML. 2 Ticket Booth 0 Base Level 0 of the real Ticket Booth program. Gets information for a single venue from an XML.
1 Project 12: Cars from File. This is an extension of Project 11, Car Class You may use the posted solution for Project 11 as a starting point for this.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
CprE 185: Intro to Problem Solving (using C)
Introduction to Programming
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Conditions and Ifs BIS1523 – Lecture 8.
Please use speaker notes for additional information!
More Loops.
Do While (condition is true) … Loop
Do … Loop Until (condition is true)
3.1 Iteration Loops For … To … Next 18/01/2019.
Python programming exercise
Functions continued.
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

1 Project 7: Looping

Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on the following slides. Your.java source files for both programs will be submitted together as one assignment. 2

Program 1: Counting Spaces The file Count.java contains the skeleton of a program to read in a string (a sentence or phrase) and count the number of blank spaces in the string. Create a test directory on your computer and download Count.java from the class web site: Downloads/Project_7/ Downloads/Project_7/ Compile and run the program as downloaded. See next slide. 3

Initial Version of Count.java 4

Count.java The program currently has the declarations and initializations and prints the results. All it needs is a loop to go through the string character by character and count the space characters Since we know how many characters there are (the length of the string) we can use a count controlled loop. for loops are especially well-suited for this. 5

Add the for Loop Use the String method charAt() to examine the character at a specified index in a string. Remember that the first character is at index 0. The expression phrase.charAt(i); gets the character at position i of phrase. Add a for loop to examine each character in turn and count the space characters. Compare to the character literal ' ' That's a single space character inside single quotes. 6

Sample Run 7 Test your program on several phrases to make sure it is correct.

Next Version of Count.java Extend your program to count additional characters: a, e, s, and t (upper and lower case) Declare and initialize four additional counting variables. e.g., aCount, eCount, etc. Replace the current if with a switch to test for the 9 cases that we want to count. Upper and lower case a, e, s, and t, and space. 8

The switch Statement Your switch statement should like somthing like this: switch (ch) { case 'a': case 'A': countA++; break; case.... } 9 Add statements to output all of the counts. Compile and test your program.

Sample Runs 10

Make it more user-friendly It would be nice to have the program let the user keep entering phrases rather than having to restart it every time. To do this we need another loop surrounding the current code. That is, the current loop will be nested inside a new loop. Add an outer loop that will continue to execute as long as the user does NOT enter the phrase quit. Be sure to handle at least one phrase. After the first iteration, tell the user to enter a phrase or quit to quit. 11

Make it more user-friendly Note that all of the initializations for the counts should be inside the new outer loop. We want the counts to start over for each new phrase entered by the user. Be sure to go through the program and properly indent after adding code. With nested loops the inner loop should be indented. 12

13

Program 2: Correcting a defect In the Downloads area of the class web site there is a defective version of the extremes program from Project 6. Downloads/Project_7/ Downloads/Project_7/ This program correctly computes the min, max, and average of numbers input by the user. But it does not automatically stop after 10 inputs. It continues asking for inputs until the user enters a 0. 14

Program 2: Correcting a defect Download defective_extremes.java Rename the file as corrected_extremes.java Change the class name to corrected_extremes. Correct the defect by adding code to stop the program after 10 inputs (if not stopped previously by a 0 input.) Use a logical AND in the condition for the do loop to exit the loop if either the user has entered a 0 or 10 numbers have been input and processed. 15

Program 2: Correcting a defect Compile and test the corrected program using data from the sample runs in the assignment for Project 6: 055_Project_6_Extremes.pdf 16

17 Submission Put both of your Java source files into a folder and zip it. Submit your zipped Java source files via Canvas Assignments. Project is due by 11:59 PM Sunday, February 21 Sections 001 and 002 Monday, February 22 Sections 003 and 004

18 Ground Rules It is OK to discuss the project with other students BUT Do not share your code with other students. Before or after submitting the project. Do not copy any other student’s code. Or even look at it. Do not let anyone copy or examine your code.

Ground Rules Except for code posted on the class web site Do not copy code from the Internet or any other source (other than the textbook.) Do not ask for help on an Internet forum. If you need help, ask your instructor or a TA. Come to lab and help sessions. Write your own code.