Java Program - Factorial
Java Program - Factorial
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();
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:
Data member : EmpId, EmpName, Designation, Age, salary (in employee class)
Create two e1,e2 objects and compare who has more salary.
package javaassignment2;
import java.util.Scanner;
{
// TODO Auto-generated method stub
class employee
{
int empid , age;
String empname , desig;
float salary;
void getemployeedetail()
{
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)
{
}
else
{
System.out.println("Both employee's Salary same .");
}
}
3. WAP for following specification:
Create two objects of the shape class and check whether they have the same volume using equals()
method.
package javaassignment2;
import java.util.Scanner;
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);
}
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;
{
// 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());
}
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)
{
}
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 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;
}
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;
int x=s.nextInt();
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();
cp.display();
int x1=sc.nextInt();
int y1=sc.nextInt();
cp1.display();
cp1.move(x1, y1);
cp1.display();
cp1.move(x1);
cp1.display();
c1.display();
c2.display();
}
return x;
this.x=x;
return y;
this.y=y;
this.x=x;
this.y=y;
void display()
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()
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
int i=0;
double sum=0;
for(i=0;i<intdouble.length;i++)
sum+=intdouble[i];
}
return sum/intdouble.length;
int i=0,j=0;
double week[][];
InputStreamReader(System.in));
week=new double[4][7];
for(i=0;i<4;i++)
for(j=0;j<7;j++)
week[i][j]=Double.parseDouble(br.readLine());
System.out.println("");
}
System.out.println("AVERAGE TEMPRATURE : ");
for(i=0;i<4;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;
String str;
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;
String a, b = "";
a = s.nextLine();
int n = a.length();
b = b + a.charAt(i);
if(a.equalsIgnoreCase(b))
else
}}}