Light Distance Calculation in Java
Light Distance Calculation in Java
class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
[Link]("In " + days);
[Link](" days light will travel about ");
[Link](distance + " miles.");
}
}
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
[Link]("Area of circle is " + a);
}
}
Output
class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
[Link]("b is " + b);
b = true;
[Link]("b is " + b);
// a boolean value can control the if statement
if(b) [Link]("This is executed.");
b = false;
if(b) [Link]("This is not executed.");
// outcome of a relational operator is a boolean value
[Link]("10 > 9 is " + (10 > 9));
}
}
b is false
b is true
This is executed.
10 > 9 is true
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
[Link]("April has " + month_days[3] + " days.");
}
}
Output
April has 30 days.
Average an array of values.
class Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
[Link]("Average is " + result / 5);
}
}
Output
Average is 12.299999999999999
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
[Link](twoD[i][j] + " ");
[Link]();
}
}
}
01234
56789
1011121314
1516171819
Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
[Link](twoD[i][j] + " ");
[Link]();
}
}
}
class Matrix {
public static void main(String args[]) {
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
int i, j;
for(i=0; i<4; i++) {
for(j=0; j<4; j++)
[Link](m[i][j] + " ");
[Link]();
}
}
}
Output
class threeDMatrix {
public static void main(String args[]) {
int threeD[][][] = new int[3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
for(k=0; k<5; k++)
[Link](threeD[i][j][k] + " ");
[Link]();
}
[Link]();
}
}
}
OUTPUT:
00000
00000
00000
00000
00000
01234
02468
0 3 6 9 12
00000
02468
0 4 8 12 16
0 6 12 18 24
Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
[Link]("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("c = " + c);
[Link]("d = " + d);
[Link]("e = " + e);
// arithmetic using doubles
[Link]("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
[Link]("da = " + da);
[Link]("db = " + db);
[Link]("dc = " + dc);
[Link]("dd = " + dd);
[Link]("de = " + de);
}
}
Output
Integer Arithmetic
a=2
b=6
c=1
d = -1
e=1
class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.25;
[Link]("x mod 10 = " + x % 10);
[Link]("y mod 10 = " + y % 10);
}
}
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
[Link](" a = " + binary[a]);
[Link](" b = " + binary[b]);
[Link](" a|b = " + binary[c]);
[Link](" a&b = " + binary[d]);
[Link](" a^b = " + binary[e]);
[Link]("~a&b|a&~b = " + binary[f]);
[Link](" ~a = " + binary[g]);
}
}
Output
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
Bitwise Operator Assignments
class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("c = " + c);
}
}
The output of this program is shown here:
a=3
b=1
c=6
Demonstrate the boolean logical operators.
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
[Link](" a = " + a);
[Link](" b = " + b);
[Link](" a|b = " + c);
[Link](" a&b = " + d);
[Link](" a^b = " + e);
[Link]("!a&b|a&!b = " + f);
[Link](" !a = " + g);
}
}
Output
a = true
b = false
a|b = true
a&b = false
a^b = true
a&b|a&!b = true
!a = false
Demonstrate if-else-if statements.
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
[Link]("April is in the " + season + ".");
}
}
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
[Link]("i is zero.");
break;
case 1:
[Link]("i is one.");
break;
case 2:
[Link]("i is two.");
break;
case 3:
[Link]("i is three.");
break;
default:
[Link]("i is greater than 3.");
}
}
}
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.
In a switch, break statements are optional.
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
[Link]("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
[Link]("i is less than 10");
break;
default:
[Link]("i is 10 or more");
}
}
}
This program generates the following output:
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more
Demonstrate the while loop.
class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
[Link]("tick " + n);
n--;
}
}
}
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
Using a do-while to process a menu selection
class Menu {
public static void main(String args[])
throws [Link] {
char choice;
do {
[Link]("Help on:");
[Link](" 1. if");
[Link](" 2. switch");
[Link](" 3. while");
[Link](" 4. do-while");
[Link](" 5. for\n");
[Link]("Choose one:");
choice = (char) [Link]();
} while( choice < '1' || choice > '5');
[Link]("\n");
switch(choice) {
case '1':
[Link]("The if:\n");
[Link]("if(condition) statement;");
[Link]("else statement;");
break;
case '2':
[Link]("The switch:\n");
[Link]("switch(expression) {");
[Link](" case constant:");
[Link](" statement sequence");
[Link](" break;");
[Link](" // ...");
[Link]("}");
break;
case '3':
[Link]("The while:\n");
[Link]("while(condition) statement;");
break;
case '4':
[Link]("The do-while:\n");
[Link]("do {");
[Link](" statement;");
[Link]("} while (condition);");
break;
case '5':
[Link]("The for:\n");
[Link]("for(init; condition; iteration)");
[Link](" statement;");
break;
}
}
}
Output
Help on:
1. if
2. switch
3. while
4. do-while
5. for
Choose one:1
The if:
if(condition) statement;
else statement;
Help on:
1. if
2. switch
3. while
4. do-while
5. for
Choose one:2
The switch:
switch(expression) {
case constant:
statement sequence
break;
// ...
}
Help on:
1. if
2. switch
3. while
4. do-while
5. for
Choose one: 4
The do-while:
do {
statement;
} while (condition);
Loops may be nested.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
[Link]("*");
[Link]();
}
}
}
The output produced by this program is shown here:
**********
*********
********
*******
******
*****
****
***
**
*
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i<3; i++) {
[Link]("Pass " + i + ": ");
for(int j=0; j<100; j++) {
if(j == 10) break; // terminate loop if j is 10
[Link](j + " ");
}
[Link]();
}
[Link]("Loops complete.");
}
}
This program generates the following output:
Pass 0: 0 1 2 3 4 5 6 7 8 9
Pass 1: 0 1 2 3 4 5 6 7 8 9
Pass 2: 0 1 2 3 4 5 6 7 8 9
Loops complete.
Class
A Simple Class
class Box
{
double width;
double height;
double depth;
}
class class1
{
public static void main(String args[])
{
Box mca = new Box();
double vol;
Output
Volume is 3000.0
A Simple Class2
class Box {
double width;
double height;
double depth;
}
class BoxDemo2
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
[Link] = 10;
[Link] = 20;
[Link] = 15;
[Link] = 3;
[Link] = 6;
[Link] = 9;
Output
Volume is 3000.0
Volume is 162.0
This program uses a parameterized method.
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}
Output
Volume is 3000.0
Volume is 162.0
Constructors
class mca
{
double width;
double height;
double depth;
mca() {
[Link]("Constructing Box");
width = 10;
height = 10;
depth = 10;
}
double volume() {
return width * height * depth;
}
}
class BoxDemo6
{
public static void main(String args[]) {
mca mybox1 = new mca();
mca mybox2 = new mca();
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}
Output
Constructing Box
Constructing Box
Volume is 1000.0
Volume is 1000.0
Parameterized Constructors program
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
class example
{
public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}
}
Volume is 3000.0
Volume is 162.0
Demonstrate method overloading.
class OverloadDemo {
void test() {
[Link]("No parameters");
}
void test(int a) {
[Link]("a: " + a);
}
double test(double a) {
[Link]("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
[Link]();
[Link](10);
[Link](10, 20);
result = [Link](123.25);
[Link]("Result of [Link](123.25): " + result);
}
}
class Factorial {
// this is a recursive function
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
[Link]("Factorial of 3 is " + [Link](3));
[Link]("Factorial of 4 is " + [Link](4));
[Link]("Factorial of 5 is " + [Link](5));
}
}
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
DEMONSTRATE AN INNER CLASS.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
[Link]();
}
class Inner {
void display() {
[Link]("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
[Link]();
}
}
Demonstrating Strings.
class StringDemo {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;
[Link](strOb1);
[Link](strOb2);
[Link](strOb3);
}
}
class StringDemo2 {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
[Link]("Length of strOb1: " +[Link]());
[Link]("Char at index 3 in strOb1: " +[Link](3));
if([Link](strOb2))
[Link]("strOb1 == strOb2");
else
[Link]("strOb1 != strOb2");
if([Link](strOb3))
[Link]("strOb1 == strOb3");
else
[Link]("strOb1 != strOb3");
}
}
class A {
int i;
}
class B extends A {
int i;
B(int a, int b) {
super.i = a;
i = b;
}
void show() {
[Link]("i in superclass: " + super.i);
[Link]("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
[Link]();
}
}
class A {
A() {
[Link]("Inside A's constructor.");
}
}
class B extends A {
B() {
[Link]("Inside B's constructor.");
}
}
class C extends B {
C() {
[Link]("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
Inside As constructor
Inside Bs constructor
Inside Cs constructor
Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
void show() {
[Link]("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
[Link]("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
[Link]();
}
}
void callme() {
[Link]("Inside B's callme method");
}
}
class C extends A {
void callme() {
[Link]("Inside C's callme method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
A r;
r = a;
[Link]();
r = b;
[Link]();
r = c;
[Link]();
}
}
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
[Link]("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
double area() {
[Link]("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
double area() {
[Link]("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class FindAreas {
public static void main(String args[]) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
[Link]("Area is " + [Link]());
figref = t;
[Link]("Area is " + [Link]());
figref = f;
[Link]("Area is " + [Link]());
}
}
package college;
import [Link].*;
public class student
{
String regno;
String name;
public student()throws IOException
{
DataInputStream in = new DataInputStream([Link]);
[Link]("Enter the register no:");
regno=[Link]();
[Link]("Enter the name:");
name=[Link]();
}
public void print()
{
[Link]("\tRegister no\t:"+" "+regno);
[Link]("\tName\t\t:"+" "+name);
}
}
import college.*;
import course.*;
import [Link].*;
public class details
{
public static void main(String args[])throws IOException
{
student s = new student();
MCA d = new MCA();
[Link]("\n\n\tYour package details are show");
[Link]("\t*****************************");
[Link]();
[Link]();
[Link]("\t*****************************");
}
}
interface inter1
{
int add(int a, int b);
int sub(int a, int b);
}
interface inter2
{
int mul(int a, int b);
int div(int a, int b);
}
class multi implements inter1, inter2
{
public int add(int a, int b)
{
return a+b;
}
public int sub (int a, int b)
{
return a-b;
}
public int mul (int a, int b)
{
return a*b;
}
public int div (int a, int b)
{
return a/b;
}
public static void main(String args[])
{
multi m = new multi();
[Link]([Link](10,20));
[Link]([Link](10,20));
[Link]([Link](10,20));
[Link]([Link](10,20));
}
}
30
-10
200
0
ArithmeticException generated by the division-by-
zero error
class Exc2 {
public static void main(String args[]) {
int d, a;
try {
d = 0;
a = 42 / d;
[Link]("This will not be printed.");
} catch (ArithmeticException e) {
[Link]("Division by zero.");
}
[Link]("After catch statement.");
}
}
This program generates the following output:
Division by zero.
After catch statement.
Output
C:\>java MultiCatch
a=0
Divide by 0: [Link]: / by zero
After try/catch blocks.
C:\>java MultiCatch TestArg
a=1
Array index oob: [Link]
After try/catch blocks.
class NestTry {
public static void main(String args[]) {
try {
int a = [Link];
int b = 42 / a;
[Link]("a = " + a);
try {
if(a==1) a = a/(a-a);
if(a==2) {
int c[] = { 1 };
c[42] = 99;
}
} catch(ArrayIndexOutOfBoundsException e) {
[Link]("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
[Link]("Divide by 0: " + e);
}
}
}
Output
C:\>java NestTry
Divide by 0: [Link]: / by zero
C:\>java NestTry One
a=1
Divide by 0: [Link]: / by zero
C:\>java NestTry One Two
a=2
Array index out-of-bounds:
[Link]
Throws simple programs
class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
[Link]("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
[Link]("Caught " + e);
}
}
}
Output
class FinallyDemo {
static void procA() {
try {
[Link]("inside procA");
throw new RuntimeException("demo");
} finally {
[Link]("procA's finally");
}
}
static void procB() {
try {
[Link]("inside procB");
return;
} finally {
[Link]("procB's finally");
}
}
static void procC() {
try {
[Link]("inside procC");
} finally {
[Link]("procC's finally");
}
}
public static void main(String args[]) {
try {
procA();
} catch (Exception e) {
[Link]("Exception caught");
}
procB();
procC();
}
}
Output
inside procA
procAs finally
Exception caught
inside procB
procBs finally
inside procC
procCs finally
Output
Called compute(1)
Normal exit
Called compute(20)
Caught MyException[20]
Controlling the main Thread.
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = [Link]();
[Link]("Current thread: " + t);
[Link]("My Thread");
[Link]("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
[Link](n);
[Link](1000);
}
} catch (InterruptedException e) {
[Link]("Main thread interrupted");
}
}
}
Output
try {
[Link]("Waiting for threads to finish.");
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Main thread Interrupted");
}
[Link]("Thread One is alive: "+ [Link]());
[Link]("Thread Two is alive: "+ [Link]());
[Link]("Thread Three is alive: "+ [Link]());
[Link]("Main thread exiting.");
}
}
An example of deadlock.
class A {
synchronized void foo(B b) {
String name = [Link]().getName();
[Link](name + " entered [Link]");
try {
[Link](1000);
} catch(Exception e) {
[Link]("A Interrupted");
}
[Link](name + " trying to call [Link]()");
[Link]();
}
synchronized void last() {
[Link]("Inside [Link]");
}
}
class B {
synchronized void bar(A a) {
String name = [Link]().getName();
[Link](name + " entered [Link]");
try {
[Link](1000);
} catch(Exception e) {
[Link]("B Interrupted");
}
[Link](name + " trying to call [Link]()");
[Link]();
}
synchronized void last() {
[Link]("Inside [Link]");
}
}
class Deadlock implements Runnable {
A a = new A();
B b = new B();
Deadlock() {
[Link]().setName("MainThread");
Thread t = new Thread(this, "RacingThread");
[Link]();
[Link](b); .
[Link]("Back in main thread");
}
public void run() {
[Link](a); .
[Link]("Back in other thread");
}
public static void main(String args[]) {
new Deadlock();
}
}
When you run this program, you will see the output shown here:
Demonstrate PrintWriter
import [Link].*;
public class PrintWriterDemo {
public static void main(String args[]) {
PrintWriter pw = new PrintWriter([Link], true);
[Link]("This is a string");
int i = -7;
[Link](i);
double d = 4.5e-7;
[Link](d);
}
}
The output from this program is shown here:
This is a string
-7
4.5E-7
String Handling
Construct one String from another.
class MakeString {
public static void main(String args[]) {
char c[] = {'J', 'a', 'v', 'a'};
String s1 = new String(c);
String s2 = new String(s1);
[Link](s1);
[Link](s2);
}
}
class SubStringCons {
public static void main(String args[]) {
byte ascii[] = {65, 66, 67, 68, 69, 70 };
String s1 = new String(ascii);
[Link](s1);
String s2 = new String(ascii, 2, 3);
[Link](s2);
}
}
ABCDEF
CDE
getChars
class getCharsDemo {
public static void main(String args[]) {
String s = "This is a demo of the getChars method.";
int start = 10;
int end = 14;
char buf[] = new char[end - start];
[Link](start, end, buf, 0);
[Link](buf);
}
}
Here is the output of this program:
Demo
class SortString {
static String arr[] = {
"Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"
};
public static void main(String args[]) {
for(int j = 0; j < [Link]; j++) {
for(int i = j + 1; i < [Link]; i++) {
if(arr[i].compareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
[Link](arr[j]);
}
}
}
class StringReplace {
public static void main(String args[]) {
String org = "This is a test. This is, too.";
String search = "is";
String sub = "was";
String result = "";
int i;
do {
[Link](org);
i = [Link](search);
if(i != -1) {
result = [Link](0, i);
result = result + sub;
result = result + [Link](i + [Link]());
org = result;
}
} while(i != -1);
}
}
The output from this program is shown here:
Demonstrate append().
class appendDemo {
public static void main(String args[]) {
String s;
int a = 42;
StringBuffer sb = new StringBuffer(40);
s = [Link]("a = ").append(a).append("!").toString();
[Link](s);
}
}
The output of this example is shown here:
a = 42!
Demonstrate insert().
class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("I Java!");
[Link](2, "like ");
[Link](sb);
}
}
The output of this example is shown here:
I like Java!
import [Link].*;
class buf
{
public static void main(String args[]) throws Exception
{
FileReader filereader = new FileReader("[Link]");
BufferedReader bufferedreader = new BufferedReader(filereader);
String instring;
Output
import [Link].*;
class filewriter
{
public static void main(String args[]) throws Exception
{
char data[] = {'B','C','A','s',' ','i','s',' ','a',' ','s','t','r','i','n','g',' ','o','f',
' ','t','e','x','t','.'};
[Link]();
[Link]();
[Link]();
}
}
Output
Networking
DatagramServers
import [Link].*;
import [Link].*;
class DatagramServers
{
public static DatagramSocket ds;
public static int clientport = 789 , serverport = 790;
public static void main(String args[])throws Exception
{
byte buffer[]=new byte[1024];
ds = new DatagramSocket(serverport);
DataInputStream dis=new DataInputStream([Link]);
[Link]("Server waiting for the input");
InetAddress addr = [Link]("localhost");
[Link](addr);
while(true)
{
String str=[Link]();
if(str==null || [Link]("end"))
break;
buffer = [Link]();
[Link](new DatagramPacket(buffer,[Link](),addr,clientport));
}
}
}
DatagramClient
import [Link].*;
import [Link].*;
class DatagramClient
{
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static int Clientport = 789, serverport=790;
public static void main(String args[])throws Exception
{
ds = new DatagramSocket(Clientport);
[Link]("client is waiting for server to send the data");
[Link]("Press Ctrl + c to come to dos prompt");
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,[Link]);
[Link](p);
String s=new String([Link](),0,[Link]());
[Link](s);
}
}
}