Professional Documents
Culture Documents
Q1-Write A Program To Print Hello World
Q1-Write A Program To Print Hello World
Code:
class Hello{
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Code:
Output:
Code:
import java.io.*;
int a = 6;
double db = (double)a;
System.out.println(db);
System.out.println(db1);
Output:
Code:
import java.util.Scanner;
float devide;
first = scanner.nextInt();
second = scanner.nextInt();
CODE:-
import java.util.Scanner;
CODE:-
class CMD {
OUTPUT:-
CODE:-
import java.util.Random;
OUTPUT:-
CODE:-
import java.util.Random;
public class AC {
OUTPUT:-
CODE:-
OUTPUT:-
CODE:-
public class Sa {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 7, 1};
int temp = 0;
OUTPUT:-
CODE:-
import java.util.Scanner;
class Linear {
public static void main(String args[]) {
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter those " + n + " elements");
for (c = 0; c < n; c++) {
array[c] = in.nextInt();
}
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++) {
if (array[c] == search) { /* Searching element is present */
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n) { /* Element to search isn't present */
System.out.println(search + " isn't present in array.");
}
}
}
OUTPUT:-
CODE:-
import java.util.Arrays;
import java.util.Scanner;
Arrays.sort(arr);
int low = 0;
int high = arr.length - 1;
boolean found = false;
if (arr[mid] == searchElement) {
System.out.println("Element " + searchElement + " found at index " + mid);
found = true;
break;
} else if (arr[mid] < searchElement) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (!found) {
OUTPUT:-
CODE:-
public class Small {
public static void main(String[] args) {
//Initialize array
int[] arr = new int[] {8, 59, 11 , 55, 56};
OUTPUT:-
CODE:-
import java.util.Arrays;
class Diffrence {
public static void main(String[] args) {
int[] array_nums = {5, 9,1 , 4, 8};
System.out.println("Original Array: " + Arrays.toString(array_nums));
System.out.println("Difference between the largest and smallest values of the said array: "
+ (max_val - min_val));
}
}
OUTPUT:-
CODE:-
class Add {
public static void main(String[] args) {
int rows = 2, columns = 3;
int[][] firstMatrix = { {5, 9, 1}, {8, 5, 6} };
int[][] secondMatrix = { {-4, 2, 1}, {5, 6, 3} };
OUTPUT:-
CODE:-
class Fu{
public static void main(String[] args) {
String s = "SeeksforCakes";
// or String s= new String ("SeeksforCakes");
// Converting cases
String word1 = "SeeKyMe";
System.out.println("Changing to lower Case " + word1.toLowerCase());
// Converting cases
String word2 = "SeekyME";
System.out.println("Changing to UPPER Case " + word2.toUpperCase());
// Replacing characters
String str1 = "feeksforfeeks";
System.out.println("Original String " + str1);
String str2 = "feeksforfeeks".replace('f', 's');
System.out.println("Replaced f with s -> " + str2);
}
}
OUTPUT:-
CODE:-
import java.util.Scanner;
CODE:
OUTPUT:
CODE:
public class Box {
double length;
double width;
double height;
length = l;
width = w;
height = h;
OUTPUT:
CODE:
OUTPUT:
CODE:
public class Static {
static int staticVar1 = 10;
static String staticVar2 = "Hello, World!";
static {
// static block to perform some operation
staticVar1 += 5;
staticVar2 += " Welcome to Java!";
}
OUTPUT:
CODE:
public class Add {
int num1, num2;
Add() {
num1 = 0;
num2 = 0;
}
int addNumbers() {
return num1 + num2;
}
OUTPUT:
CODE:
OUTPUT:
CODE:
class Rectangle {
int length;
int width;
int calculateArea() {
return this.length * this.width;
}
OUTPUT:
CODE:
class Area {
OUTPUT:
CODE:
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
}
class Main {
public static void main(String[] args) {
BoxWeight myBox = new BoxWeight(10, 20, 30, 40);
double volume = myBox.volume();
OUTPUT:
CODE:
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
}
double getWeight() {
return weight;
}
}
class Main {
public static void main(String[] args) {
BoxWeight myBox = new BoxWeight(10, 20, 30, 40);
double volume = myBox.volume();
OUTPUT:
CODE:
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
double area() {
System.out.println("Inside Area for Triangle.");
return 0.5 * dim1 * dim2;
}
}
class Main {
public static void main(String[] args) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(8, 6);
figref = f;
System.out.println("Area is " + figref.area());
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
OUTPUT:
CODE:
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
double area() {
System.out.println("Inside Area for Triangle.");
return 0.5 * dim1 * dim2;
}
}
class Main {
public static void main(String[] args) {
Figure f = new Rectangle(10, 10);
Figure r = new Rectangle(9, 5);
Figure t = new Triangle(8, 6);
OUTPUT:
CODE:
1.Final Class
// This will give a compile error since the MyClass class is final and cannot be subclassed.
class MySubClass extends MyClass {
// subclass code
}
2.Final Variable
class MyClass {
final int myValue = 10;
void printValue() {
System.out.println("The value is: " + myValue);
}
}
class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.myValue = 20; // This will give a compile error since myValue is final
obj.printValue(); // This will output "The value is: 10"
}
}
OUTPUT:
class MyClass {
final void printMessage() {
System.out.println("Hello World");
}
}
class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.printMessage(); // This will output "Hello World"
MySubClass subObj = new MySubClass();
subObj.printMessage(); // This will also output "Hello World"
}
}
OUTPUT:
CODE:
class ParentClass {
int a = 10;
public int b = 20;
protected int c = 30;
private int d = 40;
void showData() {
System.out.println("Inside ParentClass");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
CODE:
import java.io.*;
public class MyFile {
public static void main(String[] args) {
// Create a new File object
File file = new File("myFile.txt");
try {
// Create a new FileWriter object
FileWriter writer = new FileWriter(file);
OUTPUT: