Java Practical
Java Practical
Roll No :-46
Class :- BCA III Sem V.
Lab Exercise 1
1) if Statement :-
class If
{
public static void main(String args[])
{
int x = 10;
if( x < 20 )
{
System.out.print("This is if statement");
}
}
}
Output :-
Output :-
b) Looping Statement :-
1)while Statement :-
class While
{
public static void main(String args[])
{
int x = 10;
while( x < 20 )
{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
Output :-
2) do-while :-
class Dowhile
{
public static void main(String args[])
{
int x = 10;
do
{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
while( x < 20 );
}
}
Output :-
3) For :-
class For
{
public static void main(String args[])
{
for(int x = 10; x < 20; x = x+1)
{
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 2
Java programs based on Type Casting.
1) Implicit Type Casting :-
public class ImplicitCasting {
public static void main(String[] args) {
int intValue = 42;
long longValue = intValue;
float floatValue = longValue;
System.out.println("Int value: " + intValue);
System.out.println("Long value: " + longValue);
System.out.println("Float value: " + floatValue);
}
}
Output :-
2)Parameterized Constructor :-
class Student4
{
int id;
String name;
Student4(int i,String n)
{
id=i;
name=n;
}
void display()
{
System.out.println(id+" "+name);
}
}
public class ParameterizedCon
{
public static void main(String args[])
{
Student4 s1=new Student4(17,"Ashwini");
Student4 s2=new Student4(12,"Sneha");
s1.display();
s2.display();
}
}
Output :-
3)Copy Constructor :-
class CopyConstructor
{
int id;
String name;
CopyConstructor(int i, String nm)
{
id=i;
name=nm;
}
CopyConstructor(CopyConstructor s)
{
id=s.id;
name=s.name;
}
void display()
{
System.out.println(id+""+name);
}
public static void main(String[] args) {
CopyConstructor s1= new CopyConstructor(46,”Shweta);
CopyConstructor s2= new CopyConstructor(s1);
System.out.println("Object s1= ");
s1.display();
}
}
Output :-
Name :- Shweta Sadashiv Mali.
Roll No :- 46
Class :- BCA III Sem V.
Lab Exercise 5
Java programs based on inheritance.
1) Single Inheritance :-
class Student{
int rollno;
String name;
}
class Marks1 extends Student{
int sub1,sub2;
void dispStud(){
System.out.println("-------------------------------------");
System.out.println(" New English School");
System.out.println("----------------------------------------");
System.out.println();
System.out.println("Roll No :- " + rollno);
System.out.println("Name of Student :- " + name);
System.out.println("Marks in Marathi :- " + sub1);
System.out.println("Marks in English :- " + sub2);
System.out.println("Marks(total) :- " + (sub1+sub2));
}
}
public class SingleInheritance{
public static void main(String args[]){
Marks1 obj=new Marks1();
obj.rollno=1;
obj.name="Shweta Sadashiv Mali";
obj.sub1=80;
obj.sub2=90;
obj.dispStud();
}
}
Output :-
2) Multilevel Inheritance :-
class A
{
int a,b;
void display(){
System.out.println(a+" "+b);
}
}
class B extends A{
int c;
void show(){
System.out.println(a+" "+b+" "+c);
}
}
class c extends B{
int d;
void shows()
{
System.out.println(a+" "+b+" "+c+" "+d);
}
}
public class MultilevelInherit{
public static void main(String args[]){
c obj=new c();
obj.a=10;
obj.b=20;
obj.c=30;
obj.d=40;
obj.display();
obj.show();
obj.show();
}
}
Output :-
3) Hierechial Inheritance:-
class Vehicle {
public void start() {
System.out.println("The vehicle starts.");
}
}
class Car extends Vehicle {
public void drive() {
System.out.println("The car drives.");
}
}
class Bike extends Vehicle {
public void ride() {
System.out.println("The bike rides.");
}
}
public class Hierechial{
public static void main(String[] args) {
4) Hybrid Inheritance :-
interface CanFly {
void fly();
}
interface CanSwim {
void swim();
}
class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
class Duck extends Animal implements CanFly, CanSwim {
public void fly() {
System.out.println("The duck can fly.");
}