JAVA PRACTICAL FILE
JAVA PRACTICAL FILE
LAB EXPERIMENTS
while(z!=0){
int num=z%10;
p=p*num;
z=z/10;
}
System.out.println(p);
}
}
Q2)
import java.util.Scanner;
if(z%k==0){
System.out.println("first number is multiple of second");
}
else{
System.out.println("first number is not a multiple of
second");
}
}
}
Q3)
import java.util.Scanner;
public class firstq3lab {
}
k=0;
for(int i=0;i<n-cnt;i++){
if(arr[i]==0){
arr[i]=ans[k];
k++;
}
}
for(int i=0;i<n;i++){
System.out.println(arr[i]);
}
}
}
Q4)
import java.util.Scanner;
public class first4lab {
static public void main(String args[]){
int[] arr=new int[101];
}
}
}
}
Q5)import java.util.Scanner;
public class first5lab {
static public void main(String args[]){
int n;
System.out.println("enter size of array");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
summ+=arr[r];
if(summ<=0){
summ=0;
r++;
l=r;
summ+=arr[r];
}
if(summ>maxi)
maxi=summ;
r++;
}
System.out.println(maxi);
}
}
EXPERIMENT 2
Q1)
import java.util.*;
public class second1lab {
public static boolean compare(String str1,String str2){
boolean ans=true;
int n=str1.length();
int m=str2.length();
if(n!=m){
ans=false;
}
else{
for(int i=0;i<n;i++){
if(str1.charAt(i)!=str2.charAt(i)){
ans=false;
break;
}
}
}
return ans;
}
String str1=sc.next();
System.out.println("enter second string");
String str2=sc.next();
boolean ans=compare(str1,str2);
if(ans==true)
System.out.println("strings are equal");
else{
System.out.println("Strings are not equal");
}
}
}
Q2)
public class second2lab {
public static void main(String[] args)
{
// Create two Dog objects
Dog spot = new Dog("spot", "Ruff!");
Dog scruffy = new Dog("scruffy", "Wurf!");
}
}
class Dog
{
String name;
String says;
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Dog otherDog = (Dog) obj;
return name.equals(otherDog.name) && says.equals(otherDog.says);
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + name.hashCode();
result = 31 * result + says.hashCode();
return result;
}
}
Q3)
EXPERIMENT 3
Q1)
import java.util.*;
class MyInteger
{
private int value;
MyInteger(int value)
{
this.value = value;
}
int getValue()
{
return value;
}
boolean isEven()
{
return this.value % 2 == 0;
}
}
}
Q2)import java.util.*;
class Dog
{
void bark()
{
System.out.println("The dog is barking!");
}
class WithoutConstructor
{
class WithConstructors
{
WithConstructors()
{
this(0);
System.out.println("First constructor called");
}
WithConstructors(int value)
{
System.out.println("Second constructor called with value: " +
value);
}
}
public class thirdlab1 {
public static void main(String args[]){
EXPERIMENT 4:
Q1)
import java.util.*;
class phone{
private
String make;
int model;
int year;
phone(String maker,int model1,int year1){
make=maker;
model=model1;
year=year1;
}
int get_year(){
return this.year;
}
String get_make(){
return this.make;
}
int get_model(){
return this.model;
}
void set_year(int year1){
this.year=year1;
}
void set_model(int model1){
this.model=model1;
}
void set_year(String maker){
this.make=maker;
}
String tostring(){
return this.make+" model no: "+this.model+"year of manufacture
"+this.year;
}
Boolean isObsolete(){
System.out.println("enter current year");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
if(num-this.year>10)
return true;
else
return false;
}
}
public class fourthlab1{
public static void main(String args[]){
phone p1=new phone("abs",3678,2020);
System.out.println(p1.tostring());
}
}
Q2)
import java.util.*;
while(true)
{
System.out.print("Enter your choice ( 1 for First Class, 2 for
Economy Class, 3 for exiting menu ) : ");
int choice = sc.nextInt();
switch (choice)
{
case 1 :
{
int seatAllocated = bookFirstClassTicket();
if(seatAllocated == -1)
{
System.out.println("All seats are already reserved
in First Class. Reservation not successful!!");
}
else
{
generateBoardingPass(seatAllocated, "First");
}
break;
}
case 2 :
{
int seatAllocated = bookEconomyClassTicket();
if(seatAllocated == -1)
{
System.out.println("All seats are already reserved
in Economy Class. Reservation not successful!!");
}
else
{
generateBoardingPass(seatAllocated, "Economy");
}
break;
}
case 3 :
{
System.exit(0);
}
default:
{
System.out.println("Wrong Choice!!!");
System.out.println("Please enter again!!!");
break;
}
}
}
}
EXPERIMENT 5 :
Q1)
public class Program1
{
static String str1 = "Lab5Program1";
static String str2;
static
{
str2 = "staticBlockString";
}
EXPERIMENT 6
Q1)
import java.util.Calendar;
class MyDate
{
private int year;
private int month;
private int day;
public MyDate()
{
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
}
System.out.println("Date 1:");
System.out.println("Year: " + date1.getYear());
System.out.println("Month: " + (date1.getMonth() + 1)); // 0-based
to 1-based
System.out.println("Day: " + date1.getDay());
System.out.println("\nDate 2:");
System.out.println("Year: " + date2.getYear());
System.out.println("Month: " + (date2.getMonth() + 1)); // 0-based
to 1-based
System.out.println("Day: " + date2.getDay());
}
}
Q2)
import java.util.*;
// LinkedList demonstration
System.out.println("\nLinkedList Demo:");
LinkedList<String> carsBrand = new LinkedList<>();
carsBrand.add("Range Rover");
carsBrand.add("M.G.");
carsBrand.add("Jeep");
carsBrand.add("Ford");
// Frequency of an element
indianTeamPlayers.add("Apple"); // Adding "Apple" to demonstrate
frequency
System.out.println("Frequency of 'Apple' in ArrayList: " +
Collections.frequency(indianTeamPlayers, "Apple"));
}
}
EXPERIMENT 8
Q1)
class A {
// Default constructor for class A
public A() {
System.out.println("Constructor of Class A");
}
}
class B {
// Default constructor for class B
public B() {
System.out.println("Constructor of Class B");
}
}
class C extends A {
// Class C inherits from A and has a member of class B
B b = new B(); // Composition: an object of class B is created inside
class C
Q2)
class Base {
// Non-default constructor in Base class
public Base(String message) {
System.out.println("Base class constructor called with message: "
+ message);
}
}
// Derived class
class Derived extends Base {
// Default (no-arg) constructor for Derived class
public Derived() {
super("Default call from Derived"); // Calling the base class
constructor
System.out.println("Derived class no-arg constructor called");
}
Q3)
class BaseClass {
// First overloaded method
public void display(int a) {
System.out.println("BaseClass display method with int argument: "
+ a);
}
Q4)
class FinalMethodClass {
// Final method that cannot be overridden
public final void display() {
System.out.println("This is a final method in FinalMethodClass.");
}
}
// Derived class that attempts to override the final method
class ChildClass extends FinalMethodClass {
// Uncommenting the following method will cause a compile-time error
/*
@Override
public void display() {
System.out.println("Trying to override the final method.");
}
*/
}
import Exp8.Program1.Parent.NotificationDriver;
package Exp8.Program1.Child;
import Exp8.Program1.Parent.NotificationDriver;
package Exp8.Program1.Parent;
import Exp8.Program1.Child.SmsNotification;
EXPERIMENT 9
Q1)
class OuterClass {
EXPERIMENT 10
Q1)
Q2)
class CustomException extends Exception {
// Private field to store the message
private String message;