Java Record 2024 Batch
Java Record 2024 Batch
B.Sc (IT)
2024-25
NAME :
REGISTER NUMBER :
SEMESTER :
CLASS :
GURU NANAK COLLEGE (AUTONOMOUS)
Chennai – 600 042.
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.
Submitted for the Second Semester Practical Examination held on ___________________ at the
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:
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
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
import java.util.Scanner;
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);
for(int i=0;i<n;i++)
{
System.out.println("Enter the number");
num[i]=sc.nextInt();
findmaxmin(num);
}
}
Output:
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:
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;
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