Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parker Series Set One By John B. Owen All rights reserved ©2011.

Similar presentations


Presentation on theme: "Parker Series Set One By John B. Owen All rights reserved ©2011."— Presentation transcript:

1 Parker Series Set One By John B. Owen All rights reserved ©2011

2 Preface Lab1A – Alpha Triangle Lab1B – Star Triangles Lab1B – Star Triangles Lab1C – Pascals Triangle Lab1D – Fibonacci Loop Lab1E – Fibonacci Recursion Lab1F – Call Me Today Lab1G– Merge Files Acknowledgement of use Table of Contents Parker Series Set One2

3 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 One3

4 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. Parker Series Set One4

5 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. Parker Series Set One5

6 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 Parker Series Set One6

7 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 Parker Series Set One7

8 Notice carefully that the letters start again when z is reached, so youll 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 Parker Series Set One8

9 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 Parker Series Set One9

10 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 Parker Series Set One10

11 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 Parker Series Set One11

12 WAP to print the following pyramid of digits, commonly known as Pascals 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 1 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One12

13 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 1 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One13

14 It might help to visualize the matrix in a normal fashion, as you see below. 1 0 0 0 0 0 1 1 0 0 0 0 1 2 1 0 0 0 1 3 3 1 0 0 1 4 6 4 1 0 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One14

15 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 0 1 1 0 0 0 0 1 2 1 0 0 0 1 3 3 1 0 0 1 4 6 4 1 0 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One15

16 When you have finished building the matrix, proceed to the output phase of this program. 1 0 0 0 0 0 1 1 0 0 0 0 1 2 1 0 0 0 1 3 3 1 0 0 1 4 6 4 1 0 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One16

17 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 1 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One17

18 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 1 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One18

19 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 1 1 5 10 10 5 1 Lab P1C – Pascals Triangle Parker Series Set One19

20 Here s something to help you get started. int [][] grid = new int[6][6]; int r,c; //initialize first column and diagonal for(r=0;r<grid.length;r++) { grid[r][0]=1; grid[r][?]=1;} //now calculate the inner values for(r=2;r<___;r++) { for(c=1;c<=__;c++) { grid[?][?] = ????; } //now output the triangle…have fun! Lab P1C – Pascals Triangle Parker Series Set One20

21 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 Parker Series Set One21

22 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 Parker Series Set One22

23 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: 512WA44745 913D926351 3617902220 CALLMETODAY Lab P1F – Call Me Today Parker Series Set One23

24 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) 512WA44745 913D926351 3617902220 CALLMETODAY Resulting output: 512-924-4745 913-392-6351 361-790-2220 225-563-8632 Lab P1F – Call Me Today Parker Series Set One24

25 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 Parker Series Set One25

26 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 Parker Series Set One26

27 You have now reviewed many important skills for basic data processing. CONGRATULATIONS! Parker Series Set One27

28 Please copy, paste, and customize the brief acknowledgement shown below in an email to jowen@acisd.org, indicating that you used this lesson. jowen@acisd.org 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 Parker Series Set One28


Download ppt "Parker Series Set One By John B. Owen All rights reserved ©2011."

Similar presentations


Ads by Google