OOP-I Programs
OOP-I Programs
Programs
import java.util.*;
class MyProgram {
public static void main(String[] args) {
int x;
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
if (x > 0) {
System.out.println("Number is positive.");
} else if (x < 0) {
System.out.println("Number is negative.");
} else {
System.out.println("Number is zero.");
}
sc.close();
}
}
Q.2 Write a program which reads two numbers and based on the difference
between them, prints either the following message DIFFERENCE IS POSITIVE or
DIFFERENCE IS NEGATIVE.
import java.util.Scanner;
import java.util.*;
public class MyProgram {
public static void main (String[] args) {
int x;
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
if( x % 2 == 1 ) {
System.out.println("number is a odd");
}
if( x % 2 == 0 ) {
System.out.println("number is a even");
}
sc.close();
}
}
Q.4 A year is entered through the keyboard, write a program to determine
whether the year is leap year or not.
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.close();
}
}
Q.5 Write a program to check whether a triangle is valid or not, when the three
angles of the triangle are entered through the keyboard. A triangle is valid if the
sum of all the three angles is equal to 180 degrees.
import java.util.Scanner;
class Fibonacci {
Q.7 Write a program to find the maximum number from the given three integers.
import java.util.Scanner;
scanner.close();
}
}
Q.8 Write a menu driven program that allows users to enter five numbers and
then choose between finding the smallest, largest, sum or average. Use switch
case to determine the action to take. Provide an error message if an invalid
choice is entered.
import java.util.Scanner;
import java.util.*;
class WhileDemo {
public static void main(String[] args) {
int n, i = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:");
n = sc.nextInt();
while (i <= n) {
if (i % 2 == 1)
System.out.println(i);
i++;
}
sc.close();
}
}
import java.util.*;
class WhileDemo {
public static void main(String[] args) {
int i = 1, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number: ");
n = sc.nextInt();
System.out.print("Factors: ");
while (i <= n) {
if (n % i == 0)
System.out.print(i + " ");
i++;
}
sc.close();
}
}
Q.11 Write a program that calculates and prints the sum of the even integers
from 1 to 10.
Q.12 Write a program to check whether the entered number is prime or not.
import java.util.Scanner;
import java.util.*;
class ArrayDemo1 {
public static void main(String[] args) {
int i, n;
int[] a = new int[5]; // Creating an integer array of length 5
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array Length:");
n = sc.nextInt(); // Taking input for the length of the array
for (i = 0; i < n; i++) {
System.out.print("Enter a[" + i + "]:");
a[i] = sc.nextInt(); // Taking input for each element of the array
}
for (i = 0; i < n; i++)
System.out.println(a[i]); // Printing out each element of the array
}
}
Q.14 Write a program to print elements of an array in reverse order.
import java.util.*;
System.out.println("Reverse Array");
// Printing out the elements of the array in reverse order
for (i = n - 1; i >= 0; i--)
System.out.println(a[i]);
}
}
Q.15 Write a program to count positive number, negative number and zero from
an array of n size.
import java.util.*;
class ArrayDemo1 {
public static void main(String[] args) {
int n, pos = 0, neg = 0, z = 0;
int[] a = new int[5]; // Creating an integer array of length 5
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array Length:");
n = sc.nextInt(); // Taking input for the length of the array
Q.16 Write a program to find the smallest and largest element from an array.
import java.util.*;
class MethodDemo {
public static void main(String[] args) {
int a = 10, b = 20, c;
MethodDemo md = new MethodDemo();
c = md.add(a, b); // Calling the add method with parameters a and b
System.out.println("a + b = " + c);
}
// Method to add two integers
int add(int i, int j) {
return i + j; // Returning the sum of the two integers
}
}
import java.util.*;
import java.util.Scanner;
Q.23 Write a program using class Person to display name and age with method.
class MyProgram {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
p1.name = "modi";
p1.age = 71;
p2.name = "bachchan";
p2.age = 80;
p1.displayName();
p2.displayName();
p1.displayAge();
p2.displayAge();
}
}
class Person {
String name;
int age;
Q.24 Write a program using class Rectangle to calculate the area with method.
import java.util.*;
class MyProgram {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
Scanner sc = new Scanner(System.in);
System.out.print("Enter height: ");
r1.height = sc.nextFloat();
System.out.print("Enter width: ");
r1.width = sc.nextFloat();
r1.calArea();
}
}
class Rectangle {
float height;
float width;
public void calArea() {
System.out.println("Area = " + height * width);
}
}
Q.25 Design a java class Rectangle which contains following field and methods:
(i) Field: length, width: int (ii) Default Constructor: initialize all fields with 0 value
(iii) Method: int getArea() will return area of rectangle.
// Default Constructor
public Rectangle() {
// Initialize all fields with 0 value
this.length = 0;
this.width = 0;
}
// Parameterized Constructor
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
// Method to calculate the area of the rectangle
public int getArea() {
return length * width;
}
// Getters and Setters
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public static void main(String[] args) {
// Create a Rectangle object using default constructor
Rectangle rectangle1 = new Rectangle();
// Display area of rectangle
System.out.println("Area of rectangle1: " + rectangle1.getArea());
// Create a Rectangle object using parameterized constructor
Rectangle rectangle2 = new Rectangle(5, 4);
// Display area of rectangle
System.out.println("Area of rectangle2: " + rectangle2.getArea());
}
}
Q.26 Create a class called Employee that includes: I. Three instance variables— id
(type String), name (type String) and monthly_salary (double). II. A default
constructor that initializes the three instance variables. III. A setter and a getter
method for each instance variable (for example for id variable void setId(String
id), String getId( )). IV. displayEmployee() method for displaying employee details.
Write a driver class named EmployeeTest that demonstrates class Employee’s
capabilities. Create two Employee objects and display each object’s yearly salary.
Then give each Employee a 10% raise and display each Employee’s yearly salary
again.
class Employee {
private String id;
private String name;
private double monthlySalary;
// Default constructor
public Employee() {
id = "";
name = "";
monthlySalary = 0.0;
}
// Setter methods
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
import java.util.Random;
Q.28 Write a method to enter two integers and compute the gcd of two integers.
Q.29 Write a test program that prompts the user to enter ten numbers, invoke a
method to reverse the numbers, and display the numbers.
import java.util.Scanner;
Q.30 The employee list for a company contains employee code, name,
designation and basic pay. The employee is given HRA of 10% of the basic pay
and DA of 45% of the basic pay. The total pay of the employee is calculated as
Basic pay+HRA+ DA. Write a class to define the details of the employee. Write a
constructor to assign the required initial values. Add a method to calculate HRA,
DA and Total pay and print them out. Write another class with a main method.
Create objects for three different employees and calculate the HRA, DA and total
pay.
class Employee {
private int employeeCode;
private String name;
private String designation;
private double basicPay;
Q.31 Write a Java program to count the number of words in a given string.
public class WordCounter {
public static void main(String[] args) {
String str = "Hello world, how are you?";
// Call the method to count the number of words
int wordCount = countWords(str);
class Cube {
private double side;
// Constructor to initialize the side of the cube
public Cube(double side) {
this.side = side;
}
// Method to calculate the surface area of the cube
public double calculateSurfaceArea() {
return 6 * side * side;
}
}
public class CubeAreaCalculator {
public static void main(String[] args) {
// Create two cube objects with different side lengths
Cube cube1 = new Cube(5.0);
Cube cube2 = new Cube(3.0);
// Calculate the surface area of each cube
double surfaceArea1 = cube1.calculateSurfaceArea();
double surfaceArea2 = cube2.calculateSurfaceArea();
// Display the surface area of each cube
System.out.println("Surface area of cube 1: " + surfaceArea1);
System.out.println("Surface area of cube 2: " + surfaceArea2);
}
}