0% found this document useful (0 votes)
4 views3 pages

Java Escape Sequences and Constants

The document contains 7 Java programs that demonstrate various Java concepts like escape sequences, variables, constants, casting, and format specifiers. Program 1 draws a boat using escape sequences. Program 2 demonstrates the use of tab escape sequences to format a table. Program 3 calculates the area of a room using variables for length and breadth. Program 4 calculates the area of a semicircular disc using constants and variables. Program 5 calculates the period of a pendulum. Program 6 demonstrates different number formats using printf. Program 7 shows examples of casting between data types like byte, int, and double.

Uploaded by

dinu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Escape Sequences and Constants

The document contains 7 Java programs that demonstrate various Java concepts like escape sequences, variables, constants, casting, and format specifiers. Program 1 draws a boat using escape sequences. Program 2 demonstrates the use of tab escape sequences to format a table. Program 3 calculates the area of a room using variables for length and breadth. Program 4 calculates the area of a semicircular disc using constants and variables. Program 5 calculates the period of a pendulum. Program 6 demonstrates different number formats using printf. Program 7 shows examples of casting between data types like byte, int, and double.

Uploaded by

dinu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Programs - Day 2

[Sample programs on:Escape sequences, variables, constants, casting, format


specifiers]
1.
// Character Boat Using \\
public class ToyBoat {
public static void main (String[ ] args) {
[Link]("\\__^__/"); // Start with \ (Escape) and draw a boat using \, ^ and /
}
}

2.
// Demonstrates the use of \t (Tab) escape sequence
public class TabDemo {
public static void main (String [ ] age) {
[Link]("Item\t\t\tPrice"); // Display "Item", leave 3 tabs and display "Price"
[Link]("------\t\t\t-------"); // Display "----" leave 3 tabs and display "-----"
[Link](); // Create a new line
[Link]("Pencil\t\t\tRs. 22.50");// Display "Pencil", leave 3 tabs and display "Rs. 22.50"
[Link]("Eraser\t\t\tRs. 34.00");// Display "Eraser", leave 3 tabs and display "Rs. 34.00"
}
}

3.

// Area of a rectangular room whose Length and Breadth are given


public class RoomArea {
public static void main (String[ ] args) {
int length = 100; // Assign 100 to length variable
int breadth = 50; // Assign 50 to breadth variable
int area; // Declare a variable called area to assign area
area = length*breadth; // Calculate area and assign it to variable area
// Display the area of the room
[Link]("The area of the room = " + area + " [Link]");
}
}

Withanage@faculty of Information Technology - UOM


4.
// Area of a Semi-Cricular Disc
public class DiscArea {
public static void main (String[ ] args) {
final double PI = (float) 355.0/113; // Store the value of PI as a constant called PI
double radius = 7; // Declare a variable called radius and assign 7 to it
double area = PI*radius*radius/2; // Assign the area of the disc the variable area
// Display the area of the disc
[Link]("The area of the given semi-circular disc = " + area + " [Link]");
}
}

5.
// Period of a Simple Pendulum
public class Period {
public static void main (String[ ] args) {
final double PI = 355.0/113;
final double g = 9.8;
int length = 5;
double period = 2*PI*[Link](length/g);
[Link]("The period of the given pendulum = " + period + " secs.");
}
}

6.
// Number Formats
public class NumberFormats {
public static void main (String[ ] args){
int n = 12, m = -12;
float x = 1234567.34567F;
[Link]("%d\n", n);
[Link]("%5d\n", n);
[Link]("%+d %+d\n", n, m);
[Link]("%05d\n", n);
[Link]("%.2f\n", x);
[Link]("%,.2f\n", x);
[Link]("%e\n", x);
[Link]("%E\n", x);
[Link]("%.2E\n", x);
}
}

Withanage@faculty of Information Technology - UOM


7.
// Data Casting Demo
public class DataCastDemo {
public static void main(String[ ] args){
/* int i = 121;
byte small = i; // Compilation Error - int cannot fit into byte
[Link]("Small Value =" + small);
*/
byte b = 125;
int b2i = b; // byte can fit into int
[Link]("Byte up casted to int =" + b2i);
int i = 125;
byte i2b = (byte) i; // Casting int into byte
[Link]("Down casted int value to byte =" + i2b);
int d2i = (int) 123.456; // Casting double into int
[Link]("Down casted double value to int =" + d2i);

}
}

Withanage@faculty of Information Technology - UOM

You might also like