Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COMPUTER SCIENCE MR. TAYLOR Unit Java Program for Character and String Data Using a Weight Program for Class ************************************************************************* Concept map of Computer Science: Assign_Char_String Lesson plan ********************************************************************** M R . T A Y L O R C O M P U T E R S C I E N C E R O O M MR. TAYLOR COMPUTER SCIENCE Java Coding for Character and String Data Using a Weight Program for Class Lab APK – activate prior knowledge: (5 min) <26-29> Match each value with the appropriate data type. 26> 27> 28> 29> ‘C’ 3.5 -16 “S” (A) (B) (C) (D) integer character double String Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Lesson 1 Objectives: To understand and be able to use the data types char and String. Students will model mathematic equations in Java program code. To understand how to declare a character variable, and initializing the variable. To understand how to declare a string variable, and initializing the variable. To assign a letter (character) or string to a variable. How do we create a character or string variable in Java program code? Next lesson 1A To show multiple outputs string and introducing the Escape Sequence “\n” for paragraph start. The Escape Sequence using the back slash (\) within a string (“word word\” word”). Measured Outcomes Know the difference between the declaration string, char, double and int. Next lesson 1B What the integer division symbol (/) means in Java? Measured Outcomes: How do we convert a math equation to Java program code? Understand the mathematical difference between integer versus decimal, and how Java treats integer division of a ratio versus any decimal division. Future lesson 2 How do we capture the remainder for the integer division operator? What is the modulus (%) operator in Java? What type of program is used with the modulus operator (%)? Measured Outcomes: Understand the mathematical difference between integer versus decimal, and how Java treats integer division of 2 numbers versus any decimal division. Know how modulus is used with integer division in a Java program. 1 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Concept: To understand how to declare and use a character variable, and initializing the variable. To understand how to declare and use string variable, and initializing the variable. To understand how to assign a character or string to a variable. To understand what integer division symbol (/) means and how it is used in Java? How do we capture the remainder for the integer division operator? What is the modulus (%)? How is the modulus operator used in Java? Context: The concept of variables is understood by students. The students have been exposed and have a prior knowledge of Java links with mathematics. The students have prior knowledge of mathematic integer and decimal and how they are interpreted by Java program code. There were 4 programs done prior to this unit on character and string assignments. Here a scaffold is used to reach all level of students. The scaffold allows individual learning needs by walking through the code line by line and establishing a link with math and English grammar. The right side of the scaffold charting is math and English and left side of charting worksheet is direct conversion for the Java code needed. The first lesson of the program unit is about the reserve or keywords String and Char. The students are taught that reserve words are Java library words that cannot be used as variables. The program used displays how string and char is utilized in a program called ElvisWeight. The program shows students how Java stores and prints out character and string data (one character or sentences). The program has char, string and mathematic equations. The mathematic refreshes skill through practical exercise on how variables are used in left side of an equation and stored in right side using the same variable. For example, weight = weight + 10, the right side of equation is summed and stored or a better term is assign to the right side. The mathematic equations used emphasize future skills link to program loop increments (i = i + 1) used in For, Do and While loops. The second lesson introduces the Escape Sequence. The Escape Sequence is described by the backslash (\) that allows Java to ignore reserve code, such as the quote ( “ ) within a string. For example, the code System.out.println( “Open the \“Bombay doors\” Al” ); has 2 Escape Sequence to allow the quotes around Bombay doors to printout. The output string in the System coding appears on the screen as Open the “Bombay doors” Al. Without the Escape Sequence back slash an error in syntax would result. The skill student are taught in this part of the Java Elvis Weight program models data type differences, and how data types relate to mathematic data, such as decimal, integer and character. A program is started to emphasize the use of char and String keywords. The third lesson of the same program introduces the integer division mathematic operator (/). The program unit uses the division operator. The division operator takes the integer number of the answer only. Thus, if 25 is divided by 3 in Java the answer is 8, 25/3=8. The fourth lesson in this threading sequence is the modulus command that takes the remainder, so 25 % 3 = 1. The modulus command is used for programs, such as Marbles per Child, Clock Time and Making Change. The skill for this part of the Elvis Weight program is modeling mathematic equations in Java. The Work Time Practical sheets show a sample run (page 6). The sample run and stated problem gets student use to how to compare their output run with exercise requirements. The skill here is to understand how to read, comprehend and model a program from a written paragraph. The left side of worksheet (see sample on page 10) is mathematics and English data. The right side of worksheet is Java code both guided with mentor and student work on own after acquiring lesson. The skill for each lesson is emphasized and reiterated to students with the Essential Question of each lesson. The essential question provides secondly the focus of each lesson assessment. 2 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Concept map of lesson 1: 3 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Lesson I: APK: 5 minutes int total=0; int newScore; total = 5; newScore = 8; total = total + newScore; System.out.println(“Total_” + total); What is printed out on the screen?----------------bjectives: To understand and be able to use the data types char and String To understand how to declare a character variable, and initializing the variable. To understand how to declare a string variable, and initializing the variable. To assign a letter (character) or string to a variable. O Essential Question: How do we create a character or string variable for Java program code? Materials: Handout pages 10-12 worksheets, Textpad Java editor, Java 1.4.6 SDK, and PC resources. Mini Lesson: (15 to 23 min) In Java the program words that have a predefined meaning that cannot be changed are called reserved words or Keywords. Words that are predefined cannot be used in a program for anything other than the purpose for which they are reserved. some examples: char, String, int, double, if, for, while, new, class, else, switch The instructor emphasizes the phrase data type as vocabulary item. The instructor engages students with the following data types. The instructor refreshes student knowledge that they have been using the data type int and double on their pass programs. These keywords are used for number data types int is used integers values being stored. The keyword double is used for decimal numbers being stored. Declaring a String and Character Storage Area char letter = ‘ ’; String string = “ ”; Variable name is initialized before storing anything. Here the variables are initialized with a null. Note that the variable “string” is not a reserved or keyword. Assigning a Character letter = ‘A’; letter = “A” //is an ERROR To store a character use the apostrophe. This char code = “A”; //won’t compile. Using the apostrophe the correct way. Variable name is letter and the data type is a char. Assigning a String String book = “liftoff”; Stores the string liftoff when the quote is used. Variable name is book and the data type is a string. The instructor emphasizes Java keyword ‘char’ for character, and ‘String’ for any string or more than one character. The ‘char’ keyword creates a location with the variable name. The “String” keyword creates a location to store more than one character. Modeling variable location again using Excel students used earlier. Excel has a cell with names we call A6 or D6. Relate the Excel cell to the Java ‘char’ and “String” keyword that creates location for storage value. 4 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M A program is started to emphasize the use of char and String keywords. The program is called Elvis Weight. The program has char, string and mathematic equations. The mathematic equations used emphasize future link to program Loop increments ( i = i + 1). Stop here: Let students start work on the worksheet and computer entry. Follow guided practice while students fill out the worksheets of page 10 and 11. Handout pages 8-12. Guided practice –Identifying variables in the Weight program: (10 minutes) Students fill out code chart sheet during lecture. On the chart code sheet there is model of complete statement, and incomplete statements for student to fill-in. The instructor writes the variable codes without the semicolon. Wait for student correction or ask students, “What is missing from the variable statements?” Semicolon, as an end-of-line or end-of-statement, is still a strange alphanumeric symbol / key for students. The Work Time Practical sheet is referred to. Relate what is being done on the Guided worksheet to the requirements of the program. Guided practice -Creating equations (8 minutes) Students fill out code chart sheet during lecture. On the chart code sheet there is model of complete statement, and incomplete statements for student to fill-in. Activate prior knowledge on Java programs right side is done first then left side. The students are prodded to solve each equation first before moving to on. weight = weight + 150; Equation states: Sum on right side with what is in location weight to 150, store or write over what is in location weight. In Java, once the equal sign is used, the right side is done first. Right side is assigned (stored in) to the Left side. Student Activity practice on Own - (5 minutes) Students fill out code chart sheet equations on own. The instructor verifies the students understanding before going on. The coding chart must be completed before doing work on the computer. Work time (40 minutes next 3 classes): Students set up from previous program template. Using file menu, select “save as.” The name of file is ElvisWeight. Instructor mentors students in naming their file. Closure: (2 minutes) Essential Question: How do we create a character or string variable in Java program code? Give out post-it. Ask scholars to write 1 code statement in Java they learned today? 5 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Name______________________________________Part 1. Open Textpad Open program template HelloWorld with complete header. Go to the File menu. Select ‘Save As’ and enter the file name ElvisWeight. Select the correct path for saving, that is your ID number drive: ‘C:\My Documents\Programming\YourFileName’ Make sure your file Type in the save window is ‘java’ extension. Select the Save document button. Now you have a program to augment. You can write program from start. Problem: (Requirements) Write a program that will ask for an integer value weight. The weight is added by 10, and then output to the screen. The weight is subtracted by 15 then output. The weight is then divided by 3 then output. The weight is then added to itself and multiplied by 12 and output to screen. Adjust the letters a, b, and c to line up under the letter ‘g’ of the word “ignition”. Programming 1 class must use the format / justify method to adjust letters A / B /C. See sample run. Sample Run: This program was written by Teachers World. Program was run Thu Jan 26 12:36:20 EST 2006 This program demonstrates value assignments. Enter Elvis weight--->250 Elvis weighs 250 pounds. Elvis visits an all-you-can-eat ribs joint. Elvis throws a Thanksgiving Day luau! Elvis now weighs 260 pounds. Elvis discovers aerobics. Elvis now weighs 245 pounds. Elvis falls into a washing machine during the shrink cycle! Elvis now weighs 81 pounds. Oops! Elvis clones himself 12 times! The 13th Elvis now weigh 1053 pounds. All the Elvis sat down on a Java for Kids book that turn to lava. After a loud sound an ignition started by letter A B C At the letter E they said, "We have Liftoff!" STAPLE PROGRAM and the RUN to this direction. Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. All boxed items in sample run are variables. You must use variables to printout the boxed items. 6 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Char and String Teacher’s copy Declaring a String and Character Storage Area (use for overhead) char letter = ‘ ’; String string = “ ”; //null Assigning a Character For DATA TYPE char letter = ‘A’; letter = “A” // ***Correct*** // is an ERROR Variable name is initialized with 1 empty space before storing anything. Here the variables are initialized with a null. A null is an empty space. Note that the variable “string” is not a reserved or keyword. To store a character use the apostrophe. This char code = “A”; Using the apostrophe is the correct way. Variable name is letter type is a char . and the data Assigning a String: What is the variable? What is being assigned? Stores the string liftoff when the quote String book = “liftoff”; is used. Variable name is book and the data type is a string. RESERVE WORDS OR KEYWORDS – cannot be used as variables, they are use only by the Java program code. some examples: char, String, int, double, if, for, while, new, class, else, switch Data types ‘char’ and ‘String’ usage are shown for the Input area of the program. These methods stop the program, and wait for input from the keyboard. The methods return the data from the keyboard after the Enter key is pressed. Method (to input data from keyboard) readLine( ) readChar( ) readInt( ) readDouble( ) Description reads and returns a String reads and returns a character reads and returns an integer reads and returns a decimal 7 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Char and String Student Handout for class folder Declaring a String and Character Storage Area (use for overhead) char letter = ‘ ’; String string = “ ”; //null Assigning a Character For DATA TYPE char letter = ‘A’; letter = “A” // ***Correct*** // is an ERROR Variable name is initialized with an empty space before storing anything. Here the variables are initialized with a null. To store a data type character use the apostrophe. This char code = “A”; Using _______________________ ___________________ Variable name is type is a . and the data Assigning a String: What is the variable? What is being assigned? Stores the string liftoff when the quote String book = “liftoff”; is used. Variable name is ________ and the data type is a ____________. RESERVE WORDS OR KEYWORDS – reserve words cannot be used as variables, they are used only by the Java program. some examples: char, String, int, double, if, for, while, new, class, else, switch Data types ‘char’ and ‘String’ usage are shown for the Input area of the program. These methods stop the program, and wait for input from the keyboard. The methods return the data from the keyboard after the Enter key is pressed. Method (to input data from keyboard) readLine( ) readChar( ) readInt( ) readDouble( ) Description reads and returns a reads and returns a reads and returns an reads and returns a 8 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Guided Worksheet 1: Now create the code for Elvis weight First write out the given results below for known and unknown variables Weight is a known integer Weight = 250 (model on overhead) Create a name to store the phrase, “Java for kids book” Create a name to store the word (with a space at start), “ Liftoff!” Java Program code //***Declare Identifiers or Variables int ; String book = “ ” String liftOff = “ ”; char count1 = ‘ ’; Create a name to store each of the characters A, B, C, and E Enter Elvis weight Weight of 250 is read in from keyboard //***Input S.o.p(“Enter Elvis weight weight = reader. “) Place each string in its own Output //***Process / Output method Elvis weighs 250 pounds. S.o.p(“Elvis weighs “+weight+“ pounds. “); (Paragraph) Elvis visits an all-you-can-eat ribs joint. S.o.p(“ __________________________ _________________________ ) Elvis Throws a Thanks giving Day luau! S.o.p(“ __________________________ _________________________ ) All boxed items in sample run are variables. You must use variables to printout the all boxed items. 9 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Guided Worksheet 1: Now create the code for Elvis weight Enter the equation to add 10 pounds to weight: weight = 250 + 10 Elvis weighs 260 pounds. Create the next 2 lines of data from the requirements work sheets (Paragraph) Elvis discovers aerobics. Enter the equation to subtract 15 pounds from weight: = 260 - 15 Elvis now weighs 245 pounds weight = weight + S.o.p(“Elvis weighs ”+ + “ “); S.o.p(“ __________________________ _________________________ ) weight = ― S.o.p(“Elvis now weighs ”+ + “ “); //Sample Output only System.out.println (“Elvis now weighs ” + weight +“ pounds. “); What is On Screen? All boxed items in sample run are variables. You must use variables to printout the boxed items. 10 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Acquired Worksheet 1: Now create the program code for Elvis Weight from scaffold charting worksheet All data below is typed in at computer. Fill out the areas that match your scaffold charting area (//). /* **** File Identification ******************************************** Period: 2,4,6,7,8 Written By: YourName Date: January 30, 2006 Description: This program will (I) Elvis weight is entered as an integer only. For this program we are not interested in any remainder after a division. (P/O) assigns weight and displays results. Java drops the remainders for the integer division operator. Understanding loop assignment in programming. ********************************************************************* */ //import BreezySwing.*; //import TerminalIO.KeyboardReader; class YourFileName //Name of class definition { //Marks the beginning of the of this class definition public static void main (String [] args) { //Begin of program //***Declarations*** – Declare all variable here KeyboardReader reader = new KeyboardReader(); ____________________________________________________________________________________________________________ ____________________________________________________________________________________________________________ ____________________________________________________________________________________________________________ //Program Information System.out.println("This program was written by Teachers World."); System.out.println("Program was run " + new java.util.Date()); System.out.println("This program demonstrates value assignments."); System.out.println(); //***INPUT ____________________________________________________________________________ ____________________________________________________________________________ //***PROCESS / OUTPUT ______________________________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ // ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ } //End of public static void main method } //end of class definition 11 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Concept map of lesson 1A: Understanding Escape Sequences 12 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Lesson 1A (second lesson) APK: To assign a letter or string to a variable. Which of the following are type char? (prior knowledge) a. ‘H’ f. ‘%’ b. B g. “r56” c. “J” h. 9 d. ‘he’ i. ‘ ‘ e. ‘5’ j. “y” bjectives: To show multiple outputs string and introducing the Escape Sequence “\n” for paragraph start. The Escape Sequence using the back slash (\) within a string (“word word\” word”). O Materials: Handout page 10, pages 15-16 worksheets, Textpad Java editor, Java 1.4.6 SDK, and PC resources. Essential Question: How do we use the linefeed Escape Sequence? Escape Sequences \b \t \n \f \r \” \’ \\ Explanation Backspace Horizontal tab Linefeed (currently using) Form feed Carriage return or enter key Double quote Single quote Backslash In the program ElvisWeight the Escape Sequence used is the linefeed (\n). This allows the program output to start a new paragraph. The linefeed (\n) will only be used between the double quotes or within a string. See your program ElvisWeight. Go to your work sheets and fill out the use of the Escape Sequence. Finish typing in program on computers. Understand how the program uses the linefeed (\n). Essential Question: How do we use the linefeed Escape Sequence? 13 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Guided Worksheet 2: Now create the Declare, Input and Process / Output for Elvis weight Understanding Escape Sequences……… Place each string in its own Output // Process / Output method, use the Escape Sequence to create a paragraph for “Elvis falls…” (Paragraph)Elvis falls into a washing S.o.p(“ \n _____________________ machine during the shrink cycle! _____________________________ ______________________________ ) Enter the equation to divide weight by weight = weight 3: weigh = 245 / 3 Elvis now weighs 81 pounds S.o.p( (Paragraph) Oops! Elvis clones himself 12 times! Enter the equation to clone 12 times and add 81 again for the 13th clone weight = 81 + 81 * 12 The 13th Elvis now weighs 1053 pounds. All the Elvis sat down on a Java for Kids book that turn to lava. Create the next 2 lines of data from the requirements work sheets (paragraph) / ) S.o.p(“ __________________________ _________________________ ) weight = S.o.p(“The 13th Elvis now weighs” + + “ S.o.p(“All the Elvis sat down on a” + _________ + “ “); “); S.o.p( __________________________ _________________________ ) Set countX variables for output: F.j (‘l’ or ‘r’, count1, 99) S.o.p(Format. justify ( (paragraph) At the letter E they said, “We have Liftoff!” S.o.p (“ 14 At the letter + + “they said, “We have ” + + ” ); ) ); M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Guided Worksheet 2: Now create the Declare, Input and Process / Output for Elvis weight Understanding Escape Sequences……… //Sample Output S.o.p (“ \n At the letter “ + count4 + “they said, \“We have ” + liftOff + “\” ” ); On Screen: In the program ElvisWeight the Escape Sequence used is the linefeed (\n). This allows the program output to start a new paragraph. The linefeed (\n) will only be used between the double quotes or within a string. See your program ElvisWeight. The Escape Sequence to allow a quote to print out is \”. Escape Sequences \b \t \n \f \r \” \’ \\ Explanation Backspace Horizontal tab Linefeed (currently using) Form feed Carriage return or enter key Double quote Single quote Backslash Essential Question: How do we use the linefeed Escape Sequence? All boxed items in sample run are variables. You must use variables to printout the boxed items 15 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Acquired Worksheet 2: Now create the program code for Elvis Weight from scaffold charting worksheet. All data below is typed in at computer. Fill out the areas that match your scaffold charting area (//). /* **** File Identification ******************************************** Period: 2,4,6,7,8 Written By: YourName Date: January 30, 2006 Description: This program will (I) Elvis weight is entered as an integer only. For this program we are not interested in any remainder after a division. (P/O) assigns weight and displays results. Java drops the remainders for the integer division operator. Understanding loop assignment in programming. ********************************************************************* */ //import BreezySwing.*; //import TerminalIO.KeyboardReader; class YourFileName //Name of class definition { //Marks the beginning of the of this class definition public static void main (String [] args) { //Begin of program //***Declarations*** – Declare all variable here KeyboardReader reader = new KeyboardReader(); ____________________________________________________________________________________________________________ ____________________________________________________________________________________________________________ ____________________________________________________________________________________________________________ //Program Information System.out.println("This program was written by Teachers World."); System.out.println("Program was run " + new java.util.Date()); System.out.println("This program demonstrates value assignments."); System.out.println(); //INPUT ____________________________________________________________________________ ____________________________________________________________________________ //PROCESS / OUTPUT ______________________________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ } //End of public static void main method } //end of class definition 16 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Work Time Practical (45 min) Work Time: Practical Exercise (60 min), --Teacher’s Copy /* **** File Identification ******************************************** Period: 2,4,6,7,8 Written By: YourName Date: January 3, 2006 Description: This program will (I) Elvis weight is entered as an integer only (P/O) assigns weight and displays results. Understanding loop assignment in programming. ********************************************************************* */ import BreezySwing.*; // justify output import TerminalIO.KeyboardReader; // allows keyboard input //import TurtleGraphics.*; // allows graphics class Elvis_Weight1 { public static void main (String [ ] args) { // *** Declarations *** KeyboardReader reader = new KeyboardReader(); int weight; //start with 250lbs, 150, 200, 300 String book = " "; String liftOff = " "; char count1 = ' '; //initialize with nulls char count2 = ' '; char count3 = ' '; char count4 = ' '; // *** Info *** System.out.println("This program was written by Teachers World."); System.out.println("Program was run " + new java.util.Date()); System.out.println("This program demonstrates value assignments."); System.out.println(); // *** Input *** System.out.print("Enter Elvis weight--->"); weight = reader.readInt(); // letter for count down book = "Java for Kids book"; liftOff = " Liftoff!"; count1 = 'A'; count2 = 'B'; count3 = 'C'; count4 = 'E'; // *** Process and Output *** System.out.println("Elvis weighs " + weight + " pounds."); System.out.println("\nElvis visits an all-you-can-eat ribs joint."); System.out.println("Elvis throws a Thanksgiving Day luau!"); weight = weight + 10; 17 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M System.out.println("Elvis now weighs " + weight + " pounds."); System.out.println("\nElvis discovers aerobics."); weight = weight - 15; //End of Lesson I System.out.println("Elvis now weighs " + weight + " pounds."); System.out.println("\nElvis falls into a washing machine during " + "the shrink cycle!"); weight = weight / 3; System.out.println("Elvis now weighs " + weight + " pounds."); System.out.println("\nOops! Elvis clones himself 12 times!"); weight = weight + weight * 12; System.out.println("The 13 Elvis now weigh " + weight + " pounds."); System.out.println( ); //End of Lesson II System.out.println("All the Elvis sat down on a " + book + " that turn to lava."); System.out.println("After a loud sound an ignition started by letter "); //List of choices System.out.println(Format.justify('r', count1, 24) + "\n" + Format.justify('r', count2, 24) + "\n" + Format.justify('r', count3, 24)); System.out.println("\nAt the letter " + count4 + " they said, \"we have" + liftOff + "\""); } } (Cadenhead, 2006) 18 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. a. b. c. d. e. Examples of Strings: “Hello Joe!” “45.67” “” empty string—contains no characters “Good morning! How are you?” “ ” – a null, or 1 blank space Assessment: (10 min) What type of data would be used to print each of the following? Data types: int, double, String and char a. your age b. your grade point average c. your name d. a test score e. the average test score f. your hourly pay g. your letter grade 19 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Concept map of lesson 1C: Understanding integer division 20 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Lesson 1C (third lesson) APK: (7 min) int firstNumber = 21, secondNumber = 6; int sum = firstNumber + secondNumber; // What is stored in variable sum? int difference = firstNumber – secondNumber; // What is stored in variable difference? int multiply = firstNumber * secondNumber; // What is stored in variable multiply? Objective To explain integer division symbol (/) in Java and what it is used for. The instructor explains the essential question of integer division. The students are now using the program equation and must be made aware of what the integer division is doing. Materials: Handout page 10 and page 23-24 worksheets, Textpad Java editor, Java 1.4.6 SDK, and PC resources. Essential Question: Why do we use the integer division? How is the integer division accomplished? (Anytime we want to ignore a remainder or decimal, such as 115 cents / 10 cents yields 11 dimes). The 5 cents is dropped. If the numerator or denominator is a decimal the answer is 11.5. In the program, Elvis weight does not need to be accurate to the nearest tenth or decimal value. In the wait program Elvis weight does not need to be accurate to the nearest tenth or decimal value. Integer division is accomplished by making sure the numerator and denominator are integer quantities / values. Guided practice -Creating equations (8 minutes) Students fill out code chart sheet during lecture. On the chart code sheet there is model of complete statement, and incomplete statements for student to fill-in. Activate prior knowledge on Java programs right side is done first then left side. The students are prodded to solve each equation first before moving to on. Work Time then finish program: Essential Question: How is the integer division accomplished? Why do we use the integer division? APK: Divide the following (student handouts) int firstNumber=21, secondNumber = 6; int division = firstNumber / secondNumber; // What is stored in variable division? Student Activity practice on Own - (10 minutes) int weight = 265; weight = weight / 3; the variable weight, on the left of equal sign has 88 and the remainder of 1 is dropped. Right is done first then the left of the equal sign is done next Equation states: The variable weight is divided by 3. An integer division is accomplished and the result is assigned to the left side of equation. Why is the variable weight used on both sides of the equations? 1) Saves a variable declaration which saves memory space. 2) Remember: In Java, once the equal sign is used, the right side is done first; Then Left side is done. Right side is assigned (stored in) to the Left side. What is stored in the left side of equation? What is truncated or dropped? 21 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Review the integer division after the “Elvis falls into a washing machine.” Essential Question: Why do we use the integer division? How is the integer division accomplished? Why? Anytime we want to ignore a remainder or decimal value, such as 115 cents / 10 cents yields 11 dimes. The 5 cents is dropped. If the numerator or denominator is a decimal the answer is 11.5. In the program, Elvis weight does not need to be accurate to the nearest tenth or decimal value. How? Integer division is accomplished by making sure the numerator and denominator are integer quantities / values. Closure: Go back to division problem and answer correctly: (use as ticket out the door) 1) Divide the following (student handouts) int firstNumber=21, secondNumber = 6; int division = firstNumber / secondNumber; // What is stored in variable division? 2) Divide the following (student handouts) int firstNumber=10, secondNumber = 3; int division = firstNumber / secondNumber; // What is stored in variable division? 22 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Guided Worksheet 3: Now create the output for Elvis weight for Integer Division: Enter Elvis weight Weight of 250 is read in from keyboard Add to input the assigned variables Assign the string “Java for Kids book” to variable book Assign the string “ Liftoff!” to variable liftOff //Input S.o.p(“Enter Elvis weight weight = reader. // letter for count down book = " “) "; liftOff = Assign the characters A, B, C, and E to count1 thru count4: type char count1 = 'A'; Observe the integer division equation. Why is it 81 and not 81.6667? (Paragraph)Elvis falls into a washing machine during the shrink cycle! // Process / Output S.o.p(“ \n _____________________ _____________________________ ______________________________ ) ***Enter the equation to divide weight weight = weight /3 by 3: weigh = 245 / 3, R -2 is numerator and denominator are both dropped integer: thus, the remainder is drop Elvis now weighs 81 pounds S.o.p( ) For 5 / 5 Quiz points: In the Quiz points: Declaration area change the integer declaration of weight to a double (or double weight;). What happens to “Elvis now weighs 81 pounds? Change the declaration area back to integer: int weight; 23 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Create the next 2 lines of data from the requirements work sheets (paragraph) S.o.p( __________________________ _________________________ ) Set countX variables for output: F.j (‘l’ or ‘r’, count1, 99) S.o.p(Format. justify ( (paragraph) At the letter E they said, “We have Liftoff!” System.o.p (“ At the letter + + they said, “We have + ) ) ); Essential Question: Why do we use the integer division? How is the integer division accomplished? 24 ” M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Concept map of lesson 1A: Understanding Modulus Operator 25 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Lesson 2 : New to thread modulus operator to the integer division APK: int firstNumber = 21, secondNumber = 6; int sum = firstNumber + secondNumber; // What is stored in variable sum? int difference = firstNumber – secondNumber; // What is stored in variable difference? int multiply = firstNumber * secondNumber; // What is stored in variable multiply? Add this: int division = firstNumber / secondNumber; // What is stored in variable division? Objective To explain modulus operator use with the integer division symbol (/) in Java. How we use the modulus operator in programs. Mini Lesson (10 minutes) The instructor explains the essential question of integer division. The students must be made aware of how the integer division and modulus operator are used together. The first program using the modulus command is the MarblePerChild program. Materials: Handout page 25-26 worksheets, Textpad Java editor, Java 1.4.6 SDK, and PC resources. Essential Question: Why do we use the modulus operator? (After using the integer division to make change, such as 115 cents / 10 cents yields 11 dimes. The remainder is gathered by the modulus command (%). Thus, 115/10 = 5 cents or a nickel in change. The modulus is used to also divvy up seconds and minutes from hour). Guided work sheet (20 minutes) Instruct models how the program starts and the use of the modulus operator. Closure: consensus to the essential question Essential Question: Why do we use the modulus operator? (After using the integer division to make change, the program if can divide the marbles per kid. The remainder is gathered by the modulus command (%). Thus, 115/10 = 5 cents or a nickel in change.) Instructor introduces the modulus operator with 5-7 minutes lesson on the essential question paragraph. The instructor guides the student with The scaffold below instructs the student on chart slot (***divide and drop remainder***) to enter the modulus command (%) between “numberOfMarble numberOfChildren.” 26 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Work Time: Practical (20 min) Open Textpad Open program template HelloWorld with complete header. Go to the File menu. Select ‘Save As’ and enter the file name DivvyMarbles. Select the correct path for saving, that is your ID number drive: ‘C:\My Documents\Programming\ DivvyMarbles Make sure your file Type in the save window is ‘java’ extension. Select the Save document button. Now you have a program to augment. You can write program from start. Problem: Write a program that will ask for the number of marbles and number of friends. An integer division is used to retrieve marbles per friend. (The number of marbles is the numerator and the number of friends is the denominator). The program also captures the number of marbles left over. The number of marbles for friends and number of marbles left is output to screen. In addition, you have 109 marbles and 6 friends. What is the output run or outcome? Sample Run: -----------------------------------------This program will divide up Marbles This program was written by Teacher's World Program was run at Fri Jan 27 14:56:12 EST 2006 -----------------------------------------Enter the number of marbles: Enter number of children: 93 5 Give each friend 18 marbles. Marbles left in your bowl is 3 out of 93 -----------------------------------------/*******STAPLE TO YOUR OUTPUT RUN**************** Write a segment program with Java code Written by: ___________________________________________________________________ Period: ______________________________________________________________________ Date: _______________________________________________________________________ Description: This program will ____________________________________________________ (I- what is given) ____________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ (P- equation(s)) ______________________________________ ______________________________________________________ ______________________________________________________ (O- print evaluations) ________________________________ ______________________________________________________ ______________________________________________________ 27 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Guided Worksheet 4: Now create the output for Marble Divvy Up program. First write out the given results below in Math for known and unknown variables numberOfMarble int numberOfChildren; Marbles Per Child Java Program code //Declare Identifiers or Variables int numberOfMarble; numberOfChildren; int = 0; Marbles Left Over Enter the number of marbles: ; // Input S.o.print(" "); = reader.readInt( ); Enter number of children: (" Number Of Children ); = reader.read ( ); Here is the equation that is used //Process … compute final cost marblesPerChild = numberOfMarble/numberOfChildren ***divide and drop remainder*** marblesLeftOver = numberOfMarble numberOfChildren Give each friend quantity of marbles Marbles left in your bowl quantity out of total marbles //Output System.out.println ("Give each friend " + marblesPerChild + " marbles" System.out.println ("Marbles left in your bowl " + + " out of " + ); 28 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Acquired Worksheet 4: Now create the program code for DivvyMarble from the scaffold charting worksheet. All data below is typed in at computer. Fill out the areas that match your scaffold charting area (//). /* **** File Identification ******************************************** Period: 2,4,6,7,8 Written By: YourName Date: January 30, 2006 Description: This program will (I) Ask user for input number of children and number of marbles. (P) Computes the final number Of Marble. The program calculates the final number of Marble using identifiers for numberOfChildren and marblePerChild. All calculations are stored in the variable or identifier called marblesPerChild and marblesLeftOver. (O) The program prints out the computed value final numberOfMarble and A string is appended to the variable output for identity. *******************Marbles************************************ */ //import BreezySwing.*; //import TerminalIO.KeyboardReader; class YourFileName //Name of class definition { //Marks the beginning of the of this class definition public static void main (String [] args) { //Begin of program //***Declarations*** – Declare all variable here KeyboardReader reader = new KeyboardReader(); __________________________________________________________________________________________ _______________________________________________________________________________________________________________ _______________________________________________________________________________________________________________ _______________________________________________________________________________________________________________ _______________________________________________________________________________________________________________ //Program Information System.out.println("This program was written by Teachers World."); System.out.println("Program was run " + new java.util.Date()); System.out.println("This program demonstrates divvy up marbles."); System.out.println("Using the integer division and modulus operator."); System.out.println(); // INPUT ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ // PROCESS ______________________________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ // OUTPUT ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ } //End of public static void main method } //end of class definition 29 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Work Time: Practical Exercise (60 min), Teachers Copy /************ Tax Charge ******************************************* Period: 2,4,6,7,8 Written By: YourName Date: January 30, 2006 Description: This program will (I) Ask user for input number of children and number of marbles. (P) Computes the final number Of Marble. The program calculates the final number of Marble using identifiers for numberOfChildren and marblePerChild. All calculations are stored in the variable or identifier called marblesPerChild and marblesLeftOver. (O) The program prints out the computed value final numberOfMarble and A string is appended to the variable output for identity. ******************************Work sheet**********************************/ //import BreezySwing.*; // justify output import TerminalIO.KeyboardReader; // allows keyboard input //import TurtleGraphics.*; // allows graphics class MarbleDivvyUp // the filename { public static void main (String [ ] args) { // ***** Body of Program - engages the user System.out.println ("------------------------------------------"); System.out.println ("This program was written by Teacher's World"); System.out.println ("Program was run at " + new java.util.Date( )); System.out.println ("This program will divide up Marbles"); System.out.println ("------------------------------------------"); System.out.println ( ); //Declarations of variables int numberOfMarble; //known inputs int numberOfChildren; int marblesPerChild = 0; //unknown outputs int marblesLeftOver = 0; KeyboardReader reader = new KeyboardReader( ); // ***** INPUT - reader.readInt( ) etc... System.out.print("Enter the number of marbles: "); numberOfMarble = reader.readInt( ); System.out.print("Enter number of children: "); numberOfChildren= reader.readInt( ); // ***** PROCESS - computes the final number of Marbles marblesPerChild = numberOfMarble / numberOfChildren; marblesLeftOver = numberOfMarble % marblesPerChild; // ***** OUTPUT System.out.println ( ); System.out.println ("Give each friend " + marblesPerChild + " marbles"); System.out.println ("Marbles left in your bowl " + marblesLeftOver + " out of " + numberOfMarble); System.out.println ("------------------------------------------"); } // Ends program main method } //End of class definition (Lowe, 2005) 30 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Integer Division Assessment: Compute the correct answer for each mathematical expression (as it would be computed in Java): total 1> total = 20 - 4 * 10 / 3 (A) 19 2> total = (20 - 4) * (10 / 3) (B) 7 3> total = (20 - 4) + (10 * 0.3) (C) 48 4> total = ((20 - 4) * 10) / 3 (D) 53.33 5> total = ((20 - 4) * 10) /3.0 (E) 53 ________ 6. When dividing the numerator and dominator are integers. What happens in Java? A. B. C. D. ________ The remainder is saved and used Java won’t compile Run Java Application will not work The remainder is dropped or truncated 7. What is the modulus operator (%) used for in Java? A. B. C. D. Only the remainder is saved and used Java won’t compile Run Java Application will not work The remainder is dropped or truncated 31 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Open Textpad Open program template HelloWorld with complete header. Go to the File menu. Select ‘Save As’ and enter the file name NickelDimeChange. Select the correct path for saving, that is your ID number drive: ‘C:\My Documents\Programming\ NickelDimeChange Make sure your file Type in the save window is ‘java’ extension. Select the Save document button. Now you have a program to augment. You can write program from start. Problem: Understanding the use of Integer Division and Modulus Write a program that will ask for the given change to a customer. An integer division is used to retrieve dimes per customer. (The total change is the numerator and the number of dimes needed is the denominator). The number is dimes will be generated by a random generator. The program also captures the number of change left. The number of dimes per customer, the number of nickels per customer and number of change left is output to screen with the total change. Sample Run: -----------------------------------------This program will divvy up change in dimes and nickels This program was written by Teacher's World Program was run at Fri Jan 27 14:56:12 EST 2006 -----------------------------------------Enter the total amount of change (cents): 257 Give the customer 25 dimes. And 1 nickel(s) and 2 cents out of 257 cents. -----------------------------------------/*******STAPLE TO YOUR OUTPUT RUN****************/ Write a Segment Program using Java code Written by: ___________________________________________________________________ Period: ______________________________________________________________________ Date: _______________________________________________________________________ Description: This program will ____________________________________________________ (I- what is given) ____________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ (P- equation(s)) ______________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ (O- print evaluations) _______________________________ ______________________________________________________ ______________________________________________________ ______________________________________________________ 32 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Acquired Input Worksheet: Now create the program code for NickelDimeChange. All data below is typed in at computer. /* **** File Identification ******************************************** Period: 2,4,6,7,8 Written By: YourName Date: January 30, 2006 Description: This program will (I) Ask for (P) Compute the number of (O) Print out dimes… *******************Change************************************ */ //import BreezySwing.*; import TerminalIO.KeyboardReader; class YourFileName //Name of class definition { //Marks the beginning of the of this class definition public static void main (String [] args) { //Begin of program //***Declarations*** – Declare all variable here KeyboardReader reader = new KeyboardReader(); __________________________________________________________________________________________ _______________________________________________________________________________________________________________ _______________________________________________________________________________________________________________ _______________________________________________________________________________________________________________ _______________________________________________________________________________________________________________ //Program Information System.out.println("This program was written by Teachers World."); System.out.println("Program was run " + new java.util.Date()); System.out.println("This program demonstrates divvy up change."); System.out.println("Using the integer division and modulus operator."); System.out.println(); // INPUT ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ // PROCESS ______________________________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ // OUTPUT ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ ______________________________________________________________________________________________ } //End of public static void main method } //end of class definition 33 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M Mathematic Standard 3 and 5 Students will understand concepts and become proficient with the skills of mathematics. Students will communicate and reason mathematically. Students will become problem solvers by using computer programming and other appropriate tools and strategies. With the aid of mathematics, students will develop an understanding of computer programming, and attain some facility in writing computer programs. The modeling tool used is Java programming language. Rubric: 25 points for Program Assessment Target Acceptable Hand Written Program Structure is comparable to type code: 1 public class down is identical to type code: 3 Comments are Comparable: 1 Program must use 3 structures for coding. Input, Process, Output: 3 Format to program template HelloWorld All indents are appropriate: 1 Declarations for Variables Good Comments: 1 Variables are correct for constant vs. variable: 1 Initialization are used only, no calculations in declaration area: 1 Input code Area Inputs are correct: 2 Inputs are needed: 1 Process area – Equation is correct: 2 Model of mathematic equation in Java code Use of correct quantity of variables: 1 Output code Area Is consistent to exercise output: 1 Variable used for output run: 1 Output format done:1 Output correct:3 Output clear: 1 RUNNING PROGRAM 34 Unacceptable Structure is incomparable to type code: 1 Written equation is not the same as typed code: 1 Comments are incomparable with type code: 1 No structure. 3 Incomplete Structured: 1 per Grammatical errors: 1 No indents were used: 1 None or sparse comment: 2 Un-needed variable(s): 1 Points 5 3 2 2 Calculations in area: 1 Variable types not consistence with Exercise: 1 Using store constant location instead of variable location: 1 Output interactive inconsistent with exercise: 1 Calculations in Input area: 1 No equation: 3 All equations are not present: 1 Unfinished equation or too much equation located in declared statement: 2 Is inconsistent to exercise code: 1 Variables are not used for output: 2 No format: 2-3 Output incomplete: 2-3 Output not clear: 1-3 Output grammar errors: 1 3 3 2 5 M R . T A Y L O R C O M P U T E R S C I E N C E R O O M C I T A T I O N : (Cadenhead, 2006; Helios, 2004; Inspiration, 2003; Lambert & Osborne, 2003; Lowe, 2005; Sierra & Bates, 2005; Sun, 2006) Citations Cadenhead, R. (2006). Sams teach yourself programming with Java in 24 hours (4th ed.). Indianapolis, Ind.: Sams Publishing. Helios. (2004). TextPad (Version 4.7.3): Helios Software Solutions. Inspiration. (2003). Inspiration 8 (Version 8.0): Inspiration 8 Software, Inc. Lambert, K. A., & Osborne, M. (2003). Fundamentals of java (2nd ed.). Pacific Grove, Calif.: Brooks/Cole. Lowe, D. (2005). Java all-in-one desk reference for dummies. Hoboken, N.J. ; [Great Britain]: Wiley. Sierra, K., & Bates, B. (2005). Head first Java (2nd ed.). Sebastopol, CA: O'Reilly. Sun, M. (2006). Java(TM) 2 SDK (Version 1.4): Sun Microsystem, Inc. 35