0% found this document useful (0 votes)
2 views

Java Record 2024 Batch

The document outlines the practical programming assignments for the B.Sc (IT) program at Guru Nanak College for the academic year 2024-25. It includes various Java programming tasks such as checking for prime numbers, calculating factorials, and implementing classes and interfaces. Additionally, it provides sample code and expected outputs for each task.

Uploaded by

nishanthp2806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Record 2024 Batch

The document outlines the practical programming assignments for the B.Sc (IT) program at Guru Nanak College for the academic year 2024-25. It includes various Java programming tasks such as checking for prime numbers, calculating factorials, and implementing classes and interfaces. Additionally, it provides sample code and expected outputs for each task.

Uploaded by

nishanthp2806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

GURU NANAK COLLEGE (AUTONOMOUS)

Chennai – 600 042.

PROGRAMME OF INFORMATION TECHNOLOGY

B.Sc (IT)

2024-25

PRACTICAL – II: PROGRAMMING IN JAVA

NAME :

REGISTER NUMBER :

SEMESTER :

CLASS :
GURU NANAK COLLEGE (AUTONOMOUS)
Chennai – 600 042.

PROGRAMME OF B.Sc (It)


BONAFIDE CERTIFICATE

This is to certify that, this is the bonafide record of the practical work done in
________________________________ at Guru Nanak College Computer Lab, during the Year
2024 - 2025.

Staff-In-Charge Head of the Department

Submitted for the Second Semester Practical Examination held on ___________________ at the

PROGRAMME OF INFORMATION TECHNOLOGY , B.Sc(IT), GURU NANAK COLLEGE

(AUTONOMOUS), VELACHERY, CHENNAI – 00 042.

Internal Examiner External Examiner


INDEX
S.No Date Title Page No Sign
1 NUMBER IS PRIME OR NOT

2 TO READ STUDENT MARKS FOR FIVE SUBJECTS

3 FACTORIAL OF A GIVEN NUMBER

4 TO SWAP THE VALUES OF TWO VARIABLES


WITHOUT
5 AREA OF A CIRCLE

6 SUM OF ALL EVEN NUMBERS IN AN ARRAY

7 JAVA CLASS NAMED PERSON WITH ATTRIBUTES

8 FIND THE LARGEST AND SMALLEST ELEMENTS IN AN


ARRAY
9 DEMONSTRATE METHOD OVERRIDING

10 DEMONSTRATE CONSTRUCTOR OVERLOADING IN A


CLASS
11 CREATE AN ARRAY OF BOOK OBJECTS

12 IMPLEMENT THIS INTERFACE IN CLASSES LIKE CIRCLE


AND RECTANGLE
13 JAVA PROGRAM THAT THROWS A CUSTOM
EXCEPTION
14 JAVA SWING PROGRAM TO CREATE A SIMPLE GUI
WINDOW
15 JAVA SWING PROGRAM THAT DISPLAYS A WINDOW
CONTAINING A BUTTON
APPLET PROGRAM
16 DRAW A RECTANGLE

17 DRAW AN ARCS

18 DRAW AN ELLIPSE
CHECK WHETHER A GIVEN NUMBER IS PRIME OR NOT

import java.util.Scanner;
class prime
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a Number: ");
int n = in.nextInt();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
System.out.println(n + " is a Prime Number");
}
else
{
System.out.println(n + " is not a Prime Number");
}
}
}
Output

Enter a Number: 11
11 is a Prime Number
JAVA PROGRAM TO READ STUDENT MARKS FOR FIVE SUBJECTS AND PRINT
THE TOTAL AND AVERAGE.
import java.util.Scanner;
class subject
{
public static void main(String args[])
{
int tot_sub, i;
float marks, total = 0, aver;
Scanner sc = new Scanner(System.in);
System.out.print(" Enter the Total Number of Subjects : ");
tot_sub = sc.nextInt();
System.out.print(" Enter the Subjects Marks : ");
for(i = 0; i < tot_sub; i++)
{
marks = sc.nextInt();
total = total + marks;
}
aver = total / tot_sub;
System.out.println(" Total Marks = " + total);
System.out.println(" Average Marks = " + aver);
}
}
Output:
Enter the Total Number of Subjects: 6
Enter the Subjects Marks: 80 85 75 65 90 95
Total Marks = 490.0
Average Marks = 81.66
TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER USING A LOOP.

import java.util.Scanner;
class factorial
{
public static void main(String args[])
{
int n,i, fact = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Number: ");
n = s.nextInt();
for( i = 1; i <= n; i++)
{
fact = fact * i;
}
System.out.println("Factorial of "+n+" is "+fact);
}
}
Output
Enter the Number: 5
Factorial of 5 is 120
SWAP THE VALUES OF TWO VARIABLES WITHOUT USING A
TEMPORARY VARIABLE

import java.util.*;
class Swap
{
public static void main (String a[])
{
System.out.println("Enter the value of x and y");
Scanner sc = new Scanner(System.in);
/*Define variables*/
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println("Before swapping numbers: “+x +” “+ y);
/*Swapping*/
x = x + y;
y = x – y;
x = x – y;
System.out.println(“After swapping: “+x +” “+ y);
}
}
Output:

Before swapping numbers:


X=12
Y=15
After swapping:
X=15
Y=12
AREA OF A CIRCLE USING THE FORMULA A = πr^2.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int r;
double pi = 3.14, area;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the radius of circle: ");
r = sc.nextInt();
area = pi * r * r;
System.out.println("The area of the circle: "+area);
}
}
Output:
Enter the radius of circle: 5

The area of the circle:


SUM OF ALL EVEN NUMBERS IN AN ARRAY.

import java.util.Scanner;
class Sumofeven
{
public static void main(String[] args)
{
int size, i, evensum = 0;
Scanner sc = new Scanner(System.in);
System.out.print(" Please Enter Number of elements in an array : ");
size = sc.nextInt();
int [] a = new int[Size];
System.out.print(" Please Enter " + Size + " elements of an Array : ");
for (i = 0; i < size; i++)
{
a[i] = sc.nextInt();
}
for(i = 0; i < Size; i++)
{
if(a[i] % 2 == 0)
{
evensum = evensum + a[i];
}}
System.out.println("\n The Sum of Even Numbers in this Array = " +
evensum);
}}
Output

Please Enter Number of elements in an array :5


Please Enter 8 elements of an Array: 10 20 15 35 50

The Sum of Even Numbers in this Array = 80


JAVA CLASS NAMED PERSON WITH ATTRIBUTES

public class person


{
String name;
int age;
String address;
public person(String name, int age, String address)
{
this.name = name;
this.age = age;
this.address = address;
}
public String getName()
{
return name;
}
public int getage()
{
return age;
}
public String getaddress()
{
return address;
}
public static void main(String[] args)
{
person c1 = new person("Raj",15, "Main Street Chennai");
System.out.println("The Person name is "+ c1.getName());
System.out.println("Age of the person is "+ c1.getage());
System.out.println("Address is “+ c1.getaddress());
}
}
Output:
The Person name is : Raj
Age of the person is : 15
Address is : Main Street Chennai
FIND THE LARGEST AND SMALLEST ELEMENTS IN AN ARRAY

import java.util.Scanner;

public class max


{
static void findmaxmin(int[] num1)
{
int max=num1[0];
int min=num1[0];

for(int j=1;j<num1.length;j++)
{
if(max<num1[j])
{

max=num1[j];
}
if(min>num1[j])
{

min=num1[j];
}
}
System.out.println("Maximum Element in Array"+max);

System.out.println("Maximum Element in Array"+min);


}

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbers of elements ");
int n=sc.nextInt();
int[] num =new int[n];

for(int i=0;i<n;i++)
{
System.out.println("Enter the number");
num[i]=sc.nextInt();

findmaxmin(num);
}
}
Output:

Enter the number of elements


5
Enter the number
19
Enter the number
20
Enter the number
42
Enter the number
30
Enter the number
32

Maximum Element in Array


42
Maximum Element in Array
19
DEMONSTRATE METHOD OVERRIDING

class rectangle
{
protected double width , height;
public rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double area()
{
return width * height;
}
public double perimeter()
{
return 2 * (width + height);
}
}
class square extends rectangle
{
public square(double side)
{
super(side, side); //to refer parent class
}
public double area()
{
return width * width;
}
public double perimeter()
{
return 4 * width;
}
}
public class mainover
{
public static void main(String[] args)
{
rectangle rect = new rectangle(5, 10);
System.out.println("Rectangle Area: " + rect.area());
System.out.println("Rectangle Perimeter: " + rect.perimeter());
square squ = new square(5);
System.out.println("Square Area: " + squ.area());
System.out.println("Square Perimeter: " + squ.perimeter());
}
}
Output
Rectangle Area : 50.0
Rectangle Perimeter : 30.0
Square Area : 25.0
Square Perimeter : 20.0
DEMONSTRATE CONSTRUCTOR OVERLOADING IN A CLASS

class student
{
String name;
int age;
String course;
public student()
{
this.name = "Unknown";
this.age = 0;
this.course = "Not Assigned";
System.out.println("Student details (No argument constructor):");
}
public Student(String name, int age)
{
this.name = name;
this.age = age;
this.course = "Not Assigned";
System.out.println("Student details (Constructor with name and age):");
}
public student(String name, int age, String course)
{
this.name = name;
this.age = age;
this.course = course;
System.out.println("Student details (Constructor with name, age, and
course):");
}
public void displaydetails()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Course: " + course);
System.out.println();
}
}
public class maincons
{
public static void main(String[] args)
{
student student1 = new student();
student1.displaydetails();
student student2 = new student("Alice", 20);
student2.displaydetails();
student student3 = new student("Bob", 22, "Computer Science");
student3.displaydetails();
}
}
Output:

Student details (No argument constructor):


Name: Unknown
Age: 0
Course: Not Assigned

Student details (Constructor with name and age):


Name: Alice
Age: 20
Course: Not Assigned

Student details (Constructor with name, age, and course):


Name: Bob
Age: 22
Course: Computer Science
CREATE AN ARRAY OF BOOK OBJECTS
import java.util.Arrays;
import java.util.Comparator;
class Book
{
String title;
String author;
int publicationYear;
public Book(String title, String author, int publicationYear)
{
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
public String toString()
{
return "Title: " + title + ", Author: " + author + ", Year: " +
publicationYear;
}
}
public class mainnew
{
public static void main(String[] args)
{
Book[] books = {
new Book("Programming in C ", "E.Balagurusamy", 1978),
new Book("Programming in C++", "E.Balagurusamy", 1985),
new Book("Programming in Java", "E. Balagurusamy ", 1995),
new Book("Operating System", "S.Tanenbaum", 1956),
new Book("Python Programming", "Van Rossum", 1991)
};
Arrays.sort(books, Comparator.comparingInt(b -> b.publicationYear));
System.out.println("Books sorted by publication year:");
for (Book b : books)
{
System.out.println(b);
}
}
}
Output:

Books sorted by publication year:


Title: Operating System, Author: S.Tanenbaum, Year: 1956
Title: Programming in C, Author: E.Balagurusamy, Year: 1978
Title: Programming in C++, Author: E.Balagurusamy, Year: 1985
Title: Python Programming, Author: Van Rossum, Year: 1991
Title: Programming in Java, Author: E. Balagurusamy , Year: 1995
IMPLEMENT THIS INTERFACE IN CLASSES LIKE CIRCLE
AND RECTANGLE

interface Shape
{
double area();
double perimeter();
}
class Rectangle implements Shape
{
private double length;
private double breadth;
public Rectangle(double length, double breadth)
{
this.length = length;
this.breadth = breadth;
}
public double area()
{
return length * breadth;
}
public double perimeter()
{
return 2 * (length + breadth);
}
}
class Circle implements Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double area()
{
return Math.PI * radius * radius;
}
public double perimeter()
{
return 2 * Math.PI * radius;
}
}
public class sample
{
public static void main(String[] args)
{
double length = 2.0;
double breadth = 3.0;

Rectangle r = new Rectangle(length, breadth);


System.out.println(“Rectangle – Area: “ + r.area());
System.out.println(“Rectangle – perimeter: “ + r.perimeter());

double radius = 2.0;


Circle c = new Circle(radius);
System.out.println(“Circle – Area:” + c.area());
System.out.format(“Circle-Area: %.2f\n”, c.area());
System.out.println(“Circle – perimeter: “ + c.perimeter());
}
}
Output:
Rectangle - Area: 6.0
Rectangle - perimeter: 10.0
Circle - Area: %.2f 12.566370614359172
Circle-Area: 12.57
Circle - perimeter: 12.566370614359172
JAVA PROGRAM THAT THROWS A CUSTOM EXCEPTION

class DivideBy extends Exception


{
public DivideBy(String message)
{
super(message);//to access parent class
}
}
public class DivisionExample
{
public static double divide(int numerator, int denominator) throws DivideBy
{
if (denominator == 0)
{
throw new DivideBy("Error: Cannot divide by zero.");
}
return (double) numerator / denominator;
}
public static void main(String[] args)
{
try
{
int numerator = 10;
int denominator = 10;

double result = divide(numerator, denominator);


System.out.println("Result: " + result);
} catch (DivideBy e)
{
System.out.println(e.getMessage());
}
}
}
Output:
Error: Cannot divide by zero.
JAVA SWING PROGRAM TO CREATE A SIMPLE GUI WINDOW

import javax. swing.JFrame;


import javax. swing.JLabel;
import javax.swing.SwingConstants;
public class HelloSwing
{
public static void main(String[] args)
{
JFrame frame = new JFrame("New Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
JLabel label = new JLabel("Hello, Swing!", SwingConstants.CENTER);
frame.add(label);
frame.setVisible(true);
}
}
Output
JAVA SWING PROGRAM THAT DISPLAYS A WINDOW
CONTAINING A BUTTON

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class buttonswing
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Swing Button Click Example");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me!");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame, "Hello! This is a custom
message.");
}
});
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
Output
RECTANGLE

import java.awt.*;
import java.applet.*;
/*<applet code="Lines.class" width=300 height=300>
</applet>*/
public class Lines extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10,10,50,100);
g.fillRect(10,10,50,100);
g.drawRoundRect(80,100,150,200,100,50);
g.fillRoundRect(80,100,150,200,100,50);
}
}
Output
ARC

import java.awt.*;
import java.applet.*;
/*<applet code="arc.class" width=300 height=300> </applet>*/
public class arc extends Applet
{
public void paint(Graphics g)
{
g.drawArc(30,5, 200, 200, 0, -90);
g.fillArc(30, 5, 200, 200, 0, -90);

}
}
Output
ELLIPSE

import java.awt.*;
import java.applet.*;
/*<applet code="ellipse.class" width=300 height=300>
</applet>*/
public class ellipse extends Applet
{
public void paint(Graphics g)
{
g.drawOval(150,100,75,50);
g.fillOval(150,100,75,50);

}
}
Output

You might also like