0% found this document useful (0 votes)
12 views

Java Experiment

Uploaded by

priyansh2005r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java Experiment

Uploaded by

priyansh2005r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

NAME-PRIYANSH -RAJ

ROLL NO-123102038
CS-A(03)
EXPERIMENT 1:
1-code
class abc{
float value;

abc(float value) {
this.value = value;
}
}

public class q1 {
public static void main(String[] args) {
abc a = new abc(5.5f);
abc b = a;

System.out.println("before");
System.out.println("a value: " + a.value);
System.out.println("b value: " + b.value);
b.value = 10.0f;

System.out.println("after");
System.out.println("a value: " + a.value);
System.out.println("b value: " + b.value);
}
}
OUTPUT-

2-CODE
import java.util.Scanner;
public class q2 {
public static void main(String[] args) {
int s=2;
System.out.println("give the limt upto which you want to print
prime nos.");
int limit ;
Scanner sa=new Scanner(System.in);
limit=sa.nextInt();

System.out.println("Prime numbers up to " + limit + ":");


for (int num = s; num <= limit; num++) {
boolean isPrime = true;

for (int i = 2; i <= Math.sqrt(num); i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime) {
System.out.print(num + " ");
}
}
}
}

OUTPUT-
3-CODE
public class q3 {
public static void main(String[] args) {

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


System.out.print("Switch case for value: " + i+" ");
switch (i) {
case 1:
System.out.println("first case.");
break;
case 2:
System.out.println("second case.");
break;
case 3:
System.out.println("third case.");
break;
case 4:
System.out.println("fourth case.");
break;
case 5:
System.out.println("fifth case.");
break;
default:
System.out.println("no other cases match.");
}

}
}
}
OUTPUT-

4-CODE:
public class q4 {
private String str;
public void displayStringValue() {
System.out.println(this.str);
}

public static void main(String[] args) {


q4 demo = new q4();
demo.displayStringValue() ;
}
}

OUTPUT-

EXPERIMENT 2:
1-CODE:
public class q1 {

public static void compareStrings(String str1, String str2) {


System.out.println("Comparing " + str1 + "\" and \"" + str2 +
"\":");
System.out.println("== : " + (str1 == str2));
System.out.println("!= : " + (str1 != str2));
System.out.println("equals() : " + str1.equals(str2));
System.out.println("!equals() : " + !str1.equals(str2));
System.out.println();
}

public static void main(String[] args) {


String string1 = new String("Hello");
String string2 = new String("Hello");
String string3 = "World";
String string4 = string1;

compareStrings(string1, string2);
compareStrings(string1, string3);
compareStrings(string1, string4);
compareStrings(string2, string3);
}
}

OUTPUT-
2-CODE:
class Dog {
String name;
String says;
Dog(String name, String says) {
this.name = name;
this.says = says;
}
}

public class q2{


public static void main(String[] args) {
Dog spot = new Dog("spot", "Ruff!");
Dog scruffy = new Dog("scruffy", "Wurf!");
System.out.println("Dog 1: " + spot.name + " says: " + spot.says);
System.out.println("Dog 2: " + scruffy.name + " says: " +
scruffy.says);
Dog anotherReferenceToSpot = spot;
System.out.println("spot == scruffy: " + (spot == scruffy));
System.out.println("spot.equals(scruffy): " +
spot.equals(scruffy));
System.out.println("spot == anotherReferenceToSpot: " + (spot
== anotherReferenceToSpot));
System.out.println("spot.equals(anotherReferenceToSpot): " +
spot.equals(anotherReferenceToSpot));
}
}

OUTPUT:-
EXPERIMENT 3:
1-CODE:
class Dog {

void bark() {
System.out.println("Woof! Woof!");
}

void bark(int times) {


System.out.println("Woof! ".repeat(times));
}

void bark(double volume) {


System.out.println("Howl! (Volume: " + volume + ")");
}
void bark(char tone) {
if (tone == 'L') {
System.out.println("Loud Bark: WOOF WOOF!");
} else if (tone == 'S') {
System.out.println("Soft Bark: woof woof...");
} else {
System.out.println("Normal Bark: Woof!");
}
}

void bark(boolean isHappy) {


if (isHappy) {
System.out.println("Excited Bark: WOOF WOOF!!!");
} else {
System.out.println("Calm Bark: woof.");
}
}
}

public class q1 {
public static void main(String[] args) {
Dog dog = new Dog();

System.out.println("Calling different versions of bark():\n");


dog.bark();
dog.bark(3);
dog.bark(0.8);
dog.bark('L');
dog.bark('S');
dog.bark(false);
dog.bark(true);
}
}

OUTPUT:-

2-CODE:-
class MyClass {
int number;
String text;
}

public class q2 {
public static void main(String[] args) {
MyClass obj = new MyClass();

System.out.println("Default number: " + obj.number);


System.out.println("Default text: " + obj.text);
}
}

OUTPUT-

3-CODE:-
class MyClass {
int number;
String text;

MyClass(int number) {
this(number, "Default Text"); // Call the second constructor
}

MyClass(int number, String text) {


this.number = number;
this.text = text;
}

void display() {
System.out.println("Number: " + number);
System.out.println("Text: " + text);
}
}

public class q3 {
public static void main(String[] args) {
MyClass obj = new MyClass(10);
obj.display();
}
}

OUTPUT:-
EXPERIMENT 4:
1-CODE:-

public class q1 {
static String field1 = "Initialized at definition";
static String field2;

static {
field2 = "Initialized in static block";
}

public static void printFields() {


System.out.println("Field1: " + field1);
System.out.println("Field2: " + field2);
}

public static void main(String[] args) {


printFields();
}
}

OUTPUT:-

2-CODE:-

public class q2 {
public static void main(String... args) {
System.out.println("Number of arguments: " + args.length);
for (String arg : args) {
System.out.println(arg);
}
}
}

OUTPUT-
EXPERIMENT 5:
1-CODE:-
class AccessModifiersDemo {
public String publicField = "Public";
private String privateField = "Private";
protected String protectedField = "Protected";
String packageAccessField = "default";

public void publicMethod() {


System.out.println("Public");
}

private void privateMethod() {


System.out.println("Private");
}

protected void protectedMethod() {


System.out.println("Protected");
}
void packageAccessMethod() {
System.out.println("default");
}
}

public class q1{


public static void main(String[] args) {
AccessModifiersDemo demo = new AccessModifiersDemo();

System.out.println(demo.publicField);
// System.out.println(demo.privateField);
System.out.println(demo.protectedField);
System.out.println(demo.packageAccessField);

demo.publicMethod();
// demo.privateMethod();
demo.protectedMethod();
demo.packageAccessMethod();
}
}
OUTPUT:-

EXPERIMENT 6:
1:-CODE-
class A {
public A() {
System.out.println("class A constructor");
}
}

class B {
public B() {
System.out.println("class B constructor");
}
}

class C extends A {
B bMember = new B();
}

public class q1 {
public static void main(String[] args) {
C c = new C();
}
}
OUTPUT-

2-CODE:-
class Base {
public Base(String message) {
System.out.println("Base class constructor: " + message);
}
}

class Derived extends Base {


public Derived() {
super("no arg constructor");
System.out.println("derived class (no arg) constructor");
}

public Derived(String message) {


super(message);
System.out.println(message);
}
}

public class q2 {
public static void main(String[] args) {

Derived derived1 = new Derived();

Derived derived2 = new Derived("abc");


}
}

OUTPUT:-
3-CODE:-
class Person {
public void introduce() {
System.out.println("Hi I'm a person");
}

public void introduce(String name) {


System.out.println("Hi, my name is " + name );
}

public void introduce(String name, int age) {


System.out.println("Hi, I'm " + name + " and I'm " + age + " years
old");
}
}

class Friend extends Person {


public void introduce(String name, String hobby) {
System.out.println("Hey! I'm " + name + " and I love " + hobby );
}
}

public class q3 {
public static void main(String[] args) {
Friend friend = new Friend();

friend.introduce();
friend.introduce("pranav");
friend.introduce("pranav", 20);
friend.introduce("pranav", "playing cricket");
}
}

OUTPUT:-

4(I)-CODE:-
class abc {
public final void greet() {
System.out.println("abc");
}
}

class def extends abc {


// Attempting to override the final method greet() will cause a
compiler error
// public void greet() {
// System.out.println("def");
// }
}

public class q41 {


public static void main(String[] args) {
def o = new def();
o.greet();
}
}
OUTPUT-
4(II)-CODE:
final class Fina {
public void sayHello() {
System.out.println("Hello ");
}
}

// class Sub extends Fina {


// }

public class q42 {


public static void main(String[] args) {
Fina fi = new Fina();
fi.sayHello();
}
}

OUTPUT:-
EXPERIMENT 7:-
1-CODE;-
abstract class Base {
public Base() {
print();
}

public abstract void print();


}

class Derived extends Base {


int value = 42;

public void print() {


System.out.println("Value in Derived class: " + value);
}
}
public class q1 {
public static void main(String[] args) {
Derived derived = new Derived();
derived.print();
}
}

OUTPUT:-

2-CODE:-
interface me {
void method1();
void method2();
}

class my implements me {

public void method1() {


System.out.println("Method 1 is called");
}

public void method2() {


System.out.println("Method 2 is called");
}
}

public class q2{


public static void main(String[] args) {
me myClass = new my();
myClass.method1();
myClass.method2();
}
}

OUTPUT:-

3-CODE:
interface Interface1 {
void methodA();
void methodB();
}

interface Interface2 {
void methodC();
void methodD();
}

interface Interface3 {
void methodE();
void methodF();
}

interface CombinedInterface extends Interface1, Interface2,


Interface3 {
void methodG();
}

class ConcreteClass {
public void concreteMethod() {
System.out.println("Concrete method");
}
}

class my extends ConcreteClass implements CombinedInterface {

public void methodA() {


System.out.println("Method A Interface1");
}

public void methodB() {


System.out.println("Method B Interface1");
}

public void methodC() {


System.out.println("Method C Interface2");
}

public void methodD() {


System.out.println("Method D Interface2");
}

public void methodE() {


System.out.println("Method E Interface3");
}
public void methodF() {
System.out.println("Method F Interface3");
}

public void methodG() {


System.out.println("Method G CombinedInterface");
}
}

public class q3{


public static void acceptInterface1(Interface1 obj) {
obj.methodA();
obj.methodB();
}

public static void acceptInterface2(Interface2 obj) {


obj.methodC();
obj.methodD();
}

public static void acceptInterface3(Interface3 obj) {


obj.methodE();
obj.methodF();
}

public static void acceptCombinedInterface(CombinedInterface


obj) {
obj.methodG();
}

public static void main(String[] args) {


my myClass = new my();

acceptInterface1(myClass);
acceptInterface2(myClass);
acceptInterface3(myClass);
acceptCombinedInterface(myClass);

myClass.concreteMethod();
}
}

OUTPUT:-
EXPERIMENT 8:
1-CODE:
class q1 {
private int value = 10;

private void displayValue() {


System.out.println(value);
}

class InnerClass {
public void modifyOuter() {
value += 5;
displayValue();
}
}
public void useInnerClass() {
InnerClass inner = new InnerClass();
inner.modifyOuter();
}

public static void main(String[] args) {


q1 outer = new q1();
outer.displayValue();
outer.useInnerClass();
outer.displayValue();
}
}

OUTPUT:-

2-CODE:
class q2 {
private String outerField = "outer var";
class InnerClass {
private String innerField = "inner variable";

private void innerMethod() {


System.out.println("inner method called");
}
}

public void accessInnerClass() {


InnerClass inner = new InnerClass();
System.out.println(inner.innerField);
inner.innerMethod();
}

public static void main(String[] args) {


q2 outer = new q2();
outer.accessInnerClass();
}
}

OUTPUT-
EXPERIMENT 9:
1-CODE;
class q1 {
public static void main(String[] args) {
try {
throw new Exception("this is an exception message");
} catch (Exception e) {
System.out.println("caught exception: " + e.getMessage());
} finally {
System.out.println("finally executed");
}
}
}
OUTPUT:-
2-CODE:-
class MyCustomException extends Exception {
private String message;

public MyCustomException(String message) {


this.message = message;
}

public void displayMessage() {


System.out.println( message);
}
}

public class q2 {
public static void main(String[] args) {
try {
throw new MyCustomException("This is a custom exception");
} catch (MyCustomException e) {
e.displayMessage();
}
}
}
OUTPUT:-

You might also like