0% found this document useful (0 votes)
3 views20 pages

Java Programming Manual

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)
3 views20 pages

Java Programming Manual

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/ 20

Ex No 1.

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


into Fahrenheit.

import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the temperature in Celsius
System.out.print("Enter the temperature in Celsius: ");
double celsius = scanner.nextDouble();
// Convert Celsius to Fahrenheit
double fahrenheit = (celsius * 9 / 5) + 32;
// Display the result
System.out.println("The temperature in Fahrenheit is: " + fahrenheit);

// Close the scanner


scanner.close();
}
}
Output :
Enter the temperature in Celsius: 25
The temperature in Fahrenheit is: 77.0
Ex No 2. Write a program to read 2 integers and find the largest number
using conditional operator.

import java.util.Scanner;

public class LargestNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
int largest = (num1 > num2) ? num1 : num2;
System.out.println("Largest number: " + largest);
scanner.close();
}
}
Output :
Enter first number: 15
Enter second number: 20
Largest number: 20
Ex No 3: Write a Java program to implement command line arguments.

public class CommandLineExample {


public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Command-line arguments:");
for (int i = 0; i < args.length; i++) {
System.out.println("Argument " + (i + 1) + ": " + args[i]);
}
} else {
System.out.println("No command-line arguments were provided.");
}
}
}
Output :
java CommandLineExample Hello World Java
Command-line arguments:
Argument 1: Hello
Argument 2: World
Argument 3: Java
java CommandLineExample
No command-line arguments were provide
Ex No 4: Write a Java program to find the sum and average of your tenth
standard marks.

import java.util.Scanner;

public class MarksSumAndAverage {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of subjects: ");


int numSubjects = scanner.nextInt();
int[] marks = new int[numSubjects];
int sum = 0;

// Input marks for each subject


for (int i = 0; i < numSubjects; i++) {
System.out.print("Enter marks for subject " + (i + 1) + ": ");
marks[i] = scanner.nextInt();
sum += marks[i];
}

// Calculate average
double average = (double) sum / numSubjects;

// Display the result


System.out.println("Total Marks: " + sum);
System.out.println("Average Marks: " + average);

scanner.close();
}
}
Output :
Enter the number of subjects: 5
Enter marks for subject 1: 85
Enter marks for subject 2: 90
Enter marks for subject 3: 88
Enter marks for subject 4: 92
Enter marks for subject 5: 87
Total Marks: 442
Average Marks: 88.4
Ex No 5: Write a Java Program to sort 10 student names in alphabetical order
using bubble sort

import java.util.Scanner;

public class SortStudentNames {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Array to store 10 student names
String[] names = new String[10];
// Input 10 student names
System.out.println("Enter 10 student names:");
for (int i = 0; i < 10; i++) {
System.out.print("Name " + (i + 1) + ": ");
names[i] = scanner.nextLine();
}
// Bubble sort algorithm to sort names
for (int i = 0; i < names.length - 1; i++) {
for (int j = 0; j < names.length - i - 1; j++) {
if (names[j].compareToIgnoreCase(names[j + 1]) > 0) {
// Swap names[j] and names[j + 1]
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
// Display sorted names
System.out.println("\nStudent names in alphabetical order:");
for (String name : names) {
System.out.println(name);
}
scanner.close();
}
}

Output :
Enter 10 student names:
Name 1: Ramesh Name 2: Suresh Name 3: Karthik
Name 4: Arjun Name 5: Priya Name 6: Meena
Name 7: Nithya Name 8: Lakshmi Name 9: Bala Name 10: Kavitha
Student names in alphabetical order:
Arjun
Bala
Karthik
Kavitha
Lakshmi
Meena
Nithya
Priya
Ramesh
Suresh
Ex No 6 : Write a Java program to collect student details using constructors.

import java.util.Scanner;

class Student {
// Fields to store student details
String name;
int age;
int rollNumber;
String course;
// Constructor to initialize student details
public Student(String name, int age, int rollNumber, String course) {
this.name = name;
this.age = age;
this.rollNumber = rollNumber;
this.course = course;
}
// Method to display student details
public void displayDetails() {
System.out.println("Student Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Roll Number: " + rollNumber);
System.out.println("Course: " + course);
}
}
public class StudentDetails {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Collect student details from the user
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();
scanner.nextLine(); // Consume the leftover newline
System.out.print("Enter student course: ");
String course = scanner.nextLine();
// Create a Student object using the constructor
Student student = new Student(name, age, rollNumber, course);
// Display the student details
student.displayDetails();
scanner.close();
}
}
Output :
Enter student name: Arun
Enter student age: 20
Enter student roll number: 101
Enter student course: Computer Science
Student Details:
Name: Arun
Age: 20
Roll Number: 101
Course: Computer Science
Ex No 7: Write a Java program to calculate area of rectangle, triangle and
square using method overloading.

class AreaCalculator {
// Method to calculate the area of a rectangle
public double calculateArea(double length, double breadth) {
return length * breadth;
}

// Method to calculate the area of a triangle


public double calculateArea(double base, double height, boolean isTriangle) {
return 0.5 * base * height;
}

// Method to calculate the area of a square


public double calculateArea(double side) {
return side * side;
}
}

public class AreaCalculation {


public static void main(String[] args) {
AreaCalculator calculator = new AreaCalculator();

// Calculate area of rectangle


double rectangleArea = calculator.calculateArea(10.0, 5.0);
System.out.println("Area of Rectangle: " + rectangleArea);

// Calculate area of triangle


double triangleArea = calculator.calculateArea(8.0, 6.0, true);
System.out.println("Area of Triangle: " + triangleArea);

// Calculate area of square


double squareArea = calculator.calculateArea(4.0);
System.out.println("Area of Square: " + squareArea);
}
}
Output :
Area of Rectangle: 50.0
Area of Triangle: 24.0
Area of Square: 16.0
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.

class Shape {
// Method to get perimeter (default implementation)
public double getPerimeter() {
return 0.0;
}
// Method to get area (default implementation)
public double getArea() {
return 0.0;
}
}
// Subclass Circle that extends Shape
class Circle extends Shape {
private double radius;
// Constructor to initialize radius
public Circle(double radius) {
this.radius = radius;
}
// Override method to calculate perimeter of the circle
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
// Override method to calculate area of the circle
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
public class ShapeDemo {
public static void main(String[] args) {
// Create a Circle object with radius 5.0
Circle circle = new Circle(5.0);
// Display the perimeter and area of the circle
System.out.println("Perimeter of Circle: " + circle.getPerimeter());
System.out.println("Area of Circle: " + circle.getArea());
}
}
Output :
Perimeter of Circle: 31.41592653589793
Area of Circle: 78.53981633974483
Ex No 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.

// Shape interface with getArea method


interface Shape {
double getArea(); // Abstract method to calculate area
}

// Rectangle class that implements Shape


class Rectangle implements Shape {
private double length;
private double breadth;

// Constructor to initialize length and breadth


public Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}

// Implement the getArea method for Rectangle


@Override
public double getArea() {
return length * breadth;
}
}

// Circle class that implements Shape


class Circle implements Shape {
private double radius;

// Constructor to initialize radius


public Circle(double radius) {
this.radius = radius;
}

// Implement the getArea method for Circle


@Override
public double getArea() {
return Math.PI * radius * radius;
}
}

// Triangle class that implements Shape


class Triangle implements Shape {
private double base;
private double height;

// Constructor to initialize base and height


public Triangle(double base, double height) {
this.base = base;
this.height = height;
}

// Implement the getArea method for Triangle


@Override
public double getArea() {
return 0.5 * base * height;
}
}

public class ShapeDemo {


public static void main(String[] args) {
// Create objects of Rectangle, Circle, and Triangle
Rectangle rectangle = new Rectangle(10.0, 5.0);
Circle circle = new Circle(7.0);
Triangle triangle = new Triangle(6.0, 4.0);

// Display the area of each shape


System.out.println("Area of Rectangle: " + rectangle.getArea());
System.out.println("Area of Circle: " + circle.getArea());
System.out.println("Area of Triangle: " + triangle.getArea());
}
}

Output :

Area of Rectangle: 50.0


Area of Circle: 153.93804002589985
Area of Triangle: 12.0
Ex No 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.

import java.awt.*;
import java.awt.event.*;

public class ColorChangePanel extends Frame implements ActionListener {


// Create buttons for each color
Button redButton, blueButton, yellowButton;
public ColorChangePanel() {
// Set the title of the frame
setTitle("Color Change Panel");
// Set the layout of the panel
setLayout(new FlowLayout());
// Create buttons and add them to the frame
redButton = new Button("Red");
blueButton = new Button("Blue");
yellowButton = new Button("Yellow");
add(redButton);
add(blueButton);
add(yellowButton);
// Add action listeners to the buttons
redButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
// Set the size of the frame
setSize(400, 200);

// Set the frame to be visible


setVisible(true);

// Close the program when the window is closed


addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}

// Action listener method to change background color based on button clicked


@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == redButton) {
setBackground(Color.RED); // Change background to red
} else if (e.getSource() == blueButton) {
setBackground(Color.BLUE); // Change background to blue
} else if (e.getSource() == yellowButton) {
setBackground(Color.YELLOW); // Change background to yellow
}
}

public static void main(String[] args) {


new ColorChangePanel(); // Create the ColorChangePanel object
}
}

Output :

|[ Red Button ] [ Blue Button ] [ Yellow Button ] |


-------------------------------------------------
* Initially, the background will be white (default).
* After clicking "Red", the background will turn red.
* After clicking "Blue", the background will turn blue.
* After clicking "Yellow", the background will turn yellow.

You might also like