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

Ilovepdf Merged

The document discusses object-oriented programming concepts like classes, objects, inheritance and polymorphism through examples. It contains code snippets demonstrating class definitions, method overloading, exception handling, interfaces and collections like linked lists, sets and maps.

Uploaded by

itssecretblogger
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)
15 views

Ilovepdf Merged

The document discusses object-oriented programming concepts like classes, objects, inheritance and polymorphism through examples. It contains code snippets demonstrating class definitions, method overloading, exception handling, interfaces and collections like linked lists, sets and maps.

Uploaded by

itssecretblogger
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/ 25

USC '00326952314 rtrct_2023110162C99

File Edit

classCounter static method


private static int count;
Counter( )
count++•,

static void display()


System.out. "*count);

class main
public static void main(String argsll)
Counter cI-new counter();
Counter.display();
Counter c2znew Counter();
Counter c3anew counter();
Counter.display();
Counter ca-new Counter();
Counter c Sanew Counter();
counter.display();
Edrt

import single inheritance


class Data
protected int a, b;
public void read(int x, int y)

bay;

class Sumextends Data


private int sum;
public void add()
sum-a+b;

public void display()


System. out . print "Suma "+sum) ;
Edit

sum-a+b;

public void display()


System. out . print "Sum= "+sum) ;

class pract1B1
public static void main(String args[l)
int x, y;
Scanner sc=new Scanner(System.in);
System.out. print1n("Enter 1st number:
x=sc . nextlnt();
System.out.print1n("Enter 2nd number:
yasc.nextlnt();
Sum s=new Sum();
s. read(x,y);
s.add();
s.display();
(dYt

import java. util. •; multi-levelinheritance


class Data
protectedfloat r;
public void read(float x)

class Area extends Data


protected float a;
public void claculate()

public void display()

class Volumeextends Area


'00326952314

Vtew
class Volume extends Area
private float v;
public void com()

public void output()


System. out. print "Volume —

class pract1B2
public static void main(Stringargs[l)
float x;
Scanner sc=newScanner(System.in);
System.out. print1n("Enter the radius:
x=sc. nextF10at();
Volume a =newVolume();
a. read(x);
a. claculate();
a. display();
patid • e
'003269323'* error_20231'C'62B9f VtsvegO•eothOatxt (

Edit

public void output()


System.out. —

1
class pract1B2
public static void main(Stringargstl)
float x;
Scanner sc= new Scanner(System. in);
System.out.print1n("Enter the radius:
x=sc. nextF10at();
Volume a -new Volume();
a. read(x);
a. claculate();
a-display();
a.com();
a. output();
Pract2A

import java.util.*;
abstract class Base
{
protected float r,v;
public void read(float x)
{
r=x;
}
public abstract void calculate();
public void display()
{
System.out.println("Volume = "+v);
}
}
class Sphere extends Base
{
public void calculate()
{
v=3.14f*r*r*r*4/3;
}
}
class Hemisphere extends Base
{
public void calculate()
{
v=3.14f*r*r*r*2/3;
}
}
class pract2A1
{
public static void main(String args[])
{
float x;
Scanner sc =new Scanner(System.in);
System.out.println("Enter the radius: ");
x=sc.nextFloat();
Sphere s=new Sphere();
s.read(x);
s.calculate();
s.display();
Hemisphere h=new Hemisphere();
h.read(x);
h.calculate();
h.display();
}
}
Pract2B

import java.util.*;
interface Base
{

public void calculate();


public void read(float x);

}
class Sphere implements Base
{
float r,v;
public void read(float x)
{
r=x;
}
public void display()
{
System.out.println("Volume = "+v);
}
public void calculate()
{
v=3.14f*r*r*r*4/3;
}
}
class Hemisphere implements Base
{
float r,v;
public void read(float x)
{
r=x;
}
public void display()
{
System.out.println("Volume = "+v);
}
public void calculate()
{
v=3.14f*r*r*r*2/3;
}
}
class pract2A2
{
public static void main(String args[])
{
float x;
Scanner sc =new Scanner(System.in);
System.out.println("Enter the radius: ");
x=sc.nextFloat();
Sphere s=new Sphere();
s.read(x);
s.calculate();
s.display();
Hemisphere h=new Hemisphere();
h.read(x);
h.calculate();
h.display();
}
}
Pract3A

import java.io.*;

class MonthNumberException extends Exception {


public MonthNumberException(String str) {
System.out.println(str);
}
}

class Month {
public static void main(String args[]) throws IOException {
int m;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter month number: ");
str = br.readLine();
m = Integer.parseInt(str);

try {
switch (m) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
System.out.println("Month number entered is " + m);
break;
default:
throw new MonthNumberException("Invalid Month....");
}
} catch (MonthNumberException me) {

}
}
}

Pract3B
import java.io.*;
class Month {
public static void main(String args[]) throws IOException {
int m;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter month number: ");
str = br.readLine();

try {
m = Integer.parseInt(str);
switch (m) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
System.out.println("Month number entered is " + m);
break;
default:
throw new NumberFormatException();
}
} catch (NumberFormatException nfe) {
System.out.println("Invalid month number.......");
}
}
}
Practical 1
Method Overloading

Write a program to demonstrate Method overloading in a base and derived class by


overloading the methods for displaying the value of a variable contained in the class.

class Parent
{
public void display (int x)
{
System.out.println("x="+x);
}
}
class Child extends Parent
{
public void display(int x, int y)
{
System.out.println("x="+x+"\ny="+y);
}
}
class Main
{
public static void main(String args[])
{
Child c=new Child(); c.display(10);
c.display(5,5);
}
}
Output

Practical 4:
List Interface:
import java.util.*;
class LinkedListDemo
{
public static void main(String args[])
{
// create a linked list
LinkedList list = new LinkedList();
// add elements to the linked list
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
list.addLast("six");
list.addFirst("seven");
list.add(1, "eight");
System.out.println("Original contents of list: " + list);

// remove elements from the linked list


list.remove("Five");
list.remove(2);
System.out.println("\nContents of list after deletion: " + list);

// removing first and last elements


list.removeFirst();
list.removeLast();
System.out.println("\nlist after deleting first and last: " + list);

// get and set a value


Object val = list.get(2);
list.set(2, (String) val + " Changed");
System.out.println("\nlist after change: " + list);
}
}
Output:
Set Interface:
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
class SetInterfaceDemo
{
public static void main(String args[])
{
Set set = new TreeSet();
set.add(10);
set.add(30);
set.add(98);
set.add(80);
set.add(10); //duplicate value (will not be added)
set.add(99);
Iterator itr = set.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println("Size of Set is = "+set.size());
System.out.println("set is empty = "+set.isEmpty());
}
}
Output:

Map Interface:
import java.util.*;

// Main class
class GFG {

// Main driver method


public static void main(String args[])
{
// Creating an empty HashMap
Map<String, Integer> hm
= new HashMap<String, Integer>();

// Inserting pairs in above Map


// using put() method
hm.put("a", new Integer(100));
hm.put("b", new Integer(200));
hm.put("c", new Integer(300));
hm.put("d", new Integer(400));

// Traversing through Map using for-each loop


for (Map.Entry<String, Integer> me :
hm.entrySet()) {

// Printing keys
System.out.print(me.getKey() + ":");
System.out.println(me.getValue());
}
}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy