0% found this document useful (0 votes)
2 views20 pages

Classwork Programs

The document provides a series of Java programming classes and examples covering various concepts such as functions, objects, garbage collection, polymorphism, constructors, and inheritance. It includes code snippets for classes like Soldier, Box, and Weapon, demonstrating their functionalities and interactions. Additionally, it discusses arrays, static variables, command line arguments, and the implementation of inheritance in Java.

Uploaded by

BlazeSpace YT
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Classwork Programs

The document provides a series of Java programming classes and examples covering various concepts such as functions, objects, garbage collection, polymorphism, constructors, and inheritance. It includes code snippets for classes like Soldier, Box, and Weapon, demonstrating their functionalities and interactions. Additionally, it discusses arrays, static variables, command line arguments, and the implementation of inheritance in Java.

Uploaded by

BlazeSpace YT
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Class 1 (Functions and Objects) 11/06/2021 –

[Link]
public class Soldier
{
int x,y;//instance variable declaration
void fire()
{
[Link]("Firing from "+x+"
"+y);
}
}

[Link]
public class SoldierMain
{
public static void main(String[] args)
{
Soldier s1 = new Soldier();
s1.x= 10;
s1.y= 10;
[Link]();

Soldier s2 = new Soldier();


s2.x= 20;
s2.y= 20;
[Link]();

Soldier s3 = new Soldier();


s3.x= 30;
s3.y= 30;
[Link]();

}
}
Class 2 (Garbage collection in JVM and Polymorphism) 13/06/2021 –

Garbage Collection and finalize function

[Link]
public class Soldier
{
int x,y;//instance variable declaration
void fire()
{
[Link]("Firing from "+x+"
"+y);
}
@Override
protected void finalize() throws Throwable
{
// TODO Auto-generated method stub
[Link]("Soldier Object
Garbage Collected");
}

[Link]

public class SoldierMain


{
public static void main(String[] args)
{

Soldier s1 = new Soldier();

s1.x= 10;
s1.y= 10;
[Link]();

Soldier s2 = new Soldier();


s2.x= 20;
s2.y= 20;
[Link]();

Soldier s3 = new Soldier();


s3.x= 30;
s3.y= 30;
[Link]();

s1=s2;

[Link]();
}
}

POLYMORPHISM

[Link]
public class Test
{
//Define a function that takes 2 int values
as parameters and returns the sum.

int sum(int x, int y)//Function Definition


x and y are parameters.
{
//[Link](x+y);
return x+y;
}

//polymorphism
//poly means many
//morphism means meaning

//polymorphism in java is of 2 types.


// 1 is function overloading and 2nd is
function over riding
//function Overloading - Same function name
but different parameters list
//(the number and nature of the parameters
maybe different).

//Overloaded function of sum concept.


(number of parameters are different.

int sum(int x, int y, int z)


{
return x+y+z;
}
//Overloaded function of sum concept.
(nature of parameters are different).
int sum(double x, int y, int z)
{
//double is higher type and int is
lower type.
//int use 4 bytes of space to store.
//double require more bits to store.
double a= x+y+z;
//type casting or narrowing conversion.
int result = (int)a;
return result;
}

[Link]
public class TestMain
{
public static void main(String[] args)
{
Test t1 = new Test();

int result = [Link](3, 5);//funtion


call
//3 and 5 are the arguments

[Link]("result is
"+result);

result = [Link](3.3, 5, 6);


[Link]("Result is
"+result);
}
}
Class 3 (Constructor) 15/06/2021 –

[Link]
public class Box
{
double length,width,height;
// Constructor - because it constructs the
values at the time of object creation.
//Constructor - Constructor are like functions
with a same name as
//the class name and no return type not
even VOID.
//Constructor are generally used to
intialize instance variables of an Object
//when it is created.

Box()//Constructor definition- This


automatically gets executed when we will
//do new box in the main
{
length = 10;
width = 10;
height = 10;
}

//constructor overloading
// Box(double l, double w, double h)
// {
// length =l;
// width =w;
// height =h;
// }

Box(double length, double width, double


height)
{
[Link]=length;//[Link] refers
to the instance variable and only length
refers to the parameter.
//"this" is a keyword in java that
refers to the current class object
//in this context this keyword refers
to box object that got created through this
constructor.
[Link] =width;
[Link] =height;
}

double calculateVolume()
{
return length*width*height;
}

//function that take class type datatypes


as parameter and also return type as a class
type.

//Define a function called boxIncrementer


that takes a box type of object as a parameter
and also return a box type

Box boxIncrementer(Box b1)


{
return b1;
}

}
[Link]
public class BoxMain
{
public static void main(String[] args)
{
Box b1 = new Box();
Box b2 = new Box();

// [Link]= 3.5;
// [Link] = 5;
// [Link]= 10;
double volume = [Link]();
[Link]("Volume of the box
is "+volume);

[Link]= 7;
[Link] = 2;
[Link]= 13.6;
volume = [Link]();
[Link]("Volume of the box
is "+volume);

//Overloaded Constructor
Box b3 = new Box(10.3, 11.5,
12.9);//values which we pass are called
arguments.
volume = [Link]();
[Link]("Volume of the box
is: "+volume);

}
}
Class 4 20/06/2021 –

Datatype with class as parameter and used it as function to calculate value of variable
public class Box
{
double length,width,height;
// Constructor - because it constructs the
values at the time of object creation.

//Constructor - Constructor are like


functions with a same name as
//the class name and no return type not
even VOID.
//Constructor are generally used to
intialize instance variables of an Object
//when it is created.

Box()//Constructor definition- This


automatically gets executed when we will
//do new box in the main
{
length = 20;
width = 10;
height = 10;
}

//constructor overloading
// Box(double l, double w, double h)
// {
// length =l;
// width =w;
// height =h;
// }

Box(double length, double width, double


height)
{
// [Link]=length;//[Link] refers
to the instance variable and only length
refers to the parameter.
// //"this" is a keyword in java that
refers to the current class object
// //in this context this keyword refers
to
//box object that got created through
this constructor.
// [Link] =width;
// [Link] =height;

[Link] =length;
[Link] = width;
[Link] = height;

double calculateVolume()
{
return length*width*height;
}

//function that take class type datatypes


as parameter and also return type as a class
type.

//Define a function called boxIncrementer


that takes a box type of object as
//a parameter and also return a box object
//whose dimension is 10 more than the
original box.

Box boxIncrementer(Box b1)


{
Box box = new Box();
[Link] = [Link] +10;
[Link] = [Link] +10;
[Link] = [Link] +10;
return box;
}

}
[Link]

public class BoxMain


{
public static void main(String[] args)
{
Box b1 = new Box();
Box b2 = new Box();

// [Link]= 3.5;
// [Link] = 5;
// [Link]= 10;
double volume = [Link]();
[Link]("Volume of the box
is "+volume);

// [Link]= 7;
// [Link] = 2;
// [Link]= 13.6;
volume = [Link]();
[Link]("Volume of the box1
is "+volume);

//Overloaded Constructor
Box b3 = new Box(10.3, 11.5,
12.9);//values which we pass are called
arguments.
volume = [Link]();
[Link]("Volume of the box
is: "+volume);

Box box = [Link](b3);

[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
volume = [Link]();

}
}

Used Weapon class as parameter for a new function in Soldier Class and called function of weapon
class in the new function of Soldier Class

[Link]

public class Weapon


{
double size;
String name;
double weight;

Weapon()
{
name="Ak47";
size=100;
weight=100;
}
Weapon(String name, double size, double
weight)
{
[Link] = name;
[Link] = size;
[Link] = weight;
}
void attack()
{
[Link]("Attacking with
"+name+"with size "+size+"with weight
"+weight);
}

[Link]

public class Soldier


{
int x,y;//instance variable declaration
void fire()
{
[Link]("Firing from "+x+"
"+y);
}

//Define a overloaded fire function that


accepts a weapon type of object as a
//Parameter
void fire(Weapon w)
{
[Link]();
}

@Override
protected void finalize() throws Throwable
{
// TODO Auto-generated method stub
[Link]("Soldier Object
Garbage Collected");
}
}

[Link]

public class SoldierMain


{
public static void main(String[] args)
{
Soldier s1 = new Soldier();

s1.x= 10;
s1.y= 10;
[Link]();

Soldier s2 = new Soldier();


s2.x= 20;
s2.y= 20;
[Link]();

Soldier s3 = new Soldier();


s3.x= 30;
s3.y= 30;
[Link]();

s1=s2;

[Link]();

Weapon w1 = new Weapon("MAG-7", 15,


2);
//call the overloaded fire function.
[Link](w1);
}
}
Class 5 23/06/2021 –

Arrays and Static variable

Test_Main.java

public class Test_Main


{
public static void main(String[] args)
{
int i;//simple variable declaration

i=10;//simple variable can hold only 1


value at a particular time.

//Array variables can hold multiple


values at a time.

int j[];//Array variable declaration


j = new int[3];//int array object
created that can hold 3 int values and will be
reference variable.
j[0] = 10;
j[1] = 11;
j[2] = 12;

char k[];

k = new char[3];
k[0] = 'R';
k[1] = 'I';
k[2] = 'S';

String l[];
l = new String[3];

l[0] = "RI";
l[1] = "SH";
l[2] = "UL";
//Arrays have a property called length
that represents the length of the array
[Link]([Link]);
//Display all the elements of String
array using a for loop
for(int a=0;a<[Link];a++)
{
[Link](l[a]);
}
[Link]("\n");

Box box[];
box = new Box[3];

Box b1 = new Box(10,10,10);


Box b2 = new Box(20,10,30);
Box b3 = new Box(30,10,10);

box[0] = b1;
box[1] = b2;
box[2] = b3;

//Using a for loop find the total


volume of all the 3 boxes.
double total = 0;

// for (int a = 0;a<[Link];a++)


// {
// total = total +
box[a].calculateVolume();
// }

[Link]("Total is =
"+total);

[Link](box[0].calculateVolume());
}
}
Class 6 26/06/2021 –

Command Line

[Link]

public class Test


{
public static void main(String
args[])//function definition- args is a String
array type of parameter
{
//psvm will be called by jvm when we
run the program
//jvm passes a String array type of
Argument

//To compile a java program in cmd we


do javac [Link].
//We get [Link] this is also
called a byte code.

//That is the reason Java is called


platform independent.

//java Test
//TO run the java program with command
line inputs we can also do
//java Test 10 20 30
//args[0] will contain 10
//args[1] will contain 20
//args[2] will contain 30
//Using a for loop display all the
elements of the String args[]

for(int i = 0; i<[Link];i++)
{
[Link](args[i]);
}

}
}

Inheritance

[Link]
package box;

public class Box


{
int length,width,height;

Box()
{
length = 10;
width = 10;
height = 10;
}
Box(int length, int width, int height)
{
[Link] = length;
[Link] = width;
[Link] = height;
}

int calculateVolume()
{
return length*width*height;
}

}
[Link]
package box;

public class BoxWeight extends Box


{
double weight;

BoxWeight()
{
super();//Super calls the immediate
super class constructor
weight = 10;
}
BoxWeight(int length, int width, int
height, double weight)
{
// [Link] = length;
// [Link] = width;
// [Link] = height;
super(length,width,height);//it calls
the immediate super class constructor
[Link] = weight;
}
}

//Create a class called box shipment which


extends box weight
//Create a class called box main, create a
object of box shipment and call calculate
volume function.

You might also like