Java Lab Record Gul
Java Lab Record Gul
GULSHAN KUMAR
REG. NO.: 22PGMCA15
Object Oriented Programming - LAB
Course Code: PCATC20007
MCA 2nd Semester
Object Oriented Programming
1. Check_even.java
(To check ODD-EVEN using Scanner object and if-else)
import java.util.*;
public class Check_Even {
public static void main(String[] args) {
Output:
4
Central University of Karnataka
Object Oriented Programming
2. FloatPoint.java
(Arithmetic operation on floating point Numbers)
import java.util.Scanner;
public class FloatPoint {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int con=1;
while(con==1) {
System.out.print("Enter 1st Number : ");
double a=sc.nextDouble();
System.out.print("Enter (+,-,*,/,%) Symbol : ");
char operator=sc.next().charAt(0);
System.out.print("Enter 2nd Number : ");
double b=sc.nextDouble();
double result;
switch(operator) {
case '+':
result= a+b;
System.out.println("Sum : "+result);
break;
case '-':
result= a-b;
System.out.println("Sub : "+result);
break;
case '*':
result= a*b;
System.out.println("Multi : "+result);
break;
case '/':
result= a/b;
System.out.println("Div : "+result);
break;
case '%':
result= a%b;
System.out.println("Rem : "+result);
break;
default:
System.out.println("Please Enter Valid Input");
}
System.out.print("Do you want to Continue(1/0)? : ");
con=sc.nextInt();
}
}
Output:
3. ComLineTest.java
(Printing the Strings, input by command line argument)
public class ComLineTest {
Output:
6
Central University of Karnataka
Object Oriented Programming
4. Reading.java
(To read the integer and float through Console)
import java.util.Scanner;
public class Reading {
Output:
5. Displaying.java
(To print the pattern like):
1
22
333
4444
55555
666666
7777777
88888888
999999999
import java.util.Scanner;
public class Displaying {
Output:
6. Prime.java
(To check number is prime or not)
import java.util.Scanner;
public class Prime {
Output:
7. Grade.java
(To print the grade based on the given condition)
import java.util.*;
public class Grade {
}
else if(per>=60 && per<80) {
System.out.print("A Grade");
}
else if(per>=50 && per<60) {
System.out.print("B++ Grade");
}
else if(per>=45 && per<50) {
System.out.print("B Grade");
}
else if(per>=40 && per<45) {
System.out.print("C++ Grade");
}
else if(per>=33 && per<40) {
System.out.print("C Grade");
}
else {
System.out.print("Fail");
}
}
Output:
8. RectangleArea.java
(To calculate the area of the rectangle)
import java.util.Scanner;
public class RectangleArea {
Output:
9. ExSwitch.java
(To print the user choice by Switch case)
import java.util.*;
public class ExSwitch {
System.out.print("1.Addition\n2.Subtraction\n3.Multiplication\n4.D ivisi
on\n5.Remainder\n6.Exit\nEnter Choice : ");
int choice=sc.nextInt();
double result;
switch(choice) {
case 1:
12
Central University of Karnataka
Object Oriented Programming
result=a+b;
System.out.print("Addition : "+result);
break;
case 2:
result=a-b;
System.out.print("Subtraction : "+result);
break;
case 3:
result=a*b;
System.out.print("Multiplication : "+result);
break;
case 4: {
try {
result=a/b;
System.out.print("Division : "+result);
} catch(Exception e) {
System.out.println("Can't be divide by zero");
}
break;
}
case 5:
result=a%b;
System.out.print("Remainder : "+result);
break;
case 6:{
System.out.print("Exit...");
System.exit(0);
}
}
System.out.print("\nDo you want to continue(1/0)? : ");
con=sc.nextInt();
}
Output:
10. VolCuboid.java
(To calculate the volume of cuboid)
import java.util.Scanner;
public class VolCuboid {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter Length of Cuboid : ");
double l=sc.nextDouble();
System.out.print("Enter Breadth of Cuboid : ");
double b=sc.nextDouble();
System.out.print("Enter Height of Cuboid : ");
double h=sc.nextDouble();
double area=2*(l*b + b*h + h*l);
14
Central University of Karnataka
Object Oriented Programming
System.out.print("Area of Cuboid : "+area);
}
Output:
11. DowhileTest.java
(To print the multiplication table using do-while)
import java.util.Scanner;
public class DowhileTest {
}
}
Output:
12. ForTest.java
(To print “2 to the power -n || n || 2 to power n” using for and if-else)
import java.util.*;
public class ForTest {
Output:
16
Central University of Karnataka
Object Oriented Programming
13. NestingTest.java
(To print largest of two number using nested classes)
import java.util.Scanner;
class CheckGreter {
class Check {
Scanner sc = new Scanner(System.in);
int a;
int b;
public Check() {
System.out.print("Enter 1st Value : ");
a = sc.nextInt();
System.out.print("Enter 2nd Value : ");
b = sc.nextInt();
if (a > b) {
System.out.print("1st Value is Greater");
} else {
System.out.print("2nd Value is Greater");
}
}
}
}
Output:
14. TotalEvenOdd.java
(To print the total even and total odd numbers from an array)
import java.util.*;
public class TotalEvenOdd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter Value of N : ");
int n=sc.nextInt();
System.out.println("Even\t\tOdd\n----------------------");
for(int i=1;i<=n;i++) {
if(i%2==0) {
System.out.println(i+"\t|");
}
else {
System.out.println("\t|\t"+i);
}
}
18
Central University of Karnataka
Object Oriented Programming
System.out.println("--------------------------------");
}
Output:
15. StringDemo.java
(To demonstrate the use of length(), equals() and charAt() methods)
import java.util.*;
public class StringDemo {
char find=str1.charAt(index);
System.out.printf("Character at %d in %s : %c",index,str1,find);
Output:
16. Mathoperation.java
(To demonstrate the user defined mul() and divide() methods)
import java.util.*;
class Operation {
Scanner sc = new Scanner(System.in);
int a;
int b;
public Operation() {
System.out.print("Enter 1st Value : ");
a = sc.nextInt();
System.out.print("Enter 2nd Value : ");
b = sc.nextInt();
}
void mul() {
System.out.println("Multiplication : "+a * b);
}
void div() {
System.out.println("Division : "+a / b);
}
}
}
Output:
17. StringCharByChar.java
(To read the string char by char and appending these chars to StringBuffer obj
through while loop)
import java.util.Scanner;
public class {
int i = 0;
while (i < str.length()) {
char c = str.charAt(i);
sb.append(c);
i++;
}
}
Output:
19. Sort.java
(To sort the array using Bubble sort)
import java.util.*;
public class Sort {
public static void Sorting(int a[],int n) {
for(int i=0;i<n;i++) {
for(int j=0;j<n-i-1;j++) {
if(a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
public static void arrayPrint(int a[],int n) {
for(int i=0;i<n;i++) {
System.out.print(a[i]+" ");
}
}
}
}
Output:
22
Central University of Karnataka
Object Oriented Programming
20. StringMethods.java
(To demonstrate the various String methods like toLowerCase(), toUpperCase(),
replace('A','Q'), trim() and equals() methods)
import java.util.*;
public class StringMethods {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter String to Convert Lower Case : ");
String str=sc.nextLine();
System.out.println("Lower Case : "+str.toLowerCase());
System.out.print("Enter String to Convert Upper Case : ");
str=sc.nextLine();
System.out.println("Upper Case : "+str.toUpperCase());
System.out.print("Enter String to To Replace 'A' to 'Q' : ");
str=sc.nextLine();
System.out.println("To Replace 'A' to 'Q': " + str.replace('A', 'Q'));
System.out.print("Enter String to To trim() : ");
str = sc.nextLine();
System.out.println("Trim : " + str.trim());
System.out.print("Enter 1st String : ");
str=sc.nextLine();
System.out.print("Enter 2nd String to check equals or not : ");
String str2=sc.nextLine();
System.out.println("To check equals : "+str.equals(str2));
}
Output:
21. LanguageVector.java
(To assign the values in Vector list through Command line argument and then
inserting element to this list using insertElementAt("Cobol",2))
import java.util.Vector;
24
Central University of Karnataka
Object Oriented Programming
22. SimpleInterest.java
(To calculate the Simple interest when Principal amt, Interest Rate and no of year
is given)
import java.util.*;
public class SimpleInterest {
Output:
23. Interface1.java
(To implement the interface ‘Polygon’ and calculate the area of Rectangle using
getArea() {user defined} method)
import java.util.*;
interface polygon{
Scanner sc=new Scanner(System.in);
void getInput();
}
class Area implements polygon{
double l,b,area;
public void getInput() {
System.out.print("Enter Length of Rectangle : ");
l=sc.nextDouble();
System.out.print("Enter Breadth of Rectangle : ");
b=sc.nextDouble();
}
void getArea() {
getInput();
System.out.println("Area of Rectangle : "+(l*b));
}
}
public class Interface1 {
24. Interface2.java (To implement the interface ‘Language’ and concate the other
string using getName() {user defined} method)
import java.util.*;
interface Language{
Scanner sc=new Scanner(System.in);
void getName();
}
class Concate implements Language{
String s1,s2;
public void getName() {
26
Central University of Karnataka
Object Oriented Programming
System.out.print("Enter 1st String : ");
s1=sc.next();
System.out.print("Enter 2nd String : ");
s2=sc.next();
}
void getConcate() {
getName();
String s3=s1+" "+s2;
System.out.println("String Concatanation : "+s3);
}
}
public class Interface2 {
Output:
25. Interface3.java
(To implement the interface ‘Polygon’ having getArea() and getSides() and also to
override the getSides() of Polygon in Rectangle class which is implementing the
Polygon )
import java.util.Scanner;
interface Polygon {
double getArea();
int getSides();
}
@Override
public double getArea() {
@Override
public int getSides() {
return 4;
}
}
Output:
26. Interface4.java
(To calculate the Area and Perimeter of a triangle implement the Polygon
interface to Triangle class)
import java.util.*;
interface Polygon1{
Scanner sc=new Scanner(System.in);
void getInput();
}
class Find implements Polygon1{
double s1,s2,s3;
public void getInput() {
System.out.print("Enter Value of Side-1 : ");
s2=sc.nextDouble();
System.out.print("Enter Value of Side-2 : ");
s1=sc.nextDouble();
System.out.print("Enter Value of Side-3 : ");
s3=sc.nextDouble();
}
void getPerimeter() {
getInput();
28
Central University of Karnataka
Object Oriented Programming
double per=(s1+s2+s3);
System.out.println("Perimeter of Triangle : "+per);
}
void getArea() {
double sp=(s1+s2+s3)/2;//SemiPerimeter
double area = (Math.sqrt((sp * ((sp - s1) * (sp - s2) * (sp - s3)))));
System.out.println("Area of Triangle : "+area);
}
}
public class Interface4 {
public static void main(String[] args) {
Find obj=new Find();
obj.getPerimeter();
obj.getArea();
}
Output:
27. UseOfSuper.java
(To demonstrate the use of super keyword to invoke super class method)
import java.util.*;
class T{
void m() {
System.out.println("m method from Class T");
}
}
class D extends T{
void m() {
super.m();//Super Keyword is used to call super class,method,variable
System.out.println("m method from Class D");
}
}
public class UseOfSuper {
Output:
28. BasicCalculator.java
(To peform the basic arithmetic operations)
import java.util.*;
public class BasicCalculator {
Output:
29. packageProg1.java
(To access the classes and their methods of different program under same
Package)
package com.Manish.demopkg;
package com.Manish.demopkg;
public class PackageCall {
Output :
Save : PackageProgram.java
32
Central University of Karnataka
Object Oriented Programming
package BalancePackage;
public class PackageProgram2 {
public static void main(String[] args) {
int arraySize = 5;
Balance[] balances = new Balance[arraySize];
balances[0] = new Balance(100.0);
balances[1] = new Balance(200.0);
balances[2] = new Balance(300.0);
balances[3] = new Balance(400.0);
balances[4] = new Balance(500.0);
for (int i = 0; i < arraySize; i++) {
System.out.println("Balance " + (i + 1) + ": " + balances[i].getAmount());
}
}
}
Output :
31. DivByZero.java
(To demonstrate the ArithmeticException in java)
import java.util.*;
public class DivByZero {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter Dividend : ");
double a=sc.nextDouble();
System.out.print("Enter Divisor : ");
double b=sc.nextDouble();
try {
if(b==0) {
throw new ArithmeticException("Division by zero");
}
System.out.print("Quotient : "+(a/b));
}
catch(Exception e) {
System.out.print(e.getMessage());
}
}
Central University Of Karnataka
33
Object Oriented Programming
Output :
32. MultiCatch.java (To demonstrate the use of multiple catch block {with
ArithmeticException and ArrayIndexOutOfBoundsException})
public class MultiCatch {
public static void main(String[] args) {
int[] numbers = { 1, 2, 3 };
int index = 4;
try {
int result = numbers[0] / numbers[1];
System.out.println("Result: " + result);
Output :
34
Central University of Karnataka
Object Oriented Programming
33. NestedTry.java
(To demonstrate the use of nested try blocks)
import java.util.*;
public class NestedTry {
}
catch(Exception eobj) { //Inner catch Block
System.out.println(eobj.getMessage());
break;
}
}
}
catch(Exception eobj) { //Outer catch Block
System.out.println("An error Occurred");
}
System.out.print("\nDo you want to continue(1/0)? : ");
con=sc.nextInt();
}
}
Output:
34. finallyDemo.java
(To demonstrate the use of finally keyword)
import java.util.*;
public class FinallyDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int con=1;
while(con==1) {
36
Central University of Karnataka
Object Oriented Programming
System.out.print("\n\t----------DIVISION CALCULATOR--------------- -\n");
System.out.print("Enter 1st Value : ");
double a=sc.nextDouble();
System.out.print("Enter 2nd Value : ");
double b=sc.nextDouble();
try {
if(b==0) {
throw new Exception("Can't be divide by Zero");
}
else {
System.out.println("Div : "+(a/b));
}
}
catch(Exception eobj) {
System.out.println(eobj.getMessage());
}
finally {
System.out.println("This is Finally block,It'll Excecute Every time
\n\t\tThanku!!! ");
}
System.out.print("\nDo you want to continue(1/0)? : ");
con=sc.nextInt();
}
}
}
Output:
35. MultiThread.java (Creating Thread extending Thread class and using its
methods like yield(), sleep() and stop())
public class MultiThread extends Thread {
Central University Of Karnataka
37
Object Oriented Programming
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
// Using sleep() method to pause the thread for a specified time
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "
interrupted.");
}
}
}
Output :
38
Central University of Karnataka
Object Oriented Programming
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
// Using Thread.sleep() to pause the thread for a specified time
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "
interrupted.");
}
}
}
Output :
37. Rectangle.java (To draw the rectangle on applet using drawRect() method)
import java.applet.*;
import java.awt.*;
public class Applet38 extends Applet
{
int height,width;
public void init()
{
height=getSize().height;
width=getSize().width;
setName("Rectangle");
}
public void paint(Graphics g)
{
g.drawRect(10,10,100,80);
}
}
40
Central University of Karnataka
Object Oriented Programming
Html file:
<!DOCTYPE html>
<html>
<head>
<title>Draw Rectangle</title>
</head>
<body><applet code="Applet38.class" width=300 height=400></applet>
</body>
</html>
Output:
38. Hello Applet.java / appletFirst.java (To display the string on applet using
drawstring() by extending Applet class )
import java.applet.*;
import java.awt.*;
public class AppletFirst extends Applet
{
int height,width;
public void paint(Graphics g)
{
g.drawString("Hello World",150,100);
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<applet code="AppletFirst.class" width=300 height=200></applet>
</body>
</html>
Output:
39. fillColor.java (To creating a fillColor class extending Applet using setColor())
import java.applet.*;
import java.awt.*;
public class FillColor extends Applet
{
public void paint(Graphics g)
{
g.drawRect(200,150,200,100);
Color c= new Color(10,50,10,100);
g.setColor(c);
g.fillRect(200,150,200,100);
g.setColor(Color.yellow);
g.drawString("Rectangle",300,300);
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<applet code="FillColor.class" width=600 height=400></applet>
42
Central University of Karnataka
Object Oriented Programming
</body>
</html>
Output:
40. DisplayImage.java (To display the image in applet window using getImage()
and drawImage() methods)
import java.applet.*;
import java.awt.*;
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Pic Apply</title>
</head>
<body>
<applet code="DisplayImage.class" width=1000 height=700></applet>
</body>
</html>
Output:
44
Central University of Karnataka
Object Oriented Programming
41. Shapes.java
(To draw the different shapes like line, Oval and rectangle on Applet using
darwLine(), drawOval(), drawRect() methods)
import java.applet.*;
import java.awt.*;
public class Shape extends Applet
{
public void paint(Graphics g)
{
g.drawLine(85,8,280,8);
g.drawString("Line",10,15);
g.drawRect(80,50,200,100);
g.fillRect(85,55,190,90);
g.drawString("Rectagle",5,75);
//drawOval( int X, int Y, int width, int height )
g.drawOval(50,200,150,80);
g.setColor(Color.yellow);
g.drawString("Oval",5,225);
}
}
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Shapes</title>
</head>
<body>
<applet code="Shape.class" width=600 height=400></applet>
</body>
</html>
Output:
46
Central University of Karnataka
Object Oriented Programming
import java.awt.*;
import java.applet.*;
public class AnimationExample extends Applet
{
Image Pic;
public void init()
{
Pic=getImage(getDocumentBase(),"cuk.png");
}
public void paint(Graphics g)
{
for(int i=0; i<100; i++)
{
g.drawImage(Pic,i,50,this);
try
{
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>motion picture</title>
</head>
<body>
<applet code="AnimationExample.class" width=1200 height=600></applet>
</body>
</html>
Output:
48
Central University of Karnataka
Object Oriented Programming
<!DOCTYPE html>
<html>
<body>
<applet code="KeyEventHand.class" width=600 height=400></applet>
</body>
</html>
Output:
HTML Code :
<!DOCTYPE html>
<html>
<head>
<title>Mouse Movement</title>
</head>
<body>
<applet code="MouseDragEvent.class" width=600 height=400></applet>
</body>
</html>
Output:
52
Central University of Karnataka
Object Oriented Programming
46. actionEventButton.java (For showing the string in TextField on button click in
applet by implementing ActionListener interface)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ActionEventButton extends Applet implements ActionListener
{
Button b,b2;
TextField tf;
public void init()
{
tf = new TextField();
tf.setBounds(30,40,150,20);
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);
b2=new Button("Clear");
b2.setBounds(160,150,60,50);
add(b2);
add(tf);
b.addActionListener(this);
b2.addActionListener(this);
setLayout(null);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
tf.setText("Welcome");
}
else
tf.setText("");
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<applet code="ActionEventButton.class" width=600 height=400></applet>
</body>
</html>
Output:
54
Central University of Karnataka