Скачать презентацию Basic Programs q Char 2 byte q Скачать презентацию Basic Programs q Char 2 byte q

025a35bcbdd2184dade2c6ef0392a470.ppt

  • Количество слайдов: 60

Basic Programs Basic Programs

q Char 2 byte q Boolean 1 bit P. R@HIT-CSE q Char 2 byte q Boolean 1 bit P. [email protected]

Command Line P. R@HIT-CSE Command Line P. [email protected]

Command Line P. R@HIT-CSE Command Line P. [email protected]

WAP to find maximum using command line argument. (For any number of inputs) P. WAP to find maximum using command line argument. (For any number of inputs) P. [email protected]

Enter Value Through keyboard P. R@HIT-CSE Enter Value Through keyboard P. [email protected]

q. In the first line of the program, import is the keyword, followed by q. In the first line of the program, import is the keyword, followed by java. io which is the package name and * means all the classes from the package java. io will be imported to our program. q. The class Datalnput. Stream is defined in the package java. io. This class, has methods for reading the data from the console. q. The methods of this class might throw an exception (run time errors), therefore, we have placed the code for taking input into try block. P. [email protected]

q. A try block is a block created with try and { }. q. q. A try block is a block created with try and { }. q. In the try block, any Java code which might throw an exception during the execution of the program is placed. q. The exception thrown, if not caught, may terminate or crash the program abnormally. q. The catch block following the try block is responsible for catching and handling the exception thrown. P. [email protected]

q. In the text line, we create an object of the Data. Input. Stream q. In the text line, we create an object of the Data. Input. Stream class type and assign reference of it to input as: pr = new Data. Input. Stream(System. in); q. The ‘new’ is the operator for creating objects dynamically. q. In Java, all objects are created dynamically. Objects are instances of classes. q. Here, pr is an instance of the class Data. Input. Stream. P. [email protected]

q. The parameter System. in represents that we want to read from the standard q. The parameter System. in represents that we want to read from the standard input stream, i. e. from the keyboard. It is an object of the Input. Stream class type. q. The read. Line() method of the class Data. Input. Stream allows us to read a string from the keyboard. P. [email protected]

Accepting Input from the Keyboard (Details) v A stream is required to accept input Accepting Input from the Keyboard (Details) v A stream is required to accept input from the keyboard. A stream represents flow of data from one place to another place. v It is like a water-pipe where water flows. Like a waterpipe carries water from one place to another, a stream carries data from one place to another place. v A stream can carry data from keyboard to memory or from memory to printer or from memory to a file. v A stream is always required if we want to move data from one place to another. P. [email protected]

v. Basically, there are two types of streams: input streams and output streams. v. v. Basically, there are two types of streams: input streams and output streams. v. Input streams are those streams which receive or read data coming from some other place. v. Output streams are those streams which send or write data to some other place. v. All streams are represented by classes in java. io package. P. [email protected]

v. Keyboard is represented by a field, called “in” in System class. v. When v. Keyboard is represented by a field, called “in” in System class. v. When we write System. in, we are representing a standard input device, i. e. keyboard, by default. v. System class is found in java. lang package and has three fields as shown below. All these fields represent some type of stream: P. [email protected]

ØSystem. in: This represents Input. Stream object, which by default represents standard input device, ØSystem. in: This represents Input. Stream object, which by default represents standard input device, i. e. keyboard. ØSystem. out: This represents Print. Stream object, which by default represents standard output device, i. e. monitor. ØSystem. err: This field also represents Print. Stream object, which by default represents monitor. P. [email protected]

v. Note that both System. out and System. err can be used to represent v. Note that both System. out and System. err can be used to represent the monitor and hence any of these two can be used to send data to the monitor. v. What is the difference between System. out and System. err? ü System. out and System. err both represent the monitor by default and hence can be used to send data or results to the monitor. ü But System. out is used to display normal messages and results whereas System. err is used to display error messages. P. [email protected]

v. To accept data from the keyboard, i. e. System. in, we need to v. To accept data from the keyboard, i. e. System. in, we need to connect it to an input stream. v Input. Stream. Reader obj = new Input. Stream. Reader(system. in); v. Connect the keyboard to an input stream object. Here, we can use Input. Stream. Reader that can read data from the keyboard. v. In this statement, we are creating Input. Stream. Reader object and connecting the keyboard (System. in) to it. P. [email protected]

v. Connect Input. Stream. Reader to Buffered. Reader, which is another input type of v. Connect Input. Stream. Reader to Buffered. Reader, which is another input type of stream. v. We are using Buffered. Reader as it has got methods to read data properly, coming from the stream. v. Buffered. Reader br = new Buffered. Reader(obj); v. Here, we are creating Buffered. Reader object (br) and connecting the Input. Stream. Reader object (obj) to it. v. These two steps can be combined and rewritten in a single statement as: v. Buffered. Reader br = new Buffered. Reader(new Input. Stream. Reader(system. in)); P. [email protected]

 • Reading character from console P. R@HIT-CSE • Reading character from console P. [email protected]

q. To read a character, we have the read method of Data. Input. Stream q. To read a character, we have the read method of Data. Input. Stream class. The method returns an integer, so we have to typecast it to a character. q. While displaying, we have also printed the ASCII value of the character taken by typecasting it with int. P. [email protected]

 • Write a program to calculate simple interest. P and R as float • Write a program to calculate simple interest. P and R as float T as integer Calculate SI • Write the same using command line arg. P. [email protected]

 • Another technique to enter float data P. R@HIT-CSE • Another technique to enter float data P. [email protected]

q. How to get the bytecode. q. Take a program. ncr. java q. After q. How to get the bytecode. q. Take a program. ncr. java q. After javac ncr. java q. Javap –c : means disassembles the code. q. Javap –c ncr>ncr. bc enter q. View ncr. bc, this is the bytecode. P. [email protected]

 • WAP to accept details of an employee and print. P. R@HIT-CSE • WAP to accept details of an employee and print. P. [email protected]

Explanation § If you write (char)emp. read() before entering the name; name will not Explanation § If you write (char)emp. read() before entering the name; name will not accepted. § When we typed M for sex and press enter, then it releases a n code. So at sex coloumn we are giving two characters M and n; read() will accept only M and reject n, which is accepted and trapped by emp. read. Line(). § So name is not accepted by read. Line() as it already contains n. § If you use the solutions u will have the correct result. P. [email protected]

Using solution P. R@HIT-CSE Using solution P. [email protected]

 • Employee details: Entering all inputs in a single line P. R@HIT-CSE • Employee details: Entering all inputs in a single line P. [email protected]