0% found this document useful (0 votes)
10 views11 pages

13 To 20 Java

The document outlines various Java programming experiments focusing on concepts such as the 'this' keyword, instance counting, static methods, method overriding, polymorphism, inheritance, and superclass references. Each experiment includes source code examples and aims to demonstrate specific programming principles. Additionally, it contains a section of viva questions and answers related to the concepts covered in the experiments.

Uploaded by

Rohit Bora
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)
10 views11 pages

13 To 20 Java

The document outlines various Java programming experiments focusing on concepts such as the 'this' keyword, instance counting, static methods, method overriding, polymorphism, inheritance, and superclass references. Each experiment includes source code examples and aims to demonstrate specific programming principles. Additionally, it contains a section of viva questions and answers related to the concepts covered in the experiments.

Uploaded by

Rohit Bora
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/ 11

Lab 4

Experiment 13 :
Aim : WAP to display the use of this keyword.
Source code:
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void displayinfo() {
System.out.println("Name : " + name + " and age : " + age);
}
public static void main(String[] args) {
Student s1 = new Student ("Nikhil", 21);
Student s2 = new Student("Mohit", 20);
s1.displayinfo();
s2.displayinfo();
}
}
Output:
Experiment 14:
Aim : Write a program that can count the number of instances created for the class.
Source code :
class Myclass {
static int instant = 0;
Myclass() {
instant++;
}
void displaycount() {
System.out.println("Number of instant created : " + instant);
}
public static void main(String[] args) {
Myclass obj1 = new Myclass();
obj1.displaycount();
Myclass obj2 = new Myclass();
obj2.displaycount();
}
}
Output:

Experiment 15:
Aim : Java Program to get the cube of a given number using the static method.
Source code:
import java.util.Scanner;
public class Cubecalculator{
public static int getcube( int num){
return num*num*num;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();
int result = getcube(num);
System.out.println("Cube of " + num + " is " + result);
}
}
Output:

Experiment 16:
Aim : Create a base class Fruit which has name ,taste and size as its attributes. A method
called eat() is created which describes the name of the fruit and its taste. Inherit the same in 2
other class Apple and Orange and override the eat() method to represent each fruit taste.
(Method overriding).
Source code:
class Fruit {
String name, taste, size;
public Fruit(String name, String taste, String size) {
this.name = name;
this.taste = taste;
this.size = size;
}
public void eat() {
System.out.println("Fruit Name: " + name);
System.out.println("Taste: " + taste);
}
}
class Apple extends Fruit {
public Apple() {
super("Apple", "Sweet", "Medium");
}
@Override
public void eat() {
System.out.println("Fruit Name: " + name);
System.out.println("Apples are " + taste + " and juicy.");
}
}
class Orange extends Fruit {
public Orange() {
super("Orange", "Tangy and Sweet", "Medium");
}
@Override
public void eat() {
System.out.println("Fruit Name: " + name);
System.out.println("Oranges are " + taste + " and refreshing.");
}
}
public class FruitTest {
public static void main(String[] args) {
Fruit apple = new Apple();
apple.eat();
System.out.println();
Fruit orange = new Orange();
orange.eat();
}
}
Output:
Experiment 17:
Aim : Write a program to create a class named shape. It should contain 2 methods- draw()
and erase() which should print “Drawing Shape” and “Erasing Shape” respectively. For this
class we have three sub classes- Circle, Triangle and Square and each class override the
parent class functionsdraw () and erase (). The draw() method should print “Drawing Circle”,
“Drawing Triangle”, “Drawing Square” respectively. The erase() method should print
“Erasing Circle”, “Erasing Triangle”, “Erasing Square” respectively. Create objects of Circle,
Triangle and Square in the following way and observe the polymorphic nature of the class by
calling draw() and erase() method using each object. Shape c=new Circle(); Shape t=new
Triangle(); Shape s=new Square(); (Polymorphism).
Source code:
class Shape {
public void draw() {
System.out.println("Drawing Shape");
}
public void erase() {
System.out.println("Erasing Shape");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
@Override
public void erase() {
System.out.println("Erasing Circle");
}
}
class Triangle extends Shape {
@Override
public void draw() {
System.out.println("Drawing Triangle");
}
@Override
public void erase() {
System.out.println("Erasing Triangle");
}}
class Square extends Shape {
@Override
public void draw() {
System.out.println("Drawing Square");
}
@Override
public void erase() {
System.out.println("Erasing Square");
}}
public class ShapeTest {
public static void main(String[] args) {
Shape c = new Circle();
Shape t = new Triangle();
Shape s = new Square();
c.draw();
c.erase();
System.out.println();
t.draw();
t.erase();
System.out.println();
s.draw();
s.erase();
}
}
Output:
Experiment 18:
Aim : Create a new class called Calculator with the following methods: A static method
called powerInt(int num1,int num2) . This method should return num1 to the power num2.A
static method called powerDouble(double num1,int num2). This method should return num1
to the power num2.Invoke both the methods and test the functionalities.
Source code:
import java.util.Scanner;
class Calculator {
public static int powerInt(int num1, int num2) {
return (int) Math.pow(num1, num2);
}
public static double powerDouble(double num1, int num2) {
return Math.pow(num1, num2);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer base: ");
int intBase = scanner.nextInt();
System.out.print("Enter an integer exponent: ");
int intExponent = scanner.nextInt();
int result1 = Calculator.powerInt(intBase, intExponent);
System.out.println(intBase + "^" + intExponent + " = " + result1);
System.out.print("Enter a double base: ");
double doubleBase = scanner.nextDouble();
System.out.print("Enter an integer exponent: ");
int doubleExponent = scanner.nextInt();
double result2 = Calculator.powerDouble(doubleBase, doubleExponent);
System.out.println(doubleBase + "^" + doubleExponent + " = " + result2);
}
}
Output:

Experiment 19:
Aim : Create a class named ‘Animal’ which includes methods like eat() and sleep().Create a
child class of Animal named ‘Bird’ and override the parent class methods. Add a new method
named fly().Create an instance of Animal class and invoke the eat and sleep methods using
this object. Create an instance of Bird class and invoke the eat, sleep and fly methods using
this object.(Inheritance)
Source code:
class Animal {
public void eat() {
System.out.println("Animal is eating...");
}
public void sleep() {
System.out.println("Animal is sleeping...");
}
}
class Bird extends Animal {
@Override
public void eat() {
System.out.println("Bird is pecking at food...");
}
@Override
public void sleep() {
System.out.println("Bird is sleeping in its nest...");
}
public void fly() {
System.out.println("Bird is flying in the sky...");
}
}
public class InheritanceTest {
public static void main(String[] args) {
Animal animal = new Animal();
System.out.println("Animal Class:");
animal.eat();
animal.sleep();
System.out.println();
Bird bird = new Bird();
System.out.println("Bird Class:");
bird.eat();
bird.sleep();
bird.fly();
}
}
Output
Experiment 20:
Aim : Write a program illustrating a super class variable a referencing as sub class object.
Source code :
class Parent {
int a = 10;
public void show() {
System.out.println("This is the Parent class.");
}
}
class Child extends Parent {
int a = 20;
@Override
public void show() {
System.out.println("This is the Child class.");
}
public void childMethod() {
System.out.println("This method belongs to the Child class.");
}
}
public class SuperClassReference {
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
System.out.println("Value of a: " + obj.a);
Child childObj = (Child) obj;
childObj.childMethod();
}
}
Output:
Viva Questions

1."What is the use of the this keyword in Java?"

Answer: The this keyword is used to refer to the current object within a method or
constructor. It helps differentiate between instance variables and local variables when they
have the same name. For example, in a constructor, this.name = name assigns the parameter
name to the instance variable name.

2."How can you count the number of instances created for a class?"

Answer: You can count the number of instances by using a static variable that is incremented
every time the constructor is called.

3."How can you find the cube of a given number using a static method in Java?"

Answer: A static method can be used to calculate the cube by taking the number as a
parameter and returning its cube.

4."What is method overriding in Java, and how does it work with the Fruit, Apple, and
Orange classes?"

Answer: Method overriding occurs when a subclass provides a specific implementation of a


method that is already defined in its superclass. In the Fruit class, the eat() method is
overridden in the Apple and Orange classes to provide specific details about the taste of each
fruit.

5."How does polymorphism work in the Shape class example with the Circle, Triangle,
and Square subclasses?"

Answer: Polymorphism allows the Shape reference to point to objects of its subclasses
(Circle, Triangle, Square). When calling methods like draw() and erase() using the Shape
reference, the appropriate overridden method of the subclass is executed, demonstrating
dynamic method dispatch.

You might also like