KEMBAR78
Java2 Aman | PDF | Constructor (Object Oriented Programming) | Programming
0% found this document useful (0 votes)
8 views18 pages

Java2 Aman

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)
8 views18 pages

Java2 Aman

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

NAME → Aman Mishra

CLASS →IT – A‘
ROLL-NO → 2300290130027
JAVA OOPS LAB ASSIGNMENT – 02
Ques-01) WAP to find out the sum of command line
arguments.
public class Cla
{

public static void main(String[] args) {

int sum = 0;
for (String arg : args) {

try {

sum += Integer.parseInt(arg);

} catch (NumberFormatException e) {

System.out.println("Invalid number: " + arg);

}
System.out.println("Sum of command line arguments: " + sum);

}
Ques-02) WAP to count the number of characters in a given
string, to reverse the string and check whether it is
palindrome or not ?
import java.util.Scanner;

public class MatrixOperations {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


// Input the string
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Count the number of characters
int charCount = input.length();
System.out.println("Number of characters: " + charCount);
// Reverse the string
String reversed = new StringBuilder(input).reverse().toString();
System.out.println("Reversed string: " + reversed);
// Check if the string is a palindrome
if (input.equalsIgnoreCase(reversed)) {

System.out.println("The string is a palindrome.");

} else {
System.out.println("The string is not a palindrome.");

}
scanner.close();

}
}

Ques-03) WAP to Find largest and smallest numbers in an


array.
public class LargestAndSmallest {

public static void main(String[] args) {

int[] numbers = {12, 45, 2, 10, 34, 89, 1};


int largest = numbers[0];
int smallest = numbers[0];
for (int num : numbers) {

if (num > largest) {


largest = num;

}
if (num < smallest) {

smallest = num;

}
System.out.println("Largest number: " + largest);
System.out.println("Smallest number: " + smallest);

}
)
Ques-04Java Program to Find the Sum and Product of
Elements in a Row/Column of a Matrix.
import java.util.Scanner;

public class MatrixOperations {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Input matrix dimensions


System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int cols = scanner.nextInt();

int[][] matrix = new int[rows][cols];

// Input matrix elements


System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {


matrix[i][j] = scanner.nextInt();

}
}

// Input row/column choice


System.out.print("Enter 1 for row operations or 2 for column
operations: ");
int choice = scanner.nextInt();

if (choice == 1) {

System.out.print("Enter the row number (0-based index): ");


int row = scanner.nextInt();
if (row>= 0&&row<rows) {

int sum = 0;
int product = 1;
for (int j = 0; j < cols; j++) {
sum += matrix[row][j];
product *= matrix[row][j];

}
System.out.println("Sum of elements in row " + row + ": " +
sum);

System.out.println("Product of elements in row " + row + ": " +


product);

} else {

System.out.println("Invalid row number.");


}

} else if (choice == 2) {
System.out.print("Enter the column number (0-based index): ");
int col = scanner.nextInt();
if (col>= 0&&col<cols) {

int sum = 0;
int product = 1;
for (int i = 0; i < rows; i++) {

sum += matrix[i][col];
product *= matrix[i][col];

}
System.out.println("Sum of elements in column " + col + ": " +
sum);

System.out.println("Product of elements in column " + col + ": "


+ product);

} else {

System.out.println("Invalid column number.");


}

} else {
System.out.println("Invalid choice.");

scanner.close();

}
Ques-05) WAP to create class Number with only one private
instance variable as a double primitive type, include the
following methods isZero(), isPositive(), isNegative( ),
isOdd( ), isEven( ), isPrime(), isAmstrong() in this class and
all above methods should return boolean primitive type like
for isPositive() should return “Positive = True”.
public class Fifth {

public static void main(String[] args) {


Number number = new Number(153);
System.out.println("Zero = " + number.isZero());
System.out.println("Positive = " + number.isPositive());
System.out.println("Negative = " + number.isNegative());
System.out.println("Odd = " + number.isOdd());
System.out.println("Even = " + number.isEven());
System.out.println("Prime = " + number.isPrime());
System.out.println("Armstrong = " + number.isArmstrong());

}
}
class Number {
private double value;
public Number(double value) {
this.value = value;
}
public boolean isZero() {
return value == 0;
}
public boolean isPositive() {
return value>0;
}
public boolean isNegative() {
return value<0;
}
public boolean isOdd() {
return value % 2 != 0;
}
public boolean isEven() {
return value % 2 == 0;
}
public boolean isPrime() {
if (value <= 1 || value != (int) value) {
return false;
}
int intValue = (int) value;
for (int i = 2; i <= Math.sqrt(intValue); i++) {
if (intValue % i == 0) {
return false;
}
}
return true;
}
public boolean isArmstrong() {
if (value != (int) value) {
return false;
}
int intValue = (int) value;
int sum = 0, temp = intValue, digits = 0;
while (temp>0) {
temp /= 10;
digits++;
} temp =
intValue; while
(temp>0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}
return sum == intValue;
}
}

Ques-06) WAP to illustrate all types of constructors.


class ConstructorDemo {

int id;
String name;
// Default Constructor
ConstructorDemo() {

id = 0;
name = "Default Name";
System.out.println("Default Constructor Called");

// Parameterized Constructor

ConstructorDemo(int id, String name) {


this.id = id;
this.name = name;
System.out.println("Parameterized Constructor Called");

// Copy Constructor

ConstructorDemo(ConstructorDemo obj) {
this.id = obj.id;
this.name = obj.name;
System.out.println("Copy Constructor Called");

void display() {
System.out.println("ID: " + id + ", Name: " + name);
}
public static void main(String[] args) {

// Using Default Constructor


ConstructorDemo obj1 = new ConstructorDemo();
obj1.display();
// Using Parameterized Constructor
ConstructorDemo obj2 = new ConstructorDemo(101, "John Doe");
obj2.display();
// Using Copy Constructor
ConstructorDemo obj3 = new ConstructorDemo(obj2);
obj3.display();

Ques-07) Write a program to demonstrate static variables,


methods, and blocks.
public class StaticDemo

static int staticCounter;


static {

staticCounter = 10;
System.out.println("Static block executed. Initial staticCounter: " +
staticCounter);
}
static void incrementCounter() {

staticCounter++;
System.out.println("Static method called. Updated staticCounter: " +
staticCounter);
}
void displayCounter() {

System.out.println("Current staticCounter: " + staticCounter);

}
public static void main(String[] args) {

System.out.println("Main method started.");


incrementCounter();
StaticDemo demo = new StaticDemo();
demo.displayCounter();
System.out.println("Accessing staticCounter directly: " +
staticCounter);

Ques-08) WAP to illustrate use of this keyword.


class Example {

private String name;


Example(String name) {
this.name = name;

}
void display() {

System.out.println("Name: " + this.name);

}
Example setName(String name) {

this.name = name;
return this;

}
public static void main(String[] args) {

Example obj = new Example("John");


obj.display();
obj.setName("Doe").display();

}
Ques-09) WAP to create a class named Shape and create
three subclasses Circle, Triangle and Square, each class has
two-member functions named draw () and erase ().
Implement this concept using polymorphism

class Shape {

void draw() {

System.out.println("Drawing a shape");

}
void erase() {

System.out.println("Erasing a shape");

}
}
class Circle extends Shape {

@Override
void draw() {

System.out.println("Drawing a Circle");

}
@Override
void erase() {

System.out.println("Erasing a Circle");

}
}
class Triangle extends Shape {

@Override
void draw() {

System.out.println("Drawing a Triangle");

}
@Override
void erase() {

System.out.println("Erasing a Triangle");

}
class Square extends Shape {

@Override
void draw() {

System.out.println("Drawing a Square");

}
@Override
void erase() {

System.out.println("Erasing a Square");
}

}
public class PolymorphismDemo {

public static void main(String[] args) {

Shape shape;

shape = new Circle();


shape.draw();
shape.erase();
shape = new Triangle();
shape.draw();
shape.erase();
shape = new Square();
shape.draw();
shape.erase();

Ques-10) WAP to create a class Employee with a method


called calculateSalary(). Create two subclasses Manager and
Programmer. In each subclass, override the
calculateSalary() method to calculate and return the salary
based on their specific roles
class Employee {

public double calculateSalary() {

return 0.0;

}
class Manager extends Employee {

@Override
public double calculateSalary() {
double baseSalary = 50000;
double bonus = 10000;
return baseSalary + bonus;

}
}
class Programmer extends Employee {

@Override
public double calculateSalary() {

double baseSalary = 40000;


double overtimePay = 5000;
return baseSalary + overtimePay;

}
public class EmployeeDemo {

public static void main(String[] args) {


Employee manager = new Manager();
Employee programmer = new Programmer();
System.out.println("Manager's Salary: " + manager.calculateSalary());
System.out.println("Programmer's Salary: " +
programmer.calculateSalary());

}
Ques-11) WAP to count the total number of odd numbers
between 1-100, and display the sum of them.
public class OddCountSum {

public static void main(String[] args) {


int count = 0;
int sum = 0;

for (int i = 1; i <= 100; i++) {

if (i % 2 != 0) { // check for odd number


count++;
sum += i;

System.out.println("Total odd numbers between 1 to 100: " + count);


System.out.println("Sum of odd numbers: " + sum);

You might also like