[go: up one dir, main page]

0% found this document useful (0 votes)
13 views22 pages

Java Lab Manual-R23-Book

Uploaded by

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

Java Lab Manual-R23-Book

Uploaded by

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

1

P.T.Lee CNPT COLLEGE

DEPARTMENT OF COMPUTER ENGINEERING

JAVA PROGRAMMING LAB MANUAL


Subject code - 1052234340

(R23 SCHEME-II YEAR / IV SEMESTER 2024-2045)

PREPARED BY
M.SRINIVASAPRIYA, B.E/ P.JAYANTHI RANI, M.E
LECTURER
2

P.T. Lee. CHENGALVARAYA NAICKER POLYTECHNIC COLLEGE


( A Govt Aided Institution- Estd 1886 )
2-3 EVK Sampath Salai , Chennai – 600 007
DEPARTMENT OF COMPUTER ENGINEERING
JAVA PROGRAMMING LAB MANUAL

EXNO NAMEOFTHEPROGRAM MARK SIGN

PART A

1 CELSIUS TO FAHRENHEIT CONVERSION

2 LARGEST NUMBER USING CONDITIONAL OPERATOR

3 IMPLEMENTING COMMAND LINE ARGEMENTS

4 USING ARRAYS

5 SORTING NAMES USING BUBBLE SORT

PART B

6 USING CONSTRUCTORS

7 USING METHOD OVERLOADING

8 INHERITANCE AND OVERRIDING METHODS

9 IMPLEMENTING INTERFACE

10 USING SWING COMPONENTS


3
1052234340 L T P C
JAVA PROGRAMMING
Practicum 2 0 4 4

Unit I INTRODUCTION TO JAVA

Introduction to OOPS: Paradigms of Programming Languages – Basic concepts of


Object-Oriented Programming –Benefits of OOPs –.– Java features – Java Environment –
JDK – API. Creating and Executing a Java program – Java Tokens- Java Virtual Machine
6
(JVM) –Command Line Arguments – Constants –Variables – Data types - Scope of
variables – Type casting – Operators.

Ex No 1: Write a java program to read the temperature in Celsius and convert into
Fahrenheit.
Ex No 2:Write a program to read 2 integers and find the largest number using
12
conditional operator.

Ex No 3: Write a Java program to implement command line arguments.

Unit II CONTROL STRUCTURES, ARRAY AND STRING

Control structures: Decision making statements - looping statements - branching


statement - Arrays: One Dimensional Array –Multidimensional Array – String: 6
String Array – String Methods.

Ex No 4: Write a Java program to find the sum and average of your tenth standard
marks.

Ex No 5: Write a Java Program to sort 10 student names in alphabetical order 12

using bubble sort


Unit III CLASS AND OBJECTS

Class and objects: Defining a class – Methods – Creating objects – Accessing


6
class members – Constructors – Method overloading – Static members – Nesting of

Methods - Final methods.

Ex No 6 : Write a Java program to collect student details using constructors.


12
Ex No 7:Write a Java program to calculate area of rectangle, triangle and square using

method overloading.

UNIT IV INHERITANCE AND INTERFACE

Inheritance: Defining Inheritance –Types of Inheritances– Overriding Methods –


6
Final Variables and Methods - Abstract Class- Interfaces: Defining Interface –

Types of Interfaces.
4

Ex No 8: Write a Java program to create a class called Shape with methods called
getPerimeter() and getArea(). Create a subclass called Circle that overrides the
getPerimeter() and getArea() methods to calculate the area and perimeter of a
circle.
Ex No 9: Write a Java program to create an interface Shape with the getArea() method. 12

Create three classes Rectangle, Circle, and Triangle that implement the Shape
interface. Implement the getArea() method for

each of the three classes.


UNIT V EXCEPTION HANDLING AND SWING
Exception Handling: Basics of Exception Handling – try blocks – throwing an exception –
catching an exception – finally statement. Swing Components and 6
Event Handlers: – Event Handlers – Event Listeners –Input Events.

Ex No 10: Write a Java program to create a panel with three buttons, labeled
12
Red, Blue and Yellow, so that clicking each button results in the background color changing to

the appropriate color.

TOTAL PERIODS 75

Textbook for Reference:


 E. Balagurusamy, Programming with Java, 5th Edition, TataMc-Graw Hill.
 Sagayaraj, Denis, Karthick and Gajalakshmi, Java Programming for Core
and advanced learners, Universities Press (INDIA) Private Limited, 2018.
 Herbert Schildt, The complete reference Java, TataMc-Graw Hill, 7th Edition.
Website links for reference:

 NPTEL & MOOC courses titled Java: https://nptel.ac.in/courses/106105191/


Suggested List of Students Activity
 Presentation/Seminars by students on any recent technological
developments based on the course.
 Programming assignments
 Periodic class/online quizzes conducted based on the course.
 Blended learning activities to explore the recent trends and developments in
the field.
Equipment / Facilities required to conduct the Practical Portion
1.Hardware(s)
Requirement:
 Desktop / Laptop
5

 Printer
2.Software(s) Requirement:
 Windows Operating System
 Net Beans 8.0.2 / 8.2 with JDK.

BOARD PRACTICAL EXAMINATION


PART – A
1. Write a Java program to read the temperature in Celsius and convert into
Fahrenheit.
6

2. Write a Java program to read 2 integers and find the largest number
using conditional operator .
3. Write a Java program to implement command line arguments.
4. Write a Java program to find the sum and average of your tenth standard marks.
5. Write a Java Program to sort 10 student names in alphabetical order
using bubble sort.

PART – B

6. Write a Java program to collect student details using constructors.


7. Write a Java program to calculate area of rectangle, triangle and square
using method overloading.
8. Write a Java program to create a class called Shape with methods called
getPerimeter() and getArea(). Create a subclass called Circle that
overrides the getPerimeter() and getArea() methods to calculate the area
and perimeter of a circle.
9. Write a Java program to create an interface Shape with the getArea()
method. Create three classes Rectangle, Circle, and Triangle that
implement the Shape interface. Implement the getArea() method for each
of the three classes.
10.Write a Java program to create a panel with three buttons, labeled Red,
Blue and Yellow, so that clicking each button results in the background
color changing to the appropriate color.

EX NO : 1

Aim:

To Write a java program to read the temperature in Celsius and convert into
7

Fahrenheit

Program:

import java.util.Scanner;
class Ex1
{
public static void main(String args[])
{
float cel, far;
Scanner s=new Scanner(System.in);
System.out.println("Enter temperature in Celsius :");
cel=s.nextInt();
far=cel*9/5+32;
System.out.println("Temp in Fahrenheit : "+far);
}
}

Sample Output
C:\Program Files\Java\jdk1.8.0_202\bin>javac d:/JAVALAB/Ex1.java
C:\Program Files\Java\jdk1.8.0_202\bin>d:
D:\>cd JAVALAB
D:JAVALAB>javac Ex1.java
D:JAVALAB>java Ex1
Enter temperature in Celsius:
36
Temp in Fahrenheit: 96.8
D:JAVALAB

Result:
Thus the above Java program is executed and the output is obtained.

EX NO : 2

Aim:

To Write a program to read 2 integers and find the largest number using
8

conditional operator

Program:

import java.util.Scanner;

public class Ex2


{
public static void main(String args[])
{
int a,b,big;
Scanner scan=new Scanner(System.in);
System.out.println("Enter two numbers :");
a=scan.nextInt();
b=scan.nextInt();
big=(a>b)?a:b;
System.out.println("Largest= "+big);
}
}

Sample Output
C:\Program Files\Java\jdk1.8.0_202\bin>javac d:/JAVALAB/Ex2.java
C:\Program Files\Java\jdk1.8.0_202\bin>d:
D:JAVALAB>javac Ex2
Enter two numbers:
56 78
Largest-78
D:JAVALAB>java Ex2
Enter two numbers:
98 34
Largest- 98
D:JAVALAB>

Result:
Thus the above Java program is executed and the output is obtained.

EX NO : 3

Aim:

To Write a Java program to implement command line argements.


9

Program:

class Ex3
{
public static void main(String args[])
{
float area;
float r=Float.parseFloat(args[0]);
System.out.println("The radius of a circle is :"+r);
area (float)22/7*г*г;
System.out.println("The area of a circle is :"+area);
}
}

Sample Output:
C:\Program Files\Java\jdk1.8.0_202\bin>javac d:/JAVALAB/Ex3.java
Program Files\Java\jdk1.8.0_202\bin>d:
D:JAVALAB>java Ex3 7
The radius of a circle is :7.0
The area of a circle is : 154.0
D:JAVALAB>

Result:
Thus the above Java program is executed and the output is obtained.

EX NO : 4
Aim:
To write a java program to find the sum and average of your tenth standard
marks
10

Program:
import java.util.Scanner;
public class Ex4
{
public static void main(String args[])
{
// Create a scanner object to take input from the user
Scanner s = new Scanner(System.in);
// Initialize variables for sum and number of subjects
int marks[] = new int[5];
int sum = 0;
double average;
// Take marks input from the user
System.out.println("Enter your tenth standard marks one by one:");

for (int i = 0; i <=5; i++)


{
System.out.print("Subject " +( i+1) + ": ");

Marks[i] = s.nextInt();
sum += marks[i]; // Add the marks to the sum
}
// Calculate the average
average = sum / 5.0;
// Display the sum and average
System.out.println("Total marks: " + sum);
System.out.println("Average marks: " + average);
// Close the scanner object
scanner.close();
}
}
Sample Output:
C:\Program Files\Java\jdk1.8.0_202\bin>javac d:/JAVALAB/Ex4.java
Program Files\Java\jdk1.8.0_202\bin>d:
D:JAVALAB>java Ex4
Enter you tenth standard Marks one by one:
Subject 1: 78
Subject 2: 97
Subject 3: 100
Subject 4: 97
Subject 5: 85
TOTAL MARKS:457
AVERAGE MARKS:91.4

Result
Thus the above Java program is executed and the output is obtained.
EX NO : 5

Aim:
11

To Write a java program to sort 10 students names in alphabetical order using


bubble sort

Program:
import java.util.Scanner;
class Ex5
{
public static void main(String args[])
{
Scanners s= new Scanner(System.in);
String names[] = new String[10];
System.out.println("Enter 10 student names:");
for (int i=0; i<names,length; i++)
{
System.out.print("Name “+ (i + 1) + ": ");
names[i] = s.nextLine();
}
for (int i=0; i<names.length - 1; i++)
{
for (int j=0; j<names.length-1-i; j++)
{
if (names[j].compareTo(names[j + 1]) > 0)
{
String temp =names[j]:
names[j] =names[j+1];
names[j+1] =temp;
}
}
}
System.out.println("Student names in alphabetical order.");
for (int i=0; i<names.length; i++)
{
System.out.println(names[i]);
}
}
}

Sample Output
C:\Program Files\Java\jdk1.8.0_202\bin>javac d:/JAVALAB/Ex5.java
CAProgram Files/Java/jdk1.8.0_202\bin>d:
DAJAVALAB java Ex5
Enter 10 student names:
Name 1: Deepan
Name 2: Sounthar
Name 3: Ajay
Name 4: Kishore
Name 5: Danial
Name 6: Aniruth
Name 7: Muthu
Name 8: Ratheesh
12

Name 9: Abishek
Name 10: Shifan
Student names in alphabetical order:
Abishek
Ajay
Aniruth
DanialDeepan
Kishore
Muthu
Ratheesh
Shifan
Sounthar
D:JAVALAB

Result
Thus the above Java program is executed and the output is obtained.

EX NO : 6

Aim:
13

To Write a java program to collect student details using constructors

Program:
import java.util.Scanner;

class Student {
// Member variables
String name;
int rollNumber;
double marks;

// Constructor to initialize student details


public Student(String name, int rollNumber, double marks) {
this.name = name;
this.age = age;
this.rollNumber = rollNumber;
this.course = course;
}

// Method to display student details


public void displayDetails() {
System.out.println("Student Name: " + name);
System.out.println("Student Age: " + age);
System.out.println("Roll Number: " + rollNumber);
System.out.println("Course: " + course);
}
}

public class StudentDetails {

public static void main(String[] args) {


// Create a scanner object for input
Scanner scanner = new Scanner(System.in);

// Taking student details as input


System.out.print("Enter student name: ");
String name = scanner.nextLine();

System.out.print("Enter student Age: ");


int age = scanner.nextInt();

System.out.print("Enter student roll number: ");


int rollNumber = scanner.nextInt();

System.out.print("Enter student Course: ");


String course = scanner.nextLine();

// Creating a Student object using the constructor


Student student = new Student(name,age, rollNumber,course);
14

// Displaying the details of the student


System.out.println("\nStudent Details:");
student.displayDetails();

// Closing the scanner


scanner.close();
}
}

Output:

C:\Program Files\Java\jdk1.8.0_202\bin>javac d:/JAVALAB/Ex6.java


C:\Program Files\Java\jdk1.8.0_202/bin>d:
D:JAVALAB> java Ex6

Enter the student's name: Alice


Enter the student's roll number: 101
Enter the student's marks: 89.5

Student Details:
Student Name: Alice
Roll Number: 101
Marks: 89.5

Result
Thus the above Java program is executed and the output is obtained.

EX NO : 7

Aim:
15

To Write a Java program to calculate area of rectangle, triangle and square using
method overloading

PROGRAM:

import java.util.Scanner;
class Area
{
double findArea(double length, double breadth)
{
return length breadth;
}
public double findArea(double base, double height, boolean is Triangle)
{
return 0.5 base height;
}
public double findArea(double side)
{
return side * side;
}
}
class Ex7
{
public static void main(String args[])
{
Scanner scan new Scanner(System.in);
Area c =new Area():
double le,br,rect, ba,ht,tri,s,square;
System.out.println("Enter Length and Breadth of Rectangle:");
le=scan.nextInt();
br=scan.nextInt();
rect=c.findArea(le, br);
System.out.println("Enter Base and Height of Triangle:");
ba=scan.nextInt();
ht=scan.nextInt();
tri=c.findArea(ba, ht, true);
System.out.println("Enter Side of Square:");
s=scan.nextInt();
square =c.findArea(s);
System.out.println("Area of the rectangle: " + rect);
System.out.println("Area of the triangle:"+tri);
System.out.println("Area of the square:" + square);
}
}

Sample Output
C:\Program Files\Java\jdk1.8.0_202\bin>javac d:JAVALAB\Ex7.java
CAProgram Files\Java\jdk1.8.0_202\bin>d:
D:JAVALAB java Ex7
16

Enter Length and Breadth of Rectangle:


10.8
Enter Base and Height of Triangle:
10 20
Enter Side of Square:
10
Area of the rectangle: 80.0
Area of the triangle: 100.0
Area of the square: 100.0
D:JAVALAB>

Result
Thus the above Java program is executed and the output is obtained.

EX NO : 8

Aim:
17

To write a Java program to create a class called Shape with methods called
getPerimeter() and getArea() and to create a subclass called Circle that overrides the
getPerimeter() and getArea() methods to calculate the area and perimeter of a circle.

PROGRAM:
import java.util.Scanner;
// Abstract class Shape

class Shape
// Abstract method to calculate the perimeter
double getPerimeter()
{
return 0.0;
}
// Abstract method to calculate the area

double getArea()
{
return 0.0;
}
}
// Circle class extends Shape

class Circle extends Shape


{
// Member variable for radius
double radius;
// Constructor to initialize the radius of the circle

Circle(double r)
{
radius=r;
}
// Override getPerimeter() to calculate the perimeter of the circle double
getPerimeter()
{
return 2 *22.0/7.0 *radius; // Perimeter of a circle: 2 * π * radius
}
// Override getArea() to calculate the area of the circle double getArea()
getArea()
{
return 22.0/7.0 * radius * radius;
}
}
class Ex8
{
public static void main(String args[])
18

{
Scanner scan=new Scanner(System.in);
System.out.println("Enter Radius of Circle:");
double rad=scan.nextFloat();
Circle circle=new Circle(rad);
System.out.println("Perimeter of the circle: " + circle.getPerimeter());
System.out.println("Area of the circle: " + circle.getArea());
}
}

Sample Output
C:\Program Files\Java\jdk1.8.0_202 bin javac d\JAVALAB Ex8.java
CAProgram Files\Java\jdk1.8.0 202 bind
D:JAVALAB java Ex8
Enter Radius of Circle:
7
Perimeter of the circle: 44.0
Area of the circle: 154.0

Result
Thus the above Java program is executed and the output is obtained.

EX NO : 9
Aim:
19

To Write a java program to create an interface Shape with getArea() method. Create
three classes Rectangle , Circle, and Triangle that implement the shape interface.
Implement the getArea() method for each of three classes.

PROGRAM:

import java.util.Scanner;
interface Shape
{
double getArea();
}
class Rectangle implements Shape
{
double length;
double breadth;
Rectangle(double 1, double b)
{
length = 1;
breadth = b;
}
public double get Area()
{
return length*breadth;
}
}
class circle implements shape
{
double radius;
circle(double r)
{
radius=r.
}
public double getarea()
{
return 22.0/7.0*radius*radius;
}
}
class triangle implements shape
{
double base;
double height;
Triangle(double b,double h)
{
base=b;
height=h;
}
public double hetArea()
{
return 0.5*base*height;
}
20

}
class Ex9
{
public static void main(string args[])
{
Scanner scan=new Scanner(system.in);
System.out.println("Enter Length and Breadth of Rectangle:");
double le=scan.nextInt();
double br=scan.nextInt();
Shape rectangle=new Rectangle(le,br);
System.out.println("Enter Radius of Circle:");
double rad=scan.nextInt();
Shape circle=new Circle(rad);
System.out.println("Enter Base and Height of Triangle");
double ba=scan.nextInt();
double ht=scan.nextInt();
Shape triangle=new Triangle(ba,ht);
System.out.println("Area of the rectangle:" +rectangle.getArea());
System.out.println("Area of the circle: " + circle.getArea());
System.out.println("Area of the triangle: " + triangle.getArea());
}
}
Sample Output
C:\Program Files\Java\jdk1.8.0_202\bin>javac d:\JAVALAB\Ex9.java
C:\Program Files/Java\jdk1.8.0_202\bin>>d:
D:JAVALAB>java Ex9
Enter Length and Breadth of Rectangle:
10 20
Enter Radius of Circle:
14
Enter Base and Height of Triangle:
20 10
Area of the rectangle: 200.0
Area of the circle: 616.0
Area of the triangle: 100.0
D:JAVALAB

Result
Thus the above Java program is executed and the output is obtained.

EX NO : 10
Aim:
21

To Write a Java program to create a panel with three buttons, labeled red, blue and
yellow, so that clicking each button results in the background color changing to the
appropriate color

PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex10
{
public static void main(String args[])
{
// Create the main frame
JFrame frame=new JFrame("Color Changer");
frame setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the panel
JPanel panel=new JPanel();
// Create buttons
JButtonredButton=new JButton("Red");
JButtonblueButton= new JButton("Blue");
JButtonyellowButton = new JButton("Yellow");

// Add buttons to the panel


panel.add(redButton);
panel.add(blueButton);
paneladd(yellowButton);

// Define the action listener


ActionListener colorChanger new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource()=redButton)
{
panel.setBackground(Color.RED);
}
else if (e.getSource() blueButton)
{
panel.setBackground(Color.BLUE);
}
else if (e.getSource() yellowButton);
}
panel setBackground(Color. YELLOW);
}
}
};

//Attach the action listener to the buttons


22

redButton.addActionListener(colorChanger);
blueButton.addActionListener(colorChanger);
yellowButton.addActionListener(colorChanger);

// Add the panel to the frame


frame.add(panel);

// Make the frame visible


frame.setVisible(true);
}
}

Result
Thus the above Java program is executed and the output is obtained

You might also like