Java Practical Submission Format
Java Practical Submission Format
import java.io.*;
class Ticket
public static void main(String S[]) throws IOException
{
BufferedReader
br
=
new
BufferedReader(new
InputStreamReader(System.in));
Train T[] = new Train[5];
T[0] = new Train("RAJDHANI
",100,200,300);
T[1] = new Train("SHATABDI
",130,200,120);
T[2] = new Train("KARNAVATI
",150,200,250);
T[3] = new Train("INTERCITY
",120,160,200);
T[4] = new Train("METRO EXPRESS ",100,100,100);
int x;
do
{
System.out.print("\n-------------------------------");
System.out.print("\n Main Menu ");
System.out.print("\n-------------------------------");
for(int i=0;i<5;i++)
{
System.out.print("\n"+(i+1) +" "+T[i].Train_Name + "
"+ T[i].TotalSeat());
}
System.out.print("\n0 EXIT ");
System.out.print("\n-------------------------------");
System.out.print("\n ENTER TRAIN NO : ");
x = Integer.parseInt(br.readLine());
if(x <= 5 && x > 0)
{
T[x-1].menu();
}
}
while(x!=0);
}
}
class Train
{
public
BufferedReader
br
=
new
BufferedReader(new
InputStreamReader(System.in));
public String Train_Name;
privateint c1,c2,c3;
public Train()
{
c1 = 0;
c2 = 0;
c3 = 0;
}
public Train(String Tnm,int First,int Second,int Third)
{
Train_Name = Tnm;
c1 = First;
c2 = Second;
c3 = Third;
Break;
}
case 1:
case 2:
case 3:
default:
Available Seats();
break;
GetSeatno();
break;
break;
System.out.print("\nINVALID CHOICE CODE");
}
2.
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.
class Asspra2
{
public static void main(String s[])
{
if(s.length <= 0)
System.out.print("Pass Arguments Properly...!");
else
{
char c = s[1].charAt(0);
for(int i=0;i<=Integer.parseInt(s[0]);i++)
{
for(int j=0;j<i;j++)
{
System.out.print(c);
}
System.out.print("\n");
}
}
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
class Asspra3
{
public static void main(String s[])
{
String a[];
a = new String[20];
a[1] = "First";
a[2] = "Second";
a[3] = "Third";
a[4] = "Forth";
a[5] = "Fifth";
a[6] = "Sixth";
a[7] = "Seventh";
a[8] = "Eighth";
a[9] = "Nineth";
a[10] = "Tenth";
System.out.println("Number of arguments = " +s.length);
for(int i=0;i<s.length;i++)
System.out.println( (i+1) + ":" +a[i+1]+" Student Name is :
" +s[i]);
}
}
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
5.Write a class called Statistics, which has a static method called average, which
takes a one-dimensional 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 temperatues for the
four weeks.
import java.util.*;
class Statistics
{
public static double average(double temp[])
{
int i=0;
double sum=0;
for(double x:temp)
{
i++;
sum += x;
}
return sum/i;
}
}
class Asspro5
{
public static void main(String args[])
{
double[][] a = new double[4][7];
a[0][0] = 25.34;
a[0][1] = 30.33;
a[0][2] = 35.34;
a[0][3] = 40.23;
a[0][4] = 45.12;
a[0][5] = 34.35;
a[0][6] = 22.56;
of
of
1st
3rd
Week
Week
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 TestProduct, with the main method to test the methods and
constructors of the Product class.
import java.util.*;
import java.io.*;
class Product
{
private int P_code;
private String P_name;
private String P_manufacture;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public Product()
{
P_code = 0;
P_name = "";
P_manufacture = "";
}
public Product(int code,String name,String manufacture)
{
P_code = code;
P_name = name;
P_manufacture = manufacture;
}
public void setpdetail() throws IOException
{
System.out.printf("\nEnter Product Code
:");
7. Define a class called CartesianPoint, which has two instance variables, x and y. Provide the
methods getX() and getY() 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
thesame values, . Provide constructors with two parameters and overload to work with one
parameter as well. Now define a class called TestCartesianPoint, with the main method to test the
various methods in the CartesianPoint class.
class Cartesian_Point
{
int x;
int y;
public Cartesian_Point(int x,int y)
{
this.x = x;
this.y = y;
}
public Cartesian_Point(int x)
{
this(x,x);
}
public String toString()
{
return "("+x+","+y+")";
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
public void move(int x,int y)
{
this.x = x;
this.y = y;
}
public void move(int x)
{
move(x,x);
}
}
class Test_Cartesian_Point
{
public static void main(String args[])
{
Cartesian_Point s = new Cartesian_Point(0,0);
System.out.println(s);
s.move(10,20);
System.out.println(s);
s.move(50);
System.out.println(s);
System.out.println(s.getx());
System.out.println(s.gety());
Cartesian_Point t = new Cartesian_Point(5);
System.out.println(t);
}
}
8. Define a class called Triangle, which has constructor with three parameters, which are of type
CartesianPoint, defined in the exercise 7. Provide methods to find the area and the perimeter of the
Triangle, a method display() to display the three CartesianPoints separated by ':' character, a
method move() to move the first CartesianPoint 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 CartesianPoint and other is the angle in clockwise direction. Overload the
move method to work with CartesianPoint as a parameter. Now define a class called TestTriangle to
test the various methods defined in the Triangle class. Similarly also define a class called Rectangle
which has four CartesianPoint.
class Triangle
{
Cartesian_Point p1;
Cartesian_Point p2;
Cartesian_Point p3;
public Triangle(Cartesian_Point p1,Cartesian_Point p2,Cartesian_Point p3)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public void move(Cartesian_Point p1,Cartesian_Point p2,Cartesian_Point p3)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public void display()
{
System.out.print("["+p1+":"+p2+":"+p3+"]\n");
}
public String toString()
{
return "["+p1+":"+p2+":"+p3+"]\n";
}
public double area()
{
double a = Math.pow((p1.getx() - p2.getx()),2)+Math.pow((p1.gety() p2.gety()),2);
double b = Math.pow((p2.getx() - p3.getx()),2)+Math.pow((p2.gety() p3.gety()),2);
double c = Math.pow((p3.getx() - p1.getx()),2)+Math.pow((p3.gety() - p1.gety()),2);
a = Math.sqrt(a);
b = Math.sqrt(b);
c = Math.sqrt(c);
double s =(a+b+c)/2;
p1;
p2;
p3;
p4;
Math.pow((p1.getx()
p2.getx()),2)+Math.pow((p1.gety()
double
Math.pow((p2.getx()
p3.getx()),2)+Math.pow((p2.gety()
p2.gety()),2);
p3.gety()),2);
double c = Math.pow((p3.getx() - p4.getx()),2)+Math.pow((p3.gety() - p4.gety()),2);
double d = Math.pow((p4.getx() - p1.getx()),2)+Math.pow((p4.gety() p1.gety()),2);
//System.out.print("\nA="+a+"\nB="+b+"\nC="+c+"\nD="+d);
if(a == c || a == b || a == d )
{
if( a == c && b == d)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
//
System.out.print("ac,bd");
}
else if( a == b && c == d)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
//
System.out.print("ab,cd");
}
else if( a == d && b == d)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
//
System.out.print("ad,bc");
}
}
else
System.out.print("Not A Rectangle");
}
public void move(Cartesian_Point p1,Cartesian_Point p2,Cartesian_Point p3,Cartesian_Point
p4)
{
this.p1
this.p2
this.p3
this.p4
=
=
=
=
p1;
p2;
p3;
p4;
}
public void display()
9. Override the toString, equals and the hashCode methods of the classes Triangle and Rectangle
defined in exercises 7 and 8 above, in appropriate manner, and also redefine the display methods
to use the toString method.
class Triangle
{
Cartesian_Point p1;
Cartesian_Point p2;
Cartesian_Point p3;
public Triangle(Cartesian_Point p1,Cartesian_Point p2,Cartesian_Point p3)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public void move(Cartesian_Point p1,Cartesian_Point p2,Cartesian_Point p3)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public void display()
{
System.out.print("["+p1+":"+p2+":"+p3+"]\n");
}
public String toString()
{
return "["+p1+":"+p2+":"+p3+"]\n";
}
public double area()
{
double a = Math.pow((p1.getx() - p2.getx()),2)+Math.pow((p1.gety() p2.gety()),2);
double b = Math.pow((p2.getx() - p3.getx()),2)+Math.pow((p2.gety() p3.gety()),2);
double c = Math.pow((p3.getx() - p1.getx()),2)+Math.pow((p3.gety() - p1.gety()),2);
a = Math.sqrt(a);
b = Math.sqrt(b);
c = Math.sqrt(c);
double s =(a+b+c)/2;
double x = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return x;
p1;
p2;
p3;
p4;
Math.pow((p1.getx()
p2.getx()),2)+Math.pow((p1.gety()
double
Math.pow((p2.getx()
p3.getx()),2)+Math.pow((p2.gety()
p2.gety()),2);
p3.gety()),2);
double c = Math.pow((p3.getx() - p4.getx()),2)+Math.pow((p3.gety() - p4.gety()),2);
double d = Math.pow((p4.getx() - p1.getx()),2)+Math.pow((p4.gety() p1.gety()),2);
//System.out.print("\nA="+a+"\nB="+b+"\nC="+c+"\nD="+d);
if(a == c || a == b || a == d )
{
if( a == c && b == d)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
//
System.out.print("ac,bd");
}
else if( a == b && c == d)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
//
System.out.print("ab,cd");
}
else if( a == d && b == d)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
//
System.out.print("ad,bc");
}
}
else
System.out.print("Not A Rectangle");
}
public void move(Cartesian_Point p1,Cartesian_Point p2,Cartesian_Point p3,Cartesian_Point
p4)
{
this.p1
this.p2
this.p3
this.p4
=
=
=
=
p1;
p2;
p3;
p4;
}
public void display()
{
System.out.print("["+p1+":"+p2+":"+p3+":"+p4+"]\n");
Math.pow((x[2].getx()
x[3].getx()),2)+Math.pow((x[2].gety()
double
Math.pow((x[3].getx()
x[0].getx()),2)+Math.pow((x[3].gety()
x[3].gety()),2);
x[0].gety()),2);
a = Math.sqrt(a);
b = Math.sqrt(b);
c = Math.sqrt(c);
d = Math.sqrt(d);
if( a == c && b == d)
{
return a*b;
//
System.out.print("ac,bd");
}
else if( a == b && c == d)
{
//
System.out.print("ab,cd");
return a*c;
}
else if( a == d && b == d)
{
//
System.out.print("ad,bc");
return a*b;
}
else
return 0;
}
public void rotate(Cartesian_Point T,double angle)
{
}
}
class Test
{
public static void main(String args[])
{
Cartesian_Point a[] = new Cartesian_Point[5];
a[0] = new Cartesian_Point(0,0);
a[1] = new Cartesian_Point(5,0);
a[2] = new Cartesian_Point(5,5);
Polygon T1 = new Triangle(a);
System.out.print(T1);
System.out.print("AreaofT1 = "+T1.area()+"\n");
a[0] = new Cartesian_Point(0,0);
a[1] = new Cartesian_Point(5,0);
a[2] = new Cartesian_Point(5,5);
a[3] = new Cartesian_Point(0,5);
Rectangle R1 = new Rectangle(a);
System.out.print(R1);
System.out.print("AreaofR1 = "+R1.area()+"\n");
}
}
11. Make the class CartesianPoint, belong to a package called edu.gtu.geometry, the classes
Polygon, Triangle and Rectangle belong to the package edu.gtu.geometry.shapes and the classes
TestCartesianPoint, TestTriangle, TestRectangle and TestPolygon belong to the package edu.gtu.test.
Use appropriate access specifiers for the classes and the members of the classes defined in the
earlier exercises. Now onwards all the classes must be defined in a package.
package mca.third.geometry;
public class Triangle extends Polygon
{
public Triangle(Cartesian_Point p1[])
{
super(p1);
}
public void move(Cartesian_Point p1,Cartesian_Point p2,Cartesian_Point p3)
{
x[0] = p1;
x[1] = p2;
x[2] = p3;
}
public void display()
{
System.out.print("["+x[0]+":"+x[1]+":"+x[2]+"]\n");
}
public String toString()
{
return "["+x[0]+":"+x[1]+":"+x[2]+"]\n";
}
public double area()
{
double a = Math.pow((x[0].getx() - x[1].getx()),2)+Math.pow((x[0].gety() x[1].gety()),2);
double b = Math.pow((x[1].getx() - x[2].getx()),2)+Math.pow((x[1].gety() x[2].gety()),2);
double c = Math.pow((x[2].getx() - x[0].getx()),2)+Math.pow((x[2].gety() x[0].gety()),2);
a = Math.sqrt(a);
b = Math.sqrt(b);
c = Math.sqrt(c);
double s =(a+b+c)/2;
double A = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return A;
}
public void rotate(Cartesian_Point T,double angle)
{
}
=
=
=
=
p1;
p2;
p3;
p4;
}
public void display()
{
System.out.print("["+x[0]+":"+x[1]+":"+x[2]+":"+x[3]+"]\n");
}
public String toString()
{
return "["+x[0]+":"+x[1]+":"+x[2]+":"+x[3]+"]\n";
}
public double area()
{
double a = Math.pow((x[0].getx() - x[1].getx()),2)+Math.pow((x[0].gety()
x[1].gety()),2);
double b = Math.pow((x[1].getx() - x[2].getx()),2)+Math.pow((x[1].gety()
x[2].gety()),2);
double c = Math.pow((x[2].getx() - x[3].getx()),2)+Math.pow((x[2].gety()
x[3].gety()),2);
double d = Math.pow((x[3].getx() - x[0].getx()),2)+Math.pow((x[3].gety()
x[0].gety()),2);
a = Math.sqrt(a);
b = Math.sqrt(b);
c = Math.sqrt(c);
d = Math.sqrt(d);
if( a == c && b == d)
{
return a*b;
//
System.out.print("ac,bd");
}
else if( a == b && c == d)
{
//
System.out.print("ab,cd");
return a*c;
}
else if( a == d && b == d)
{
//
System.out.print("ad,bc");
return a*b;
}
else
return 0;
}
public void rotate(Cartesian_Point T,double angle)
{
}
}
Polygon.java
package mca.third.geometry;
abstract class Polygon
{
public Cartesian_Point[] x;
int n;
public Polygon(Cartesian_Point[] x)
{
int i=0;
for(Cartesian_Point y : x)
{
i++;
12. Update the classes Triangle and Rectangle, to throw an exception if the CartesianPoint
instances passed as parameter does not specify an appropriate Triangle or Rectangle. eg. In case of
Triangle, if the three points are in a straight line, or in case of Rectangle, if the lines when
connected cross each other.
import edu.gtu.geometry.*;
class Test
{
public static void main(String args[])throws DemoException
{
Cartesian_Point a[] = new Cartesian_Point[4];
a[0] = new Cartesian_Point(0,0);
a[1] = new Cartesian_Point(5,0);
a[2] = new Cartesian_Point(5,5);
Polygon T1 = new Triangle(a);
System.out.print(T1);
System.out.print("AreaofT1 = "+T1.area()+"\n");
//
//
}
}
DemoException.java
pacakge mca.third.geometry;
public class DemoException extends Exception
{
public DemoException(String msg)
{
super(msg);
}
public String toString()
{
return msg;
13. Define a class called PolygonManager, which manages a number of Polygon instances. Provide
methods to add, remove and list the Polygon instances managed by it.Test the methods of
PolygonManager by writing appropriate class with main method.
import java.io.*;
interface input
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
}
abstract class polygon_manager implements input
{
int x[];
int i;
int size;
int y[];
polygon_manager()
{
}
polygon_manager(int xy)
{
x=new int[xy];
y=new int[xy];
size=xy;
i=0;
}
abstract void display();
}
class triangle extends polygon_manager
{
triangle()
{
}
triangle(int xy)
{
super(xy);
}
void add()throws IOException
{
System.out.println("\nEnter the Three Co-Ordinates :");
for(i=0;i<3;i++)
{
System.out.println("Enter the x"+(i+1)+" And Y"+(i+1)+"=->");
14. Define a class called StatisticalData which manages a number of readings of type int. Provide a
method to setData available in the form of an array, a method add to individually add data of type
int, a method to reset the entire data, and methods to return the following statistics:
1. mean
2. median
3. mode
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class StatisticalData
{
int v[],N;
String vname;
public StatisticalData()
{
N=0;
vname="";
}
public void setData()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
System.out.printf("How many values You want to Enter? ");
try{
N=Integer.parseInt(br.readLine());
}
catch(IOException e)
{
System.out.println("Error while Reading from console");
}
v=new int[N];
System.out.printf("Enter variable Name and its values: ");
try{
vname = br.readLine();
for(i=0;i<N;i++)
15. Update the class StatisticalData, and define a method called loadFromCSV, which takes as
parameter an InputStream, where numeric data is available in an ASCII format, in a comma
separated form. Overload this method to take a File instance as parameter. Test the new methods
using appropriate data.
import java.util.*;
import java.io.*;
class StatisticalData
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int marks[];
int n;
StatisticalData(){
marks=new int[60];
n=0;
}
void add()throws IOException{
if(n==60) {
System.out.println("There is 60 student in the class...");
System.out.println("You can't enter any more marks.....");
}
else{
System.out.print("Enter Mark of Student : "+(n+1)+" : ");
try{
int temp=Integer.parseInt(br.readLine());
if(temp>100 || temp<0) {
System.out.println("Marks should be in range between 0 - 100...");
add();
}
else{
marks[n]=temp;
n++;
}
}
catch(NumberFormatException nfe) {
System.out.println("Marks can't be the String..... OR can not leave it
blank.....");
add();
}
}
}
float mean() {
display();
float sum=0;
for(int i=0;i<n;i++){
16. A college maintains the information about the marks of the students of a class in a text file with
fixed record length. Each line in the file contains data of one student. The first 25 characters have
the name of the student, next 12 characters have marks in the four subjects, each subject has 3
characters. Create a class called StudentMarks, which has studentName, and marks for four
subjects. Provide appropriate getter methods and constructors, for this class. Write an application
class to load the file into an array of StudentMarks. Use the StatisticalData class to compute the
statistics mean, median, mode for each of the subjects in the class.
import java.io.*;
import java.util.*;
class Student_Marks
{
int Rollno;
String stu_name;
int sub_marks[] = new int[5];
Student_Marks()
{}
Student_Marks(int Rollno,String stu_name,int mark1,int mark2,int mark3,int mark4,int
mark5)
{
this.Rollno = Rollno;
this.stu_name = stu_name;
sub_marks[0] = mark1;
sub_marks[1] = mark2;
sub_marks[2] = mark3;
sub_marks[3] = mark4;
sub_marks[4] = mark5;
}
public void getdata()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Rollno: ");
this.Rollno = Integer.parseInt(br.readLine());
System.out.print("Enter Name: ");
this.stu_name = br.readLine();
17. In the above exercise, use multithreading, to compute the statistics, after loading the
StudentMarks from the file, for marks information available for different classes available from files
placed in a directory. Create atleast five files in a directory with fixed record length to test your
code.
import java.io.*;
import java.util.*;
class Student_Marks
{
int Rollno;
String stu_name;
int sub_marks[] = new int[5];
Student_Marks()
{}
Student_Marks(int Rollno,String stu_name,int mark1,int mark2,int mark3,int mark4,int
mark5)
{
this.Rollno = Rollno;
this.stu_name = stu_name;
sub_marks[0] = mark1;
sub_marks[1] = mark2;
sub_marks[2] = mark3;
sub_marks[3] = mark4;
sub_marks[4] = mark5;
}
public void getdata()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Rollno: ");
this.Rollno = Integer.parseInt(br.readLine());
System.out.print("Enter Name: ");
this.stu_name = br.readLine();
for(int i=0;i<5;i++)
{
System.out.print("Enter Marks["+(i+1)+"] : ");
this.sub_marks[i] = Integer.parseInt(br.readLine());
}
}
public int getmark(int i)
{
return sub_marks[i];
}
public int getTotal()
{
int sum = 0;
for(int x : sub_marks)
System.out.print("=========================================");
System.out.printf("\nMEAN
%3.2f",sd.mean());
System.out.printf("\nMEDIAN
%3.2f",sd.median());
System.out.printf("\nMODE
%3.2f",sd.mode());
System.out.printf("\nVARIANCE %3.2f",sd.variance());
System.out.printf("\nSD
%3.2f",sd.SD());
sd.set(sub_mark[1]);
System.out.println("\nDetails of sub2");
System.out.print("=========================================");
System.out.printf("\nMEAN
%3.2f",sd.mean());
System.out.printf("\nMEDIAN
%3.2f",sd.median());
System.out.printf("\nMODE
%3.2f",sd.mode());
System.out.printf("\nVARIANCE %3.2f",sd.variance());
System.out.printf("\nSD
%3.2f",sd.SD());
sd.set(sub_mark[2]);
System.out.println("\nDetails of sub3");
System.out.print("=========================================");
System.out.printf("\nMEAN
%3.2f",sd.mean());
System.out.printf("\nMEDIAN
%3.2f",sd.median());
System.out.printf("\nMODE
%3.2f",sd.mode());
System.out.printf("\nVARIANCE %3.2f",sd.variance());
System.out.printf("\nSD
%3.2f",sd.SD());
sd.set(sub_mark[3]);
System.out.println("\nDetails of sub4");
System.out.print("=========================================");
System.out.printf("\nMEAN
%3.2f",sd.mean());
System.out.printf("\nMEDIAN
%3.2f",sd.median());
System.out.printf("\nMODE
%3.2f",sd.mode());
System.out.printf("\nVARIANCE %3.2f",sd.variance());
System.out.printf("\nSD
%3.2f",sd.SD());
sd.set(sub_mark[4]);
System.out.println("\nDetails of sub5");
System.out.print("=========================================");
System.out.printf("\nMEAN
%3.2f",sd.mean());
System.out.printf("\nMEDIAN
%3.2f",sd.median());
System.out.printf("\nMODE
%3.2f",sd.mode());
System.out.printf("\nVARIANCE %3.2f",sd.variance());
System.out.printf("\nSD
%3.2f",sd.SD());
}
}
||
||
b=0;a=0;bs=0;da=0;ma=0;it=0;pf=0;gr=0;de=0;net=0;flag=0;
try
{
a=Integer.parseInt(tno.getText());
}
catch(Exception e)
{
str = str + "Invalid N0. \n";
flag = 1;
}
try
{
for(int i=0;i<tnm.getText().length();i++)
{
if( (tnm.getText().charAt(i)<97 ||
tnm.getText().charAt(i)>122 ) &&
(tnm.getText().charAt(i)<65 ||
tnm.getText().charAt(i)>90 ) &&
(tnm.getText().charAt(i)!=32 ) )
{
throw new Exception("");
}
}
}
catch(Exception e)
{
str = str + "Invalid NAME. \n";
flag = 1;
}
try
{
b=Integer.parseInt(tage.getText());
}
catch(Exception e)
{
str = str + "Invalid age. \n";
flag = 1;
}
try
{
da=Integer.parseInt(tda.getText());
}
catch(Exception e)
{
str = str + "Invalid DA. \n";
flag = 1;
}
try
{
ma=Integer.parseInt(tma.getText());
}
catch(Exception e)
{
str = str + "Invalid MA. \n";
flag = 1;
}
try
{
pf=Integer.parseInt(tpf.getText());
}
catch(Exception e)
{
str = str + "Invalid PF. \n";
flag = 1;
19.Create an applet which has a TextField to accept a URL string, and displays the document of the
URL string in a new browser window.
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.awt.event.*;
20.Consider two types of residency, a Flat or a Villa. All types of residences have an area (square
yards) and a rate (per square yard). The property price of a residency is by default calculated as
area * rate. In case of Flat, the price get incremented by the maintenance charges, and in case of
Villa the price is incremented by furniture charges. Now define the following classes in a
common package called residence.
1. An abstract class called Residency, with appropriate methods and constructors.
2. Two sub-classes called Flat and Villa, which inherit from the Residence class and override
the appropriate methods, from Residency class.
3. Also override appropriate methods from the Object class.
package residence;
abstract class Residency
{
float price,area,rate;
Residency()
{
area=0;
rate=0;
setprice();
}
public Residency(float a, float b)
{
area=a;
rate=b;
setprice();
}
public void setprice()
{
price=area*rate;
}
public void display()
{
System.out.println("Price="+price);
}
abstract void moveprice();
}
//flat.java
public class Flat extends Residency
{
int mainten;
Flat()
{
super();
}
public Flat(float a, float r)
{
super(a,r);
}
&&
this.getRate()==f.getRate()
&&
23. Create an applet named UnitConversion, which allows user to select a particular
conversion from following options.(Use list)
1. Decimal to HexaDecimal [Use Integer.toHexString()].
2. Decimal to Octal [Use Integer.toOctalString()].
3. Feet to Centimeter.(1 feet = 30.48cm)
4. Inches to Feet (1 feet = 12 inches)
Once user selects particular conversion then show the converted value with proper
formatted message. (Like if user selects Inches to Feet option and input value is 60
then message should be 60 inches is equal to 5 Feet.)
import java.awt.*;
import java.awt.event.*;
public class P23 extends Applet
{
Label l1,l2;
Button b1,b2,b3,b4;
TextField t1,t2;
String op;
public void init()
{
l1 = new Label("Input");
t1 = new TextField("100");
l2 = new Label("Output");
t2 = new TextField("0");
t2.setEditable(false);
b1
b2
b3
b4
=
=
=
=
new
new
new
new
Button("Dec->Hexa");
Button("Dec->Octal");
Button("Feet->CM");
Button("Inches->Feet");
setLayout(new GridLayout(4,2,5,5));
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int ip = Integer.parseInt(t1.getText());
op = Integer.toHexString(ip);
t2.setText(op);
}
});
24. Create a class called Statistical Data, which has capability of maintaining data
regarding multiplevariables. It should have a method to specify the variable names as
String array and the method to load values from a file regarding the variables. eg. We
consider two variables as percentage of
marks in GCET exam and percentage of marks in 1st year of MCA, Provide methods in
the class to compute the correlation coefficient between any two variables, specified in
the parameter. Test the class by computing the correlation coefficient between the
marks of GCET and marks of 1st year
MCA for students of your class.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class StatisticalData
{
int v1[],v2[];
String v1name,v2name;
public StatisticalData()
{
v1name=new String("str1");
v2name=new String();
v1=new int[5];
v2=new int[5];
v2name="str2";
}
public void get()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
System.out.println("Enter First variable name and its values: ");
try{
v1name = br.readLine();
for(i=0;i<5;i++)
{
v1[i]=Integer.parseInt(br.readLine());
}
System.out.println("Enter Second variable name and its values: ");
v2name = br.readLine();
for(i=0;i<5;i++)
{
v2[i]=Integer.parseInt(br.readLine());
}
}
catch(IOException e)
{
System.out.println("Error while Reading from console");
}
}
public void correl_coeff()
{
double d,m1=0,m2=0,tatb=0,sab,ta2=0,tb2=0,sa,sb,cor_coef;
int i;
System.out.printf("%s: ",v1name);
for(i=0;i<5;i++)
{
System.out.printf("%d ",v1[i]);
}
System.out.println();
System.out.printf("%s: ",v2name);
for(i=0;i<5;i++)
{
System.out.printf("%d ",v2[i]);
m1=m1+v1[i];
m2=m2+v2[i];
}
System.out.println();
m1=m1/5;
m2=m2/5;
for(i=0;i<5;i++)
{
tatb=tatb+(v1[i]-m1)*(v2[i]-m2);
ta2=ta2+(v1[i]-m1)*(v1[i]-m1);
tb2=tb2+(v2[i]-m2)*(v2[i]-m2);
}
sab=tatb/4;
sa=ta2/5;
sb=tb2/5;
sa=Math.sqrt(sa);
25. Simple random sampling uses a sample of size n from a population fo size N to
obtain data that can be used to make inferences about the characteristics of a
population. Suppose that, from a population of 50 bank accounts, we want to take a
random sample of four accounts in order to
learn about the population. How many different random samples of four accounts are
possible.Write a Java class which can compute the number of random samples for size
n from a population of N, Also provide method to display all possible samples, and also
a method to return a sample,
derived from the possible samples. (Hint use random method of the Math class to select
the random sample).
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="prg5_25" width = 500 height=500>
</applet>
*/
public class prg5_25 extends Applet implements ActionListener
{
Label lno,lnm,lst,lplan,lsms,lcall,lhr,lmin,lsec,ltax,lext,lbill;
TextField tno,tnm,tsms,thr,tmin,tsec,ttax,tbill;
Choice cst;
CheckboxGroup cbg;
Checkbox gen,pre;
Checkbox gprs,gps,iwrld;
Button b1,b2;
TextArea ta;
public void init()
{
lno = new Label("Cell Number");
lnm = new Label("Customer Name");
}
}
catch(Exception ex)
{
System.out.print(ex);
}
}
public void init()
{
l1 = new ArrayList<String>();
l2 = new ArrayList<String>();
l3 = new ArrayList<String>();
l4 = new ArrayList<String>();
l1.add("zero");
l1.add("one");
l1.add("two");
l1.add("three");
l1.add("four");
l1.add("five");
l1.add("six");
l1.add("seven");
l1.add("eight");
l1.add("nine");
l2.add("eleven");
l2.add("tweleve");
l2.add("therteen");
l2.add("fourteeh");
l2.add("fifteen");
l2.add("sixteen");
l2.add("seventeen");
l2.add("eighteen");
l2.add("nineteen");
l3.add("twenty");
l3.add("thirty");
l3.add("fourty");
l3.add("fifty");
l3.add("sixty");
l3.add("seventy");
l3.add("eighty");
l3.add("ninety");
l4.add("hundreds");
l4.add("thousands");
l4.add("lakhs");
l4.add("crores");
}
public static void main(String args[])
{
new P28();
}
}
30. shape, a public abstract method to compute the area of the shape, and a public
abstract method paint() to draw the shape on a Graphics object, which is passed as
parameter. Derive subclasses for Point, Line, Circle and Rectangle. You can represent
a line as two points, a circle as a centerPoint and a radius, and
a Rectangle as two points on diagonally opposite corners. Now define a class called
DrawingBoard, which extends the Canvas class and maintains instances of the Shape
objects in a List, also oerrides the paint method to draw the Shape instances
maintained in the List,
List-2
1.Write a JAVA program which performs the following listed operations:
A.Create a package named MyPackage which consists of following classes
1. A class named Student which stores information like the roll number, first name, middle name,
last name, address and age of the student. The class should also contain appropriate get and set
methods.
2. A class named AddStudentFrame which displays a frame consisting of appropriate controls to
enter the details of a student and store these details in the Student class object. The frame should
also have two buttons with the caption as Add Record and Search Record.
3. A class named MyCustomListener which should work as a user defined event listener to handle
required events as mentioned in following points.
B.The Add record button should add the record entered in the frame controls to a pre defined
file.
C.Provide a menu on the AddStudentFrame which has menu items titled, Set the record file and
Exit.
1. When the Set the record file menu item is clicked, the user should be asked to input the
complete path of the file where he desires to save the records.
2. When the Exit menu item is clicked, the frame should be closed.[Note: Use the
MyCustomListener class only to handle the appropriate events]
D.1. The Search record button should open a new frame which should take input of search
criteria using a radio button. The radio button should provide facility to search on basis of first
name, middle name or last name.
2. The new frame should also have a text box to input the search criteria value.
3. The search result should be displayed in a proper format on the same frame in a text area. [The
records should be searched from the pre defined file which consists all saved records][Note: Use
the MyCustomListener class only to handle the appropriate events]
package MyPackage;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
//import MyPackage.*;
public class AddStudentFrame extends Frame implements ActionListener, WindowListener
{
Label lblroll;
Label lblfirst;
Label lbllast;
Label lblstatus;
Label lblmsg;
TextField tfroll;
TextField tffirst;
TextField tflast;
Button b,b1;
Student st;
public AddStudentFrame()
{
st = new Student();
setLayout(new FlowLayout());
lblroll = new Label("Roll No: ");
add(lblroll);
tfroll = new TextField(30);
add(tfroll);
lblfirst = new Label("First Name");
add(lblfirst);
tffirst = new TextField(30);
add(tffirst);
lbllast = new Label("Last Name");
add(lbllast);
tflast = new TextField(30);
add(tflast);
b = new Button("Save");
add(b);
b.addActionListener(this);
b1 = new Button("Search");
add(b1);
b1.addActionListener(this);
lblmsg = new Label(" ");
add(lblmsg);
lblstatus = new Label(" ");
add(lblstatus);
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu a = new Menu("File");
mb.add (a);
MenuItem a1 = new MenuItem("Exit");
a1.addActionListener(this);
a.add(a1);
Menu b = new Menu("Color");
mb.add (b);
MenuItem c1 = new MenuItem("Red");
c1.addActionListener(this);
b.add(c1);
void
void
void
void
void
void
windowActivated(WindowEvent we) {}
windowDeactivated(WindowEvent we) {}
windowClosing(WindowEvent we) {}
windowDeiconified(WindowEvent we) {}
windowIconified(WindowEvent we) {}
windowOpened(WindowEvent we) {}
return t;
}catch(Exception e)
{
}
}
else if(str.equals("Search"))
{
try
{
File f = new File("cur.txt");
FileReader fr = new FileReader(f);
int i;
while ( (i = fr.read() ) != -1 )
{
t = t + (char)i;
}
fr.close();
String [] items = t.split(",");
for (int j = 0; j < items.length; j++)
{
if( items[j].equals(val) )
{
return items[j+1] + " " + items[j+2];
}
}
}catch(Exception e)
{
return "Not Found";
}
}
return "Not Found";
}
}
public AddBoatFrame()
{
st = new Student();
setLayout(new FlowLayout());
lblbid = new Label("Boat Id: ");
add(lblbid);
tfbid = new TextField(30);
add(tfbid);
lblbnm = new Label("Boat Name");
add(lblbnm);
tfbnm = new TextField(30);
add(tfbnm);
lblbclr = new Label("Boat Color");
add(lblbclr);
tfbclr = new TextField(30);
add(tfbclr);
lblbprice = new Label("Boat Price");
add(lblbprice);
tfbprice = new TextField(30);
add(tfbprice);
b = new Button("Save");
add(b);
b.addActionListener(this);
b1 = new Button("Delete");
add(b1);
b1.addActionListener(this);
b2 = new Button("Exit");
add(b2);
b2.addActionListener(this);
lblmsg = new Label(" ");
add(lblmsg);
");
void
void
void
void
void
void
void
windowActivated(WindowEvent we) {}
windowDeactivated(WindowEvent we) {}
windowClosing(WindowEvent we) {}
windowDeiconified(WindowEvent we) {}
windowIconified(WindowEvent we) {}
windowOpened(WindowEvent we) {}
windowClosed(WindowEvent we) {}
}catch(Exception e)
{
lblmsg.setText("Sorry.. Record Not Saved !");
}
}
else if (str.equals("Exit"))
{
System.exit(0);
}
else if(str.equals("Red"))
{
}
}
public static void main(String[] args)
{
AddBoatFrame t = new AddBoatFrame();
}
}
class Student
{
int bid;
String boat_name;
String bclr;
int bprice;
Student()
{
bid=0;
boat_name="";
bclr="";
bprice=0;
}
public void setstudent(int r, String bn, String bc , int mm)
{
try
{
bid=r;
boat_name=bn;
bclr=bc;
bprice=mm;
//address=ad;
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public String getname()
{
return(bid +"," + boat_name + "," + bclr + "," + bprice);
}
}
class MyCustomListener
{
MyCustomListener()
{
}
public void setdata(String str,String val)
{
if(str.equals("Save"))
{