Professional Documents
Culture Documents
File Edit
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
bay;
sum-a+b;
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
Vtew
class Volume extends Area
private float v;
public void com()
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
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
{
}
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 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
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);
Map Interface:
import java.util.*;
// Main class
class GFG {
// Printing keys
System.out.print(me.getKey() + ":");
System.out.println(me.getValue());
}
}
}
Output: