Você está na página 1de 40

/*1*/

import java.io.*;

class Student

int rollno;

String name;

String depart;

Student(){}

Student(int r, String n, String d)

rollno=r;

name=n;

depart=d;

Student(Student h)

rollno=h.rollno;

name=h.name;

depart=h.depart;

void read()throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the rollno of the Student:");

rollno=Integer.parseInt(br.readLine());
System.out.println("Enter the name of the Student:");

name=br.readLine();

System.out.println("Enter the department of the Student:");

depart=br.readLine();

void display()

System.out.println(rollno+"\t"+name+"\t"+depart);

public class StudentMain

public static void main(String args[])throws IOException

Student x=new Student();

x.read();

Student y=new Student(1,"Sachin","cricket");

Student z=new Student(x);

x.display();

y.display();

z.display();

/*2.*/
import java.util.*;

class Date

int d;

int m;

int y;

Date(){}

Date(int d, int m, int y)

this.d=d;

this.m=m;

this.y=y;

public String toString()

return d+"/"+m+"/"+y;

class Point

int x;

int y;

Point(){}

Point(int x, int y)

{
this.x=x;

this.y=y;

public String toString()

return "("+x+","+y+")";

public class VectorMain

public static void main(String args[])

Vector v=new Vector(5);

v.add(new Date(1,1,2001));

v.add(new Point(1,1));

v.add(new Integer(2));

v.add(new Double(2.3));

v.add("Hi");

Object o;

for(int i=0; i<v.size(); i++)

o=v.get(i);

System.out.println(o);

}
}

/*3.*/

class Volume

double findVolume(double r, double h)

return Math.PI*r*r*h;

double findVolume(double r)

return 4/3*Math.PI*r*r*r;

public class VolumeMain

public static void main(String args[])

Volume v = new Volume();

double cylinder=v.findVolume(2.5,3.5);

double sphere=v.findVolume(4.5);

System.out.println(cylinder);

System.out.println(sphere);

}
/*4.*/

public class Area

static double getArea(double r)

return Math.PI*r*r;

static double getArea(double l, double b)

return (l*b);

public static void main(String args[])

double circle=getArea(1.0);

double rectangle=getArea(1,4);

System.out.println(circle);

System.out.println(rectangle);

/*5*/

class Main

{
public static void main(String[] args)

int a=0,b=-1,i=2,x=3,s,j[]={10,20};

String m="hi";

try{

int y=x/a;

catch(ArithmeticException e)

System.out.println(e);

try{

int p=j[b];

catch(NegativeArraySizeException e)

System.out.println(e);

try{

System.out.println(j[3]);

catch(ArrayIndexOutOfBoundsException e)

{System.out.println(e);}
try{

s=Integer.parseInt(m);

catch(NumberFormatException e)

{System.out.println(e);}

/*6*/

class LessNumberOfArguments extends RuntimeException

String s="";

LessNumberOfArguments(){}

LessNumberOfArguments(String x)

s=x;

public String toString()

return "LessNumberOfArguments:There are no Arguments passed";

public class UserdefinedException

public static void main(String args[])


{

int x=0;

try

x=fun(args.length);

System.out.println(x);

catch(LessNumberOfArguments e)

System.out.println(e);

static int fun(int a)

if(a==0)

LessNumberOfArguments b=new LessNumberOfArguments("error");

throw b;

return a;

/*7*/

class Complex
{

int r;

int i;

Complex(){}

Complex(int r, int i)

this.r=r;

this.i=i;

Complex add(Complex h)

Complex d= new Complex();

d.r=r+h.r;

d.i=i+h.i;

return d;

public String toString()

return r+"+"+"i"+i;

public class ComplexMain

public static void main(String args[])

{
Complex a=new Complex(1,2);

Complex b=new Complex(2,2);

Complex d;

d=a.add(b);

System.out.println("The Answer is "+d);

/*9*/

import java.io.*;

class Book
{
String title;
double price;

Book()
{
price = 0;
}

Book(String t, double p)
{
title = t;
price = p;
}

boolean compare(double x)
{
boolean b = true;
if(price < x)
b = false;

return b;
}
void disp()
{
System.out.print("Title: "+title);
System.out.print("\nPrice: "+price);
}
}

class fred
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Book r1 = new Book("Lie to me",1000);
Book r2 = new Book("Two Men in a Boat",800);

if(r1.compare(r2.price))
{
System.out.println("The Costlier Book is:");
r1.disp();
}
else
{
System.out.println("The Costlier Book is:");
r2.disp();
}
}
}

/*10*/
 
import java.io.*;
 
class Nos
{
    int n,temp1,temp2;
 
    //Check condition
    void check()
    {
        for(int n=1000;n<=9999;n++)
        {
            /*The no will be a perfect square if its square root is an integer*/
            double a=Math.sqrt(n);
            int b=(int)a;
            if(a==b)
            {
                temp1=n%100;                
                temp2=n-temp1;
                temp2=temp2/100;
 
                double a2=Math.sqrt(temp2);
                int b2=(int)a2;
                if(b2==a2)
                {
                    double a1=Math.sqrt(temp1);
                    int b1=(int)a1;
                    if(a1==b1)
                    {
                        System.out.println(n);
                    }
                }
            }
            else
                continue;
        }
    }
}
 
class PerfectSquare
{
    public static void main(String args[])throws IOException
    {
        Nos n=new Nos();
        n.check();
    }
}

/*11*/
class Reverse

public static void main(String args[])

for(int i=10000;i<25000;i++)

int x=i*4;

int newno=0;

while(x!=0)

int digit=x%10;

newno=newno*10+digit;

x=x/10;

if(newno==i)

System.out.println(i+"\t");

/*12*/

import java.io.*;

class Symmatrix

public static void main(String args[])throws IOException

{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int a[][]=new int[3][3];

boolean flag=true;

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

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

a[i][j]=Integer.parseInt(br.readLine());

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

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

if(a[i][j]!=a[j][i])

flag=false;

break one;

if(flag)

System.out.println("Symmetric");

}
else

System.out.println("not Symmetric");

/*13*/

class Prime

public static void main(String args[])

one:for(int i=2; i<=100; i++)

for(int j=2; j<i; j++)

if(i%j==0)

continue one;

System.out.println(i);

/*14*/

class Date2
{

int d;

int m;

int y;

Date2(){}

Date2(int d, int m, int y)

this.d=d;

this.m=m;

this.y=y;

boolean compare(Date2 h)

if(d==h.d && m==h.m && y==h.y)

return true;

else

return false;

public String toString()

return d+":"+m+":"+y;

public class DateMain

{
public static void main(String args[])

Date2 a=new Date2(1,1,1);

Date2 b=new Date2(1,11,1);

boolean x=a.compare(b);

if(x)

System.out.println("Dates are equal");

else

System.out.println("Dates are not equal");

/*15*/

class Time

int h;

int m;

Time(){}

Time(int h, int m)

this.h=h;

this.m=m;

Time add(Time q)
{

Time x=new Time();

int a=m+q.m;

if(a>60)

x.m=a-60;

x.h=h+q.h+1;

else

x.m=a;

x.h=h+q.h;

return x;

public String toString()

return h+":"+m+"\t";

public class TimeMain

public static void main(String args[])

Time a=new Time(5,30);


Time b=new Time(6,45);

Time c=a.add(b);

System.out.println("Total Time "+c);

/*16. 17. 18. 19.*/

import java.io.*;

class Matrix

int r;

int c;

int [][]a;

Matrix()

r=3;c=3;

a=new int[r][c];

Matrix(int r, int c)

this.r=r;

this.c=c;

a=new int[r][c];

void set()throws IOException

{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

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

a[i][j]=Integer.parseInt(br.readLine());

Matrix add(Matrix h)

if(h.r==r && h.c==c)

Matrix x=new Matrix(r,c);

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

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

x.a[i][j]=a[i][j]+h.a[i][j];

return x;
}

else

return null;

Matrix sub(Matrix h)

if(h.r==r && h.c==c)

Matrix x=new Matrix(r,c);

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

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

x.a[i][j]=a[i][j]-h.a[i][j];

return x;

else

return null;

Matrix transpose()

{
Matrix x=new Matrix(r,c);

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

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

x.a[i][j]=a[j][i];

return x;

Matrix multi(Matrix h)

Matrix x=new Matrix(r,c);

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

for(int j=0; j<a[0].length; j++)

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

x.a[i][j]=a[i][k]*h.a[k][j];

}
}

return x;

void display()

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

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

System.out.print(a[i][j]+"\t");

System.out.println();

public class MatrixMain

public static void main(String args[])throws IOException

Matrix a=new Matrix(3,3);

Matrix b=new Matrix(3,3);

a.set();
b.set();

Matrix p=a.add(b);

Matrix q=a.sub(b);

Matrix r=a.multi(b);

Matrix s=a.transpose();

p.display();

q.display();

r.display();

s.display();

// 20

import java.io.*;

class Employee
{
private String empname;
private int empno;

Employee()
{
empno = 0;
}

Employee(String name, int no)


{
empname = name;
empno = no;
}

Employee(Employee x)
{
empname = x.empname;
empno = x.empno;
}

void update(Employee x)
{
empname = x.empname;
empno = x.empno;
}

String getname()
{
return empname;
}

void disp()
{
System.out.println(empno+"\t"+empname);
}

class SortEmployee
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Employee e[] = new Employee[5];
System.out.println("\nEnter Details of the Employees:");
for(int i = 0; i < 5; i++)
{
System.out.println("Employee "+(i+1));
System.out.print("\nName : ");
String n = br.readLine();
System.out.print("\nNumber : ");
int num = Integer.parseInt(br.readLine());
e[i] = new Employee(n, num);
}
int j = 0;
for(int i = 1; i < 5; i++)
{
for(j = 0; j < 5-i; j++)
{
String n1 = e[j].getname();
String n2 = e[j+1].getname();
if(n1.compareTo(n2) > 0)
{
Employee temp = new Employee(e[j]);
e[j].update(e[j+1]);
e[j+1].update(temp);
}
}
}

System.out.println("List of Employee in Ascending order of Name is:");

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


e[i].disp();
}
}

/*22. */
 
import java.io.*;
 
class occur
{
    String s[]=new String[50];
    String s2,s3;
    int size;
    int count=0;
 
    void read_info()throws IOException
    {
        System.out.print("Enter the string: ");
        DataInputStream d1=new DataInputStream(System.in);
        s2=d1.readLine();
 
        System.out.print("Enter the no. of words: ");
        DataInputStream d=new DataInputStream(System.in);
        size=Integer.parseInt(d.readLine());        
 
        for(int i=0;i<size;i++)
        {
            System.out.print("Enter the "+(i+1)+"th word: ");
            DataInputStream d2=new DataInputStream(System.in);
            s[i]=d2.readLine();
        }
 
        System.out.println("Enter the word whose occurence is to be checked");
        DataInputStream d3=new DataInputStream(System.in);
        s3=d3.readLine();
    }
 
    void check()
    {
        for(int i=0;i<size;i++)
        {
            if(s3.equalsIgnoreCase(s[i]))
                count++;
        }
        System.out.println("The occurence of the word "+s3+" is "+count);
    }
}
 
class Occurence
{
    public static void main(String args[])throws IOException
    {
        occur o=new occur();
        o.read_info();
        o.check();
    }
}

/*24*/
import java.io.*;
class Palindromee
{
public static void main(String args[])throws IOException
{
String s=new String();
DataInputStream d=new DataInputStream(System.in);
System.out.println("ENTER ANY STRING: ");
s=d.readLine();
String temp=s.toUpperCase();
char t[]=temp.toCharArray();
int l=temp.length();
boolean flag=true;
for(int i=0;i<l/2;i++)
{
if(t[i]!=t[l-1-i])
{
flag=false;
break;
}
}
if(flag==true)
System.out.println("ENTERED STRING \""+s+"\" IS PALINDROME");
else
System.out.println("ENTERED STRING \""+s+"\" IS NOT PALINDROME");
}
}

/* 25*/

import java.io.*;

class Main {

public static void main(String[] args)throws IOException

{ BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter string");

StringBuffer sb=new StringBuffer(sc.readLine());


String s=sb.toString();

char c[]=s.toCharArray();

for(int i=0;i<c.length/2;i++)

char temp=c[i];

c[i]=c[c.length-i-1];

c[c.length-i-1]=temp;

String bs=new String(c);

if(s.compareTo(bs)==0)

System.out.print("its palindrome");

else

System.out.print("not palindrome");

//Program 26

package college;

public class Student 


{
private String name;
private int rno;
public Student(){ }
public Student(String name,int rno) 
{
this.name = name;
this.rno = rno;
}
public String toString()
{
return "Name is "+name+"\nRoll no is "+rno; 
}
}
package college;

public class Employee 


{
private String name;
private int empno;
public Employee(){ }
public Employee(String name,int empno) 
{
this.name = name;
this.empno = empno;
}
public String toString()
{
return "Name is "+name+"\nEmp no is "+empno; 
}
}
import college.*;

public class MyClass


{
public static void main(String[] args)
{
Student s1 = new Student("Name",1);
Employee e1 = new Employee("Name",2);
System.out.println(s1);
System.out.println(e1);
}
}
27)
import java.util.*;
import P2.*;
 
public class SegmentMain
{
 
    public static void main(String[] args)
    {Segment s = new Segment(0, 2, 0, 5);
     System.out.println(s);
    }
}
 
There should be a subfolder by the name P1 in which following file should be there:
 
package P2;
import java.util.*;
import P2.P1.*;
 
public class Segment
{Point pt1, pt2;
 public Segment(double x1, double y1, double x2, double y2)
 {pt1 = new Point(x1, y1);
  pt2 = new Point(x2, y2);
 }
 public String toString()
 {return "Distance between points "+pt1+" and "+pt2
          +" is "+ pt1.getDistance(pt2);
 }
}
 
There should be a subfolder P1 in folder P2 in which there should be following file:
 
package P2.P1;
import java.util.*;
 
public class Point
{private double x, y;
 public Point(double x, double y)
 {this.x = x; this.y = y;
 }
 public double getDistance(Point z)
 {return Math.sqrt((y-z.y)*(y-z.y)+(x-
 
z.x)*(x-z.x));
 }
 public String toString()
 {return "("+x+", "+y+")";
 }
}
/* 28*/

import java.io.*;
import java.util.*;

class Employee
{
private String name,designation;
private double salary;
static int COUNT=0;
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

public Employee() throws IOException


{
userInput();
finalize();
}

void userInput() throws IOException


{
System.out.println("Enter the Name : ");
name=in.readLine();
System.out.println("Enter the Designation : ");
designation=in.readLine();
System.out.println("Enter the Salary : ");
salary=Double.parseDouble(in.readLine());
}

public void finalize() //over-riding the finalize() inheritted from the Object class
{
COUNT++;
}
}

class CountDemo
{
public static void main(String arg[]) throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Vector v=new Vector();
char c;
do
{
v.addElement(new Employee());
System.out.println("Do you want to create more Employees (Y/N) : ");
c=in.readLine().charAt(0);
} while(c=='y'||c=='Y');
System.out.println("The Number of Employees Created is "+Employee.COUNT);
}
/*29*/
import java.io.*;
 
class Student
{ String name;
  int rolNo;
  int m1,m2,m3;
 
  void read()throws IOException
  { DataInputStream r= new DataInputStream(System.in);
    System.out.println("enter name:");
    name=r.readLine();
    System.out.println("enter rollno:");
    rolNo=Integer.parseInt(r.readLine());
    System.out.println("enter marks of sub 1:");
    m1=Integer.parseInt(r.readLine());
    System.out.println("enter marks of sub 2:");
    m2=Integer.parseInt(r.readLine());
    System.out.println("enter marks of sub 3:");
    m3=Integer.parseInt(r.readLine());
 }
 
  double avg()
  { return( m1+m2+m3)/3.0;
 }
 
  void display()
  { System.out.println("name of the student:"+name);
       System.out.println("roll no is:"+rolNo);
       System.out.println("average marks are:"+avg());
 
 }
}
public class abc
{   public static void main(String[] args)throws IOException
  { Student s[]=new Student[3];
 
    for(int i=0; i<3;i++)
       {s[i] = new Student();
           s[i].read();// error is comin here...
       }
 
    for(int i=1;i<2;i++)
    for(int j=0;j<3-i;j++)
    if(s[j].avg()<s[j+1].avg())
    { Student temp=s[j];
    s[j]=s[j+1];
    s[j+1]=temp;
    }
 
    for(int i=0;i<3; i++)
    s[i].display();
 
 }
 
}
/*31*/
class Rectangle
{protected int length, breadth;
 public Rectangle(int length, int breadth)
 {this.length = length; this.breadth= breadth;}
 public int area()
 {return length*breadth;}
}
 
class Box extends Rectangle
{int depth;
 public Box(int l, int b, int d)
 {super(l, b); depth = d; }
 public double volume()
 {return length*depth*breadth;}
}
 
class RectBox
{   public static void main(String[] args)
    {Rectangle r = new Rectangle(10, 5);
     System.out.println("Area of Rectangle is "+r.area());
     Box b = new Box(4, 5, 6);
     System.out.println("volume of box is "+b.volume());
    }
}
/*32*/
import java.io.*;
 
abstract class Employee
{
    int eno;
    String ename;
    Employee(int empNo,String empName)
    {
        eno = empNo;
        ename = empName;
    }
    abstract double calSalary();
}
 
class SalariedEmployee extends Employee
{
    double salary;
    SalariedEmployee(int empNo, String empName,double sal)
    {
        super(empNo,empName);
        salary = sal;
    }
    double calSalary()
    {
        return salary;
    }
}
 
class HourlyEmployee extends Employee
{
    double wages;
    int days;
    HourlyEmployee(int empNo,String empName,double wage, int day)
    {
        super(empNo,empName);
        wages = wage;
        days = day;
    }
    double calSalary()
    {
        return wages * days;
    }
}
 
class CommissionEmployee extends Employee
{
    double totalsale, per_rate,totSal;
    CommissionEmployee(int empNo,String empName, double totSale, double per_Rat)
    {
        super(empNo,empName);    
        totalsale = totSale;
        per_rate = per_Rat;
    }
  
 double calSalary()
    {
        return totalsale * per_rate;
    }
}
 
class EmployeeImplement
{
    public static void main(String args[])
    {
        SalariedEmployee SE = new SalariedEmployee(1,"Sachin",1200);
        HourlyEmployee HE = new HourlyEmployee(2,"Sam",50,10);
        CommissionEmployee CE = new CommissionEmployee(3,"Sammy",15,100);
 
        Employee objEmp;
        objEmp = SE;
        System.out.println("Salary of Salaried Employee is "+objEmp.calSalary());
 
        Employee objEmp1;
        objEmp1 = HE;
        System.out.println("Salary of Hourly Employee is "+objEmp1.calSalary());
 
        Employee objEmp2;
        objEmp2 = CE;
        System.out.println("Salary of Commission Employee is "+objEmp2.calSalary());
    }
}
/*34*/
import java.io.*;
interface TwoDShape
{
public void getArea();
public void getName();
}

interface ThreeDShape extends TwoDShape


{
public void getVol();
}

class Sphere implements ThreeDShape


{
double r;

Sphere(double a)
{r=a;
}
public void getArea()
{ System.out.println("area of circle is: "+3.14*r*r);
}

public void getName()


{ System.out.println("this is a circle");
}

public void getVol()


{ System.out.println("volume of sphere: "+(4*3.14*r*r/3.0));
}
}
class JavaApplication42
{

public static void main(String[] args)


{ Sphere s=new Sphere(5);
s.getName(); s.getArea();s.getVol(); 
}
}
/*36*/
import java.io.*;
 
abstract class Media
{
  String title;
  double price;
 
  Media(String ti, double pr)
  { title=ti; price=pr;
 }
  abstract void display();
}
 
class Book extends Media
{ int pages;
 
 public Book(String ti, double pr, int p)
 { super(ti,pr);
   pages=p;
 }
 void display()
 {   System.out.println(title+"book is of price"+price+"Rs");
      System.out.println("this book contains "+pages+" pages");
 }
}
 
class Tape extends Media
{ double time;
 
 public Tape(String ti,double pr, double t)
  { super(ti,pr);
    time=t;
 }
 void display()
 { System.out.println(title+"tape is of price"+price+"Rs");
     System.out.println("this tape is of "+time+" mins");
 }
}
public class q36
{
    public static void main(String[] args)throws IOException
    {
     DataInputStream s=new DataInputStream(System.in);
     System.out.println("enter title");
     String ti=s.readLine();
     System.out.println("enter price");
     Double pr=Double.parseDouble(s.readLine());
     System.out.println("enter pages");
     int p=Integer.parseInt(s.readLine());
     System.out.println("enter time");
     double t=Double.parseDouble(s.readLine());
 
Media a= new Book(ti,pr,p);
Media b=new Tape(ti,pr,t);
a.display(); b.display();
    }
}
//Q 42
 
class Password
{
public static void main(String args[])
{
String pass=new String();
int l = args.length;
for(int i = 0; i<3; i++)
{
String temp = args[i];
String name = temp.toLowerCase();
int len = name.length();
pass=pass+name.charAt(0)+name.charAt((len-1));
}
pass += args[3];
 
if(l != 0)
System.out.println("Your Password is: "+pass);
 
}
}

Você também pode gostar