WAP To Illustrate The Concept of Type Casting
WAP To Illustrate The Concept of Type Casting
import java.io.*;
class Date_DayMethod1
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
String
days[]={"","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satur
day"};
System.out.print("Enter the day : ");
int d=Integer.parseInt(br.readLine());
System.out.print("Enter the month : ");
int m=Integer.parseInt(br.readLine());
System.out.print("Enter the year : ");
int y=Integer.parseInt(br.readLine());
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
{
month[2]=29;
}
if(m<0 || m>12 || d<0 || d>month[m] || y<0 || y>9999) // Performing Date
Validation
{
System.out.println("Invalid Date");
}
else
{
int dn=0;
for(int i=1;i<m;i++)
{
dn=dn+month[i];
}
dn=dn+d;
//finding the day of the week which corresponds to the given day
name
int x=0;
for(int i=1;i<=7;i++)
{
if (s.equalsIgnoreCase(days[i]))
x=i;
}
import java.util.Scanner;
5. WAP to print only those first 20 prime numbers for which sum of
digits is also prime.
121
12321
1234321
import java.util.Scanner;
int rowCount = 1;
System.out.println();
rowCount++;
}
}
}
import java.util.Scanner;
import java.util.*;
char ch;
int i, correct=0;
quiz[i]=new Quiz();
try{
quiz[6].quizQuestions="What is an Method?";
quiz[8].quizQuestions="What is an Object?";
catch(ArrayIndexOutOfBoundsException e){
System.err.println("Error Occur!\n"+e.getMessage());
System.exit(0);
try{
quiz[0].quizOptions="A: Charles Babbage\nB: Lady Ada Lovelace\nC: Tim Berners-Lee\nD: James Gosling";
quiz[1].quizOptions="A: C++\nB: Groovey\nC: Oak\nD: HTML";
quiz[2].quizOptions="A: Java Developing Kit\nB: Java Development Kit\nC: Java Developed Kit\nD: Java Disk
Kit";
quiz[4].quizOptions="A: Representation for the specific behaviour of an object\nB: Wrapping up of data into
a single unit\nC: Type of an Java Program\nD: Act of Representing essential features only without any background
details";
quiz[8].quizOptions="A: Blue print of an Program\nB: Identifiable entity with some Characteristics and
Behaviour\nC: Visibility mode of an Java statement\nD: Type of a Stream used in Java";
catch(ArrayIndexOutOfBoundsException e){
System.err.println("Error Occur!\n"+e.getMessage());
System.exit(0);
quiz[0].answer="D";
quiz[1].answer="C";
quiz[2].answer="B";
quiz[3].answer="A";
quiz[4].answer="D";
quiz[5].answer="A";
quiz[6].answer="C";
quiz[7].answer="A";
quiz[8].answer="B";
quiz[9].answer="C";
quiz[10].answer="B";
try{
String temp="";
Collections.shuffle(Arrays.asList(quiz));
Collections.shuffle(Arrays.asList(quiz));
temp=scan.next();
ch=temp.charAt(0);
temp=Character.toString(ch);
System.out.println("\n\n");
if(temp.equalsIgnoreCase(quiz[i].answer)){
correct++;
else{
System.out.println("Answer: "+quiz[i].answer);
System.out.println("\n\n");
}
catch(ArrayIndexOutOfBoundsException e){
System.err.println("Error Occur!\n"+e.getMessage());
System.exit(0);
catch(InputMismatchException e){
System.err.println("Error Occur!\n"+e.getMessage());
System.exit(0);
System.out.println("You answered 10 questions out of which you answered "+correct+" correct and "+(10-
correct)+" incorrect!");
if(correct<=5)
else if(correct==10)
obj.initialise();
obj.quizQuestions();
obj.quizOptions();
obj.quizAnswers();
obj.play();
obj.result();
Bubble sort
public class MyBubbleSort {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Output:
2, 4, 6, 9, 12, 23, 0, 1, 34,
Quick sort
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number, I am taking pivot as middle index number
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
10. Implement Java program that checks for given strings whether
they are anagrams or not.
import java.util.Scanner;
len1 = str1.length();
len2 = str2.length();
if(len1 == len2)
{
len = len1;
for(i=0; i<len; i++)
{
found = 0;
for(j=0; j<len; j++)
{
if(str1.charAt(i) == str2.charAt(j))
{
found = 1;
break;
}
}
if(found == 0)
{
not_found = 1;
break;
}
}
if(not_found == 1)
{
System.out.print("Strings are not Anagram to Each
Other..!!");
}
else
{
System.out.print("Strings are Anagram");
}
}
else
{
System.out.print("Both Strings Must have the same number of
Character to be an Anagram");
}
}
}
1. class PalindromeExample{
2. public static void main(String args[]){
3. int r,sum=0,temp;
4. int n=454;//It is the number variable to be checked for palindrome
5.
6. temp=n;
7. while(n>0){
8. r=n%10; //getting remainder
9. sum=(sum*10)+r;
10. n=n/10;
11. }
12. if(temp==sum)
13. System.out.println("palindrome number ");
14. else
15. System.out.println("not palindrome");
16. }
17. }
String
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String inputString;
Scanner in = new Scanner(System.in);
System.out.println("Input a string");
inputString = in.nextLine();
begin = 0;
end = length - 1;
middle = (begin + end)/2;
import java.util.Scanner;
class calc
{
int fibo(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
}
class box
int l,w,h;
box()
l=w=h=10;
box(int x)
l=w=h=x;
l=x;
w=y;
h=z;
box(box B)
l=B.l;
w=B.w;
h=B.h;
}
void duplicatebox(box b)
this.l=b.l;
this.w=b.w;
this.h=b.h;
void disp()
count++;
System.out.println(" ");
System.out.println("Length = "+l);
System.out.println("Width = "+w);
System.out.println("Height = "+h);
String color;
colorbox()
super();
color = "white";
colorbox(int x,String c)
super(x);
color = c;
super(x,y,z);
color = c;
colorbox(box b,String c)
super(b);
color = c;
void disp()
super.disp();
System.out.println("Color = "+color);
class testbox
int i;
b1.disp();
class Height {
private int feet;
private int inches;
//Bonus
public String toString() {
return String.format("%s'%s\"", feet, inches);
}
}
15. WAP to illustrate the concept of inheritance by making the use of
public, private, protected and default Classes and Methods.
class Teacher {
private String designation = "Teacher";
private String collegeName = "Beginnersbook";
default final int salary = 200;
public String getDesignation() {
return designation;
}
protected void setDesignation(String designation) {
this.designation = designation;
}
protected String getCollegeName() {
return collegeName;
}
protected void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
void does(){
System.out.println("Teaching");
}
}
16. WAP to illustrate the concept of Method Overriding. Also show the
use of final keyword.
class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
class Person
{
void message()
{
System.out.println("This is person class");
}
}
/* Subclass Student */
class Student extends Person
{
void message()
{
System.out.println("This is student class");
}
package relational;
if ( x > y ) {
return x;
else {
return y;
}
import java.lang.Math;
import java.util.scanner;
int x;
x=sc.nextInt();
System.out.println(x);
} }
19. Create your own package and use it to perform some meaningful
task.
package letmecalculate;
import letmecalculate.Calculator;
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10. class TestInterface2{
11. public static void main(String[] args){
12. Bank b=new SBI();
13. System.out.println("ROI: "+b.rateOfInterest());
14. }}
class Division {
public static void main(String[] args) {
int a, b, result;
a = input.nextInt();
b = input.nextInt();
// try block
try {
result = a / b;
System.out.println("Result = " + result);
}
// catch block
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}