Sie sind auf Seite 1von 30

JAVA PROGRAMMING LANGUAGE

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Simple Input/Output Operation


Objectives
Simple Input Command Character String Simple Output Command Convert String to Integer Real Programming Exercises
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Simple Input Command


The form of input can be keyboard entry from the user. Values input by user can be numerical or non-numerial. Java has some standard input commands that allows the user to use the commands to input values via the keyboard. We shall look at the character and String input
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Character Input Command


Characters are read from the keyboard using System.in.read() method. One of Javas console function. Accepts input from the keyboard. in object has access to method read() which retrieves data from keyboard. The main() method header had a phrase throws Exception added at the end of the line.
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Character Input Command


Exception is an error situation, by throwing the exception, the compiler will handle the different sorts of errors. Programs accepting input from keyboard will not be able to compile successfully without the phrase throws Exception.
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

String Input Command


The InputStreamReader() is used to read characters from the keyboard. The BufferedReader() is used, for it provides the readLine() method to allow programs to read from keyboard a line at a time. Java implements I/O operations through streams. Represented by two abstract classes, InputStream and OutputStream.
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

String Input Command


These are found in java.io package. Thus, have to import this package into the program, which is place at the beginning of the program.

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Simple Output Command


System.out is a PrintStream object that allows output to be displayed. Two methods used, i.e. print() and println() method. print() method allows the cursor to be positioned on the next available position on the same line. println() method allows the cursor to be positioned on the next line.
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Convert String to Integer


Converts String containing numbers to numerical data to perform arithmetic calculation. Use te Integer class, which is part of the java.lang package. parseInt() method from the Integer class is used to convert String to integer.
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Convert String to Real


Use the Double class, which is part of the java.lang package. parseDouble() method from the Double class is used to convert String to real numbers.

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Programming Exercises
Q1) Write a Java program to capture the users name and age and displays the name and converts the age to an integer and displays the age with a meaningful message.

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Control Structures
Objectives
To write Selection statements To write Iteration statements Programming Exercises

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Selection Statements
Based on the user input, decision making will take place. It involves choosing between two alternative courses of action. Java supports two kinds of selection statements The if, if..else or nested if statement

The switch statement

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Selection Statements
The if structure A single-alternate if as it only performs an action based on one alternative. The if..else structure A dual-alternate if as it requires two options for the next course of action.

Performs one action when a boolean expression evaluates as true and another when the boolean expression evaluates as false.

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Selection Statements
The Nested if structure An if within another if statement. Nested if statements are useful when two conditions must be met before some action is taken. The switch structure Another alternative for nested if statements. Useful when testing single variable against a series of exact integer or character values.
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of if statement #1
class SampleIf{ public static void main(String[] test) { int number = 5; if(number > 0){ System.out.println(The number is positive.); } else System.out.println(The number is negative.); } }

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of if statement #2
class SampleIf { public static void main(String args[]) { System.out.print("Hello "); System.out.println(args.length); if (args.length > 0) { System.out.println(args[0]); } if (args.length <= 0) { System.out.println("who are you ?"); } } }

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of switch statement


class SampleIf { public static void main(String args[]) throws Exception { char ans , upans ; System.out.println("Enter A or B "); ans = (char) System.in.read(); upans = Character.toUpperCase(ans); switch (upans) { case 'A : System.out.println(" First option"); break; case 'B : System.out.println(" Second option"); break; } } }
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Iteration Statements
A structure that allows a block of statements to be executed repeatedly depending on the evaluation of the boolean expression. A loop that never ends is called an endless loop or infinite loop. A loop within a loop is called a nested loop. Java supports three kinds of iteration statements The while loop

The do..while loop The for loop


CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Iteration Statements
The while loop Used to repeat a statement or block only if the condition is true. The do..while loop Used when we need to execute the loop body at least once before checking the condition. like the while loop, it will only repeat a statement or block if the condition is true.

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Iteration Statements
The testing of the condition is placed differently while loop test the condition first, then execute the body of statements.

do.. while execute the body of statements before testing the condition.

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of while loop #1


class SampleWhile{ public static void main(String[] test) { int count = 1; System.out.println(Good Day!); while (count <= 3) { System.out.print(INFORMATICS); count = count + 1; } } }
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of while loop #2


class SampleWhile{ public static void main(String[] test) { int num = 1; while (num < 5) { System.out.print(num*2); num = num + 1; } } }
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of do-while loop #1


class SampleDoWhile{ public static void main(String[] test) { int count = 1; System.out.println(Good Day!); do { System.out.print(INFORMATICS); count = count + 1; } while (count <= 3) ; } }
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of do-while loop #2


class SampleDoWhile{ public static void main(String[] test) { int num = 1; do { System.out.print(num*2); num = num + 1; } while (num < 5) ; } }
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Iteration Statements
The for loop Allows the statements in the loop body to be executed a specific number of times.

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of for loop #1


class SampleFor{ public static void main(String[] test) { System.out.println(Good Day!);

for(int count = 1; count <= 3;count++) { System.out.print(INFORMATICS); } }


}
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of for loop #2


class SampleFor { public static void main(String[] test) { System.out.println(ASCII"); for (int count = 0 ; count <127 ; count++) { char ans = (char) (count); System.out.println(ans); } } }
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Example of for loop #3


class SampleFor { public static void main(String[] test) { System.out.println("Graphics "); for (int x = 1; x <=4 ; x++) { for (int y = 5; x <=y ; y--) { System.out.print("*"); } System.out.println(); } } }

CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Programming Exercises
Write a Java program to capture the users name and age and display a message based on the following information: if age > 65 display retired if age is between 22 to 65 display working if age is between 7 to 21 display studying if age < 7 display playing The user will be prompted to enter another name and age. If the user decides not to enter another name, the program will terminate.
CAREER ENTRY COURSE FOR SOFTWARE DEVELOPERS - JAVA

Das könnte Ihnen auch gefallen