ALGORITHM DEVELOPMENT CS 139
Popular in Course
Popular in ComputerScienence
This 14 page Class Notes was uploaded by Mable Windler on Saturday September 26, 2015. The Class Notes belongs to CS 139 at James Madison University taught by Mohamed Aboutabl in Fall. Since its upload, it has received 10 views. For similar materials see /class/214130/cs-139-james-madison-university in ComputerScienence at James Madison University.
Reviews for ALGORITHM DEVELOPMENT
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 09/26/15
CS 139 7 Algorithm Development Notes 090604 Important terms from today s lecture in no particular order program application comment whitespace indenting block vs inline comment class method identifier block statement parameter reserved word case sensitive declaration assignment 1nt page 1 constanUnamed constant strongly typed language data type primitive data type literal String literal CS 139 7 Algorithm Development 090604 llotes In the Java programming language I A program is made up of one or more classes I A class contains one or more methods I A method contains program statements Java Program Structure cements about the class public class MyProgram class header class body Comments can be placed almost anywhere page 2 CS 139 7 Algorithm Development 090604 Notes Java Program Structure comments about the class public class MyProgram comments about the method public static void main String args method body method header page 3 CS 139 7 Algorithm Development 090604 Notes Basic program heading inline comments Class name begins with capital letter Inhneconunenm Comment for overall program and the method Every statement ends in a semicolon Every statement in java has a particular syntax See Appendix L for complete syntax Lincolnjava Author LewisLoftus Demonstrates the basic structure of a Java application public class Lincoln Prints a presidential quote public static void main String args Systemoutprintln quotA quote by Abraham Lincolnquot Systemoutprintln quotWhatever you are be a good onequot page 4 CS 139 7 Algorithm Development 090604 Notes Second example is not formatted for readability Lincoln2 java Author LewisLoftus Demonstrates a poorly formatted though valid program public class Lincoln2public static void mainStringargs SystemoutprintlnquotA quote by Abraham Lincolnquot SystemoutprintlnquotWhatever you are be a good onequot page 5 CS 139 7 Algorithm Development 090604 Notes Third example incorporates our classroom style Name LewisLoftus Date 090604 Assignment PianoKeysjava Header to include the author s name date and assignment A description of the overall class If this were a programming test the header would have been followed by the reference PianoKeys class Demonstrates the declaration initialization and use of an integer variable Class name is in public class PianoKeys Titlecase Prints the number of keys on a piano public static void main String args int keys keys will hold the number of piano keys keys 88 number of keys on a standard piano Systemoutprintln quotA piano has quot keys quot keysquot Main method is described using javadoc format method name main is in lower case keys is a descriptive variable name in lower case Declaration associates a memory location with a name and a length type Declaration and initialization are separated into two statements Everything Within the class block is indented three spaces Everything Within the main block is indented three additional spaces page 6 CS 139 7 Algorithm Development 090604 Notes Final example shows more complex program using successive assignment Author LewisLoftus Date 090604 Assignment Geometryjava Demonstrates the use of an assignment statement to change the value stored in a variable public class Geometry Prints the number of sides of several geometric shapes public static void main String args int sides stores number of sides for the shape Begin changing number of sides and printing shape sides 7 Systemoutprintln quotA heptagon has quot sides quot sidesquot sides 10 Systemoutprintln quotA decagon has quot sides quot sidesquot sides 12 Systemoutprintln quotA dodecagon has quot sides quot sidesquot Declaration is separated from rest of the code Each assignment print group is separated by Whitespace Program and main method are documented Inline comments describe the variable sides and the actions to be carried out page 7 CS 139 7 Algorithm Development 090604 Notes Named constants Name LewisLoftus Date 090604 Assignment PianoKeys4java PianoKeys class Demonstrates the declaration initialization and use of an integer variable public class PianoKeys4 Prints the number of keys on a piano public static void main String args final int KEYS8 8 4 since a constant does not change we declare and initialize in same step Systemoutprintln quotA piano has quot KEYS quot keysquot Declare and initialize done as one step only for CONSTANTS Note naming convention of constants They may not appear to the left of an assignment after rst value assigned Keyword nal designates KEYS as a constant page 8 Lecture Notes for Sept 13 2004 Monday Operators cont Order of operation to determine data type EX 5 7 250 7 What is the value and data type Shortcuts Never use this one instead use the convention c a b increment decrement 7 adds 1 to the variable Like a a l Pre x and post X form a a Tells when the increment occurs Okay to use increment and decrement as long as they are on a line by themselves a not b a The latter form is too confusing Data Conversion What if you are dealing with integers but want to carry out long division What if you are working with oating point numbers but want to save the result as an integer Type casting Recall 7 We can always widen and Java does this automatically int mylnteger double myDouble mylnteger 15 myDouble mylnteger no problem myDouble myDouble mylnteger no problem mylnteger myDouble 145 cannot shrink Possible loss of precision error Widening conversion promotion of the value within an operation Narrowing conversion casting type casting mylnteger int myDouble 145 changes the value not the variable myDouble casting is a higher order of precedence than the binary two operand arithmetic operations mylnteger int myDouble 145 will not have desired effect Interactive programs Book describes a Scanner class This is in an upcoming release of j ava and is not available on our system Part of what is on your cd which is Java 15 but the Sun website has it as Java 5 We will be using a class called Keyboard Keyboard provides methods that return the values entered via the keyboard The method waits until the Enter key is pressed then evaluates the input If it is not correct an error message is produced Review what information is provided in the method header Packages are a way of putting like classes together to organize them for program use The cs1 package contains the Keyboard class If we put it where Java will look for packages when imported then we do not have to have the class in our working directory The import statement tells the compiler and interpreter that we are using a class that is not part of the Java language It is part of the extension of that language Some packages come as part of the compiler Others are user de ned Keyboard is in a user de ned package called cs1 See examples Reference Types Keyboard is a class that provides input services There is only one Keyboard Strings are a reference type defined by a class Strings have data that we use For example String myString myString abc creates a variable myString and assigns it a value of abc We also call variables associated with reference types objects myString is an object of the String class If we want to work with Strings except for the concatenation operator what can we do Book page 119 Methods are the services that the String class provides for working with the objects of the String class A class is a declaration of data and methods that operate on that data Lecture Notes for Sept 9 2004 Wednesday Continue declarations and data types Primitive types int long short byte 7 integer space of different sizes 7 when in doubt use int 1 23 459 oat and double 7 rational numbers 7 oat 7 positions double 15 positions 31345 55000 char 7 character representation 7 See chart Appendix C boolean 7 values and reserved words of true and false true false Reference type 7 provides a user defined or java library means of adding abstract data types String 7 special built in class to represent character strings This is a string of characters n t 6 see escape character chart All variables used must be declared at the beginning of the method in which they are used It is a way of documenting the code An acceptable abbreviation for the declaration is int numberOne numberTwo numberThree note the space after each comma OR int numberOne numberTwo numberThree this format lets you describe each variable Operations and Operators Two meanings of 39 addition of numbers concatenation of Strings How does java decide Think ofthe lab assignment Context 7java is strongly typed Automatic conversion widening not narrowing Order of evaluation is evaluated from left to right EX 75 89 62 Only one operation can be done at atime So when we miX the types demo program Lecture 10 September 22 2004 Follow up from review exercise If we ask for a variable don t include its type If we ask for an object don t give its class An expression is an operator and its operands that can be evaluated to provide a resulting value 3 5 1722 29 6 Hello Each expression should result in a value and that value will be of a speci c type Literals are the literal values not the complete assignment statement Identi ers are made up words Reserved words are not identi ers A declaration associates a variable with its type and allocates memory Show declaration and its parts An initialization puts a beginning value into that variable It is a subset of all assignment statements You can declare variables for both reference and primitive types You can initialize variables for both reference and primitive types When you initialize a reference variable you are giving it the reference to an object The object may be made by new or by assignment of an existing object or return of an object from a method Number Systems Schaum s Chap 1 Decimal system 7 counting 7 base 10 7 10 digits Schaum s 7 Given any positive integer basis or radix N there are N different individual symbols that can be used to write numbers in the system The value of these symbols ranges from 0 to Nl Positional representation Arithmetic 7 adding subtracting 7 carry borrow Octal system 7 base 8 counting comparison to decimal converting to decimal 7 algorithm 1 Number digits of number in base n from right to left beginning with zero These numbers are the superscripts 2 Use each superscript as an exponent of the base to form the power for that digit 3 Multiply the digit by its corresponding power 4 Add all of the multiplication results together to form the decimal equivalent Note All operations are carried out in decimal basis 10 arithmetic Example 4571 in base 8 to base 10 Sum 2425 10 Hexidecimal 7 base 16 counting 7 need extra digits 7 0 7 9 A 7 F comparison to decimal converting to decimal Sum 1087910 Binary 7 base 2 counting 7 only two digits 7 0 1 follow the same principles converting to decimal Sum 1100 Binary shortcuts 7 Binary numbers can easily be represented in octal and hex Octal 7 8 the base is just 23 so 3 digits can be represented as a base 8 symbol Hexidecimal 7 16 the base is just 2 so 4 digits can be represented as a base 16 symbol Example above 7 Sum is 0B15 which is 11 decimal or 0000 1011 in binary Converting among binary octal hex 10110110001001001112 To octal group in three digit groupings beginning with the right most digit 1 011 011 000 100 100 1112 Convert each of the groups 1 3 3 0 4 4 7 8 To hex group in four digit groupings beginning with the right most digit 101 1011 0001 0010 01112 Convert each of the groups to a digit in the hex system 5 B 1 2 715 Often binary numbers are written in bundles of 4 digits with the leftmost group padded Going back to memory and the binary representation Chunks of memory are bytes 1 byte 8 bits 1 bit 1 binary digit What is the biggest unsigned integer that can fit into 1 byte of memory 255 How many different unsigned values can fit into 1 byte of memory 7 256 includes 0 IfI had a word of4 bytes or 32 bits what is the largest unsigned value that I can store Review slides So why is a byte only allowed a value up to 127 How many bits does it take to represent 127 1 byte is reserved for the sign Since each position can have two values if we have n bits we have 2n possible values that can be stored See Hi Heather