You are on page 1of 14

PROGRAM NO# 1

public static void main(String[] args) {

apple obj = new apple();

class Food {

Food() {

System.out.println("Food");

class fruit extends Food {

fruit() {

super();

System.out.println("Fruit");

class apple extends fruit {

apple() {

super();

System.out.println("apple");

}
PROGRAM NO#2
Shape[] objj= new Shape[6];

objj[0]=new Circle();

objj[1]=new Square();

objj[2]=new Triangle();

objj[3]=new Sphere();

objj[4]=new Cube();

objj[5]=new TetraHedron();

for(int i=0;i<obj.length; i++)

if(obj[i] instanceof TwoDimensionShape ){

obj[i].getArea(5);

else if (obj[i] instanceof ThreeDimensionShape )

obj[i].getArea(5);

obj[i].getVolume(5);

class Shape{

void getArea(double x){

System.out.println("Area");}

void getVolume(double x){

System.out.println("Volume");}

class TwoDimensionShape extends Shape{

@Override
void getArea(double x){

System.out.println("TwoD.Area");}}

class ThreeDimensionShape extends Shape{@Override

void getArea(double x){System.out.println("ThreeD Area");}

@Override

void getVolume(double x){System.out.println("ThreeD Volume");}

class Circle extends TwoDimensionShape{

@Override

void getArea(double x){

System.out.println("Area of Circle "+ Math.PI*x*x);}

class Square extends TwoDimensionShape{@Override

void getArea(double x){

System.out.println("Area of Square "+ x*x);}}

class Triangle extends TwoDimensionShape{

@Override

void getArea(double x){

System.out.println("Area of Triangle " + .5*x*x );}}

class Sphere extends ThreeDimensionShape{@Override

void getArea(double x){System.out.println("Area Of Sphere "+ Math.PI*4*x*x);}

@Override

void getVolume(double x){

System.out.println(" Volume OF SPHERE " + (3.14*(4/3)*x*x*x ));}

class Cube extends ThreeDimensionShape{@Override

void getArea(double x){System.out.println("Area "+ 6*x);}

@Override

void getVolume(double x){System.out.println("Volume Of Cube "+ (x*x*x));}

}
class TetraHedron extends ThreeDimensionShape{@Override

void getArea(double x){System.out.println("Area of TetreHedron " + Math.sqrt(3)*x*x);}

@Override

void getVolume(double x){System.out.println("Volume of TetraHedron "+ ((x*x*x)/


(6*Math.sqrt(2)) ) );}

PROGRAM NO# 3
public static void main(String[] args) {

Scanner s = new Scanner(System.in);

String str = s.next();

getOccuringChar(str);

class NoOfOccurenceOfCharacters {

static final int MAX_CHAR = 256;

static void getOccuringChar(String str)

// Create an array of size 256 i.e. ASCII_SIZE

int count[] = new int[MAX_CHAR];

int len = str.length();

// Initialize count array index

for (int i = 0; i < len; i++)

count[str.charAt(i)]++;

// Create an array of given String size

char ch[] = new char[str.length()];

for (int i = 0; i < len; i++) {


ch[i] = str.charAt(i);

int find = 0;

for (int j = 0; j <= i; j++) {

// If any matches found

if (str.charAt(i) == ch[j])

find++;

if (find == 1)

System.out.println("Number of Occurrence of " +

str.charAt(i) + " is:" + count[str.charAt(i)]);

PROGRAM NO# 4
public static void main(String[] args) {

int mat[][] = { { 1, 2, 3, 4 },

{ 25, 6, 7, 8 },

{ 9, 10, 11, 12 },

{ 13, 14, 15, 16 } };

System.out.println(findMax(mat)) ;

class array {

final static int N = 4;


final static int M = 4 ;

static int findMax(int mat[][])

int maxElement = Integer.MIN_VALUE;

for (int i = 0; i < N; i++) {

for (int j = 0; j < M; j++) {

if (mat[i][j] > maxElement) {

maxElement = mat[i][j];

return maxElement;

}}

PROGRAM NO# 5
public static void main(String[] args) {

Scanner s = new Scanner(System.in);

boolean valid = false;

String password;

do {

System.out.print("Please enter password and then hit enter:");


password = s.nextLine();

if (password.length()<8)

valid = false;

System.out.println("Password must have at least 8 characters");

continue;

for (int i = 0; i < password.length(); i++){

char c = password.charAt(i);

if ( ('a' <= c && c <= 'z')

|| ('A' <= c && c <= 'Z')

|| ('0' <= c && c <= '9')

valid = true;

else

System.out.println("Only letter & digits are acceptable.");

valid = false;

break;

}
} while(!valid);

System.out.println("Password Accepted");

PROGRAM NO# 6
class Fibonacci {

void method(){

Scanner s=new Scanner(System.in);

System.out.println("Enter How many Times u want to run Loop");

int n , t1 = 0, t2 = 1;

n=s.nextInt();

System.out.print("First " + n + " terms: ");

for (int i = 1; i <= n; ++i)

System.out.print(t1 + " ");

int sum = t1 + t2;

t1 = t2;

t2 = sum;

PROGRAM NO# 7
public static void main(String[] args) {

Scanner input=new Scanner(System.in);


System.out.println("Enter your name: ");

String name=input.next();

System.out.println("Enter the starting the direction (N/E/W/S) : ");

String direction=input.next();

System.out.println("Enter the position: ");

int postion=input.nextInt();

Car c=new Car(name,direction.charAt(0),postion);

String m;

while (true){

System.out.println("Enter the direction you want to move in (W/A/S/D): ");

m=input.next();

c.move(m.charAt(0));

if(m.equalsIgnoreCase("S")){

break;

public static class Car {

String name;

char direction;

int[] dirP = new int[4];

int position;

public Car() { //Default Constructor

name = "N/A";

String direction="N/A" ;
for (int i = 0; i < dirP.length; i++) {

dirP[i] = 0;

position = 0;

public Car(String name, char direction, int position) { // Four argument constructor

this.name = name;

this.direction = direction;

this.position = position;

public void setDirection(char direction) {

this.direction = direction;

public void move(char m) {

if (m == 'w' || m == 'W'){

dirP[0]++;

setDirection('N');

} else if(m == 'a' || m == 'A'){

dirP[1]++;

setDirection('W');

}else if( m == 'd' || m == 'D') {

dirP[2]++;

setDirection('E');

} else if( m == 's' || m == 'S'){

display();

else {

System.out.println("Invalid direction!");
}

public void display(){

System.out.println("Name: "+name);

System.out.println("Direction: "+direction);

System.out.println("You have moved " + dirP[0] + " points in the north direction, " +
dirP[1] + " points in west and " + dirP[2] + " points in east direction and " + dirP[3] +"
points in the South.");

for(int i=0;i<dirP.length;i++) {

position = position + dirP[i];

System.out.println("Total distance covered: "+position);

PROGRAM NO# 8
public static void main(String[] args) {

int number = 10, temp;

Scanner s = new Scanner(System.in);

int x[] = new int[number];

System.out.println("Enter any 10 Numbers:");

for (int i = 0; i < number; i++) {

x[i] = s.nextInt();

for (int i = 0; i < number; i++) {

for (int j = i + 1; j < number; j++) {

if (x[i] > x[j]) {

temp = x[i];

x[i] = x[j];
x[j] = temp;

System.out.print("Ascending Order Array :");

for (int i = 0; i < number - 1; i++) {

System.out.print(x[i] + ", ");

System.out.print(x[number - 1]);

PROGRAM NO# 9
class Book {

int BookId;

int Pages;

int Price = 0;

void gets(int x, int y, int z) {

BookId = x;

Pages = y;

Price = z;

int showBookId() {

return BookId;

int showPages() {
return Pages;

int getsPrice() {

return Price;

PROGRAM NO# 10
public static void main(String[] args) {

results x = new results();

x.input();

x.show();

x.total();

x.average();

class results {

private int RollNumber;

private String Name;

private final int[] Score = new int[3];

int i, total;

public void input() {

Scanner s = new Scanner(System.in);

System.out.println("Enter Roll Number");

RollNumber = s.nextInt();

System.out.println("Enter Name");
Name = s.next();

for (i = 0; i < 3; i++) {

System.out.println("Enter marks for Subject No" + i);

Score[i] = s.nextInt();

total = Score[0] + Score[1] + Score[2];

public void show() {

System.out.println("Roll Number :" + RollNumber);

System.out.println("Name :" + Name);

System.out.println("Marks :" + Arrays.toString(Score));

public void total() {

System.out.println("Total Marks of All Subjects is " + total);

public void average() {

System.out.println("average :" + total / 3);

You might also like