Java Practical 1
Java Practical 1
Java Practical 1
Input Code:
class Welcome{
public static void main(String[] args){
System.out.println("Welcome in Java");
}
}
Output:
2. Write a program in java that demonstrate the constructor i) default ii) parameterized constructor
i) default constructor
Input Code:
class Default_Con{
s1.display();
}
}
Output:
s1.display();
s2.display();
}
}
Output:
3. . Write a program in java that demonstrate the constructor i) default ii) parameterized constru
Input Code:
import java.util.Scanner;
class SimpleInterest{
System.out.println("Enter Price");
p = sc.nextDouble();
n = sc.nextDouble();
System.out.println("ENter Rate");
r = sc.nextDouble();
SI = (p*n*r)/100;
Output:
4. Write a program to find the average & the sum of the ‘n’ number using user input.
Input Code:
import java.util.Scanner;
class Average {
int n, count = 1;
n = sc.nextInt();
Number = sc.nextInt();
sumN += Number;
++count;
averageN = sumN/n;
Output:
5. Write a program to create a simple class to findout area & perimeter of rectangle & boxes.
Input:
class Shape{
float area,perimeter;
super.area(length,breadth);
super.perimeter (length,breadth);
System.out.println("Area of this rectangle is "+super.area);
System.out.println("Perimeter of this rectangle is "+super.perimeter);
}
}
class Shape2{
float area,perimeter;
//2*(l*w + l*h + w*h).
void area(float length,float width,float height){
this.area=2*((length*width)+(length*width)+(width*height));
}
super.area(length,width,height);
super.perimeter (length,width,height);
System.out.println("Area of this box is "+super.area);
System.out.println("Perimeter of this box is "+super.perimeter);
}
}
class ShapeSuper1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Abox r2=new Abox();
r1.parameters(10.5f,11.5f,44f);
r2.parameters(10f,11.5f,44f);
}
}
Output:
6. Write a program to design a class using abstract method & classes.
Input:
import java.util.Scanner;
abstract class Shape{
abstract public void input();
abstract public double area();
abstract public double perimeter();
}
class circle extends Shape{
double r;
public void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius");
r = sc.nextDouble();
}
public double area(){
return 3.14*r*r;
}
public double perimeter(){
return 3.14*2*r;
}
}
class rect extends Shape{
double l,b;
public void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter length :");
l = sc.nextDouble();
System.out.println("Enter breadth :");
b = sc.nextDouble();
}
public double area(){
return l*b;
}
public double perimeter(){
return 2*l+2*b;
}
}
class square extends Shape{
double s;
public void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter side :");
s = sc.nextDouble();
}
public double area(){
return s*s;
}
public double perimeter(){
return 4*s;
}
}
class abstract6{
public static void main(String[] args){
circle s =new circle();
s.input();
System.out.println("Circle area =" + s.area()+" Circle perimeter = "+s.perimeter());
rect s1 = new rect();
s1.input();
System.out.println("Rectangle area =" + s1.area()+" Rectangle perimeter = "+s1.perimeter());
square s2 = new square();
s2.input();
System.out.println("Square area =" + s2.area()+" Square perimeter = "+s2.perimeter());
}
}
Output:
7. write a program using calendar to display Current year, month and date
Input Code:
import java.util.Calendar;
class Calendar7{
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("Year = " + cal.get(Calendar.YEAR));
System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1));
System.out.println("Date = " + cal.get(Calendar.DATE));
}
}
Output:
8. write a program using calendar that displays date using YYYY/MM/DD & day of week & week of
month , hours, minutes, seconds, millisecond
Input Code:
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
class Calendar8{
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String strDate= formatter.format(date);
System.out.println( "Date in YYYY/MM/DD = " +strDate);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int Weekofmonth = cal.get(Calendar.WEEK_OF_MONTH);
package mypack;
System.out.println("Welcome to package");
Output:
10. Write a program to create package array list using 10 elements.
Input Code:
import java.util.ArrayList;
int n=10;
for(int i=1;i<=n;i++)
arr.add(i);
System.out.print(arr);
Output: