Java Record
Java Record
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
Program 2:
import java.util.Scanner;
class Week1b{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
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){
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++)
{
for(i=0;i<3;i++)
{
int big = 0,bigid = 0;
big = a[i][j];
bigid = j;
}
}
}
}
}
Output:
Year 1
Gokul Annam
322103383053
8
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
Gokul Annam
322103383053
9
Week 3:
Program 1:
class Box{
public int ht,wt,dpt;
{
this.ht = ht;
this.wt = wt;
this.dpt = dpt;
}
}
}
}
Output:
24 is volume
Program 2:
class Calculator{
static int count;
Gokul Annam
322103383053
10
}
public static double powerDouble(double n1, double n2)
{
return Math.pow(n1,n2);
}
public Calculator()
{
count++;
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){
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
int vC = 0;
int cC = 0;
for(i=0;i<s.length();i++)
{
}
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
Gokul Annam
322103383053
13
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);
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
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");
}
}
void sound() {
System.out.println("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");
}
}
Gokul Annam
322103383053
17
}
}
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();
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
{
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);
}
}
{
super();
System.out.println("this is cost constructor");
}
}
Output:
this is parameterised vehicle constructor
this is brand 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
this.height = height;
}
@Override
public double calculateVolume() {
Gokul Annam
322103383053
21
@Override
public double calculateSurfaceArea() {
return 2 * Math.PI * radius * (radius + height);
}
this.height = height;
}
@Override
@Override
public double calculateSurfaceArea() {
double slantHeight = Math.sqrt(radius * radius + height * height);
return Math.PI * radius * (radius + slantHeight);
Gokul Annam
322103383053
22
this.radius = radius;
}
@Override
@Override
Gokul Annam
322103383053
23
}
Output:
Volume: 37.69911184307752
Surface Area: 62.83185307179586
----------------------
Volume: 37.69911184307752
Surface Area: 75.39822368615503
----------------------
Volume: 523.5987755982989
Gokul Annam
322103383053
24
Week 7:
package studentPackage; //package 1
public Week6a1 () {
this.name = "Gokul";
this.rollNumber = 53;
}
Gokul Annam
322103383053
25
System.out.println("Student Report:");
System.out.println("Name: " + w.getName());
}
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
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) {
}
}
public static void main(String args[])
{
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
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();
for(int i=1;i<=n;i++)
{
}
}
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()
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListEx {
sc.close();
}
}
OUTPUT:
The array created was [10, 20, 30, 40]
Gokul Annam
322103383053
31
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
this.id = id;
}
public String getName() {
return name;
Gokul Annam
322103383053
33
}
public String getDepartment() {
return department;
}
} else {
Gokul Annam
322103383053
34
}
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;
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:
Gokul Annam
322103383053