Q.
1) Write a program to java to display java welcome
massage .
Java code :
class simple{
static public void main(String args[])
{
System.out.println(" Welcome to Java ....! ");
}
}
Output :
Q.2) Write a program in java that demonstrate
constructor i) Default ii) Parameterized Constructor .
Java code :
class student
{
int i ;
String n;
//Constructor with no parameter
private student()
{
i = 418;
}
// constructor accepting single value
student(String name)
{
n=name;
System.out.println("Student name:" +name);
}
public static void main(String[] args)
{
student obj1 = new student();
System.out.println("Roll number:" +obj1.i);
//call constructor by passsing a single value
student obj2 = new student("omkar");
}
}
Output :
Q 3.) Write a program to calculate simple interest input
by the user .
Java code :
import java.util.Scanner;
class Interest
{
float SimpleInterest;
public void si (float p , float r , float t)
{
SimpleInterest = p*r*t/100;
System.out.println("Simple inteest:" +SimpleInterest);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
float p, r, t;
System.out.println("Enter the Principal : ");
p = sc.nextFloat();
System.out.println("Enter the Rate of interest : ");
r = sc.nextFloat();
System.out.println("Enter the Time period : ");
t = sc.nextFloat();
Interest obj = new Interest();
obj.si(p,r,t);
}
}
Output :
Q4 .) Write a program to find the average and the sum of the n
number using user input
Java code :
import java.util.Scanner;
class Avareage
{
public static void main(String[] args)
{
int n,sum=0;
float ave;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of element you want in array:");
n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter All the element you want in array:");
for(int i = 0; i < n ; i++)
{
a[i] = sc.nextInt();
sum = sum + a[i];
}
System.out.println("Sum of all element:" +sum);
ave = (float)sum / n ;
System.out.println("Avareage of all element:" +ave);
}
}
Output :
Q5) To create simple class to find out area and perimeter of
rectangle and box using super keyword.
Java code :
import java.util.Scanner;
class simple
{
float area,p,SurfaceArea,perimeter;
void Area(float l , float w){
area = l * w ;
}
void perimeter(float l , float w ){
p = 2*( l + w );
}
void box(float l , float w , float h)
{
SurfaceArea=2*l*w + 2*l*h + 2*w*h;
}
void box1(float l , float w , float h)
{
perimeter=4*(l+h+w);
}
}
class rectangle extends simple{
void parameters(float l , float w , float h){
super.Area(l,w);
System.out.println("Area of rectangle is :" +super.area);
super.perimeter(l,w);
System.out.println("Perimeter of rectangle:" +super.p);
super.box(l,w,h);
System.out.println("Surface Area of box is :" +super.SurfaceArea);
super.box1(l,w,h);
System.out.println("Perimeter of box is :" +super.perimeter);
}
}
class simpleSuper1{
public static void main(String[] args){
float l,w,h;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length:");
l = sc.nextFloat();
System.out.println("Enter the width:");
w = sc.nextFloat();
System.out.println("Entern the hight:");
h = sc.nextFloat();
rectangle r1 = new rectangle();
r1.parameters(l,w,h);
}
}
Output:
Q6) To design a shape class using abstract method and classes ?
Circle , rectangle , square .
Java Code :
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing rectangle");}
}
class circle extends Shape
{
void draw()
{
System.out.println("Drawing circle");}
}
class square extends Shape
{
void draw()
{
System.out.println("Drawing square");}
}
class Abstraction
{
public static void main(String args[])
{
Shape s=new Rectangle();
s.draw();
Shape s1=new circle ();
s1.draw();
Shape s2=new square();
s2.draw();
}
}
Output :
Q 7 ) Write a program using calendar that display current year ,
month ,date.
Java code :
import java.text.SimpleDateFormat;
import java.util.*;
class format
{
public static void main(String[] args)
{
Date dnow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Current date:" +ft.format(dnow));
}
}
Output :
Q 8) Write a program using calendar that display date using
yy/mm/dd/ and day of week ,week of month ,
hours:minuts:seconds:milliseconds .
Java code :
import java.util.*;
import java.text.*;
class cal
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
Date d = new Date();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Current date:" +f.format(d));
System.out.println("Current year: " + cal.get(Calendar.YEAR));
System.out.println("Current month: " + cal.get(Calendar.MONTH));
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
System.out.println("Day of week:" +dayofweek);
int wk=cal.get(Calendar.WEEK_OF_MONTH);
System.out.println("Week of Month :"+wk);
Date dnow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss:SS");
System.out.println("Currrent time:" +ft.format(dnow));
}
}
Output :
Q 9) Write a program to create package that display welcome to
package .
Java code :
package mypack;
class simple
{
public static void main(String[] args)
{
System.out.println("Wlcome to package...!");
}
Output :
Q 10 ) Write a program to create package array list using
10 elements .
Java code :
package Demo;
import java.util.ArrayList;
class BuiltInPackage
{
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
list.add(2);
list.add(5);
list.add(55);
list.add(6);
list.add(24);
list.add(36);
list.add(78);
list.add(99);
list.add(41);
list.add(25);
System.out.println("Array:" +list);
}
}
Output :