Java Program1
Java Program1
System.out.print("End : ");
int end = s.nextInt();
int evenSum =
0; int oddSum =
0;
for (int i = start; i <= end; i++)
{ if (i % 2 == 0) {
evenSum += i;
} else {
oddSum += i;
}
}
System.out.println("Sum of even numbers: " + evenSum);
System.out.println("Sum of odd numbers: " + oddSum);
}
}
Output:
Date: / /
}
}
Output:
Output:
Date: / /
5. WAP in java that uses length property for displaying any number of command line arguments.
Code:
public class command {
public static void main(String[] args) {
System.out.println("Number of command line arguments: " + args.length);
System.out.println("Command line arguments:");
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
}
output:
Date: / /
6. WAP in java to sort n numbers using bubble sort, selection sort and insertion sort.
Code:
import java.util.Scanner;
}
System.out.println();
bubbleSort(arr);
System.out.println(" Bubble
Sort:"); for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
selectionSort(arr);
System.out.println(" Selection
Sort:"); for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
insertionSort(arr);
System.out.println(" Insertion
Sort:"); for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Output:
Date: / /
if (cols1 != rows2) {
System.out.println("Matrix multiplication is not possible!");
return;
}
int[][] matrix2 = new int[rows2][cols2];
}
public static void displayMatrix(int[][] matrix) { int rows = matrix.length;
int cols = matrix[0].length;
for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Date: / /
System.out.println("Enter "+n+"
elements:"); for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
}
}
class A
{
private String message;
public void setmessage(String msg)
{
message=msg;
}
public String getmessage()
{
return message;
}
}
Output:
Date: / /
10. WAP in java to create constructor of a class and initialize values in it and later print them.
Code:
public class MyClass {
private int age;
private String name;
11. WAP in Java to implement the concept of abstract classes and interface.
Code:
// Define an interface for a
vehicle interface Vehicle {
void start();
void stop();
}
// Define an abstract class for a car that implements the Vehicle interface
abstract class Car implements Vehicle {
// Common properties for all
cars protected String make;
protected String
model; protected int
year; protected String
color;
// Constructor
public Car(String make, String model, int year, String color)
{ this.make = make;
this.model =
model; this.year =
year; this.color =
color;
}
// Main program
public class AbstractClassInterfaceExample { public static void main(String[] args) {
SportsCar myCar = new SportsCar("Ferrari", "458 Italia", 2015, "Red"); myCar.display();
myCar.start(); myCar.drive(); myCar.stop();
}
}
Output:
Date: / /
12. WAP in Java to implement the concept of method overloading and constructor overloading
Code:
class Shape {
private int length;
private int width;
private int height;
// Constructor with no
arguments public Shape() {
length = 0;
width = 0;
height = 0;
}
// Method with no
arguments public void area()
{
System.out.println("Area: " + (length * width));
}
public static void main(String[] args) { Shape shape1 = new Shape(); Shape shape2 = new Shape(5); Shape shape3 = new Shape(
13. WAP in Java to create a class Shape and override area() method to calculate area of rectangle
Square and circle
Code:
abstract class Shape {
// abstract method for calculating area
public abstract double area();
}
14. WAP in Java to demonstrate wrapper class, boxing auto boxing, unboxing auto unboxing .
Code:
public class WrapperExample {
public static void main(String[] args) {
// Wrapper class objects
Integer intObj = new Integer(10);
Double doubleObj = new
Double(3.14);
Boolean booleanObj = new Boolean(true);
System.out.println("Boxed objects:");
System.out.println("Integer: " + intBoxed);
System.out.println("Double: " + doubleBoxed);
System.out.println("Boolean: " +
booleanBoxed);
System.out.println("Auto-boxed objects:");
System.out.println("Integer: " + intAutoBoxed);
System.out.println("Double: " + doubleAutoBoxed);
System.out.println("Boolean: " + booleanAutoBoxed);
System.out.println("Unboxed values:");
System.out.println("int: " + intUnboxed);
System.out.println("double: " + doubleUnboxed);
System.out.println("boolean: " + booleanUnboxed);
System.out.println("Auto-unboxed values:");
System.out.println("int: " + intAutoUnboxed);
System.out.println("double: " + doubleAutoUnboxed);
System.out.println("boolean: " + booleanAutoUnboxed);
}
}
Date: / /
Output:
Date: / /
15. WAP in Java to implement the concept of simple inheritance , multilevel inheritance ,and
hierarchical
Code:
// Class representing a Vehicle
class Vehicle {
protected String name;
protected int wheels;
System.out.println(name + " is pedaling with " + (isMotorized ? "a" : "no") + " motor");
}
}
Output:
Date: / /
// Interface representing a
MusicPlayer interface MusicPlayer {
void playMusic();
}
// Class representing a Car that implements both Vehicle and MusicPlayer interfaces
class Car implements Vehicle, MusicPlayer {
private String name;
@Override
public void drive() {
System.out.println(name + " is
driving");
}
@Override
public void playMusic() {
System.out.println(name + " is playing
music");
}
}