Comprog - Java Chap 3
Comprog - Java Chap 3
Streams
JAVA programs perform input and output through streams. A
stream is either a source or destination for the data or
information. A stream in linked to a physical device by the JAVA
I/O stream. All streams in the same manner, even if the actual
physical devices to which they are linked differ. So the same I/O
classes and methods can be applied to perform I/O from any type
of device. Streams are the clean way to deal with input / output.
JAVA implements streams within class hierarchies defined in the
java.io package.
Byte Streams
Byte streams provide easy way for handling input and output of
Bytes. These are used while reading and writing binary data. When
you write data to a stream as a series of bytes, it is written to the
stream exactly as it appears in memory. No transformation of data
takes place.
44
JAVA PROGRAMMING-CHAPTER 3
Character Streams
Character streams are used for storing and retrieving text from a
location or file by a JAVA program. All numeric data is converted
to textual from before being written to the stream. Reading
numeric data from the stream of characters involves much more
work than reading binary data.
45
JAVA PROGRAMMING-CHAPTER 3
Buffered Streams
Buffered streams ensure that data transfer between memory and
external device are in large chunks to make this process efficient.
A buffer is simply a block of memory that is used to batch up a
data transfer. When you write to a buffered stream the data is
sent to buffer (not to external device). The amount of data in the
buffer is tracked automatically, and the data is sent to the device
when the buffer is full. You can do it manually by flushing the
buffer.
System.out
Refers to the standard output stream. By default, this is the
console. System.in refers to the standard input, which is keyboard
by default. System.err refers to the standard error stream, which
46
JAVA PROGRAMMING-CHAPTER 3
System.in
An object of type InputStream; System.out and System.err are
objects of type PrintStream. These are byte streams, even though
they typically are used to read and write characters from and to
the console.
47
JAVA PROGRAMMING-CHAPTER 3
Learning Exceptions
These errors are called exceptions because, they are not usual
occurrences; they are “exceptional.” The object-oriented
techniques to manage such errors comprise the group of methods
known as exception handling.
48
JAVA PROGRAMMING-CHAPTER 3
Like all other classes in Java, exceptions are Objects. Java has
two basic classes of errors: Error and Exception. Both of these
classes descend from the Throwable class.
49
JAVA PROGRAMMING-CHAPTER 3
import java.util.Scanner;
public class GetInfoTryCatch{
public static void main(String [] args){
int age;
String name, s_addr, gender;
try{
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter your age: ");
age = inputDevice.nextInt();
inputDevice.nextLine();
System.out.print("Please enter your name: ");
name = inputDevice.nextLine();
System.out.print("Please enter your the address: ");
s_addr = inputDevice.nextLine();
System.out.print("Please enter gender: ");
gender = inputDevice.nextLine();
System.out.println();
System.out.println();
System.out.println(“Nice to meet you!” + name);
System.out.println(“You will turn “ + age+1 + ”next year”);
System.out.println(“May we visit you at “ + s_addr +”?”);
System.out.println(“Are you sure you are “ + gender + “?”);
}
catch(Exception e){
System.out.println("Error: " +e);
}
}
}
50
JAVA PROGRAMMING-CHAPTER 3
51
JAVA PROGRAMMING-CHAPTER 3
52
JAVA PROGRAMMING-CHAPTER 3
53
JAVA PROGRAMMING-CHAPTER 3
Syntax:
Math.methods()
Here methods is replaced by different math class methods word
54
JAVA PROGRAMMING-CHAPTER 3
Syntax Function
Absolute Value. Here variable can be
double, float, int and long data type.
Math.abs(variable) abs method is used to find absolute
value of variable.
Math.abs(-2.3) = 2.3
Minimum Value. Here variable can be
double, float, int and long data type.
Math.min(variable1,v min method is used to find minimum
ariable2) value between variable1 and
variable2.
Math.min(2,8) = 2
Maximum Value. Here variable can be
double, float, int and long data type.
Math.max(variable1, max method is used to find maximum
variable2) value between variable1 and
variable2.
Math.max(2,8) = 8
Rounding Number. Here variable is
double data type. ceil method is used
Math.ceil(variable) round up the variable and floor method
is used round down the variable.
Math.floor(variable) Math.ceil(8.4) = 9
Math.floor(8.4) = 8
Power Of The Number. Here variable
is double data type. Variable1 is base
Math.pow(variable1, value and variable2 is power value.
variable2) Math.pow(2,3) = 8
55
JAVA PROGRAMMING-CHAPTER 3
Syntax Function
56
JAVA PROGRAMMING-CHAPTER 3
Syntax Function
Math.expm1(2)=6.3890560989306
5
Source: http://www.aboutcodes.com/2012/07/math-class-
methods-in-java-with-examples.html
57
JAVA PROGRAMMING-CHAPTER 3
num=Math.abs(num1);
System.out.println("absolute:"+num);num=Math.min(num1,num2);
System.out.println("min:"+num);
num=Math.max(num1,num2);
System.out.println("max:"+num);num=Math.ceil(num2);
System.out.println("ceil:"+num);num=Math.floor(num1);
System.out.println("floor:"+num);num=Math.pow(num1, num2);
System.out.println("power:"+num);num=Math.sqrt(num1);
System.out.println("sqrt:"+num);num=Math.cbrt(num2);
System.out.println("cbrt:"+num);num=Math.copySign(num1,-8.2);
System.out.println("copysign:"+num);num=Math.exp(2);
System.out.println("exp:"+num);
num=Math.expm1(2);
System.out.println("expm1:"+num);
}
}
Sample output
layout:
58