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

Lab 4

The document describes 7 experiments that demonstrate different object-oriented programming concepts in Java like classes, objects, constructors, this keyword, static methods, and instance variables. Each experiment provides the aim, source code, and output for a Java program implementing the concept.

Uploaded by

Horde Lucifer
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)
14 views14 pages

Lab 4

The document describes 7 experiments that demonstrate different object-oriented programming concepts in Java like classes, objects, constructors, this keyword, static methods, and instance variables. Each experiment provides the aim, source code, and output for a Java program implementing the concept.

Uploaded by

Horde Lucifer
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

EXPERIMENT-4

AIM (a): WAP that creates a class circle with instance variables for the centre and the radius. Initialize
and display its variables.

TOOLS USED: Notepad, CommandPrompt

SOURCE CODE:

public class circleclass {

public double x=9,y=9;


public double radius=7;

public static void main(String[] args) {


circleclass c=new circleclass();
System.out.println("CENTRE OF CIRCLE:");
System.out.println("x-coordinate:"+c.x+" y-coordinate:"+c.y);
System.out.println("RADIUS OF CIRCLE="+c.radius+" UNITS");
}

}
OUTPUT (a):
AIM (b): Modify experiment 1 to have a constructor in class circle to initialize its variables.

TOOLS USED: Notepad, CommandPrompt

SOURCE CODE:

public class circleclass {

public double x,y;


public double radius;

public circleclass()
{
System.out.println("CONSTRUCTOR CALLED");
x=9;
y=9;
radius=7;
}

public static void main(String[] args) {


circleclass c=new circleclass();
System.out.println("CENTRE OF CIRCLE:");
System.out.println("x-coordinate:"+c.x+" y-coordinate:"+c.y);
System.out.println("RADIUS OF CIRCLE="+c.radius+" UNITS");
}
}
OUTPUT (b):
AIM (c): Modify experiment 2 to show constructor overloading.

TOOLS USED: Notepad, CommandPrompt

SOURCE CODE:

public class circleclass {

public double x,y;


public double radius;

public circleclass()
{
System.out.println("CONSTRUCTOR 1 CALLED");
x=9;
y=9;
radius=7;
}

public circleclass(double newradius)


{
System.out.println("CONSTRUCTOR 2 CALLED");
System.out.println("NEW RADIUS OF CIRCLE="+newradius);
}

public static void main(String[] args) {


circleclass c=new circleclass();
System.out.println("CENTRE OF CIRCLE:");
System.out.println("x-coordinate:"+c.x+" y-coordinate:"+c.y);
System.out.println("RADIUS OF CIRCLE="+c.radius+" UNITS");
circleclass c2=new circleclass(10);
}

}
OUTPUT (c):
AIM (d): Write a program to display the use of this keyword.

TOOLS USED: Notepad, CommandPrompt

SOURCE CODE:

public class thiskeyword {

static int val1;


static int val2;
public thiskeyword(int val1,int val2)
{
this.val1=val1+val1;
this.val2=val2+val2;
}
static void display()
{
System.out.println("VALUE 1="+val1);
System.out.println("VALUE 2="+val2);
}
public static void main(String[] args) {
thiskeyword obj=new thiskeyword(30,20);
thiskeyword.display();
}
}
OUTPUT (d):
AIM (e): Write a program that can count the number of instances created for the class.

TOOLS USED: Notepad, CommandPrompt

SOURCE CODE:

public class objects_number {

static int count=0;


public objects_number()
{
count++;
}

public static void main(String[] args) {

objects_number obja=new objects_number(); objects_number


objb=new objects_number(); objects_number objc=new
objects_number(); objects_number objd=new objects_number();
System.out.println("NUMBER OF OBJECTS CREATED="+count);

}
}
OUTPUT (e):
AIM (f): Write a Java Program to get the cube of a given number using the static method.

TOOLS USED: Notepad, CommandPrompt

SOURCE CODE:

import java.util.Scanner;
public class static_cube {

static void cube(float num)


{
System.out.println("CUBE OF "+num+"="+num*num*num);
}

public static void main(String[] args) {

float number;
Scanner s=new Scanner(System.in);
System.out.print("ENTER THE NUMBER:");
number=s.nextInt();
cube(number);
}
}
OUTPUT (f):
AIM (g): Write a program that describes a class person. It should have instance variables to record name,
age and salary. Create a person object. Set and display its instance variables.

TOOLS USED: Notepad, CommandPrompt

SOURCE CODE:

public class person {

String name;
int age;
float salary;

public void display()


{
System.out.println("NAME:"+name);
System.out.println("AGE:"+age);
System.out.println("SALARY:"+salary);
}

public static void main(String[] args) {


person p1=new person();
System.out.println("PERSON 1");
p1.name="Avinab";
p1.age=20;
p1.salary=10000;
p1.display();
System.out.print("\n");
person p2=new person();
System.out.println("PERSON 2");
p2.name="Sambhav";
p2.age=22;
p2.salary=20000;
p2.display();
}

}
OUTPUT (g):

You might also like