You are on page 1of 21

JAVA ASSIGNMENT 2

ROLL NO: 23
NAME: NIDHI SONI
1. WAP to create circle class with area and perimeter function to find area and perimeter of circle.

package javaassignment2;
import java.util.Scanner;
public class circle
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
float r;
Scanner s=new Scanner(System.in);
System.out.print("Enter Radius:");
r=s.nextFloat();

periarea pa=new periarea();


pa.area(r);
pa.peri(r);}
}
class periarea
{ double pi=3.14,x,y;

void area(float r)
{ x=pi*r*r;
System.out.println("Area of circle :"+x);
}
void peri(float r)
{
y=2*pi*r;
System.out.println("Perimeter of circle :"+y);
}
}
2. WAP for following specification:

Class name : Employee, DemoEmployee(contain main method)

Data member : EmpId, EmpName, Designation, Age, salary (in employee class)

Methods : GetEmployeeDetail(), DisplayEmployeeDetail(), Compare_salary()

Create two e1,e2 objects and compare who has more salary.

package javaassignment2;

import java.util.Scanner;

public class demoemployee


{

public static void main(String[] args)

{
// TODO Auto-generated method stub

employee e1=new employee();


e1.getemployeedetail();
e1.displayemployeedata();
employee e2=new employee();
e2.getemployeedetail();
e2.displayemployeedata();
e1.compare(e2);

class employee
{
int empid , age;
String empname , desig;
float salary;

void getemployeedetail()
{

Scanner s=new Scanner(System.in);


System.out.println("Enter Employee Id:");
empid=s.nextInt();
System.out.println("Enter Employee Name:");
empname=s.next();
System.out.println("Enter Degingnation:");

desig=s.next();
System.out.println("Enter Employee Age:");
age=s.nextInt();
System.out.println("Enter Employee Salary:");
salary=s.nextFloat();

void displayemployeedata()
{
System.out.println("Employee Id:"+empid);

System.out.println("Employee Name:"+empname);
System.out.println(" Degingnation:"+desig);
System.out.println(" Age:"+age);
System.out.println(" Salary:"+salary);

}
void compare(employee e2)
{

if(this.salary>e2.salary)
{
System.out.println("Employee "+this.empname+"'s Salary Is Greater");

}
else if(this.salary<e2.salary)
{

System.out.println("Employee "+e2.empname+"'sSalary Is Greater");

}
else
{
System.out.println("Both employee's Salary same .");

}
}
3. WAP for following specification:

Main Class : DemoShape

Secondary class : Shape class

Data member : d1,d2,d3

Methods : getdata( ) using method overloading and volume( )

If the shape is a cube then d1=d2=d3 so use the getdata(int d) method .

If shape is cuboid then getdata(int d1,int d2,int d3)

Create two objects of the shape class and check whether they have the same volume using equals()
method.

package javaassignment2;

import java.util.Scanner;

public class demoshape


{

public static void main(String[] args)


{
// TODO Auto-generated method stub

Scanner s=new Scanner(System.in);


int d,a,b,c;
shape s1=new shape();
shape s2=new shape();

System.out.println("Enter Length :");


a=s.nextInt();
s1.getdata(a);
s1.volume();
System.out.println("Enter length :");
b=s.nextInt();
System.out.println("Enter Breadth :");
c=s.nextInt();
System.out.println("Enter Width :");

d=s.nextInt();
s2.getdata(b,c,d);
s2.volume1();

}
class shape
{
int d1,d2,d3,d4;

void getdata(int a)
{
d1=d1=d1=a;
System.out.println("Length : "+d1);
}

void getdata(int b,int c,int d)


{
this.d2=b;
//this.b=d1;

System.out.println("Length : "+b);
this.d3=c;
System.out.println("Breadth : "+c);
this.d4=d;
System.out.println("Width : "+d);

}
void volume()
{

System.out.println("Volume : "+d1*d1*d1);

}
void volume1()
{
System.out.println("Volume : "+d2*d3*d4);
}

}
4. Define a complex class with real and imaginary parts. Describe its constructor, overload constructor,
add_complex() method to add two complex no and store in third object. Like c3=c1 + c2.

package javaassignment2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class complex


{

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

{
// TODO Auto-generated method stub

int a,b,c;
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter Value a :");
a=Integer.parseInt(br.readLine());
System.out.println("Enter Value b:");
b=Integer.parseInt(br.readLine());

complex1 c1=new complex1(a);


complex1 c2=new complex1(a,b);
complex1 c3=new complex1(a,b);
c3=c1.add(c2);
c3.display();

}
class complex1
{

int a1,b1,c1;

complex1()
{

}
complex1(int a)
{
a1=a;
b1=a;
}
complex1(int a, int b)
{
a1=a;
b1=b;
}
void display()
{
System.out.println(a1+"+"+b1+"i");
}
complex1 add(complex1 c2)
{

complex1 c= new complex1();


c.a1=this.a1+c2.a1;
c.b1=this.b1+c2.b1;
return c;

}
5. Define a class called Product with members – Product name, productCode and mfgName and
static variable count. Take no of product N from user using array.

Define default constructor which count no of object created.

Define update() method for changing the product name and mfgname for entered Product code.

Display the all Products detail in well format. (atleast store 3 product) using static method Display().

Write a class called TestProduct, with the main method to test the methods and constructors of the
Product class.
package javaassignment2;

import java.util.Scanner;

public class product {

public static void main(String[] args)


{
// TODO Auto-generated method stub

product2 p=new product2();


Scanner s=new Scanner(System.in);
System.out.println("Enter no of Product");
int n=s.nextInt();
product2 [] p1=new product2[n];
for (int i = 0; i < n; i++)
{
p1[i]= new product2( );
p1[i].getdata();
}
System.out.println("Product---Code---MFG");
System.out.println("__");
for (int i = 0; i < n; i++)
{
p1[i].display();
}
product2.display_count();
}

}
class product2
{
static int count;

int code;
String mfg;
String pname;
public void getdata()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Product Name:");
this.pname=s.next();
System.out.println("Enter Product Code:");
this.code=s.nextInt();
System.out.println("Enter Product Mfg :");
this.mfg=s.next();
}
void display()
{
System.out.println(this.pname +" "+ this.code +" "+this.mfg);
}

product2()
{
count++;
}
static void display_count()
{
System.out.println("No of Product ="+product2.count);
}
}
6. Define a class called CartesianPoint, which has two private instance variables, x and y. 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 a 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 TestCartesianPoint, with the main method to test the various methods in the
Cartesian Point class.

package javaassignment2;

import java.util.Scanner;

public class cartesianpoint

public static void main(String[] args)

// TODO Auto-generated method stub

Scanner s=new Scanner(System.in);

System.out.println("Enter the value of x :");

int x=s.nextInt();

System.out.println("Enter the value of y :");

int y=s.nextInt();
cartesianpoint c = new cartesianpoint();

c.getdata( x, y);

c.display();

c.move(x, y);

c.display();

c.move(x);

c.display();

cartesianproduct cp=new cartesianproduct(x);

cp.display();

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value of x :");

int x1=sc.nextInt();

System.out.println("Enter the value of y :");

int y1=sc.nextInt();

cartesianpoint cp1 = new cartesianpoint();

cp1.getdata( x1, y1);

cp1.display();

cp1.move(x1, y1);

cp1.display();

cp1.move(x1);

cp1.display();

cartesianproduct c1=new cartesianproduct(x1);

c1.display();

cartesianproduct c2=new cartesianproduct(x,y);

c2.display();
}

private int x,y;

public int getx()

return x;

public void setx(int x)

this.x=x;

public int gety()

return y;

public void sety(int y)

this.y=y;

void getdata(int x,int y)

this.x=x;

this.y=y;

void display()

System.out.println("cartesian point are :"+x+" ,"+y);


}

void move(int x,int y)

this.x=y;

this.y=x;

void move(int x)

this.x=x;

this.y=x;

class cartesianproduct

int x,y;

cartesianproduct(int x)

this.x=x;

this.y=x;

cartesianproduct(int x,int y)

this.x=x;

this.y=y;
}

void display()

System.out.println("Cartesian point are using Constructor :"+x+" ,"+y);

7. 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
temperatures for the four weeks.

package javaassignment2;

import java.io.*;

class statistics

public static double average(double intdouble[])

int i=0;

double sum=0;

for(i=0;i<intdouble.length;i++)

sum+=intdouble[i];

}
return sum/intdouble.length;

public class temperature

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

// TODO Auto-generated method stub

int i=0,j=0;

double week[][];

BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));

week=new double[4][7];

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

System.out.println("Week " + (i+1) + " : ");

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

System.out.print("Enter Temprature Of Day " + (j+1) +" : ");

week[i][j]=Double.parseDouble(br.readLine());

System.out.println("");

}
System.out.println("AVERAGE TEMPRATURE : ");

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

System.out.println("WEEK " + (i+1) + " : " +statistics.average(week[i]));

8. Write a program, which take Line input from the user and create a different three array for the
Character, Number, and Special character

package javaassignment2;

import java.util.Scanner;

public class checkstring

public static void main(String[] args)

// TODO Auto-generated method stub

String str;

Scanner s=new Scanner(System.in);

System.out.println("Enter a line :");


str=s.next();

StringBuffer alpha = new StringBuffer();

StringBuffer num = new StringBuffer();

StringBuffer special = new StringBuffer();

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

if (Character.isDigit(str.charAt(i)))

num.append(str.charAt(i));

else if(Character.isAlphabetic(str.charAt(i)))

alpha.append(str.charAt(i));

else

special.append(str.charAt(i));

System.out.println("alphabet :"+alpha);

System.out.println("Numberic :"+num);

System.out.println("Special :"+special);

9. Write a Program which take a string as input and check that the string is palindrome or
not

package javaassignment2;
import java.util.Scanner;

public class palindrome

public static void main(String[] args)

// TODO Auto-generated method stub

String a, b = "";

Scanner s = new Scanner(System.in);

System.out.print("Enter the string :");

a = s.nextLine();

int n = a.length();

for(int i = n - 1; i >= 0; i--)

b = b + a.charAt(i);

if(a.equalsIgnoreCase(b))

System.out.println("The string is Palindrome.");

else

System.out.println("The string is not Palindrome.");

}}}

You might also like