0% found this document useful (0 votes)
189 views21 pages

Java Lab PDF

1. The document provides 12 code examples demonstrating various Java programming concepts like printing output, taking command line arguments, loops, classes, objects, inheritance, interfaces, and applets. The examples range from simple "Hello World" programs to programs demonstrating classes, objects, and graphics. 2. The code examples are presented along with their expected output to demonstrate how each program functions. A variety of basic Java concepts are covered, from simple printing to more advanced topics like inheritance and graphics. 3. The document serves as a learning guide, walking the reader through 12 coded examples that cover fundamental Java programming techniques and provide the full code and expected output for each.

Uploaded by

Shivani Penkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
189 views21 pages

Java Lab PDF

1. The document provides 12 code examples demonstrating various Java programming concepts like printing output, taking command line arguments, loops, classes, objects, inheritance, interfaces, and applets. The examples range from simple "Hello World" programs to programs demonstrating classes, objects, and graphics. 2. The code examples are presented along with their expected output to demonstrate how each program functions. A variety of basic Java concepts are covered, from simple printing to more advanced topics like inheritance and graphics. 3. The document serves as a learning guide, walking the reader through 12 coded examples that cover fundamental Java programming techniques and provide the full code and expected output for each.

Uploaded by

Shivani Penkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

1

1. Create a hello world that simple prints out the Statement.


Program:
import java.io.*;
class Lab1
{
public static void main(String arg[])
{
System.out.println("Hello World");
}
}

Output:
2

2. Write a program that prints three arguments taken from


command line.
Program:
import java.io.*;
class Lab2
{
public static void main(String arg[])
{
System.out.println(arg[0]);
System.out.println(arg[1]);
System.out.println(arg[2]);
}
}

Output:
3

3. WAP that prints values from 1 to 100


Program:
import java.io.*;
class Lab3
{
public static void main(String arg[])
{
int i;
for(i=0;i<100;i++)
{
System.out.print(i+1);
}
}
}

Output:
4

4. Create a class with a default constructor that points a


message. Create an object of the class.
Program:
import java.io.*;
class Lab4
{
Lab4()
{
System.out.println("Constructor is Created");
}
public static void main(String arg[])
{
Lab4 obj=new Lab4();
}
}
Output:
5

5. Design and write a java program to define a class Rectangle


that contains member for representing its length and
breadth. Provide members to get and set these attributes.
Program:
import java.io.*;
class Rectangle
{
public void length(int a)
{
System.out.println("length="+a);
}
public void breadth(int a)
{
System.out.println("breadth="+a);
}
}
class Lab5
{
public static void main(String arg[])
{
try
{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int a,b;
Rectangle obj=new Rectangle();
6

System.out.println("Enter length of rectangle");


a=Integer.parseInt(br.readLine());
System.out.println("Enter breadth of rectangle");
b=Integer.parseInt(br.readLine());
obj.length(a);
obj.breadth(b);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Output:
7

6. Design a class to represent a bank account.


Program:
import java.util.Scanner;

class bankInternal {
int acno;
float bal=0;
Scanner get = new Scanner(System.in);
bankInternal()
{
System.out.println("Enter Account Number:");
acno = get.nextInt();
System.out.println("Enter Initial Balance:");
bal = get.nextFloat();
}
void deposit()
{
float amount;
System.out.println("Enter Amount to be Deposited:");
amount = get.nextFloat();
bal = bal+amount;
System.out.println("Deposited! Account Balance is "+bal);
}
void withdraw()
{
float amount;
System.out.println("Enter Amount to be Withdrawn:");
amount = get.nextFloat();
if(amount<bal)
{
bal = bal-amount;
System.out.println("Amount Withdrawn!! Available Balance:
"+bal);
8

}
else
{
System.out.println("Insufficient funds!!");
}
}
}

public class Bank {


public static void main(String[] args)
{
bankInternal myObj = new bankInternal();
myObj.deposit();
myObj.withdraw();
}
}

Output:
9

7. Write a Simple program to calculate the sum of the digits.


Program:
import java.io.*;
class Lab6
{
public void Calculate(int n)
{
int a;
a=0;
while(n!=0)
{
a=a+(n%10);
n=n/10;
}
System.out.println("Sum of digits="+a);
}
public static void main(String arg[])
{
try
{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int a;
System.out.println("Enter any number");
a=Integer.parseInt(br.readLine());
10

Lab6 obj=new Lab6();


obj.Calculate(a);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

Output:
11

8. WAP to print a * pattern in Triangle shape


Program:
import java.io.*;
class Lab7
{
public static void main(String arg[])
{
try
{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int i,j,k,n;
System.out.println("Enter number of rows");
n=Integer.parseInt(br.readLine());
j=n-1;
for(i=1;i<=n;i++)
{
for(k=1;k<=j;k++)
{
System.out.print(" ");
}
j--;
for(k=1;k<=(2*i-1);k++)
{
System.out.print("*");
}
12

System.out.println("");
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

Output:
13

9. WAP to call a method Simple from a main function. The


method Simple should accept and integer and display its
square. Demonstrate Inheritance.
Program:
import java.io.*;
class Base
{
public void Simple(int a)
{
System.out.println("Square of a number
is="+(a*a));
}
}
class Lab8 extends Base
{
public static void main(String arg[])
{
try
{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
Lab8 obj=new Lab8();
int a;
System.out.println("Enter any number");
a=Integer.parseInt(br.readLine());
obj.Simple(a);
14

}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Output:
15

10. Write an Interface with a method called display.


Implement this method to display two names.
Program:
import java.io.*;
interface inter1
{
void display(String a,String b);
}
class Lab9 implements inter1
{
public void display(String a,String b)
{
System.out.println("First name is"+a);
System.out.println("Second name is"+b);
}
public static void main(String arg[])
{
try
{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String str1,str2;
Lab9 obj=new Lab9();
System.out.println("Enter First name");
str1=br.readLine();
System.out.println("Enter second name");
16

str2=br.readLine();
obj.display(str1,str2);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Output:
17

11. Write an Applet program to display following figures.

a. Program:

import java.awt.Graphics;
import java.applet.Applet;
//<applet code=Diag1.class height=500
width=500></applet>
public class Diag1 extends Applet
{
public void init()
{
System.out.println("Applet initialized");
}
public void paint(Graphics g)
{
g.drawRect(0,0,100,100);
g.drawRect(0,0,10,10);
g.drawRect(90,90,10,10);
g.drawRect(0,90,10,10);
g.drawRect(90,0,10,10);
}
}
18

Output:
19

b. Program:
import java.awt.Graphics;
import java.applet.Applet;
//<applet code=Diag2.class height=500
width=500></applet>
public class Diag2 extends Applet
{
public void paint(Graphics g)
{
g.drawLine(50,0,0,50);
g.drawLine(50,0,100,50);
g.drawRect(0,50,100,100);
g.drawRect(40,130,20,20);
g.drawOval(40,25,20,20);
}
}
Output:

1
20

12. WAP to demonstrate the life Cycle of an Applet.

Program:
import java.applet.Applet;
import java.awt.Graphics;
//<applet code=Lab11.class height=500
width=500></applet>
public class Lab11 extends Applet
{
public void init()
{
System.out.println("Applet initialized");
}
public void start()
{
System.out.println("Applet has started");
}
public void stop()
{
System.out.println("Applet is in idle state");
}
public void destroy()
{
System.out.println("Objects destroyed");
}
public void paint(Graphics g)
{
21

g.drawString("Hellow World", 250,250);


}
}

Output:

You might also like