Java Lab Manual-R23-Book
Java Lab Manual-R23-Book
PREPARED BY
M.SRINIVASAPRIYA, B.E/ P.JAYANTHI RANI, M.E
LECTURER
2
PART A
4 USING ARRAYS
PART B
6 USING CONSTRUCTORS
9 IMPLEMENTING INTERFACE
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 4: Write a Java program to find the sum and average of your tenth standard
marks.
method overloading.
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
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
TOTAL PERIODS 75
Printer
2.Software(s) Requirement:
Windows Operating System
Net Beans 8.0.2 / 8.2 with JDK.
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
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;
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:
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:");
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
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
Program:
import java.util.Scanner;
class Student {
// Member variables
String name;
int rollNumber;
double marks;
Output:
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
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
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");
redButton.addActionListener(colorChanger);
blueButton.addActionListener(colorChanger);
yellowButton.addActionListener(colorChanger);
Result
Thus the above Java program is executed and the output is obtained