JAVA Internals
JAVA Internals
JAVA Internals
EX1
1A. Write a Java program to create Class as Registration with properties as Full
Name(String), Gender(char), Age(int), Height(double), Phone Number(long), and
isMarried(Boolean) and print their values.
/*
Output:
Name: Chaitanya
Gender: M
Age: 19
Height: 5.7
Contact Number: 9177675611
Married: false
*/
JAVA Internals 1
}
}
/*
* Output:
* 10.0
* 10
*/
}
}
/*
* Output:
* 90.01
* 90.01
*/
EX2
2A. 1. Write a Java program to take input as Regd.No and print the branch depending
upon the department code in that Regd.No using else-if and switch statements.
(EgRegNo:
19KD1A0505, 8th character is department Code, 5-CSE, 4-ECE, 3-MECH, 2-EEE etc.
import java.util.Scanner;
import javax.lang.model.util.ElementScanner14;
/*
* Output:
* Enter a Registration Numebr: 20KD1A05G6
* CSE
*/
2B. 1. Write a Java program to read input integers from Command Line Arguments and
print first and second largest numbers
JAVA Internals 2
import java.util.Arrays;
Arrays.sort(arr);
System.out.println("Largest Element: " + arr[size]);
System.out.println("Second Largest Element: " + arr[size - 1]);
}
}
/*
* Output:
* javac EX2B.java
* java EX2B 1 2 3 4 5 6 7 8 9 10
* Largest Element: 10
* Second Largest Element: 9
*/
2C. 1. Write a Java program to take input as Integer array and print even indexed even
numbers and odd indexed odd numbers.
import java.util.*;
}
}
/*
* Output:
* Enter the size of the array: 10
* Enter the elements in the array: 2 3 4 5 6 7 8 9 10 11
* Even Numbers: 2 4 6 8 10
* Odd Numbers: 3 5 7 9 11
*/
EX3
3A. Write a Java program to take input as Decimal number and convert into Roman
Number.
JAVA Internals 3
import java.util.Scanner;
/*
* Output:
* Enter a number: 1081
* Roman Numeral: MMLXXXI
*/
3B. Write a Java program to check whether given number is Extension number. The
extension number is the number which is present in the last digit(s) of its square.
(Eg. N=25, 625 is Extension number since it contains 25).
import java.util.Scanner;
class EX3B {
public static void main(String[] args) {
var input = new Scanner(System.in);
System.out.print("Enter a Number: ");
int num = input.nextInt();
long square = num * num;
String strSquare = Long.toString(square);
boolean flag = false;
for (int i = 0; i < strSquare.length(); i++) {
if (square % (Math.pow(10, i)) == num) {
flag = true;
break;
}
}
if (flag)
System.out.println("Extension Number");
else
System.out.println("Not an Extension Number");
input.close();
}
}
/*
* Output:
* Enter the number: 625
* Extension Number
*/
3C. Write a Java program to take input as Amount in rupees and print their
denominations and total number notes
import java.util.Scanner;
JAVA Internals 4
public class EX3C {
}
}
/*
* Output:
* Enter amount: 19021
* Number of 2000: 9
* Number of 500: 2
* Number of 20: 1
* Number of 1: 1
*/
EX4
4A. Create a Class named Student with properties as Student Id, Student Name, gender,
department, Age, Aggregate and methods as insertStudent() for inserting student
details and displayStudent() for printing student details.
class Student {
public String student_id;
public String full_name;
public String gender;
public String department;
public int age;
public float aggregate;
public void insertStudent(String sid, String name, String gen, String dept, int age, float agg) {
this.student_id = sid;
this.full_name = name;
this.gender = gen;
this.department = dept;
this.age = age;
this.aggregate = agg;
System.out.println();
System.out.println("---------------------------------");
System.out.println("All details inserted successfully");
System.out.println("---------------------------------");
System.out.println();
}
JAVA Internals 5
rahul.displayStudent();
}
/*
* Output:
* ---------------------------------
* All details inserted successfully
* ---------------------------------
*
* Id : 20KD1A05K4
* Name : Rahul
* Gender : M
* Department:CSE
* Age :19
* Aggregate :9.03
*/
4B. Create a class Student with same properties as above and create a constructor to
insert student details and return the data using toString() method.
class Student {
public String student_id;
public String full_name;
public String gender;
public String department;
public Integer age;
public Float aggregate;
public Student(String sid, String name, String gen, String dept, int age, float agg) {
this.student_id = sid;
this.full_name = name;
this.gender = gen;
this.department = dept;
this.age = age;
this.aggregate = agg;
}
/*
* Output:
* 20KD1A05K4, Rahul, M, CSE, 20, 8.5
*/
EX5
5A. Design a Class named Transaction to transfer amount (double) in different ways
using Account Number(int) , Phone Number(Long) and qr Code (String) as parameter into
a method transferAmount() to achieve Method or Constructor OverLoading.
class Transaction {
void transferAmount(int accountNumber, double amount, String qrCode) {
JAVA Internals 6
System.out.println("Account Number: " + accountNumber);
System.out.println("Amount: " + amount);
System.out.println("QR Code: " + qrCode);
class EX5A {
public static void main(String[] args) {
Transaction t1 = new Transaction();
Transaction t2 = new Transaction();
t1.transferAmount(123456789, 100.00, "8948348934");
t2.transferAmount("8948348934", 100.00);
}
}
/*
* Output:
* Account Number: 123456789
* Amount: 100.0
* QR Code: 8948348934
* QR Code: 8948348934
* Amount: 100.0
*/
5B.Design a super Class Account and sub Classes as LoanAccount, SavingsAccount and
CurrentAccount and implement relationship between parent and child classes. (Implement
Packages for the above classes)
class Account {
public int accNo;
public String accName;
public double accBalance;
JAVA Internals 7
public double currentRate;
public double interestAmount = currentAmount * currentRate;
}
}
/*
* Output:
* ------Loan Account------
* Loan Amount: 10000.0
* Loan Rate: 0.05
* Interest Amount: 500.0
* Account Number: 101
* Account Name: Loan Account
* Account Balance: 10000.0
* ------Savings Account------
* Savings Amount: 20000.0
* Savings Rate: 0.05
* Interest Amount: 5000.0
* Account Number: 102
* Account Name: Savings Account
* Account Balance: 20000.0
* ------Current Account------
* Current Amount: 30000.0
* Current Rate: 0.05
* Interest Amount: 15000.0
* Account Number: 103
* Account Name: Current Account
* Account Balance: 30000.0
*/
EX6
6A. 1. Write a Java program to implement this and super keywords.
class Animal {
public String name;
public int age;
JAVA Internals 8
public void displayDetails(String name, int age) {
this.name = name;
this.age = age;
/*
* Output:
* Name: Simba
* Age: 10
* Lion Roars
*/
6B. 1. Write a Java program to implement Static property, method, block and package.
class Animal {
static String name = "Animal";
static int age = 4;
}
}
/*
* Output:
* Name: Animal
* Age: 4
* Square root age: 2.0
* Animal is eating
*/
JAVA Internals 9
final void display() {
System.out.println("Account: " + account);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
}
}
/*
* Output:
* Account: 94913
* Name: SBI
* Address: Bangalore
*/
EX7
7A. 1. Write a Java program to implement Data Abstraction using Abstract class and
Interface.
interface AnimalInterface {
final public int age = 4;
void eat();
}
/*
* Output:
* Lion is eating
* Animal is eating
* Lion age: 4
*/
interface AnimalEat {
void eat();
}
JAVA Internals 10
interface AnimalTravel {
void travel();
}
/*
* Output:
* Animal is eating
* Animal is travelling
*/
EX8
8A. Write a Java program to take input as String Sentence S and print largest and
shortest word in S.
/*
* Largest Word: Sentence
* Smallest Word: a
*/
8B. 1. Write a Java program to take input as String S and remove the consecutive
repeated characters from S. (Eg. S = Raaaamaaa then, Rama)
import java.util.Scanner;
JAVA Internals 11
for (int i = 0; i < len - 1; ++i) {
if (words[i] == words[i + 1])
words[i] = '0';
}
/*
* Enter a string: Raaaaaaamaaaaaaaaa
* Rama
*/
EX9
9A. Write a Java program to implement Map interface.
import java.util.*;
/*
* Output:
* 1 : One
* 2 : Two
* 3 : Three
*/
import java.util.*;
/*
* Output:
* 1
* 2
* 3
*/
JAVA Internals 12
9C. Write a Java program to implement Set Interface.
import java.util.*;
/*
* Output:
* 1
* 2
* 3
*/
import java.util.*;
class Student {
int rollno;
String name, address;
class GFG {
public static void main(String[] args) {
ArrayList<Student> ar = new ArrayList<Student>();
System.out.println("Unsorted");
for (Student s : ar) {
System.out.println(s);
}
Collections.sort(ar, new Sortbyroll());
System.out.println("Sorted by rollno");
for (Student s : ar) {
System.out.println(s);
}
Collections.sort(ar, new Sortbyname());
System.out.println("Sorted by name");
for (Student s : ar) {
System.out.println(s);
JAVA Internals 13
}
}
}
/*
Output:
Unsorted
111 Mayank london
131 Anshul nyc
121 Solanki jaipur
101 Aggarwal Hongkong
Sorted by rollno
101 Aggarwal Hongkong
111 Mayank london
121 Solanki jaipur
131 Anshul nyc
Sorted by name
101 Aggarwal Hongkong
131 Anshul nyc
111 Mayank london
121 Solanki jaipur
*/
EX10
10A. Write a Java program to read data from Employee file and print Highest salary
employee information. (Employee File Contains: ID, name, Dept., Salary).
import java.io.*;
/*
* 102,manager,mike,60000
*/
10B. Write a java program to implements Serializable Interface to read and write
Objects to/from the file.
import java.io.*;
JAVA Internals 14
public String getName() {
return this.sname;
}
/*
* object serialized in student.ser
*/
EX11
11A. Write a Java program to implement try, catch, finally blocks.
/*
* Can't divide a number by 0
*/
11B. Write a Java program to create user defined Exception and implement throw and
throws handlers.
import java.util.Scanner;
JAVA Internals 15
}
}
/*
* enter voter age
* 12
* Exception in thread "main" VoterAgeLessException: voter age less
* at VoterAgeLessException.main(VoterAgeLessException.java:17)
*
* Output - 2
* enter voter age
* 18
* valid age
*/
EX12
12A. Write a Java program to create Thread using Thread Class and Runnable Interface.
import java.lang.Thread;
import java.lang.Runnable;
/*
* Thread-1 running
* Thread-2 running
*/
12B. Write a Java program to implement multi threading and synchronization. class
MovieReservation implements Runnable
MovieReservation(int t) {
this.t = t;
}
JAVA Internals 16
}
/*
* raviTicket booked
* kalyanTicket booked
*/
class Account {
volatile int balance_amount = 15000;
/*
* 14000 Rs/- Amount is withdrawn
* Balance Amount is :1000
* 1000 Rs/- Amount is withdrawn
* Balance Amount is :0
* Wait for the amount being deposited...
* Amount of Rs 10000 is deposited
JAVA Internals 17
* 1000 Rs/- Amount is withdrawn
* Balance Amount is :9000
*/
EX13
JAVA Internals 18