Method Practice
Method Practice
to Java
GEEN163
Founders' Day
The Founders' Day Convocation is Thursday,
March 21 from 10:00 12:00 in the Harrison
Auditorium
Classes are suspended during that time
The lab quiz will be next week instead of this
week
Exam
The second exam will be in lecture on Friday,
March 22
The exam will cover everything since the first
exam
If statements
Loops
Files
Logical Ordering
Some things must happen before others
If you are going to read a number and display
it, the read must come before the display
Variables Needed
When you write a program, consider the
variables necessary
You will probably need a variable to hold each
input
You will probably need a variable to hold the
result of a calculation
Solution
The program uses two variables
biggest
number
Problem Description
Read a positive number. If the number is not
greater than zero, display an error message
and ask the user again.
Problem Description
Read a positive number. If the number is not
greater than zero, display an error message
and ask the user again.
This program only needs one variable
Since we have not been told it is a whole
number, it should be defined as a double
Java Implementation
double number;
Java Implementation
double number;
{
System.out.print("Enter a number >");
number = keyboard.nextDouble();
}
Java Implementation
double number;
{
System.out.print("Enter a number >");
number = keyboard.nextDouble();
if (number <= 0) {
System.out.println("Be positive!");
}
}
Java Implementation
double number;
do {
System.out.print("Enter a number >");
number = keyboard.nextDouble();
if (number <= 0) {
System.out.println("Be positive!");
}
} while (number <= 0);
Problem Description
For a 400 pixel wide by 600 pixel tall picture,
set the Red intensity to zero using
setRed(int x, int y, int intensity)
setRed(x,y,0);
A
B
C
D
0%
D
0%
C
0%
B
0%
A
A.
B.
C.
D.
0%
ca
n
no
t
be
de
te
...
24
00
00
0%
10
00
0%
60
0
A.
B.
C.
D.
Problem Description
Read the first ten integers from the file
"numbers.txt" and display them on the screen
Java Implementation
java.io.File dog = new java.io.File("numbers.txt");
Scanner cat = new Scanner( dog );
cat.close();
Java Implementation
java.io.File dog = new java.io.File("numbers.txt");
Scanner cat = new Scanner( dog );
}
cat.close();
Java Implementation
java.io.File dog = new java.io.File("numbers.txt");
Scanner cat = new Scanner( dog );
int bull;
for (int counter = 0; counter < 10; counter++) {
bull = cat.nextInt();
System.out.println(bull);
}
cat.close();
Possible solution
java.io.File dog = new java.io.File("numbers.txt");
Scanner cat = new Scanner( dog );
int bull;
for (int counter = 0; counter < 10 && cat.hasNext();
counter++) {
bull = cat.nextInt();
System.out.println(bull);
}
cat.close();
Flowcharts
Flowcharts are a way to visually describe the
logic of a program
It can be helpful to write a flowchart for a
program and then convert the flowchart to
Java
Flowchart to Java
input:word converts to
word = keyboard.next();
output: word converts to
System.out.println( word );
converts to
while ( inFile.hasNext() )
Flowchart to Java
converts to
if (word.endsWith("y")) {
} else {
}
Problem Description
Read an English word from a file and display
the plural of the word
For most words, you just add an "s" on the
end
If the word ends in "y", you change the "y" to
"i" and add "es"
We will ignore the many other rules
Possible Solution
while (cat.hasNext()) {
word = cat.next();
if ( cat.endsWith( "y" )) {
yword = cat.substring( 0, cat.length()-1 );
word = yword + "ies";
} else {
word = word + "s";
}
System.out.println( word );
}
format Method
The java.io.PrintWriter class has two identical
methods, format and printf, to format output
format(String format, var1, var2, )
Writes the variables to the output as specified
by the format string
Very similar to printf in the C programming
language
Format Descriptors
The format string may contain text with
descriptors located in it.
The descriptors start with a percent sign, %
followed optionally by a length and then a
format type character
format data type
result
'd'
'f'
double or
The result is formatted as a decimal number
float
String
The string
Output length
You can specify a number between the % and
the descriptor character to indicate the
minimum number of characters to print
You can specify the maximum number of
digits to the right of the decimal point
%minlength.maxprecisionf
Format Examples
double e = 2.718281828459045;
System.out.format("answer is %5.3f", e);
will display answer is 2.718
System.out.format("answer is %7.4f", e);
will display answer is 2.7183
What is displayed?
double x = 10.0;
double y = x * 2.0 / 3.0; 25%
System.out.printf(
y is %6.3f, y);
A.
B.
C.
D.
y is 10.000
y is 6.666
y is 6.667
y is 6.66666666
A.
25%
B.
25%
C.
25%
D.
Founders' Day
The Founders' Day Convocation is Thursday,
March 21 from 10:00 12:00 in the Harrison
Auditorium
Classes are suspended during that time
The lab quiz will be next week instead of this
week
Exam
The second exam will be in lecture on Friday,
March 22
The exam will cover everything since the first
exam
If statements
Loops
Files