[go: up one dir, main page]

0% found this document useful (0 votes)
44 views14 pages

Lab Oopsjava (1) Maaz

Uploaded by

jnskp2n5nb
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)
44 views14 pages

Lab Oopsjava (1) Maaz

Uploaded by

jnskp2n5nb
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/ 14

LAB MANUAL FOR OOPS JAB

Program No.1: Java Program to Add Two Integers

class Main {

public static void main(String[] args) {

int first = 20;


int second = 20;

// add two numbers


int sum = first + second;
System.out.println(first + " + " + second + " = " + sum);
}
}
Program no 2: Demonstrate reading User Input

import java.util.Scanner;

public class HelloWorld {

public static void main(String[] args) {


// Creates a reader instance which takes input from standard input -
keyboard
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");

// Check if the input is an integer


if (reader.hasNextInt()) {
// nextInt() reads the next integer from the keyboard
int number = reader.nextInt();
// println() prints the following line to the output screen
System.out.println("You entered: " + number);
} else {
System.out.println("Error: Input is not an integer.");
}

// Close the scanner to avoid resource leaks


reader.close();
}
}
Program No. 3: PROGRAM FOR CLASS OBJECT AND METHOD
public class Person {
// Attributes of the Person class
private String name;
private int age;

// Method to set the name


public void setName(String name) {
this.name = name;
}

// Method to get the name


public String getName() {
return name;
}

// Method to set the age


public void setAge(int age) {
this.age = age;
}

// Method to get the age


public int getAge() {
return age;
}

// Method to print the person's details


public void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

// Main method to demonstrate the use of the Person class


public static void main(String[] args) {
// Create an object of the Person class
Person person = new Person();

// Set the attributes using the setter methods


person.setName("Maaz"); person.setAge(22);

// Print the person's details using the printDetails method


person.printDetails();
}
}
Program no 4: To Implement the interfaces in a class

interface Walkable {

void walk();

}
interface Running {
void run();

}
// Implement the interfaces in a class
class Dog implements Walkable, Running {
@Override
public void walk() {
System.out.println("Dog is walking.");

}
@Override
public void run() {
System.out.println("Dog is running.");

}
}
// Use the class to call the methods from the interfaces
public class Main {

public static void main(String[] args) {


Dog dog = new Dog();

dog.walk();
dog.run();
}
}
Program No. 5: To Implement Abstract Class

abstract class Sunstar {


// Abstract method to be implemented by subclasses
abstract void printInfo();
}

// Concrete class Employee extending abstract class Sunstar


class Employee extends Sunstar {
@Override
void printInfo() {
// Employee details
String name = "Maaz";
int age = 22;
float salary = 90000.90;

// Printing employee details


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}

// Main class
public class Base {
public static void main(String[] args) {
// Creating an instance of Employee with a reference of type Sunstar
Sunstar s = new Employee();
// Calling the printInfo method, which is implemented in Employee class
s.printInfo();
}
}
Program no 6: Program to Implement Inheritance concept in Java.

import java.io.*;
import java.lang.*;
import java.util.*;

// Parent class
class One {
public void print_geek()
{
System.out.println("Geeks");
}
}

class Two extends One {


public void print_for() { System.out.println("for"); }
}

// Driver class
public class Main {
// Main function
public static void main(String[] args)
{
Two g = new Two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Program no 7:Program to implement multilevel inheritence

import java.io.*;
import java.lang.*;
import java.util.*;

class One {
public void print_geek()
{
System.out.println("Hello");
}
}

class Two extends One {


public void print_for() { System.out.println("Iam"); }
}

class Three extends Two {


public void print_greek()
{
System.out.println("Maaz");
}
}

// Drived class
public class Main {
public static void main(String[] args)
{
Three g = new Three();
g.print_geek();
g.print_for();
g.print_greek();
}
}
Program no 8: Demonstrate the use of polymorphism using method overridding

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

class Rectangle extends Shape {


@Override
public void draw() {
System.out.println("Drawing a Rectangle");
}
}

class Triangle extends Shape {


@Override
public void draw() {
System.out.println("Drawing a Triangle");
}
}

public class Main { // Ensure this class is public and named Main
public static void main(String[] args) {
Shape s1 = new Rectangle();
Shape s2 = new Triangle();

s1.draw(); // Output: "Drawing a circle"


s2.draw(); // Output: "Drawing a square"
}
}
Program No. 9:Program to implement EXECTION HANDLING

import java.io.*;

class GFG1 {
public static void main (String[] args) {
int a=5;
int b=0;
try{
System.out.println(a/b);
}
catch(ArithmeticException e){
System.out.println(e.getMessage());
}
}
}
Program No. 10: Program for Multi-Threading

class A extends Thread {

public void run() {

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

System.out.println("From Thread A = " + i);

System.out.println("Exit from A");

class B extends Thread {

public void run() {

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

System.out.println("From Thread B = " + j);

System.out.println("Exit from B");

class C extends Thread {

public void run() {

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

System.out.println("From Thread C = " + k);

System.out.println("Exit from C");

}
}

public class Program15 { // Ensure this class is public

public static void main(String[] args) {

// Starting the threads

new A().start();

new B().start();

new C().start();

You might also like