Program: - 2

/*Write a simple java application to print a pyramid with 5 lines. The first line has one character, 2ndline has two characters and so on. The character to be used in the pyramid is taken as a command line argument.*/

import java.io.*;

class test

{

float a[];

test(int size)

{

a=new float[size];

}

public void getprice()

{

try

{

System.out.println();

for(int i = 0;i<a.length;i++)

{

DataInputStream ds=new DataInputStream(System.in);

System.out.print(" ==> Enter price for "+ a[i] +": ");

String nm=ds.readLine();

a[i]=Float.parseFloat(nm);

}

}

catch(IOException e)

{

}

}

public void putdata()

{

System.out.println();

for(int i = 0;i<a.length;i++)

{

System.out.println(" ==> You Enterd price : " + a[i]);

}

}

public void tot()

{

float tot =0;

System.out.println();

for (int i=0;i<a.length;i++)

{

tot +=a[i];

}

System.out.print( " ==> The total is : " + tot);

}

public void less()

{

float tot=0;

int count =0;

float A=0;

int R=0;

System.out.println();

for (int i=0;i<a.length;i++)

{

if (a[i]<5)

{

tot +=a[i];

count++;

}

A=tot/count;

}

System.out.println(" ==> The Total is : " +tot);

System.out.println(" ==> The less tha 5 is : " +count);

System.out.println(" ==> The average of less than 5 is : "+A);

for (int i=0;i<a.length;i++)

{

if (a[i]>5)

{

System.out.println(" ==> The remaing greater than 5 is : " +a[i]);

}

}

}

}

class arr

{

public static void main (String args[])

{

test t = new test(3);

t.getprice();

t.putdata();

t.tot();

t.less();

}

}

Output:-

F:\>javac arr.java

Note: arr.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

F:\>java arr

==>s Enter price for 0.0: 1

==>s Enter price for 0.0: 2

==>s Enter price for 0.0: 3

==> You Enterd price : 1.0

==> You Enterd price : 2.0

==> You Enterd price : 3.0

==> The total is : 6.0

==> The Total is : 6.0

==> The less tha 5 is : 3

==> The average of less than 5 is : 2.0

Program: - 3

/*Write a Java application which takes several command line arguments, which are supposed to be names of students and prints output as given below:

(Suppose we enter 3 names then output should be as follows):

Number of arguments = 3

1: First Student Name is =Tom

2: Second Student Name is =Dick

3: Third Student Name is =Harry

Hint: An array may be used for converting from numeric values from 1 to 20 into String. */

class my2

{

public static void main(String args[])

{

Stringa[]={"FIRST","SECOND","THIRED","FOURTH","FIFTH","SISTH","SEVENTH","EIGHTH","NINETH","Tenth","ELEVENTH","TWELVETH","THIRTEENTH","FOUREENTH","FIFTEENTH","SISTEENTH","SEVENTEENTH","EIGHTEENTH","NINENTEENTH","TWENTYTH"};

int n = args.length;

try

{System.out.println("\n\n Number of arguments = "+n+"\n");

for(int i=0;i<n;i++)

{

System.out.println(" "+(i+1) +": "+ a[i] + " Student name is: " +args[i]+"\n");

}

}

catch( Exception e)

{

System.out.println("Exception is :" +e);

}

}

}

Output:-

F:\>javac my2.java

F:\>java my2 TOM DICK HARRY

Number of arguments = 3

1: First Student name is: TOM

2: Second Student name is: DICK

3: Third Student name is: HARRY

Program: - 4

/*Write a class, with main method, which declares floating point variables and observe the output of dividing the floating point values by a 0, also observe the effect of assigning a high integer value (8 digits and above) to a float and casting it back to int and printing.*/

class casting

{

public static void main(String args[])

{

double a[] = {1.2,1.2,1.4,1.4};

double b = 0, c=0,d=0;

float e =0.0f;

System.out.println();

for(int i=0;i<a.length;i++)

{

b += a[i];

d=1234567.8912;

e=(float)d;

System.out.println(" ==> Enterd value a : " +a[i]);

}

System.out.println(" \n ==> Sum is b : " +b);

c=(b/0);

System.out.println("\n ==> The sum divided by 0 is c : " +c);

System.out.println("\n ==> The assign a 8 digit value is d : " +d);

System.out.println("\n ==> 8 digit value assigning to float :" +e);

}

}

Output:-

F:\>javac casting.java

F:\>java casting

==> Enterd value a : 1.2

==> Enterd value a : 1.2

==> Enterd value a : 1.4

==> Enterd value a : 1.4

==> Sum is b : 5.199999999999999

==> The sum divided by 0 is c : Infinity

==> The assign a 8 digit value is d : 12345678.8912

==> 8 digit value assigning to float :12345678.9

Program: - 5

/*Write a class called Statistics, which has a static method called average, which takes a onedimensional array for double type, as parameter, and prints the average for the values in the array.Now write a class with the main method, which creates a two-dimensional array for the four weeksof a month, containing minimum temperatures for the days of the week(an array of 4 by 7), anduses the average method of the Statistics class to compute and print the average temperatures forthe four weeks.*/

import java.io.*;

class Statics

{

public static void average(double d[])

{

double sum =0.0;

double average;

DataInputStream ds = new DataInputStream(System.in);

try

{

for (int i=0;i<d.length;i++)

{

sum+=d[i];

}

System.out.println(" \n ==> Sum is : " +sum);

average = sum/d.length;

System.out.println("\n ==> Average is :" +average);

}

catch(Exception e)

{

System.out.println(" Exceptoin is :" +e);

}

}

}

class p5

{

public static void main(String args[])

{

Double temp[][] = new Double[4][7];

DataInputStream ds = new DataInputStream (System.in);

try

{

for (int i=0;i<4;i++)

{

for (int j=0;j<7;j++)

{

System.out.print("[" +i+"][" +j+"] = ");

temp[i][j]=Double.parseDouble(ds.readLine());

}

}

double week[] = new double [7];

for (int i=0;i<4;i++)

{

for (int j=0;j<7;j++)

{

week[j] = temp[i][j];

}

Statics.average(week);

}

}

catch (Exception e)

{

System.out.println(" Exceptin is : " +e);

}

}

}

Program: - 6

/*Define a class called Product, each product has a name, a product code and manufacturer name.Define variables, methods and constructors, for the Product class. Write a class called Test Product, with the main method to test the methods and constructors of the Product class.*/

class p6

{

String nm;

String Prc;

String MN;

public p6()

{

nm = "Pen";

Prc = "P001";

MN = "Parker";

}

public void display()

{

System.out.print(" \n ==> Prduct Name is :"+nm);

System.out.print(" \n ==> Product Code is : "+Prc);

System.out.print(" \n ==> Manufacturer Name is :"+MN);

}

}

class Product

{

public static void main(String args[])

{

p6 p = new p6();

p.display();

}

}

Output:-

F:\>javac Product.java

F:\>java Product

==> Prduct Name is :Pen

==> Product Code is : P001

==> Manufacturer Name is :Parker

Program: - 7

/* Define a class called Cartesian Point, which has two instance variables, x and y. Provide the methods get X() and get Y() to return the values of the x and y values respectively, a method called move() which would take two integers as parameters and change the values of x and y respectively,a method called display() which would display the current values of x and y. Now overload the method move() to work with single parameter, which would set both x and y to the same values,Provide constructors with two parameters and overload to work with one parameter as well. Now define a class called Test Cartesian Point, with the main method to test the various methods in the Cartesian Point class.*/

import java.io.*;

class CartesianPoint

{

int x,y;

public CartesianPoint()

{

x=0; y=0;

}

public CartesianPoint(int a)

{

x=a; y=a;

}

public CartesianPoint(int a, int b)

{

x=a; y=b;

}

int getx()

{

return(x);

}

int gety()

{

return(y);

}

void move( int a)

{

x=a;

y=a;

}

void move( int a,int b)

{

x=a;

y=b;

}

void display()

{

System.out.println(" \n ==> value of X is : " +x);

System.out.println(" \n ==> value of y is : " +y);

}

}

class p7

{

public static void main(String args[])

{

try

{

CartesianPoint p1 = new CartesianPoint(100);

CartesianPoint p2 = new CartesianPoint(100,200);

CartesianPoint p3 = new CartesianPoint();

p3.move(200,100);

p1.display();

p2.display();

p3.display();

}

catch(Exception e)

{

}

}

}

Output:-

F:\>javac p7.java

F:\>java p7

==> value of X is : 100

==> value of y is : 100

==> value of X is : 100

==> value of y is : 200

==> value of X is : 200

==> value of y is : 100

Program: - 8

/* Define a class called Triangle, which has constructor with three parameters, which are of type Cartesian Point, defined in the exercise 7. Provide methods to find the area and the perimeter of the Triangle, a method display() to display the three Cartesian Points separated by ':' character, a method move() to move the first Cartesian Point to the specified x, y location, the move should take care of relatively moving the other points as well, a method called rotate, which takes two arguments, one is the Cartesian Point and other is the angle in clockwise direction. Overload the move method to work with Cartesian Point as a parameter. Now define a class called Test Triangle to test the various methods defined in the Triangle class. Similarly also define a class called Rectangle which has four Cartesian Point.*/

import java.io.*;

import java.lang.Math;

class CartesianPoint

{

int x,y;

public CartesianPoint()

{

x=0;

y=0;

}

public CartesianPoint(int a)

{

x=a;

y=a;

}

public CartesianPoint(int a, int b)

{

x=a;

y=b;

}

void display()

{

System.out.print("\n X: " +x);

System.out.print("\n Y: " +y);

}

}

class Triangle

{

CartesianPoint a;

CartesianPoint b;

CartesianPoint c;

public Triangle(CartesianPoint x, CartesianPoint y, CartesianPoint z)

{

a=x;

b=y;

c=z;

}

public void display()

{

a.display();

System.out.print("");

b.display();

System.out.print("");

c.display();

System.out.println("");

}

public CartesianPoint rotate(CartesianPoint cp,CartesianPoint p1,CartesianPoint a)

{

double x,y,a1,a2;

a1 = p1.x * Math.cos(a.x) - p1.y * Math.sin(a.y);

a2 = p1.x * Math.sin(a.x) + p1.y * Math.cos(a.y);

p1.x = (int) a1;

p1.y = (int) a2;

/*System.out.println(" \n ==> a1 is : "+a1);

System.out.println(" \n ==> a2 is : "+a2);*/

return p1;

}

}

class my8

{

public static void main(String args[])

{

try

{

CartesianPoint p1 = new CartesianPoint(0,0);

CartesianPoint p2 = new CartesianPoint(0,200);

CartesianPoint p3 = new CartesianPoint(200,0);

CartesianPoint ans = new CartesianPoint();

Triangle t1 = new Triangle(p1,p2,p3);

t1.display();

ans = t1.rotate(p1,p2,p3);

ans.display();

}

catch(Exception e)

{

System.out.println (" \n ==> Exception is : "+e);

}

}

}

Output:-

F:\>javac my8.java

F:\>java my8

X: 0

Y: 0

X: 0

Y: 200

X: 200

Y: 0

X: 0

Y: 200

- 1 -