1)
Define a class having 3 numbers as data members. Initialize and display the
greatest of 3 numbers
Code:
class
Greatest
{
public
static void main(String args[])
{
int
num1=10,num2=20,num3=15;
if(num1>num2
&& num1>num3)
{
System.out.println("Num="+num1+"
is greater");
}
else
if(num2>num1 && num2>num3)
{
System.out.println("Num="+num2+"
is greater");
}
else
{
System.out.println("Num="+num3+"
is greater");
}
}
}
Output:
Num=20 is greater
2) Define a class
having one number,N as a data members. Initialize and display prime numbers
from 1 to N.
Code:
class
Prime
{
public
static void main(String[] args)
{
int N=20;
System.out.print("Prime
numbers from 1 to "+N+" : ");
for (int
i=1;i
{
int j;
for (j=2;
j
{
int n = i%j;
if (n==0)
{
break;
}
}
if(i == j)
{
System.out.print(i+" , ");
}
}
}
}
Output:
Prime numbers from 1 to 20 : 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19
3) Define a class having one
3-digit number. Initialize and display reverse of that number.
Code:
class
Reverse
{
public
static void main(String args[])
{
int num ,
temp ;
num =
197;
System.out.println("
The Number is : "+num);
System.out.print("
The Reverse Number is : ");
for(int
i=0;i<4 i="" span="">4>
{
if(num !=
0)
{
temp =
num % 10 ;
System.out.print(temp);
num = num
/ 10 ;
}
}
}
}
Output:
The Number is : 197
The Reverse Number is : 791
4) Define a class Student with
four data members such as name, roll no, sub1, sub2. Define appropriate methods
to Initialize and display the values of data members. Also calculate total marks
and percentage scored by students.
Code: class Student
{
String
Name;
int
RollNo,Sub1,Sub2;
float
Total,Percentage;
void
AcceptInfo(String N,int Rno,int s1,int s2)
{
Name=N;
RollNo=Rno;
Sub1=s1;
Sub2=s2;
}
void
Calculate()
{
Total=Sub1+Sub2;
Percentage=(Total/200)*100;
}
void
displayInfo()
{
System.out.println("Name="+Name+"\nRoll
No="+RollNo);
System.out.println("Total="+Total);
System.out.println("Percentage="+Percentage+"%");
}
}
class
Studentmain
{
public
static void main(String args[])
{
Student
s1=new Student();
s1.AcceptInfo("Kushal",444,85,90);
s1.Calculate();
s1.displayInfo();
}
}
Output:
Name
=Kushal
Roll No
=444
Sub1=85 ,
Sub2=90
Total=175.0
Percentage=87.5%
5) Define a class
Circle having data members pi and radius. Initialize and display the values of
data members also calculate area of Circle and display it.
Code:
class
Circle
{
int
radius;
double
pi=3.14,Area;
void
getrad(int R)
{
radius=R;
}
void
CalArea()
{
Area=pi*radius*radius;
}
void
display()
{
System.out.println("Radius
="+radius);
System.out.println("Area
="+Area);
}
}
class
Circlemain
{
public
static void main(String args[])
{
Circle
c1=new Circle();
c1.getrad(5);
c1.CalArea();
c1.display();
}
}
Output:
Radius =5
Area
=78.5
6) Define a class Shape having
overloaded static member function area() to calculate and display area of
Square and Rectangle.
Code:
class
Shape
{
int Area;
void
Area(int l)
{
Area=l*l;
}
void
Area(int l,int b)
{
Area=l*b;
}
void
Display()
{
System.out.println("Area
="+Area);
}
}
class
Shapemain
{
public
static void main(String args[])
{
Shape
s1=new Shape();
s1.Area(10);
s1.Display();
Shape
s2=new Shape();
s2.Area(10,20);
s2.Display();
}
}
Output:
Area =100
Area =200
7) To implement a program to
accept principal amount,rate of interest,no. of years from the user and display
the simple interest.
Code:
import java.lang.*;
import java.io.*;
class SimpleInterest
{
int Principal,Noy,Roi;
double Si;
SimpleInterest(int P,int N,int R)
{
Principal=P;
Noy=N;
Roi=R;
}
void calculate()
{
Si=(Principal*Noy*Roi)/100;
}
void display()
{
System.out.println("Principal Amount="+Principal);
System.out.println("Number of Years="+Noy);
System.out.println("Rate of Interest="+Roi);
System.out.println("Simple interest="+Si);
}
}
class SimpleInterestmain
{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Principal Amount");
int p=Integer.parseInt(in.readLine());
System.out.println("Enter Number of Years");
int n=Integer.parseInt(in.readLine());
System.out.println("Enter Rate of Interest");
int r=Integer.parseInt(in.readLine());
SimpleInterest SI=new SimpleInterest(p,n,r);
SI.calculate();
SI.display();
}
}
Output:
Enter Principal Amount
2500
Enter
Number of Years
5
Enter
Rate of Interest
10
Principal
Amount=2500
Number of
Years=5
Rate of
Interest=10
Simple
interest=1250.0
No comments:
Post a Comment