Upload
lizette-venters
View
218
Download
0
Embed Size (px)
Citation preview
Parker Series Set One
OWEN COMPUTER SCIENCE LESSONS
By John B. Owen
All rights reserved
©2011
2Parker Series Set One
• Preface
• Lab1A – Alpha Triangle
• Lab1B – Star Triangles
• Lab1C – Pascal’s Triangle
• Lab1D – Fibonacci Loop
• Lab1E – Fibonacci Recursion
• Lab1F – Call Me Today
• Lab1G– Merge Files
• Acknowledgement of use
Table of Contents
Preface•This lab set is the first among several originally developed by the late Marjorie L. Parker, my original mentor in computer science.
•Marge taught for several years at Cypress Creek High School (Northwest Houston area) in the 1980s and 1990s and was instrumental in helping me “learn the ropes”, first with Pascal, and then C++.
•She was generous and kind, always willing to help, and a brilliant mind and teacher. Her legacy lives on in my teaching, as well as in the many students and teachers she affected throughout her life.
•This series is dedicated to her memory and for all the help she gave me in the early days.
•Thanks, Marge!Parker Series Set One 3
4Parker Series Set One
• The purpose of this set is to review all of the basic skills you have learned so far.
• It reviews loops, nested loops, if, if else, and string processing, file processing, and recursion, just a few among several important concepts.
Objective.
5Parker Series Set One
• Your are not required to use OOP (Object Oriented Programming) techniques, but are certainly welcome to do so if you wish to practice those as well.
Objective.
6Parker Series Set One
WAP (write a program) to print the following triangle using a nested loop process:
a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j
Lab P1A – Alpha Triangle
7Parker Series Set One
This process will require a row loop and a column loop, and a separate char variable to output the letters of the alphabet as shown.
a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j
Lab P1A – Alpha Triangle
8Parker Series Set One
Notice carefully that the letters start again when ‘z’ is reached, so you’ll need an if statement somewhere in the code to make this happen.
a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j
Lab P1A – Alpha Triangle
9Parker Series Set One
Here ‘s something to help you get started.int r,c;char a = ‘a’;for(r=0;r<8;r++){ for(c=0;c<=__;c++) { out.print(__); if(_____) _____; } out.println();}
Lab P1A – Alpha Triangle
10Parker Series Set One
WAP that will input 2 integers, N and R, from a data file (“labP1B.dat”). N is the number of triangles and R is the number of rows of asterisks per triangle to be printed (nested loop process). If N=2 and R=4, the output would be:
* * * * * * * * * * * * * * * * * * * *
Lab P1B – Star Triangles
11Parker Series Set One
This process will require a triple nested loop structure, one for the two triangles, the next for the rows of each triangle, and then the third for the columns of each row. Good luck!
* * * * * * * * * * * * * * * * * * * *
Lab P1B – Star Triangles
12Parker Series Set One
WAP to print the following pyramid of digits, commonly known as Pascal’s triangle, spaced exactly as shown. The output must be the a result of an algorithm (another nested loop), not hard-coding.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1
Lab P1C – Pascal’s Triangle
13Parker Series Set One
You must first calculate and store the values in an integer matrix, with the values calculated, not just hand-stored. Note that each inner value is the sum of the two values directly above it.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1
Lab P1C – Pascal’s Triangle
14Parker Series Set One
It might help to visualize the matrix in a normal fashion, as you see below.
1 0 0 0 0 01 1 0 0 0 01 2 1 0 0 01 3 3 1 0 01 4 6 4 1 01 5 10 10 5 1
Lab P1C – Pascal’s Triangle
15Parker Series Set One
To build it, first initialize the first column and diagonal values to 1, then loop through and calculate the inner values of the triangle. Ignore the outside zeroes.
1 0 0 0 0 01 1 0 0 0 01 2 1 0 0 01 3 3 1 0 01 4 6 4 1 01 5 10 10 5 1
Lab P1C – Pascal’s Triangle
16Parker Series Set One
When you have finished building the matrix, proceed to the output phase of this program.
1 0 0 0 0 01 1 0 0 0 01 2 1 0 0 01 3 3 1 0 01 4 6 4 1 01 5 10 10 5 1
Lab P1C – Pascal’s Triangle
17Parker Series Set One
Note carefully that the spacing distance from one number to the next is always four spaces. Hint: printf has a very nice way of doing this!
1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1
Lab P1C – Pascal’s Triangle
18Parker Series Set One
The indent distance decreases by two for each row, starting with ten spaces for the first row, then 8, and so on.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1
Lab P1C – Pascal’s Triangle
19Parker Series Set One
This indent distance must by dynamically calculated for each row. Hint: use the row number somehow to do this calculation. Do not manually start with 10! That is considered hard-coding…strictly verboten!
1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1
Lab P1C – Pascal’s Triangle
20Parker Series Set One
Here ‘s something to help you get started.int [][] grid = new int[6][6];int r,c;//initialize first column and diagonalfor(r=0;r<grid.length;r++){ grid[r][0]=1; grid[r][?]=1;}//now calculate the inner valuesfor(r=2;r<___;r++){ for(c=1;c<=__;c++) { grid[?][?] = ????; }}//now output the triangle…have fun!
Lab P1C – Pascal’s Triangle
21Parker Series Set One
WAP to output the first 16 numbers in the Fibonacci sequence. 1 1 2 3 5 ... You must use a loop that calculates each value!
1 1 2 3 5 8 13 …
Lab P1D – Fibonacci Loop
22Parker Series Set One
WAP to output the first 16 numbers in the Fibonacci sequence. 1 1 2 3 5 ... You must use a recursive method to calculate and output the values!
1 1 2 3 5 8 13 …
Lab P1E – Fibonacci Recursion
23Parker Series Set One
Given a file of 10-digit telephone numbers, some with 10 digits, some letters, and some mixed, WAP to convert the telephone numbers with letters to 7 digits. Save the new telephone numbers in a file that is different from the input data file. Print a table containing two columns - the original telephone number and the corresponding new telephone number.
Sample input:512WA44745913D9263513617902220CALLMETODAY
Lab P1F – Call Me Today
24Parker Series Set One
Note that only ten digits are used in the final number, even though a word-based number exceeds ten digits.
Sample input: (data file: “labp1e.dat”)512WA44745913D9263513617902220CALLMETODAY
Resulting output:512-924-4745913-392-6351361-790-2220225-563-8632
Lab P1F – Call Me Today
25Parker Series Set One
Given 2 files of first names arranged alphabetically, create a third file consisting of these two files merged and still in alphabetical order. Print out and turn in all three files and your source code, in this order…output file, two input files, source code. Sample input: Data file1: “labp1fa.dat”BUDDHA GABBY RICHEY ZACK
Data file2: “labp1fb.dat”BEN JAMIE KEGAN MADDIE ZACH
Resulting output file:BEN BUDDHA GABBY JAMIE KEGAN MADDIE RICHEY ZACH ZACK
Lab P1G – Merge Files
26Parker Series Set One
Hint: You will need two Scanner objects, one each for the two input files, and a PrintWriter object for the output file. Review the input file processing techniques in Lesson 6C. To output to a file, examine carefully the program segment shown below. PrintWriter pw = new PrintWriter(new FileWriter(“labp1f.out”));
pw.println(“Hello”);//put your processing statements//here and use the pw object to //write data to the file
pw.close();//It is very important to close the file, //otherwise it will not contain what you put //there.
Lab P1G – Merge Files
27Parker Series Set One
• You have now reviewed many important skills for basic data processing.
CONGRATULATIONS!
28Parker Series Set One
• Please copy, paste, and customize the brief acknowledgement shown below in an email to [email protected], indicating that you used this lesson.
• Feel free to include any comments you wish to offer, positive or negative.
• “I, (your name), from (educational institution) in (city, state, country) used Parker Series Set 1 on (date), understand, respect and honor all of the rights claimed by the author, Mr. John Owen, and offer the following comments..._______________.”• “THANK YOU!”
Acknowledgement of use