Java R23 Merged
Java R23 Merged
PART A
Algorithm:
Program:
import java.util.Scanner;
}
}
Output:
Result:
Aim: To write a program to read 2 integers and find the largest number using
conditional operator.
Algorithm:
Program:
import java.util.Scanner;
Output:
Result:
Algorithm:
1. Open Netbeans IDE.
2. Select File -> New project and rename the project as
‘CommandLineArguments’ and click ‘Finish’.
3. Read the value of radius of circle from command line.
4. Compute the area of the circle using the formula 3.14*radius*radius.
5. Print the area of the circle.
6. Right click the project folder and click ‘Properties’. Select ‘Run’ under
‘Categories’ and enter area of the circle in ‘Arguments’ and click ‘Ok’.
7. Save the program by clicking Ctrl+S.
8. Run the project by clicking Run->Run Project.
Program:
double radius;
double area;
radius=Double.parseDouble(args[0]);
area=3.14*radius*radius;
}
Output:
Result:
Aim: To write a program to find the sum and average of tenth standard marks.
Algorithm:
1. Open Netbeans IDE.
2. Select File -> New project and rename the project as ‘MarksCalculator’ and
click ‘Finish’.
3. Read the number of subjects.
4. Read the marks of all subjects using ‘for’ loop.
5. Calculate the total mark by summing up the marks of all the subject.
6. Calculate the average by dividing the total marks by the number of subjects.
7. Print the total marks and the average.
8. Save the program by clicking Ctrl+S.
9. Run the project by clicking Run->Run Project.
Program:
import java.util.Scanner;
int sum = 0;
sum += marks;
Output:
Result:
Thus, the program to find the sum and average of tenth standard marks is
executed successfully.
Ex. No: 5 BUBBLE SORT
Algorithm:
1. Open Netbeans IDE.
2. Select File -> New project and rename the project as ‘BubbleSort’ and
click ‘Finish’.
3. Initialization:
• Start with the first element in the array.
• Assume the array has n elements.
4. Pass-Through the List:
• For each element in the array (from the first to the second
last):
• Compare the current element with the next element.
• If the current element is greater than the next element,
swap them.
5. Repeat Passes:
• Repeat the above step for all elements in the array.
• After each pass, the largest unsorted element will have
“bubbled up” to its correct position at the end of the array.
• Decrease the range of comparison by one each time (since
the last elements are already sorted).
6. Stop When Sorted:
• Continue repeating the passes until no more swaps are
needed (i.e., the array is sorted).
7. Save the program by clicking Ctrl+S.
8. Run the project by clicking Run->Run Project.
Program:
import java.util.Scanner;
names[i] = scanner.nextLine();
names[j + 1] = temp;
System.out.println(name);
}
Output:
Result:
Algorithm:
1. Open Netbeans IDE.
2. Select File -> New project and rename the project as ‘StudentDetails’
and click ‘Finish’.
3. Define the Student class with student details such as name, rollnumber,
grade and marks etc.
4. Define the Constructor to initialize the student details.
5. Define the method to display the student details.
6. Define the Main class StudentDetails and read the student details.
7. Create the student object using the constructor.
8. Display the student details using the display() method.
9. Save the program by clicking Ctrl+S.
10. Run the project by clicking Run->Run Project.
Program:
import java.util.Scanner;
class Student {
}
Output:
Result:
Aim: To write a Java program to calculate area of rectangle, triangle and square
using method overloading.
Algorithm:
1. Open Netbeans IDE.
2. Select File -> New project and rename the project as
‘MethodOverloadingExample’ and click ‘Finish’.
3. Define the class AreaCalculator.
4. Define the method calculateArea() to calculate area of the rectangle
using the formula length * breadth.
5. Define the method calculateArea() to calculate area of the triangle using
the formula 0.5 * base * height.
6. Define the method calculateArea() to calculate area of the square using
the formula side * side.
7. In the main method of ‘MethodOverloadingExample’ class, create an
object for ‘AreaCalculator’ class using new operator.
8. Call the calculateArea() method using dot operator to calculate area of
the rectangle and print the ‘rectangleArea’.
9. Call the calculateArea() method using dot operator to calculate area of
the triangle and print the ‘triangleArea’.
10. Call the calculateArea() method using dot operator to calculate area of
the square and print the ‘squareArea’.
11. Save the program by clicking Ctrl+S.
12. Run the project by clicking Run->Run Project.
Program:
class AreaCalculator {
}
Output:
Result:
Aim: To 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.
Algorithm:
1. Open Netbeans IDE.
2. Select File -> New project and rename the project as ‘ShapeDemo’ and
click ‘Finish’.
3. Define the Base class ‘Shape’ and define the methods baseMethod(),
getPerimeter() and getArea().
4. Define the Subclass ‘Circle’ and extend it from ‘Shape’ class using
‘extends’ keyword.
5. Define the constructor for the subclass ‘Circle’ to initialize the radius.
6. Define the getPerimeter() method to calculate perimeter of the circle
using the formula 2* Math.PI * radius.
7. Define the getArea() method to calculate area of the circle using the
formula Math.PI * radius * radius.
8. Define the Main class ‘ShapeDemo’ and create the object for the ‘Circle’
by passing radius.
9. Print the radius of the Circle.
10. Print the perimeter of the circle by calling the circle.getPerimeter()
method using ‘dot’ operator .
11. Print the area of the circle by using call circle.getArea() method using
‘dot’ operator.
12. Save the program by clicking Ctrl+S.
13. Run the project by clicking Run->Run Project.
Program:
package shapedemo;
class Shape {
}
public double getPerimeter() {
return 0.0;
return 0.0;
this.radius = radius;
@Override
@Override
circle.baseMethod();
}
Output:
Result:
Thus, the program to create a subclass called Circle that overrides the
getPerimeter() and getArea() methods to calculate the area and perimeter of a
circle is executed successfully.
Ex.No:9 JAVA INTERFACE
Algorithm:
1. Open Netbeans IDE.
2. Select File->New project and rename the project as
‘ShapeInterfaceDemo’and click ‘Finish’.
3. Define the Interface ‘Shape’ and define the Abstract method
‘getArea()’.
4. Define the Rectangle class and implement the Shape interface, define
the Constructor to initialize length and breadth and Implement the
‘getArea()’ method to calculate area of the rectangle.
5. Define the Circle class and implement the Shape interface, define the
constructor to initialize radius and Implement the ‘getArea()’ method
to calculate area of the circle.
6. Define the Triangle class and implement the Shape interface, define
the constructor to initialize base and height and Implement the
‘getArea()’ method to calculate area of the triangle.
7. Define the Main class ‘ShapeInterfaceDemo’ and create object for
Rectangle, Circle and Triangle classes.
8. Print the Area of Rectangle by calling the ‘rectangle.getArea()’
method.
9. Print the Area of Circle by calling the ‘circle.getArea()’ method.
10. Print the Area of Triangle by calling the ‘triangle.getArea()’ method.
11. Save the program by clicking Ctrl+S.
12. Run the project by clicking Run->Run Project.
Program:
package shapeinterfacedemo;
interface Shape {
double getArea();
this.length = length;
this.breadth = breadth;
@Override
this.radius = radius;
@Override
this.base = base;
this.height = height;
@Override
}
Output:
Result:
Aim: 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.
Algorithm:
1. Open Netbeans IDE.
2. Select File -> New project and rename the project as ‘ColorChangePanel’
and click ‘Finish’.
3. Import Swing and AWT packeges
4. Create a class ColorChangerPanel which extends JFrame class.
5. Define the constructor to set up GUI.
6. Create a panel to hold the buttons and create the buttons Red, Blue and
Yellow.
7. Add action listeners to the buttons.
8. Add buttons to the panel.
9. Add the panel to the frame and make the frame visible.
10. Create an instance of the ColorChangerPanel class.
11. Save the program by clicking Ctrl+S.
12. Run the project by clicking Run->Run Project.
Program:
package colorchangerpanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public ColorChangerPanel() {
setTitle("Color Changer Panel");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel;
panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");
redButton.addActionListener((ActionEvent e) -> {
panel.setBackground(Color.RED);
});
blueButton.addActionListener((ActionEvent e) -> {
panel.setBackground(Color.BLUE);
});
yellowButton.addActionListener((ActionEvent e) -> {
panel.setBackground(Color.YELLOW);
});
panel.add(redButton);
panel.add(blueButton);
panel.add(yellowButton);
add(panel);
setVisible(true);
}
Output:
Result:
Thus, the 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 is executed successfully and the output is
verified.