1
Week 1:
Program 1:
import [Link];
class Week1a{
public static void main(String[] args){
String s1,s2;
[Link]("enter a string");
Scanner s = new Scanner([Link]);
s1 = [Link]();
[Link]("enter 2nd string");
s2 = [Link]();
[Link](s1 +" and "+ s2);
}
}
Output:
enter a string
RamCharan
enter 2nd string
NTR
RamCharan and NTR
Program 2:
import [Link];
class Week1b{
public static void main(String[] args){
Scanner s = new Scanner([Link]);
int hr, min, sec;
String m;
[Link]("enter hrs");
Gokul Annam
322103383053
2
hr = [Link]();
[Link]("enter mins");
min = [Link]();
[Link]("enter secs");
sec = [Link]();
[Link]("am or pm");
m = [Link]();
if([Link]("am"))
{
if(hr == 12)
{
hr = 0;
}
[Link]("%02d:%02d:%02d",hr,min,sec);
}
else if([Link]("pm"))
{
hr = hr+12;
[Link]("%02d:%02d:%02d",hr,min,sec);
}
else
Gokul Annam
322103383053
3
[Link]("enter am or pm only");
}
}
}
Output:
enter hrs
3
enter mins
45
enter secs
12
am or pm
pm
15:45:12
Gokul Annam
322103383053
4
Week 2:
Program 1:
import [Link];
class Week2a{
public static void main(String[] args){
int a[] = new int[10];
int i;
Week2a w = new Week2a();
Scanner s = new Scanner([Link]);
[Link]("enter 5 values");
for(i=0; i<5; i++)
{
a[i]= [Link]();
}
for(i=0;i<5;i++)
{
if([Link](a[i]) && [Link](a[i]))
{
[Link](a[i]+ " ");
}
}
boolean isOdd(int i)
{
if(i%2 != 0)
Gokul Annam
322103383053
5
{
return true;
}
else
{
return false;
}
boolean isprime(int n)
{
int count = 0;
for(int i=2; i<=n/2; i++)
{
if(n%i == 0)
{
count++;
break;
}
}
if(count == 0)
{
return true;
}
else
{
return false;
Gokul Annam
322103383053
6
}
}
Output:
enter 5 values
2
4
5
6
7
57
Program 2:
import [Link];
class Week2b{
public static void main(String[] args){
int a[][] = new int[3][5];
int i,j;
Scanner s = new Scanner([Link]);
for(i=0;i<3;i++)
{
[Link]("Year "+ (i+1));
for(j=0;j<3;j++)
{
[Link]("no. of items sold of id %d in Year %d: ",j+1,i+1);
a[i][j] = [Link]();
}
}
//consider yr1,2,3 are 3 financial years and id 1,2,3 are id numbers of 3 items
//years taken in rows and ids in columns
for(i=0;i<3;i++)
Gokul Annam
322103383053
7
{
for(j=0;j<3;j++)
{
[Link](a[i][j] + " ");
}
[Link]("\n");
}
for(i=0;i<3;i++)
{
int big = 0,bigid = 0;
[Link]("in Year %d ",i+1);
for(j=0;j<3;j++)
{
if(a[i][j] > big)
{
big = a[i][j];
bigid = j;
}
}
[Link]("for id %d demand is more, with sold items count %d",bigid+1,big);
bigid = 0;
big = 0;
[Link]("\n");
}
}
}
Output:
Year 1
Gokul Annam
322103383053
8
no. of items sold of id 1 in Year 1: 5
no. of items sold of id 2 in Year 1: 4
no. of items sold of id 3 in Year 1: 6
Year 2
no. of items sold of id 1 in Year 2: 3
no. of items sold of id 2 in Year 2: 5
no. of items sold of id 3 in Year 2: 4
Year 3
no. of items sold of id 1 in Year 3: 1
no. of items sold of id 2 in Year 3: 3
no. of items sold of id 3 in Year 3: 2
546
354
132
in Year 1 for id 3 has more demand with sold items count 6
in Year 2 for id 2 has more demand with sold items count 5
in Year 3 for id 2 has more demand with sold items count 3
Gokul Annam
322103383053
9
Week 3:
Program 1:
class Box{
public int ht,wt,dpt;
public Box(int ht, int wt, int dpt)
{
[Link] = ht;
[Link] = wt;
[Link] = dpt;
}
public int Volume()
{
return ht*wt*dpt;
}
}
public class Week3a{
public static void main(String[] args){
Box b = new Box(2,3,4);
[Link]([Link]()+" is volume");
}
}
Output:
24 is volume
Program 2:
class Calculator{
static int count;
Gokul Annam
322103383053
10
public static int powerInt(int n1, int n2)
{
return (int)[Link](n1,n2);
}
public static double powerDouble(double n1, double n2)
{
return [Link](n1,n2);
}
public Calculator()
{
count++;
[Link]("obj count is "+ count);
}
}
public class Week3b{
public static void main(String[] args){
Calculator obj = new Calculator();
Calculator obj2 = new Calculator();
[Link]([Link](2,3));
[Link]([Link](2,3.5));
}
}
Output:
obj count is 1
obj count is 2
8
11.313708498984761
Gokul Annam
322103383053
11
Week 4:
Program 1:
import [Link];
public class Week4a{
public static void main(String[] args){
String a[] = new String[5];
Scanner s = new Scanner([Link]);
int i;
int vC = 0, cC = 0;
Week4a obj = new Week4a();
[Link]("enter strings:");
for(i=0;i<5;i++)
{
a[i] = [Link]();
}
for(i=0;i<5;i++)
{
[Link](a[i]+" ");
}
[Link]("\n");
for(i=0;i<5;i++)
{
[Link](a[i]);
}
Gokul Annam
322103383053
12
public void vOrC(String s){
s = [Link]();
int i;
int vC = 0;
int cC = 0;
for(i=0;i<[Link]();i++)
{
if([Link](i) == 'a' || [Link](i) == 'e' || [Link](i) == 'i' ||
[Link](i) == 'o' ||[Link](i) == 'u' )
{
vC++;
}
else if([Link](i) >= 'a' && [Link](i) <= 'z' )
{
cC++;
}
}
[Link]("vowel count of %s is %d and consonent count is %d\n",s,vC,cC);
}
}
Output:
enter strings:
allu arjun
is
hero
of
pushpa
allu arjun is hero of pushpa
Gokul Annam
322103383053
13
vowel count of allu arjun is 4 and consonent count is 5
vowel count of is is 1 and consonent count is 1
vowel count of hero is 2 and consonent count is 2
vowel count of of is 1 and consonent count is 1
vowel count of pushpa is 2 and consonent count is 4
Program 2:
import [Link];
import [Link];
public class Week4b {
public static void main(String[] args){
String s1,s2; //initialised strings and taking input using scanner
Scanner s = new Scanner([Link]);
[Link]("enter string 1:");
s1 = [Link]();
[Link]("enter string 2:");
s2 = [Link]();
[Link](s1 + " and " + s2);
char s1char[] = new char[50]; //initialising char arrays
char s2char[] = new char[50];
s1char = [Link](); //converting strings to char arrays
s2char = [Link]();
[Link](s1char); //sorting the strings as they are converted into char arrays
[Link](s2char);
s1 = new String(s1char); //storing the sorted ones back to strings, we can also create
s2 = new String(s2char); //new string to store sorted strings
[Link](s1 + " and " + s2 + " after sorting");
Gokul Annam
322103383053
14
if([Link](s2))
{
[Link]("anagrams");
}
else
{
[Link]("not anagrams");
}
}
Output:
enter string 1:
ok
enter string 2:
aithe
ok and aithe
ko and aehit after sorting
not anagrams
Gokul Annam
322103383053
15
Week 5:
Single inheritance:
// Parent class
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal(); // Creating an object of Animal class
[Link](); // Output: Animal makes a sound
Dog dog = new Dog(); // Creating an object of Dog class
[Link](); // Output: Dog barks
}
}
OUTPUT:
Animal makes a sound
Dog barks
Gokul Annam
322103383053
16
Multilevel Inheritance:
// Parent class
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
// Grandchild class inheriting from Dog
class Puppy extends Dog {
void sound() {
[Link]("Puppy whimpers");
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal(); // Creating an object of Animal class
[Link](); // Output: Animal makes a sound
Dog dog = new Dog(); // Creating an object of Dog class
[Link](); // Output: Dog barks
Gokul Annam
322103383053
17
Puppy puppy = new Puppy(); // Creating an object of Puppy class
[Link](); // Output: Puppy whimpers
}
}
OUTPUT:
Animal makes a sound
Dog barks
Puppy whimpers
Gokul Annam
322103383053
18
Week 6:
Program 1:
import [Link];
public class Week5a{
public static void main(String[] args){
Cost obj = new Cost();
[Link]("nexon"); //method of class vehicle
[Link]("tata"); //method of class brand
[Link](1000000); //method of class cost
}
}
//vehicle is parent class, brand is child of vehicle,
//cost is child of brand
class Vehicle{
public Vehicle()
{
[Link]("this is vehicle constructor");
}
public Vehicle(int n)
{
[Link]("this is parameterised vehicle constructor");
}
public void type(String s)
{
[Link]("vehicle is "+s);
}
Gokul Annam
322103383053
19
class Brand extends Vehicle{
public Brand()
{
super(1); /*instead of calling default constructor of super class (here it is vehicle)
we are calling parameterized constructor of super class*/
[Link]("this is brand constructor");
}
public void brandType(String s)
{
[Link]("of the brand %s\n",s);
}
}
class Cost extends Brand{
public Cost()
{
super();
[Link]("this is cost constructor");
}
public void price(int n)
{
[Link]("price is "+ n);
}
}
Output:
this is parameterised vehicle constructor
this is brand constructor
this is cost constructor
Gokul Annam
322103383053
20
vehicle is nexon
of the brand tata
price is 1000000
Program 2:
// Base class Figure_3D
class Figure_3D {
// Method to calculate volume
public double calculateVolume() {
return 0.0; // Default implementation, overridden in derived classes
}
// Method to calculate surface area
public double calculateSurfaceArea() {
return 0.0; // Default implementation, overridden in derived classes
}
// Derived class Cylinder
class Cylinder extends Figure_3D {
private double radius;
private double height;
public Cylinder(double radius, double height) {
[Link] = radius;
[Link] = height;
}
@Override
public double calculateVolume() {
Gokul Annam
322103383053
21
return [Link] * radius * radius * height;
}
@Override
public double calculateSurfaceArea() {
return 2 * [Link] * radius * (radius + height);
}
// Derived class Cone
class Cone extends Figure_3D {
private double radius;
private double height;
public Cone(double radius, double height) {
[Link] = radius;
[Link] = height;
}
@Override
public double calculateVolume() {
return ([Link] * radius * radius * height) / 3;
}
@Override
public double calculateSurfaceArea() {
double slantHeight = [Link](radius * radius + height * height);
return [Link] * radius * (radius + slantHeight);
Gokul Annam
322103383053
22
// Derived class Sphere
class Sphere extends Figure_3D {
private double radius;
public Sphere(double radius) {
[Link] = radius;
}
@Override
public double calculateVolume() {
return (4.0 / 3.0) * [Link] * [Link](radius, 3);
}
@Override
public double calculateSurfaceArea() {
return 4 * [Link] * radius * radius;
}
}
public class MamWeek5b {
public static void main(String[] args) {
// Create an array of Figure_3D
Figure_3D[] figures = new Figure_3D[3];
figures[0] = new Cylinder(2.0, 3.0);
figures[1] = new Cone(3.0, 4.0);
figures[2] = new Sphere(5.0);
Gokul Annam
322103383053
23
// Call base class methods
for (Figure_3D figure : figures) {
[Link]("Volume: " + [Link]());
[Link]("Surface Area: " + [Link]());
[Link]("----------------------");
}
}
}
Output:
Volume: 37.69911184307752
Surface Area: 62.83185307179586
----------------------
Volume: 37.69911184307752
Surface Area: 75.39822368615503
----------------------
Volume: 523.5987755982989
Surface Area: 314.1592653589793
----------------------
Gokul Annam
322103383053
24
Week 7:
package studentPackage; //package 1
public class Week6a1 {
private String name;
private int rollNumber;
public Week6a1 () {
[Link] = "Gokul";
[Link] = 53;
}
public String getName() {
return name;
public int getRollNumber() {
return rollNumber;
}
package sportsPackage; //package 2
public interface Week6a2 {
public String getSportsInfo();
}
Gokul Annam
322103383053
25
import studentPackage.Week6a1; //importing packages in another file
import sportsPackage.Week6a2;
public class Week6a3 extends Week6a1 implements Week6a2 {
public String getSportsInfo() {
return "Sports information not available";
public static void main(String[] args){
Week6a3 w = new Week6a3();
//Week6a1 w6a1 = new Week6a1("gokul",53);
[Link]("Student Report:");
[Link]("Name: " + [Link]());
[Link]("Roll Number: " + [Link]());
[Link]("Sports Information: " + [Link]());
}
}
OUTPUT:
Student Report:
Name: Gokul
Roll Number: 53
Sports Information: Sports information not available
Gokul Annam
322103383053
26
Week 8:
Q)Write a program that accepts values of different data types and convert them to corresponding
wrapper classes and display using the vector
import [Link];
public class WrapperEx {
static void checkobject(Object o)
{
if (o instanceof Integer) {
[Link]("The Object belongs to Integer class: ");
} else if (o instanceof Double) {
[Link]("The Object belongs to Double class:" );
} else if (o instanceof Character) {
[Link]("The Object belongs to Character class:" );
} else if (o instanceof String) {
[Link]("The Object belongs to String class:");
} else if (o instanceof Boolean) {
[Link]("The Object belongs to Boolean class:");
}else if (o instanceof Float) {
[Link]("The Object belongs to Float class:" );
}
}
public static void main(String args[])
{
Vector<Object> v = new Vector<>();
[Link](10);
[Link](3.4f);
[Link]('S');
[Link]("Java");
Gokul Annam
322103383053
27
[Link](true);
for(int i=0;i<[Link]();i++)
{
[Link]();
[Link]([Link](i));
Object o = [Link](i);
checkobject(o);
}
}
OUTPUT:
10
The Object belongs to Integer class:
3.4
The Object belongs to Float class:
S
The Object belongs to Character class:
Java
The Object belongs to String class:
true
The Object belongs to Boolean class:
Gokul Annam
322103383053
28
Week 9:
Q)Write a program to generate a set of random numbers between two numbers x1 and x2, and
x1>0.
import [Link];
import [Link];
public class GenerateRandom {
public static void main(String args[])
{
int x1,x2, n;
[Link]("Enter two numbers");
Scanner sc = new Scanner([Link]);
x1=[Link]();
x2=[Link]();
[Link]("Enter how many randrom numbers you need");
n = [Link]();
// create instance of Random class
Random rand = new Random();
for(int i=1;i<=n;i++)
{
int num = [Link](x2 - x1 + 1) + x1;
[Link]("Random Number "+i+" is : " +num);
}
}
}
OUTPUT:
Enter two numbers:
Gokul Annam
322103383053
29
15
Enter how many random numbers you need:
7
Random Number 1 is : 9
Random Number 2 is : 11
Random Number 3 is : 13
Random Number 4 is : 7
Random Number 5 is : 5
Random Number 6 is : 14
Random Number 7 is : 10
Gokul Annam
322103383053
30
Week 10:
Q)Write a program to implement ArrayList class. It should contain add(), get(), remove(), size()
methods. Use dynamic array logic.
import [Link];
import [Link];
public class ArrayListEx {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner([Link]);
ArrayList<Integer> al = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]("The array created was "+al);
[Link]("The size of the array is "+[Link]());
[Link](2); //Removing item at index 1
[Link]("After removing item at index 1 array is "+al);
[Link]("Enter the position of which you want to get the
value");
n = [Link]();
[Link]("Value at position "+n+" is "+[Link](n));
[Link]();
}
}
OUTPUT:
The array created was [10, 20, 30, 40]
Gokul Annam
322103383053
31
The size of the array is 4
After removing item at index 1 array is [10, 20, 40]
Enter the position of which you want to get the value
2
Value at position 2 is 40
Gokul Annam
322103383053
32
Week 11:
Q)Create an employee class containing at least 3 details along with Id, setters and getters. Insert
the
employee objects dynamically key as employee id and value as its corresponding object into a
HashMap. Perform Id based search operation on the HashMap.
import [Link];
import [Link];
class Employee
{
private int id;
private String name;
private String department;
// Constructor
public Employee(int id, String name, String department) {
[Link] = id;
[Link] = name;
[Link] = department;
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public String getName() {
return name;
Gokul Annam
322103383053
33
public void setName(String name) {
[Link] = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
[Link] = department;
}
}
public class HashMapEx {
public static void main(String[] args) {
// Create a HashMap to store employee objects with ID as key
Map<Integer, Employee> employeeMap = new HashMap<>();
// Insert employee objects dynamically into the HashMap
[Link](101, new Employee(101, "Rajesh", "HR"));
[Link](102, new Employee(102, "Suresh", "Finance"));
[Link](103, new Employee(103, "Pradeep", "IT"));
// Perform ID-based search operation
int searchId = 102;
Employee searchedEmployee = [Link](searchId);
if (searchedEmployee != null) {
[Link]("Employee found with ID " + searchId + ":");
[Link]("Name: " + [Link]());
[Link]("Department: " +
[Link]());
} else {
Gokul Annam
322103383053
34
[Link]("Employee with ID " + searchId + " not
found.");
}
}
OUTPUT:
Employee found with ID 102:
Name: Suresh
Department: Finance
Gokul Annam
322103383053
35
Week 12:
EXCEPTION HANDLING:
import [Link];
public class ExceptionEx {
public static void main(String args[])
{
int n1,n2,r;
[Link]("Enter two numbers");
Scanner sc = new Scanner([Link]);
try{
n1 = [Link]([Link]());
n2 = [Link]([Link]());
if (n2 != 0)
[Link]("The division of given numbers is "+(n1/n2));
else
throw new ArithmeticException("Division by zero is invalid");
}
catch (NumberFormatException e)
{
[Link]("NumberFormatException: Please enter valid integers.");
}
catch(ArithmeticException e)
{
[Link]("ArithmeticException: " + [Link]());
}
finally
{
Gokul Annam
322103383053
36
[Link]();
[Link]("Program execution completed !! Thank you");
}
}
}
OUTPUT:
Enter two numbers
10
0
ArithmeticException: Division by zero is invalid
Program execution completed !! Thank you
Gokul Annam
322103383053