a) Write a program in Java to read differe types of data and print them.
import [Link];
public class InputAllDataTypes
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter byte value: ");
byte b = [Link]();
[Link]("Enter short value: ");
short s = [Link]();
[Link]("Enter int value: ");
int i = [Link]();
[Link]("Enter long value: ");
long l = [Link]();
[Link]("Enter float value: ");
float f = [Link]();
[Link]("Enter double value: ");
double d = [Link]();
[Link]("Enter char value: ");
char c = [Link]().charAt(0);
[Link]("Enter boolean value (true/false): ");
boolean bool = [Link]();
[Link]("You entered: ");
[Link]("byte: " + b);
[Link]("short: " + s);
[Link]("int: " + i);
[Link]("long: " + l);
[Link]("float: " + f);
[Link]("double: " + d);
[Link]("char: " + c);
[Link]("boolean: " + bool);
}
}
b) Write a Java program to display thr range of all the data types available in Java.
public class DataTypeRanges {
public static void main(String[] args) {
[Link]("byte : " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
[Link]("short : " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
[Link]("int : " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
[Link]("long : " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
[Link]("float : " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
[Link]("double : " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
[Link]("char : " + (int) Character.MIN_VALUE + " to " + (int)
Character.MAX_VALUE);
[Link]("boolean : " + [Link] + " or " + [Link]);
}
}
c) Write a Java program to display formatted data using printf method in Java
public class FormatExamples {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
[Link]("Example 1 :%d\n", 123);
[Link]("Example 2 :%8d\n", 123);
[Link]("Example 3 :%-8d\n", 123);
[Link]("Example 4 :%8d\n", -123);
[Link]("Example 5 :%f\n", 123.456);
[Link]("Example 6 :%f\n", 123.4567896);
[Link]("Example 7 :%e\n", 123.456);
[Link]("Example 8 :%e\n", 1.23456e2);
[Link]("Example 9 :%e\n", 123.456e+0);
[Link]("Example 10 :%f\n", 1.23456E+2);
[Link]("Example 11 :%4.2f\n", 123.456);
[Link]("Example 12 :%8.2f\n", 123.456);
[Link]("Example 13 :%.3e\n", 123.456);
[Link]("Example 14 :%12.3e\n", 123.456);
}
}