Title: Computer Science 111 Fundamentals of Computer Programming I
1Computer Science 111Fundamentals of Computer
Programming I
- Char, Stringsand Menu DrivenTerminal I/O
2Repeating Input
- Want program to allow user to continue giving
inputs as long as there are more problems of the
same type he/she wishes to solve. - For example, convert several temperatures without
having to run the program over and over.
3Repeating Input (cont.)
- Common structure goAgain y while
goAgain y get input calculate
and give results read goAgain
4Repeating Input (cont.)
- Often we want user to enter a letter (y or n)
rather than a number. - There is type char for variables that store a
single character. - Literals of type char are written in single
quotes (apostrophes).
5Repeating Input (cont.)
- char goAgaingoAgain ywhile (goAgain
y) fahrenheit reader.readDouble(Ente
r fahrenheit ) celsius
(fahrenheit-32.0) 5.0 / 9.0
writer.print (Celsius is )
writer.println(celsius) goAgain
reader.readChar(More input (y or n)? )
6Strings
- Java also has a type called String. A string is
an object (not primitive data type like int). A
variable of type String holds a reference to a
string object (a sequence of 0 or more
characters). - Literals of type string are contained in double
quotes. - When we do writer.print(Enter a Fahrenheit
number )we are using a string literal - We could have String prompt prompt
Enter a Fahrenheit number
writer.print(prompt)
7String Concatenation
- When dealing with strings, the operator
provides string concatenation. For example,
Today is Wednesday. results in the string
Today isWednesday. - If a string is concatenated with another data
type, the other type is first converted to its
string format.
8Concatenation Examples
- Total pay is 6 40 results in Total pay
is 240 - Total hours is 30 15 results inTotal
hours is 3015 - Total hours is (30 15) results inTotal
hours is 45 - Note we can now do things likewriter.println(Th
e quotient of dividend divided
by divisor is
dividend/divisor .)
9Newline Characters
- You can add the character \n to a string to
cause the string to break to a new line. - For example, writer.println(Now is the time
\nfor all good men \nto come to the aid \n
of their country.) results in Now
is the time for all good men to
come to the aid of their country.
10String Input
- The KeyboardReader class has a method called
readLine that reads everything the user enters up
to but not including the Enter key. - Example String dayOfWeek
writer.print(Enter the day of the week )
dayOfWeek reader.readLine()
11A Menu Driven Terminal I/O Program
- Program should allow user to continually choose
between options of - printing the first n Fibonacci numbers
- printing the primes less than or equal to n
- Quitting
- Solution is an in-class example. Also in class
folder.
12Math Class
Method/Constant Explanation double
abs(double) returns absolute value also
for float,int,longdouble exp(double) returns e
to power of parameterdouble log(double) returns
logarithm base edouble max(double,double) returns
max of two, also for float, int,long. Also
a min method.double pow(double,double) returns
first param raised to seconddouble
random() returns random num, 0.0ltnumlt 1.0long
round(double) returns nearest longdouble
sin(double) returns trig sine in radians
also cos, tandouble sqrt(double) returns square
rootdouble E double closest to edouble
PI double closest to ?
13Programming Style
- Use comments
- Give a header to program with programmers name,
date, course, purpose of the program, special
assumptions, known bugs, etc. - Give use of variables, if not perfectly clear.
- Describe sections of program
- Explain any code that may not be clear.
14Programming Style (cont.)
- Indentation and braces
- methods public void run( )
code indented
code indented - if if (cond)
code indented
code indented
else
code indented
code indented - while Same as if
15Programming Style (cont.)
- Names
- Use meaningful names
- Variables and methods use camelCase
- Classes start with upper case.
16Man, there must be 1100110001001001100 cows out
there!