0% found this document useful (0 votes)
8 views31 pages

Journal

The document contains various Java programs demonstrating concepts such as copy constructors, inheritance (single, multilevel, hierarchical, multiple, hybrid), method overloading, method overriding, interfaces, packages, multithreading, and exception handling. Each section includes code examples along with their outputs, showcasing the functionality of the Java features discussed. Practical exercises are numbered and dated, indicating a structured approach to learning Java programming.

Uploaded by

mandeakash9
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)
8 views31 pages

Journal

The document contains various Java programs demonstrating concepts such as copy constructors, inheritance (single, multilevel, hierarchical, multiple, hybrid), method overloading, method overriding, interfaces, packages, multithreading, and exception handling. Each section includes code examples along with their outputs, showcasing the functionality of the Java features discussed. Practical exercises are numbered and dated, indicating a structured approach to learning Java programming.

Uploaded by

mandeakash9
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/ 31

C:Copy Constructor

class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(Student s) {
this.name = s.name;
this.age = s.age;
}
public void display() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
public static void main(String[] args) {
Student s = new Student("Shital", 21);
System.out.println("Displaying the original object:");
s.display();
System.out.println("Displaying the copied object:");
Student copiedStudent = new Student(s);
copiedStudent.display();
}
}

Output :

C:\Users\HP\Desktop\Akash>javac Student.java
C:\Users\HP\Desktop\Akash>java Student
Displaying the original object
Name: Akash
Age: 21
Displaying the copied object
Name: Akash
Age: 21
C:\Users\HP\Desktop\Akash>
Practical No: 6
Date: / /

Q) Java Program Based on Inheritance.


1) Single Inheritance:
class Student{ void Fee(){
System.out.println("Student fees=25000");
}
}
class Student_Name extends Student{ void Name(){
System.out.println("Student Name=Shridhar");
}
}
class College{ public static void main(String args[]){
Student_Name p=new Student_Name();
p.Fee();
p.Name();
}}

Output :

C:\Users\HP\Desktop\Akash>javac College.java
C:\Users\HP\Desktop\Akash>java College
Student fees = 25000
2)Multilevel Inheritance:
class Car{ public Car(){
System.out.println("Class Car");
}
public void vehicleType(){
System.out.println("Vehicle Type:Car");
}
}
class Maruti extends Car{ public Maruti(){
System.out.println("Class Maruti");
}
public void brand(){
System.out.println("Brand:Maruti");
}
public void speed(){
System.out.println("Max:90Kmph");
}
}
public class Maruti800 extends Maruti{ public Maruti800(){
System.out.println("Maruti Model:800");
}
public void speed(){
System.out.println("Max:80Kmph");
}
public static void main(String args[]){ Maruti800 obj=new
Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}

Output :
C:\Users\HP\Desktop\Akash>javac Maruti800.java
C:\Users\HP\Desktop\Akash>java Maruti800
Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph
C:\Users\HP\Desktop\Akash>
3)Hierachical Inheritance:

class Company{ public void CompanyName(){

System.out.println("Company name is Tech Mahindra.");

class Employee1 extends Company{ public void EmployeeName1(){

System.out.println("EmployeeName1 is Maher.");

class Employee2 extends Company{ public void EmployeeName2(){

System.out.println("EmployeeName2 is Azba.");

public class Main1{

public static voidmain(String []args){ Employee1 e1=new

Employee1();

e1.CompanyName();
e1.EmployeeName1();

Employee2 e2=new Employee2();

e2.CompanyName();

e2.EmployeeName2();
}
}

Output :
C:\Users\HP\Desktop\Akash>javac Multiple.java
C:\Users\HP\Desktop\Akash >java Multiple
Hi....I am Akash
I am Student of BCA 3rd year
C:\Users\HP\Desktop\Akash >
4)Multiple Inheritance:
interface A{
public abstract void execute1();
}
interface B{
public abstract void execute2();
}
class C implements A,B
{
public void execute1()
{
System.out.println("Hi....I am Mdgous");
}
public void execute2()
{
System.out.println("I am Student of BCA 3rd year");
}
}
public class Multiple{
public static void main(String args[]){ C obj=new C();
obj.execute1();
obj.execute2();
}}
Output :
C:\Users\HP\Desktop\Akash>javac Multiple.java
C:\Users\HP\Desktop\Akash>java Multiple
Hi....I am Akash
I am Student of BCA 3rd year
C:\Users\HP\Desktop\Akash>
5)Hybrids Inheritance:
interface Animal{
void sound();
}
interface Mammal{
void feed();
}
class Dog{ public void bark(){
System.out.println("Dog barks");
}
}
class Labrador extends Dog implements Animal,Mammal
{ public void sound(){
System.out.println("Labrador makes sound");
}
public void feed(){
System.out.println("Labrador feeds its puppies");
}
}
public class Main3{
public static void main(String args[]){ Labrador lab=new Labrador();
lab.bark();
lab.sound();
lab.feed();
}
}
Output :

C:\Users\HP\Desktop\Akash>javac Main3.java
C:\Users\HP\Desktop\Akash>JavaMain3
Dog barks
Labrador makes sound
Labrador feeds its puppies
C:\Users\HP\Desktop\Akash>
Practical No:7
Date: / /

Q) java programs based on method overloading.


class Calculator{ public int add(int x,int y){
return x+y;
}
public int add(int x,int y,int z){
return x+y+z;
}
public double add(double x,double y){ return x+y;
}
public String add(String x,String y){ return x+y;
}
public static void main(String args[]){
Calculator cal=new Calculator();
System.out.println("Sum of 10 & 20 is:"+cal.add(10,20));
System.out.println("Sum of 10,20 & 30:"+cal.add(10,20,30));
System.out.println("Sum of 10.5 & 20.5:"+cal.add(10.5,20.5));
System.out.println("Concatenation of Hello and World
is:"+cal.add("Hello","World"));
}
}
Output :

C:\Users\HP\Desktop\Akash>javac Calculator.java
C:\Users\HP\Desktop\Akash>java Calculator
Sum of 10 & 20 is: 30
Sum of 10, 20 & 30: 60
Sum of 10.5 & 20.5: 31.0
Concatenation of Hello and World is: HelloWorld
C:\Users\HP\Desktop\Akash>
Practical No:8
Date: / /

Q) Java Program based on method overriding.


class Animal
{
public void makeSound()
{
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal
{
public void makeSound()
{
System.out.println("Dog barks");
}
}
class Cat extends Animal{
public void makeSound()
{
System.out.println("Cat meows");
}
}
public class Overriding
{
public static void main(String args[]){
Animal animal1=new Animal();
Animal animal2=new Dog();
Animal animal3=new Cat();
animal1.makeSound();
animal2.makeSound();
animal3.makeSound();
}
}

Output :

C:\Users\HP\Desktop\Akash>javac Overriding.java
C:\Users\HP\Desktop\Akash >java Overriding
Animal makes a sound
Dog barks
Cat meows
C:\Users\HP\Desktop\Akash >
Practical No:9

Date: / /

Q) java Program based on interface.


interface Walkable{ void walk();
}
interface Swimmable{ void swim();
}
class Duck implements Walkable,Swimmable{
public void walk(){
System.out.println("Duck is Walking.");
}
public void swim(){
System.out.println("Duck is Swimming.");
}
}
class Main{
public static void main(String args[]){ Duck duck=new Duck();
duck.walk();
duck.swim();
}
}
Output :

C:\Users\HP\Desktop\Akash>javac Main.java
C:\Users\HP\Desktop\Akash >java Main
Duck is Walking.
Duck is Swimming.
C:\Users\HP\Desktop\Akash>
2. Multiple interface
// Define Printable interface
interface Printable {
void print();
}
// Define Editable interface
interface Editable {
void edit();
}
// Class Document implements both Printable and Editable interfaces
class Document implements Printable, Editable {
public void print() {
System.out.println("Printing a document...");
}
public void edit() {
System.out.println("Editing a document...");
}
}
// Main class
class P2 {
public static void main(String[] args) {
Document document = new Document(); // Create an instance
of Document
document.print(); // Call print method
document.edit(); // Call edit method
}
}

Output :

C:\Users\HP\Desktop\Akash>javac P2.java
C:\Users\HP\Desktop\Akash>java P2
Printing a document...
Editing a document...
C:\Users\HP\Desktop\Akash >
Practical No: 10

Date: / /

Q. Java program based on packages .


Built-in package program
import java.lang.*;
import java.util.Scanner;
class Built1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Calculate area of a circle
System.out.println("Enter radius: ");
double r = sc.nextDouble();
double ar = Math.PI * Math.pow(r, 2);
System.out.println("Area: " + ar);
// Get length of a string
String message = "hello, world!";
int length = message.length();
System.out.println("Length: " + length);
// Convert an integer to binary
int number = 42;
String binary = Integer.toBinaryString(number);
System.out.println("Binary: " + binary);
// Calculate hypotenuse
System.out.println("Enter the length of side a: ");
double a = sc.nextDouble();
System.out.println("Enter the length of side b: ");
double b = sc.nextDouble();
double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
System.out.println("The length of the hypotenuse (c) is: " + c);
sc.close(); // Close the scanner to prevent resource leak
}
}

Output :

C:\Users\HP\Desktop\Akash>javac Built1.java
C:\Users\HP\Desktop\Akash >java Built1
Enter radius:
5
Area: 78.53981633974483
Length: 13
Binary: 101010
Enter the length of side a:
3
Enter the length of side b:
4
The length of the hypotenuse (c) is: 5.0
C:\Users\HP\Desktop\shridhar >
User defined package program
mypackage.java
package piyush;
public class mypackage {
public void myfunction() {
System.out.println("Hello from Myclass!");
}
}

demon.java
import piyush.*;

class demon {
public static void main(String args[]) {
mypackage mm = new mypackage(); // Create an instance of
mypackage
mm.myfunction(); // Call the myfunction method
}
}
Output :

C:\Users\HP\Desktop\Akash\piyush>javac mypackage.java
C:\Users\HP\Desktop\Akash>javac demon.java
C:\Users\HP\Desktop\Akash>java demon
Hello from Myclass!
C:\Users\HP\Desktop\Akash >
Practical No:11

Date: / /

Q11) Java programs based on multithreading.


1]By extending Thread class
// Program demonstrating multithreading by extending the Thread
class
class Demo extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println("Shivraj College");
}
}
}

class Data extends Thread {


public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println("BCA Department");
}
}
}

class Dept extends Thread {


public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println("Hello BCA Students");
}
}
}

public class College1 {


public static void main(String[] args) {
Demo d1 = new Demo();
Data d2 = new Data();
Dept d3 = new Dept();
d1.start();
d2.start();
d3.start();
}
}

Output :

C:\Users\HP\Desktop\Akash>javac College1.java
C:\Users\HP\Desktop\Akash>java College1
Shivraj College
BCA Department
Hello BCA Students
Shivraj College
Hello BCA Students
BCA Department
Shivraj College
Shivraj College
2.By implementing Runnable interface
class A implements Runnable {
public void run() {
for (int i = 1; i <= 15; i++) {
System.out.println("Accelerate");
}
}
}

class B implements Runnable {


public void run() {
for (int j = 1; j <= 10; j++) {
System.out.println("Brake");
}
}
}

class C implements Runnable {


public void run() {
for (int k = 1; k <= 5; k++) {
System.out.println("Turbo");
}
}
}

class Mustang {
public static void main(String args[]) {
Thread t1 = new Thread(new A());
Thread t2 = new Thread(new B());
Thread t3 = new Thread(new C());
t1.start();
t2.start();
t3.start();
}
}

Output :

C:\Users\HP\Desktop\Akash>javac Mustang.java
C:\Users\HP\Desktop\Akash >java Mustang
Accelerate
Brake
Turbo
Accelerate
Brake
Turbo
Accelerate
Accelerate
Brake
Turbo
Accelerate
Brake
Turbo
3. Copy constructor
class Copy {
String name;
int id;
// Parameterized constructor
Copy(String name, int id) {
this.name = name;
this.id = id;
}
// Copy constructor
Copy(Copy obj2) {
this.name = obj2.name;
this.id = obj2.id;
}
}
class Cp {
public static void main(String args[]) {
// Creating an object using the parameterized constructor
Copy aa = new Copy("Zaid", 22);
System.out.println("Name: " + aa.name + " and ID: " + aa.id);
System.out.println();

// Creating a copy of the object using the copy constructor


Copy bb = new Copy(aa);
System.out.println("Name: " + bb.name + " and ID: " + bb.id);
}
}

Output :

C:\Users\HP\Desktop\CopyExample>javac Cp.java
C:\Users\HP\Desktop\CopyExample>java Cp
Name: Akash and ID: 22
Name: Akash and ID: 22
Practical No:12

Date: / /

Q. Java program based on exception handling .


Built-in exception
class BuiltEx {
public static void main(String[] args) {
try {
String str = null; // This will cause a NullPointerException
System.out.println(str.length()); // Attempting to access the
length of a null string
} catch (NullPointerException e) {
System.out.println("Caught the exception"); // This message
will be printed
System.out.println(e.getMessage()); // This will print null since
there's no message
}
}
}

Output :

C:\Users\HP\Desktop\ExceptionExample>javac BuiltEx.java
C:\Users\HP\Desktop\ExceptionExample>java BuiltEx
Caught the exception
null
2. User-defined
exception Code
class SimpleException extends Exception {
public SimpleException(String message) {
super(message);
}
}
class SimpleClass {
public void doSomething(int value) throws SimpleException {
if (value < 0) {
throw new SimpleException("Value cannot be negative.");
}
System.out.println("Value is: " + value);
}
}
class Main {
public static void main(String[] args) {
try {
SimpleClass simple = new SimpleClass();
simple.doSomething(-5); // This will throw an exception
} catch (SimpleException e) {
System.out.println("Caught SimpleException: " +
e.getMessage());
}
}
}
Output :

C:\Users\HP\Desktop\ExceptionExample>javac Main.java
C:\Users\HP\Desktop\ExceptionExample>java Main
Caught SimpleException: Value cannot be negative.

You might also like