JAVA GTU PROGRAMMING

JAVA GTU PROGRAMMING


Write a simple java application to print a pyramid with 5 lines. The first line has one character, 2nd

line has two characters and so on. The character to be used in the pyramid is taken as a command

line argument




package gtu;

class printd

{

public void print(String ch)

{

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

{

for(int k=5;k>i;k--)

System.out.print(" ");

for(int j=0;j

{

System.out.print(ch+" ");

}

System.out.println(" ");

}

}

}

class gtu_2

{

public static void main(String args[])

{

try

{

printd p=new printd();

p.print(args[0]);

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("caught " +e);

System.out.println("renter the character");

}

}

}




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.





package gtu;

class gtu_3

{

public static void main(String args[])

{

String arr[] = {"First", "Second", "Third", "Fourth", "Fifth",

"Sixth", "Seventh", "Eight", "Nineth","tenth",

"eleventh","twelth","thirteenth","fourteenth"};

for(int i=0;i

{

System.out.println((i+1)+": "+ arr[i]+" Student Name is = "+args[i]);

}

}

}






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.






package gtu;

class gtu_4

{

public static void main(String args[])

{

float test=12.0f;

int test2=123456789;

System.out.println("float value divide by zero : "+test/0);

float test3=(int)test2; //narowing conversion

System.out.println("assigning integer to float : "+test3);

int test4;

test4=(int)test3;

System.out.println("casting float to integer : "+test4);

}

}




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 weeks of a month, containing minimum temperatures for

the days of the week(an array of 4 by 7), and uses the average method of the

Statistics class to compute and print the average temperatures for

the four weeks.




package gtu;

import java.io.*;

class Statistics

{

public static void average(double arr[])

{

double avg=0;

for(int i=0;i

{

avg=avg+arr[i];

}

System.out.println("average of the array"+ avg/(arr.length));

}

}

class gtu_5 extends Statistics

{

public static void main(String args[])

{

DataInputStream ds=new DataInputStream(System.in);

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

double week[]=new double[7];

int i,j,k;

String data;

try{

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

{

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

{

System.out.print("Enter " + (i+1) +"th Week and " +(j+1)+"th day: ");

data=ds.readLine();

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

}

}

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

{

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

{

}

average(temp[i]);

}

}

catch(Exception e)

{

System.out.println("exception caught");

}

}

}






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 Product

{

int pCode;

String pName;

String mName;

Product(int pc)

{

this.pCode=pc;

}

Product(int pc,String s1)

{

this.pCode=pc;

this.pName=s1;

}

Product(int pc,String s1,String s2)

{ this.pCode=pc;

this.pName=s1;

this.mName=s2;

}

}

class testPrduct

{

public static void main(String args[])

{

System.out.println("constructor of peoduct with pc");

Product p1=new Product(001);

System.out.println("constructor of peoduct with pc and pn");

Product p2=new Product(001,"laptop");

System.out.println("constructor of peoduct with pc,pn and mn");

Product p3=new Product(001,"laptop","lenovo");

}

}





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.






package gtu;

import java.io.*;

class CP

{

int x,y;

public CP(int x,int y)

{

this.x=x;

this.y=y;

}

public CP(int a)

{

x=y=a;

}

int getx()

{

return x;

}

int gety()

{

return y;

}

void move(int x,int y)

{

this.x=x;

this.y=y;

}

void display()

{

System.out.println("Current value of x : "+getx());

System.out.println("Current value of y : "+gety());

}

void move(int a)

{

x=y=a;

}

}

class p7

{

public static void main(String arg[])

{

CP cp1 = new CP(5,25);

System.out.println("\n\nDefault Values with "+5+" And "+25);

cp1.display();

System.out.println("After Function move with two arguments is called,");

cp1.move(11,25);

cp1.display();

System.out.println("After Function move with one arguments is called,");

cp1.move(95);

cp1.display();

System.out.println("-------------------------------------");

CP cp2 = new CP(15);

System.out.println("\n\nDefault Values with"+15);

cp2.display();

System.out.println("After Function move with two arguments is called,");

cp2.move(35,65);

cp2.display();

System.out.println("After Function move with one arguments is called,");

cp2.move(105);

cp2.display();

}

}






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.






package gtu;

class Triangle

{

CartesianPoint P1;

CartesianPoint P2;

CartesianPoint P3;

Triangle()

{

}

Triangle(CartesianPoint p1, CartesianPoint p2, CartesianPoint p3)

{

P1=p1;

P2=p2;

P3=p3;

}

void display()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

}

void move()

{

P1.x=P1.x+10;

P1.y=P1.y+10;

P2.x=P2.x+10;

P2.y=P2.y+10;

P3.x=P3.x+10;

P3.y=P3.y+10;

}

void rotate(CartesianPoint P,int degree)

{

int x,y;

x=(int)(P.x * Math.cos(degree) - P.y * Math.sin(degree));

y=(int)(P.x * Math.sin(degree) + P.y * Math.cos(degree));

P.x=x;

P.y=y;

}

public String toString()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

return "";

}

boolean equals(Triangle t)

{

boolean f;

if(P1.x==t.P1.x && P1.y==t.P1.y && P2.x==t.P2.x && P2.y==t.P2.y && P3.x==t.P3.x && P3.y==t.P3.y )

f=true;

else

f=false;

return f;

}

}

class Rectangle

{

CartesianPoint P1;

CartesianPoint P2;

CartesianPoint P3;

CartesianPoint P4;

Rectangle()

{

}

Rectangle(CartesianPoint p1, CartesianPoint p2, CartesianPoint p3,CartesianPoint p4)

{

P1=p1;

P2=p2;

P3=p3;

P4=p4;

}

void display()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

System.out.println("Cartesian Point P4.X: "+P4.x);

System.out.println("Cartesian Point P4.Y: "+P4.y);

}

void move()

{

P1.x=P1.x+15;

P1.y=P1.y+15;

P2.x=P2.x+15;

P2.y=P2.y+15;

P3.x=P3.x+15;

P3.y=P3.y+15;

P4.x=P4.x+15;

P4.y=P4.y+15;

}

void rotate(CartesianPoint P,int degree)

{

int x,y;

x=(int)(P.x * Math.cos(degree) - P.y * Math.sin(degree));

y=(int)(P.x * Math.sin(degree) + P.y * Math.cos(degree));

P.x=x;

P.y=y;

}

public String toString()

{

System.out.println("Cartesian Point P1.X: "+P1.x);

System.out.println("Cartesian Point P1.Y: "+P1.y);

System.out.println("Cartesian Point P2.X: "+P2.x);

System.out.println("Cartesian Point P2.Y: "+P2.y);

System.out.println("Cartesian Point P3.X: "+P3.x);

System.out.println("Cartesian Point P3.Y: "+P3.y);

System.out.println("Cartesian Point P4.X: "+P4.x);

System.out.println("Cartesian Point P4.Y: "+P4.y);

return "";

}

boolean equals(Rectangle t)

{

boolean f;

if(P1.x==t.P1.x && P1.y==t.P1.y && P2.x==t.P2.x && P2.y==t.P2.y && P3.x==t.P3.x && P3.y==t.P3.y && P4.x==t.P4.x && P4.y==t.P4.y )

f=true;

else

f=false;

return f;

}

}

class GTU8

{

public static void main(String arg[])

{

CartesianPoint P1=new CartesianPoint(10,20);

CartesianPoint P2=new CartesianPoint(20,20);

CartesianPoint P3=new CartesianPoint(30,20);

Triangle t=new Triangle(P1,P2,P3);

System.out.println("\n\n\t\tTriangle\n\n");

t.display();

System.out.println("\n\nOverload toString method\n\n");

System.out.println(t);

System.out.println("\nEach Cartesian Point is move 10 relatively\n");

t.move();

t.display();

System.out.println("\nEach Cartesian Point is rotating 90 degree");

t.rotate(P1,10);

t.rotate(P2,90);

t.rotate(P3,90);

t.display();

P1=new CartesianPoint(10,20);

P2=new CartesianPoint(40,30);

P3=new CartesianPoint(50,90);

Triangle t2=new Triangle(P1,P2,P3);

System.out.println("\n\n\t\tTriangle 2\n\n");

System.out.println(t2);

System.out.println("Equlas T1 and T2 "+ t.equals(t2));

CartesianPoint P4=new CartesianPoint(25,55);

Rectangle r=new Rectangle(P1,P2,P3,P4);

System.out.println("\n\n\t\tRectangle\n\n");

r.display();

System.out.println("\n\nOverload toString method\n\n");

System.out.println(r);

System.out.println("\nEach Cartesian Point is move 15 relatively\n");

r.move();

r.display();

System.out.println("\nEach Cartesian Point is rotating 90 degree");

r.rotate(P1,90);

r.rotate(P2,90);

r.rotate(P3,90);

r.rotate(P4,90);

r.display();

P1=new CartesianPoint(10,20);

P2=new CartesianPoint(20,20);

P3=new CartesianPoint(30,20);

P4=new CartesianPoint(25,55);

Rectangle r2=new Rectangle(P1,P2,P3,P4);

System.out.println("\n\n\t\tRectangle 2\n\n");

System.out.println(r2);

System.out.println("Equlas R1 and R2 "+ r.equals(r2));

}

}

0 comments:

Post a Comment