Codes
Codes
import java.util.Scanner;
System.out.println("Enter a number:");
int number = scanner.nextInt();
if (isPalindrome(number)) {
System.out.println("The number " + number + " is a palindrome.");
} else {
System.out.println("The number " + number + " is not a palindrome.");
}
scanner.close();
}
while (num != 0) {
int digit = num % 10;
reversedNumber = reversedNumber * 10 + digit;
num /= 10;
}
// prime or not
import java.util.Scanner;
System.out.println("Enter a number:");
int number = scanner.nextInt();
if (isPrime(number)) {
System.out.println("The number " + number + " is a prime number.");
} else {
System.out.println("The number " + number + " is not a prime number.");
}
scanner.close();
}
//Fibonacci nos.
import java.util.Scanner;
scanner.close();
}
switch (choice) {
case 1:
System.out.println("Enter the side length of the cube:");
double side = scanner.nextDouble();
System.out.println("Volume of the cube: " + calculateVolume(side));
break;
case 2:
System.out.println("Enter the radius of the cylinder:");
double radius = scanner.nextDouble();
System.out.println("Enter the height of the cylinder:");
double height = scanner.nextDouble();
System.out.println("Volume of the cylinder: " +
calculateVolume(radius, height));
break;
case 3:
System.out.println("Enter the radius of the sphere:");
double sphereRadius = scanner.nextDouble();
System.out.println("Volume of the sphere: " +
calculateVolume(sphereRadius));
break;
default:
System.out.println("Invalid choice.");
}
scanner.close();
}
class Animal {
// Method to be overridden
public void makeSound() {
System.out.println("Some generic animal sound");
}
}
//callbyvalueandrefernce
class ValueChanger {
// Method to demonstrate call by value for primitives
public void changePrimitive(int num) {
num = 10;
System.out.println("Inside changePrimitive method, num = " + num);
}
class MyObject {
int value;
MyObject() {
this.value = 0;
}
}
//this keyword
class Student {
private String name;
private int age;
// Method to compare the age of the current object with another object
public boolean isOlderThan(Student otherStudent) {
return this.age > otherStudent.age;
}
}
//studentclass
assignmet5
//recursion
System.out.println();
}
}
}
//string palindrome
import java.util.Scanner;
System.out.println("Enter a string:");
String input = scanner.nextLine();
if (isPalindrome(input)) {
System.out.println("The string \"" + input + "\" is a palindrome.");
} else {
System.out.println("The string \"" + input + "\" is not a
palindrome.");
}
scanner.close();
}
return true;
}
}
//hierrarchial inheritance
// Parent class
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
// Child class 1
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
// Child class 2
class Cat extends Animal {
@Override
public void sound() {
System.out.println("Cat meows");
}
}
//access specifier
class AccessSpecifierExample {
// Public variable
public int publicVariable = 10;
// Private variable
private int privateVariable = 20;
//static keyword
// Static method
public static void staticMethod() {
System.out.println("Static method: Accessing static variable = " +
staticVariable);
}
//superuse
// Superclass
class Animal {
String sound = "Generic sound";
// Constructor
Animal() {
System.out.println("Animal constructor");
}
// Method
void makeSound() {
System.out.println("Animal makes a sound: " + sound);
}
}
// Subclass
class Dog extends Animal {
String sound = "Bark";
// Constructor
Dog() {
// Call superclass constructor using super keyword
super();
System.out.println("Dog constructor");
}
// Method overriding
@Override
void makeSound() {
// Call superclass method using super keyword
super.makeSound();
System.out.println("Dog makes a sound: " + sound);
}
}
//outer innerclass
class Outer {
void display() {
System.out.println("Outer class display method");
}
class Inner {
void display() {
System.out.println("Inner class display method");
}
}
}
//final
class Main {
public static void main(String[] args) {
//finally
class TestFinallyBlock {
public static void main(String args[]){
try{
//below code do not throw any exception
int data=25/5;
System.out.println(data);
}
//catch won't be executed
catch(NullPointerException e){
System.out.println(e);
}
//executed regardless of exception occurred or not
finally {
System.out.println("finally block is always executed");
}
//throws
import java.io.IOException;
class Testthrows1{
void m()throws IOException{
throw new IOException("device error");//checked exception
}
void n()throws IOException{
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
Testthrows1 obj=new Testthrows1();
obj.p();
System.out.println("normal flow...");
}
}