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

Java

Uploaded by

Aniketh Kumar
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 views11 pages

Java

Uploaded by

Aniketh Kumar
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/ 11

Week 2 Lab Experiments

Name:- Aryan Tyagi


Branch:- IT A
Roll No:- 2300290130053
2.1 WAP to find out the sum of command line arguments.

Code:-

public class sumargs1 {


public static void main(String[] args) {
int sum = 0;

for (String arg : args) {


try {
int num = Integer.parseInt(arg);
sum += num;
} catch (NumberFormatException e) {
System.out.println("Invalid number: " + arg);
}
}

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


}
}

Input:-

1234

Output:-

Sum of command line arguments: 10

2.2 WAP to count the number of characters in a given string, to reverse the string
and check whether it is palindrome or not?

Code:-

import java.util.Scanner;
public class exp2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();

System.out.println("Total characters: " + input.length());

String reversed = new StringBuilder(input).reverse().toString();


System.out.println("Reversed: " + reversed);

if (input.equals(reversed)) {
System.out.println("It's a palindrome.");
} else {
System.out.println("Not a palindrome.");
}
}
}

Input:-

NAMAN

Output:-

Total characters: 5

Reversed: NAMAN

It's a palindrome.

2.3 WAP to Find largest and smallest numbers in an array.

Code:-

import java.util.Scanner;

public class MinMaxArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter array size: ");
int size = sc.nextInt();

int[] arr = new int[size];


System.out.println("Enter elements:");
for (int i = 0; i < size; i++) arr[i] = sc.nextInt();
int min = arr[0], max = arr[0];
for (int num : arr) {
if (num < min) min = num;
if (num > max) max = num;
}

System.out.println("Smallest: " + min);


System.out.println("Largest: " + max);
}
}

Input:-

1234

Output:-

Smallest: 1

Largest: 4

2.4 Java Program to Find the Sum and Product of Elements in a Row/Column of a
Matrix

Code:-

import java.util.Scanner;

public class MatrixOps {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int[][] matrix = new int[3][3];

System.out.println("Enter 3x3 matrix:");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)


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

System.out.print("Enter row number (0-2): ");

int row = sc.nextInt();

int sum = 0, product = 1;

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

sum += matrix[row][j];

product *= matrix[row][j];

System.out.println("Row Sum: " + sum);

System.out.println("Row Product: " + product);

}
Input:-

123456789

Output:-

Row Sum: 15

Row Product: 120

2.5 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”

Code:-

import java.util.Scanner;
public class Number {
private double num;

public Number(double num) {


this.num = num;
}

public boolean isZero() { return num == 0; }


public boolean isPositive() { return num > 0; }
public boolean isNegative() { return num < 0; }
public boolean isEven() { return num % 2 == 0; }
public boolean isOdd() { return num % 2 != 0; }
public boolean isPrime() {
if (num < 2 || num != (int) num) return false;
for (int i = 2; i <= (int)num / 2; i++) {
if ((int)num % i == 0) return false;
}
return true;
}
public boolean isAmstrong() {
int n = (int) num;
int temp = n, sum = 0, digits = String.valueOf(n).length();
while (temp != 0) {
int d = temp % 10;
sum += Math.pow(d, digits);
temp /= 10;
}
return n == sum;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter element:");
int x = sc.nextInt();
Number n = new Number(x);
System.out.println("isZero = " + n.isZero());
System.out.println("isPositive = " + n.isPositive());
System.out.println("isNegative = " + n.isNegative());
System.out.println("isEven = " + n.isEven());
System.out.println("isOdd = " + n.isOdd());
System.out.println("isPrime = " + n.isPrime());
System.out.println("isAmstrong = " + n.isAmstrong());
}
}

Console:-

Enter element:

isZero = false

isPositive = true

isNegative = false

isEven = false

isOdd = true

isPrime = true

isAmstrong = true

2.6 WAP to insert 3 numbers from the keyboard and find a greater number among 3
numbers.

Code:-

import java.util.Scanner;
public class Greatest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements:");
int n = sc.nextInt();
int n1 = sc.nextInt();
int n2 = sc.nextInt();
if(n > n1) {
if(n > n2) {
System.out.println(n + " is greatest");
}
else {
System.out.println(n2 + " is greatest");
}
}
else{
if(n1 > n2) {
System.out.println(n1 + " is greatest");
}
else {
System.out.println(n2 + " is greatest");
}
}
}
}

Console:-

Enter elements:

3 is greatest

2.7 WAP to illustrate use of this keyword.

Code:-

public class ThisKeyword {


int x;

ThisKeyword(int x) {
this.x = x;
}

void display() {
System.out.println("Value: " + this.x);
}

public static void main(String[] args) {


ThisKeyword obj = new ThisKeyword(10);
obj.display();
}
}

Console:-

Value: 10

2.8 Write a program to demonstrate static variables, methods, and blocks.


Code:-

public class StaticDemo {

static int count;

static {

count = 100;

System.out.println("Static block executed");

static void displayCount() {

System.out.println("Count = " + count);

public static void main(String[] args) {

StaticDemo.displayCount();

Console:-

Static block executed

Count = 100

2.9 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.

Code:-

class Shape {
void draw() { System.out.println("Drawing shape"); }
void erase() { System.out.println("Erasing shape"); }
}

class Circle extends Shape {


void draw() { System.out.println("Drawing circle"); }
void erase() { System.out.println("Erasing circle"); }
}

class Triangle extends Shape {


void draw() { System.out.println("Drawing triangle"); }
void erase() { System.out.println("Erasing triangle"); }
}

class Square extends Shape {


void draw() { System.out.println("Drawing square"); }
void erase() { System.out.println("Erasing square"); }
}

public class ShapeDemo {


public static void main(String[] args) {
Shape s;

s = new Circle(); s.draw(); s.erase();


s = new Triangle(); s.draw(); s.erase();
s = new Square(); s.draw(); s.erase();
}
}

Console:-

Drawing circle

Erasing circle

Drawing triangle

Erasing triangle

Drawing square

Erasing square

2.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

Code:-
class Employee {

double salary;

double calculateSalary() {

return salary;

class Manager extends Employee {

Manager() { salary = 80000; }

double calculateSalary() {

return salary + 10000; // Bonus

class Programmer extends Employee {

Programmer() { salary = 60000; }

double calculateSalary() {

return salary + 5000; // Allowance

public class SalaryDemo {

public static void main(String[] args) {

Employee e1 = new Manager();

Employee e2 = new Programmer();


System.out.println("Manager Salary: " + e1.calculateSalary());

System.out.println("Programmer Salary: " + e2.calculateSalary());

Console:-

Manager Salary: 90000.0

Programmer Salary: 65000.0

2.11 WAP to count the total number of odd numbers between 1-100, and display the
sum of them.

Code:-

public class OddCounter {


public static void main(String[] args) {
int count = 0;
int sum = 0;

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


if (i % 2 != 0) {
count++;
sum += i;
}
}

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


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

Cosole:-

Total odd numbers between 1 and 100: 50

Sum of odd numbers: 2500

You might also like