Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Practical 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

1. Write a program in java that displays Welcome in java.

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{

void display(){System.out.println("Default constructer is created");}

public static void main(String args[]){

Default_Con s1=new Default_Con();

s1.display();
}
}  
Output:

ii) parameterized constructor


Input Code:
class para_Con{
int id;
String name;

Student4(int i,String n){


id = i;
name = n;
}

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

Student4 s1 = new Student4(111,"Aniket");


Student4 s2 = new Student4(222,"Abhinandan");

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{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

double SI, p,n,r;

System.out.println("Enter Price");

p = sc.nextDouble();

System.out.println("Enter Number of Years");

n = sc.nextDouble();

System.out.println("ENter Rate");

r = sc.nextDouble();

SI = (p*n*r)/100;

System.out.println("Simple Interest =" + SI);

Output:
4. Write a program to find the average & the sum of the ‘n’ number using user input.
Input Code:

import static java.lang.Float.sum;

import java.util.Scanner;

class Average {

public static void main(String[] args)

int n, count = 1;

float Number, averageN, sumN = 0;

Scanner sc = new Scanner(System.in);

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

n = sc.nextInt();

while (count <= n)

System.out.println("Enter the "+count+" number?");

Number = sc.nextInt();

sumN += Number;

++count;

averageN = sumN/n;

System.out.println("The Sum is"+sumN);

System.out.println("The Average is"+averageN);

Output:
5. Write a program to create a simple class to findout area & perimeter of rectangle & boxes.

Input:
class Shape{
float area,perimeter;

void area(float length,float breadth){


this.area=length*breadth;
}

void perimeter(float length,float breadth){


this.perimeter=2*(length+breadth);
}
}
class Rectangle extends Shape{

void parameters(float length,float breadth,float 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));
}

void perimeter(float length,float width,float height){


this.perimeter=4*(length+width+height);
}
}
class Abox extends Shape2{

void parameters(float length,float width,float 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);

System.out.println("Day Of Week = " + dayOfWeek);


System.out.println("Week Of Month = " + Weekofmonth);

System.out.println("Hour = " + cal.get(Calendar.HOUR_OF_DAY));


System.out.println("Minute = " + cal.get(Calendar.MINUTE));
System.out.println("Second = " + cal.get(Calendar.SECOND));
System.out.println("Millisecond = " + cal.get(Calendar.MILLISECOND));
}
}
Output:
9.Write a program to create package that display welcome to package.
Input Code:

package mypack;

public class pack{

public static void main(String args[]){

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;

public class ArrayPack

public static void main(String args[])

int n=10;

ArrayList<Integer> arr=new ArrayList<Integer>(n);

for(int i=1;i<=n;i++)

arr.add(i);

System.out.print(arr);

Output:

You might also like