Sunteți pe pagina 1din 27

COP 3330

Slides 8/29
Topics

• Strings
• Basic I/O
• Control Flow
• Arrays
String Basics

• Java has a String class (Difference from


C)
• String concatenation can be performed
with the ‘+’ operator
• String literals are enclosed in double
quotes
- Example: “This is a string
literal”
String Basics

• String comparison is done using the


equals method and compareTo method
• If x and y are strings, then x.equals(y)
returns true if and only if they are the
same string
• x.compareTo(y) returns negative if x is
lexicographically less than y, 0 if they’re
the same, positive if x is greater than y
Basic I/O- To Screen

• Output to screen is handled through


System.out
• out is a static field in the System class
• System.out is an object of type
PrintStream
Basic I/O- To Screen

• Useful methods for use with System.out:


- print- Takes in a parameter of any type
and outputs it to the screen
- println- Behaves just like print, except
it also advances output to the next
line afterwards
- printf- Behaves almost identically to
C’s printf function (New to Java 5.0)
Basic I/O- To Screen

• Example- The following are all equivalent


(assuming that x is an int)
- System.out.print(“Value is “ + x + “\n”);
- System.out.println(“Value is “ + x);
- System.out.printf(“Value is %d\n”, x);
- System.out.printf(“Value is “ + x + “\n”);
Basic I/O- To file

• PrintStream can also be used to output to


file
• An appropriate PrintStream can be made
as follows: (New to Java 5.0)
- PrintStream fout = new PrintStream(new
File(“output.txt”));

• You can use your PrintStream object in


almost the same way as System.out
• You must call the close() method on it
when you are done
Basic I/O- To file

• To use a PrintStream, you must import it


at the beginning of your source file:
- import java.io.*;
• Opening a file can produce exceptions, so
file usage must be enclosed in a try/catch
construct
• Exceptions are covered in much more
detail later
Basic I/O- To file

• Example program:
- Write info about the course to a file
Basic I/O- Scanners

• Scanners make input fairly easy (New to


Java 5.0)
• Must be imported from java.util:
- import java.util.*;
• Can be used for both input from keyboard
and from file
Basic I/O- Scanners

• To read from keyboard:


- Scanner stdin = new
Scanner(System.in);
• To read from file: (must import java.io.*)
- Scanner fin = new Scanner(new File(“input.txt”));
Basic I/O- Scanners

• Useful methods:
- hasNext()- Tells whether there is an next input
token
- hasNextInt(), hasNextDouble(), etc.- Tells
whether the next token is an int, double, etc.
- next()- Gets the next input token (String)
- nextInt(), nextDouble(), etc.- Gets the next int,
double, etc.
- nextLine()- Gets the rest of line. Consumes the
line separator, but does not include it in the
returned value
Basic I/O- Scanners

• Example Programs
- Read an integer from the user and
print it to the screen
- Read every token from a file and print
it to the screen
Control Flow

• If/else if/else- allows branching of control based


on conditions
if(a<b)
System.out.println(“Less”);
else if(a==b)
System.out.println(“Equal”);
else
System.out.println(“Greater”);
Control Flow

• Switch- Allows branching to multiple pieces of code based on a


single variable

switch(answer){
case ‘y’:
System.out.println(“Confirmed”);
break;
case ‘n’:
System.out.println(“Denied”);
break;
default:
System.out.println(“Not understood”);
}
Control Flow

• For- Usually used to loop a certain


number of times
// Adds up numbers from 1 to n
for(i=1;i<=n;i++){
sum = sum + i;
}
Control Flow

• While- Loop as long as a condition is met

while(n<128){
n *= 2;
}
Contol Flow

• do-while- Similar to a while loop, except


that the loop body is always executed at
least once

do{
System.out.println(“Enter a number”);
n = stdin.nextInt();
}while(n!=0);
Arrays

• Arrays are used for sequential storage of


data
• Arrays must be created using the new
keyword
- Example: int[] a = new int[64];
Arrays

• Example: reading 5 numbers and printing them in reverse


order
int[] a = new int[5];
for(int i=0;i<5;i++){
System.out.println(“Enter a number”);
a[i] = stdin.nextInt();
}
for(int i=4;i>=0;i--){
System.out.println(a[i]);
}
Arrays

• Arrays can be multi-dimensional


• Example:
- int[][] matrix = new int[64][64];
Example

• Write a program that prints out a


multiplication table of size 10x10. (For all
integers 1 through 10.)
Example

• Write a program that takes in an integer


input from the user and determines if the
input is prime or composite.
- Recall that an integer is prime iff it is
greater than 1 and it is divisible only
by itself and 1
- Recall that an integer is composite iff
it is greater than 1 and it is not prime
Example

• Write a program that implements a


guessing game. (Where you tell the user if
their guess was too high or low after each
turn.)
- To make the game more fun, it’s best
have the program generate a random
number
- Math.random() gives a pseudo-random
double between 0 and 1
Example

• Write a program that reads in a string and


counts how many 'a's are in the string.
- Strings in Java are objects, not char
arrays
- The length() method gives the
length of the string
- The charAt(int) method gives the
char at the specified location
Example

• Write a program that allows the user to


play rock, paper, scissors against the
computer up to a score of 10

S-ar putea să vă placă și