1
Week 1:
Program 1:
import java.util.Scanner;
class Week1a{
public static void main(String[] args){
String s1,s2;
System.out.println("enter a string");
Scanner s = new Scanner(System.in);
s1 = s.next();
System.out.println("enter 2nd string");
s2 = s.next();
System.out.println(s1 +" and "+ s2);
}
}
Output:
enter a string
RamCharan
enter 2nd string
NTR
RamCharan and NTR
Program 2:
import java.util.Scanner;
class Week1b{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int hr, min, sec;
String m;
System.out.println("enter hrs");
Gokul Annam
322103383053
2
hr = s.nextInt();
System.out.println("enter mins");
min = s.nextInt();
System.out.println("enter secs");
sec = s.nextInt();
System.out.println("am or pm");
m = s.next();
if(m.equals("am"))
{
if(hr == 12)
{
hr = 0;
}
System.out.printf("%02d:%02d:%02d",hr,min,sec);
}
else if(m.equals("pm"))
{
hr = hr+12;
System.out.printf("%02d:%02d:%02d",hr,min,sec);
}
else
Gokul Annam
322103383053
3
System.out.println("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 java.util.Scanner;
class Week2a{
public static void main(String[] args){
int a[] = new int[10];
int i;
Week2a w = new Week2a();
Scanner s = new Scanner(System.in);
System.out.println("enter 5 values");
for(i=0; i<5; i++)
{
a[i]= s.nextInt();
}
for(i=0;i<5;i++)
{
if(w.isprime(a[i]) && w.isOdd(a[i]))
{
System.out.print(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 java.util.Scanner;
class Week2b{
public static void main(String[] args){
int a[][] = new int[3][5];
int i,j;
Scanner s = new Scanner(System.in);
for(i=0;i<3;i++)
{
System.out.println("Year "+ (i+1));
for(j=0;j<3;j++)
{
System.out.printf("no. of items sold of id %d in Year %d: ",j+1,i+1);
a[i][j] = s.nextInt();
}
}
//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++)
{
System.out.print(a[i][j] + " ");
}
System.out.print("\n");
}
for(i=0;i<3;i++)
{
int big = 0,bigid = 0;
System.out.printf("in Year %d ",i+1);
for(j=0;j<3;j++)
{
if(a[i][j] > big)
{
big = a[i][j];
bigid = j;
}
}
System.out.printf("for id %d demand is more, with sold items count %d",bigid+1,big);
bigid = 0;
big = 0;
System.out.print("\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)
{
this.ht = ht;
this.wt = wt;
this.dpt = dpt;
}
public int Volume()
{
return ht*wt*dpt;
}
}
public class Week3a{
public static void main(String[] args){
Box b = new Box(2,3,4);
System.out.println(b.Volume()+" 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)Math.pow(n1,n2);
}
public static double powerDouble(double n1, double n2)
{
return Math.pow(n1,n2);
}
public Calculator()
{
count++;
System.out.println("obj count is "+ count);
}
}
public class Week3b{
public static void main(String[] args){
Calculator obj = new Calculator();
Calculator obj2 = new Calculator();
System.out.println(Calculator.powerInt(2,3));
System.out.println(Calculator.powerDouble(2,3.5));
}
}
Output:
obj count is 1
obj count is 2
8
11.313708498984761
Gokul Annam
322103383053
11
Week 4:
Program 1:
import java.util.Scanner;
public class Week4a{
public static void main(String[] args){
String a[] = new String[5];
Scanner s = new Scanner(System.in);
int i;
int vC = 0, cC = 0;
Week4a obj = new Week4a();
System.out.println("enter strings:");
for(i=0;i<5;i++)
{
a[i] = s.nextLine();
}
for(i=0;i<5;i++)
{
System.out.print(a[i]+" ");
}
System.out.print("\n");
for(i=0;i<5;i++)
{
obj.vOrC(a[i]);
}
Gokul Annam
322103383053
12
public void vOrC(String s){
s = s.toLowerCase();
int i;
int vC = 0;
int cC = 0;
for(i=0;i<s.length();i++)
{
if(s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' ||
s.charAt(i) == 'o' ||s.charAt(i) == 'u' )
{
vC++;
}
else if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z' )
{
cC++;
}
}
System.out.printf("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 java.util.Scanner;
import java.util.Arrays;
public class Week4b {
public static void main(String[] args){
String s1,s2; //initialised strings and taking input using scanner
Scanner s = new Scanner(System.in);
System.out.println("enter string 1:");
s1 = s.next();
System.out.println("enter string 2:");
s2 = s.next();
System.out.println(s1 + " and " + s2);
char s1char[] = new char[50]; //initialising char arrays
char s2char[] = new char[50];
s1char = s1.toCharArray(); //converting strings to char arrays
s2char = s2.toCharArray();
Arrays.sort(s1char); //sorting the strings as they are converted into char arrays
Arrays.sort(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
System.out.println(s1 + " and " + s2 + " after sorting");
Gokul Annam
322103383053
14
if(s1.equalsIgnoreCase(s2))
{
System.out.println("anagrams");
}
else
{
System.out.println("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() {
System.out.println("Animal makes a sound");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal(); // Creating an object of Animal class
animal.sound(); // Output: Animal makes a sound
Dog dog = new Dog(); // Creating an object of Dog class
dog.sound(); // Output: Dog barks
}
}
OUTPUT:
Animal makes a sound
Dog barks
Gokul Annam
322103383053
16
Multilevel Inheritance:
// Parent class
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
// Grandchild class inheriting from Dog
class Puppy extends Dog {
void sound() {
System.out.println("Puppy whimpers");
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal(); // Creating an object of Animal class
animal.sound(); // Output: Animal makes a sound
Dog dog = new Dog(); // Creating an object of Dog class
dog.sound(); // Output: Dog barks
Gokul Annam
322103383053
17
Puppy puppy = new Puppy(); // Creating an object of Puppy class
puppy.sound(); // Output: Puppy whimpers
}
}
OUTPUT:
Animal makes a sound
Dog barks
Puppy whimpers
Gokul Annam
322103383053
18
Week 6:
Program 1:
import java.util.Scanner;
public class Week5a{
public static void main(String[] args){
Cost obj = new Cost();
obj.type("nexon"); //method of class vehicle
obj.brandType("tata"); //method of class brand
obj.price(1000000); //method of class cost
}
}
//vehicle is parent class, brand is child of vehicle,
//cost is child of brand
class Vehicle{
public Vehicle()
{
System.out.println("this is vehicle constructor");
}
public Vehicle(int n)
{
System.out.println("this is parameterised vehicle constructor");
}
public void type(String s)
{
System.out.println("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*/
System.out.println("this is brand constructor");
}
public void brandType(String s)
{
System.out.printf("of the brand %s\n",s);
}
}
class Cost extends Brand{
public Cost()
{
super();
System.out.println("this is cost constructor");
}
public void price(int n)
{
System.out.println("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) {
this.radius = radius;
this.height = height;
}
@Override
public double calculateVolume() {
Gokul Annam
322103383053
21
return Math.PI * radius * radius * height;
}
@Override
public double calculateSurfaceArea() {
return 2 * Math.PI * radius * (radius + height);
}
// Derived class Cone
class Cone extends Figure_3D {
private double radius;
private double height;
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double calculateVolume() {
return (Math.PI * radius * radius * height) / 3;
}
@Override
public double calculateSurfaceArea() {
double slantHeight = Math.sqrt(radius * radius + height * height);
return Math.PI * radius * (radius + slantHeight);
Gokul Annam
322103383053
22
// Derived class Sphere
class Sphere extends Figure_3D {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
@Override
public double calculateVolume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
@Override
public double calculateSurfaceArea() {
return 4 * Math.PI * 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) {
System.out.println("Volume: " + figure.calculateVolume());
System.out.println("Surface Area: " + figure.calculateSurfaceArea());
System.out.println("----------------------");
}
}
}
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 () {
this.name = "Gokul";
this.rollNumber = 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);
System.out.println("Student Report:");
System.out.println("Name: " + w.getName());
System.out.println("Roll Number: " + w.getRollNumber());
System.out.println("Sports Information: " + w.getSportsInfo());
}
}
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 java.util.Vector;
public class WrapperEx {
static void checkobject(Object o)
{
if (o instanceof Integer) {
System.out.println("The Object belongs to Integer class: ");
} else if (o instanceof Double) {
System.out.println("The Object belongs to Double class:" );
} else if (o instanceof Character) {
System.out.println("The Object belongs to Character class:" );
} else if (o instanceof String) {
System.out.println("The Object belongs to String class:");
} else if (o instanceof Boolean) {
System.out.println("The Object belongs to Boolean class:");
}else if (o instanceof Float) {
System.out.println("The Object belongs to Float class:" );
}
}
public static void main(String args[])
{
Vector<Object> v = new Vector<>();
v.add(10);
v.add(3.4f);
v.add('S');
v.add("Java");
Gokul Annam
322103383053
27
v.add(true);
for(int i=0;i<v.size();i++)
{
System.out.println();
System.out.println(v.get(i));
Object o = v.get(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 java.util.Random;
import java.util.Scanner;
public class GenerateRandom {
public static void main(String args[])
{
int x1,x2, n;
System.out.println("Enter two numbers");
Scanner sc = new Scanner(System.in);
x1=sc.nextInt();
x2=sc.nextInt();
System.out.println("Enter how many randrom numbers you need");
n = sc.nextInt();
// create instance of Random class
Random rand = new Random();
for(int i=1;i<=n;i++)
{
int num = rand.nextInt(x2 - x1 + 1) + x1;
System.out.println("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 java.util.ArrayList;
import java.util.Scanner;
public class ArrayListEx {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
ArrayList<Integer> al = new ArrayList<>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
System.out.println("The array created was "+al);
System.out.println("The size of the array is "+al.size());
al.remove(2); //Removing item at index 1
System.out.println("After removing item at index 1 array is "+al);
System.out.println("Enter the position of which you want to get the
value");
n = sc.nextInt();
System.out.println("Value at position "+n+" is "+al.get(n));
sc.close();
}
}
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 java.util.HashMap;
import java.util.Map;
class Employee
{
private int id;
private String name;
private String department;
// Constructor
public Employee(int id, String name, String department) {
this.id = id;
this.name = name;
this.department = department;
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
Gokul Annam
322103383053
33
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = 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
employeeMap.put(101, new Employee(101, "Rajesh", "HR"));
employeeMap.put(102, new Employee(102, "Suresh", "Finance"));
employeeMap.put(103, new Employee(103, "Pradeep", "IT"));
// Perform ID-based search operation
int searchId = 102;
Employee searchedEmployee = employeeMap.get(searchId);
if (searchedEmployee != null) {
System.out.println("Employee found with ID " + searchId + ":");
System.out.println("Name: " + searchedEmployee.getName());
System.out.println("Department: " +
searchedEmployee.getDepartment());
} else {
Gokul Annam
322103383053
34
System.out.println("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 java.util.Scanner;
public class ExceptionEx {
public static void main(String args[])
{
int n1,n2,r;
System.out.println("Enter two numbers");
Scanner sc = new Scanner(System.in);
try{
n1 = Integer.parseInt(sc.nextLine());
n2 = Integer.parseInt(sc.nextLine());
if (n2 != 0)
System.out.println("The division of given numbers is "+(n1/n2));
else
throw new ArithmeticException("Division by zero is invalid");
}
catch (NumberFormatException e)
{
System.out.println("NumberFormatException: Please enter valid integers.");
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException: " + e.getMessage());
}
finally
{
Gokul Annam
322103383053
36
sc.close();
System.out.println("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