SOME MATERIAL THAT COMPUTER SCIENCE 199 STUDENTS MUST KNOW, C O L D
(USING the File and Scanner classes for file input)
IMPORTS:
import java.io.*;
import java.util.Scanner;
DECLARATIONS:
int x, n, numGrade, a, b, c, age, evenCount, oddCount, femaleCount, maleCount, i, sum;
char ch, gender, letGrade, sex, coffee, donuts;
double avg;
Stringstr;
Scanner keyboard = new Scanner(System.in);
SETTING UP TO WORK WITH OUTPUT FILES
FileWrite fwriter = newFileWriter(“test.txt”);//associate fwriter with the actual file test.txt
OR
System.out.print(“What is the output filename? “);
str = keyboard.nextLine();
FileWriter fwriter = newFileWriter(str);//associate fwriter with the file chosen by the user
PrintWriter outputFile = new PrintWriter(fwriter);//later, write to file associated with fwriter
SETTING UP TO WORK WITH INPUT FILES
File file = new File (“test.txt”);//associatefile with the actual file test.txt
OR
System.out.print(“What is the input filename? “);
str = keyboard.nextLine();
File file = newFile(str);//associate file with the file chosen by the user
Scanner inputFile = new Scanner(file);//the actual reading is done with inputFile
TESTS (if statements):
positive, negative, zero, non-zero
if(x > 0)if(x < 0)if(x = = 0)if(x != 0)
above average, below, etc.
if(x > avg)if(x < avg)
even, odd
if(x % 2 == 0)if(x % 2 != 0)
evenCount++;oddCount++;
lower case, upper case, etc.
if(str.isLowerCase())if(str.isUpperCase())
female, male
if(sex == ‘f ‘ || sex == ‘F’)if(sex.Character.toUpperCase() == ‘M’)
femaleCount++;maleCount++;
lowercase vowel
if(ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’)
ladders
if(numGrade >= 90)
letGrade = ‘A’;
else if(numGrade >= 80)
letGrade = ‘B’;
else if(numGrade >= 70)
letGrade = ‘C’;
else if(numGrade >= 60)
letGrade = ‘D’;
else//if you fall through to here, it’s below 60—no need to ask
letGrade = ‘F’;
ladder<-->switch
if(ch == ‘X’){
p = 4;
q = -1;
}
else if(ch == ‘Y’)
{
p = 14;
q = -5;
}
else if(ch == ‘Z’ || ch == ‘A’)
{
p = -8;
q = 34;
}
else
{
p = 99;
q = 76;
} / switch(ch)
{
case ‘X’:
p = 4;
q = -1;
break;
case ‘Y’:
p = 14;
q = -5;
break;
case ‘Z’ : case ‘A’:
p = -8;
p = 34;
break;
default:
p = 99;
q = 76;
}
if x is between a and b
if(a<=x & x<=b) in math language—this implies an and
if x is not between a and b
if( !(a<=x & x<=b) )
or, even better, using one of deMorgan’s laws: not (p and q) = not p ornot q
if( xa || x b )x<a is opposite of (equivalent to ) and x>b is opposite of
nested ifs
if(coffee == ‘y’)
if(donuts == ‘y’)
System.out.println(“We have coffee and donuts”);
else
System.out.println(“We have coffee, but not donuts”);
else
if(tea == ‘y’)
System.out.println(“We have no coffee, but we have tea, and maybe donuts...”);
else
System.out.println(“No tea or coffee, but maybe donuts...”);
LOOPS (examples deal with reading in one number at a time, and adding them):
No loop—ask for and read in three numbers, then add them:
sum = 0;
System.out.print(“a? “);//ask for first number
a = keyboard.nextInt( );//read it in
System.out.print(“b? “);//etc
b = keyboard.nextInt( );
System.out.print(“c? “);
c = keyboard.nextInt( );
sum = a + b + c;//sum all of them at once
Still no loop—this time, ask for a number, add to sum; ask for number, add to sum, etc.
sum = 0;//setting up pattern for a loop
System.out.print(“number? “);//with ask for (next) number
x = keyboard.nextInt( );//get the number
sum += x;//add it to sum
System.out.print(“number? “);//etc.
x = keyboard.nextInt( );
sum += x;
System.out.print(“number? “);
x = keyboard.nextInt( );
sum += x;
Sentinel controlled loop (from user at keyboard)
sum = 0;
System.out.print(“First number (9999 to stop)? “);
x = keyboard.nextInt( );//prime pump
while(x != 9999)//9999 is sentinel value
{
sum += x;
System.out.print(“Next number? “);
x = keyboard.nextInt( );
}
Sentinel controlled loop (from file)
sum = 0;
x = inputFile.nextInt();//prime the pump
while(x != 9999)//as long as not the sentinel
{
sum += x;//add to sum
x = inputFile.nextInt();//get the next element
}
Count-controlled (from user)
sum = 0;
System.out.print(“How many numbers are there? “);
n = keyboard.nextInt();//the user has counted the x values
System.out.println(“Enter the numbers, one per line:” );//header (sort of)
for( i=0; i < n; i++)
{
System.out.print(“Next value: “);
x = keyboard.nextInt();
sum += x;
}
Count-controlled (from file)
sum = 0;
n = inputFile.nextInt();//get count of actual data lines in file
for( i=0; i < n; i++)
{
x = inputFile.nextInt();//get the next number
sum += x;
}
Eof-controlled (from a file, obviously!)
sum = 0;//initialize sum
while(inputFile.hasNext())//as long as file has stuff in it
{
x = inputFile.nextInt();//get next number
sum += x;//add to sum
}
“Bad” data loop
System.out.print(“Give me a (positive) age, please: “);
age = keyboard.nextInt();//sort of a prime-the-pump
while(age <= 0)//as long as the user enters incorrect data, keep issuing
{//error message, and asking for correct data
System.out.println(“Ages have to be positive!” );
System.out.print(“Please give me a POSITIVE age: “);
age = keyboard.nextInt();
}
Finding largest and smallest in a loop
largest = -99999;//very SMALL number
smallest = 99999;//very LARGE number
beginning of loop (number is the variable that has a largest and smallest value—any kind of loop will do)
{
//assume that number has been gotten somehow, either from the keyboard or from a file
if(number > largest)
largest = number;
if(number < smallest)
smallest = number;
}//end of loop