Sie sind auf Seite 1von 16

WRITE QUESTIONS IN BLACK PEN & PROGRAMS IN BLUE PEN.

IN ONE
PAGE ONLY ONE PROGRAM.

1. Write a Java Program To print Numbers in Below pattern:

1
23
456
7 8 9 10
11 12 13 14 15
public class NumbersFormat {
public static void main(String[] args) {
int num=15;
int temp=1;
for (int i = 1; i <= num; i++)
{
for (int k = i; k <num; k++)
System.out.print(" ");
for (int j =1; j <= i; j++){
System.out.print("" +temp+ " ");
temp++;
if(temp>15){
break;
}}
System.out.println();
if(temp>15){
break;
}}}}
2. Write a Java Program To multiply two matrices
import java.util.Scanner;
class MatrixMultiplication {
public static void main(String args[]) {
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first
matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the number of rows and columns of second
matrix");
p = in.nextInt();
q = in.nextInt();
if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied ");
else {
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
second[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ ) {
for ( d = 0 ; d < q ; d++ ) {
for ( k = 0 ; k < p ; k++ ) {
sum = sum + first[c][k]*second[k][d]; }
multiply[c][d] = sum;
sum = 0; } }
System.out.println("Product of entered matrices:-");
for ( c = 0 ; c < m ; c++ ) {
for ( d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
} } } }
3. Write a Java Program to accept a string from the console and counts
number of vowels, consonants, digits, tabs and blank spaces in a
string.
import java.io.*;
class q5vowels
{
public static void main(String args[]) throws IOException
{
String str;
int vowels = 0, digits = 0, blanks = 0;
char ch;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a String : ");
str = br.readLine();
for(int i = 0; i < str.length(); i ++)
{
ch = str.charAt(i);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowels ++;
else if(Character.isDigit(ch))
digits ++;
else if(Character.isWhitespace(ch))
blanks ++;
}
System.out.println("Vowels : " + vowels);
System.out.println("Digits : " + digits);
System.out.println("Blanks : " + blanks);
} }
4. Write a Java Program to reverse a string
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);

System.out.println("Reverse of entered string is: "+reverse);


} }
5. Write a Java Program to Sort Names in an Alphabetical Order
import java.util.Scanner;
public class Alphabetical_Order
{
public static void main(String[] args)
{
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
} } }
System.out.print("Names in Sorted Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);
} }
6. Write a Java Program to show method overloading
class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
} }
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
} }
7. Write a Java Program to Find Area of Square,Rectangle and Circle using
Method Overloading
class OverloadDemo
{
void area(float x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq
units");
}
void area(float x, float y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
} }
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5); } }
8. Write a Java Program to show constructor chaining
public class ChainingDemo {
public ChainingDemo(){
System.out.println("Default constructor");
}
public ChainingDemo(String str){
this();
System.out.println("Parametrized constructor with single param");
}
public ChainingDemo(String str, int num){
this("Hello");
System.out.println("Parametrized constructor with double args");
}
public ChainingDemo(int num1, int num2, int num3){
this("Hello", 2);
System.out.println("Parametrized constructor with three args");
}
public static void main(String args[]){
ChainingDemo obj = new ChainingDemo(5,5,15);
} }
9. Write a Java Program to show inheritance taking the example of a
vehicle as super class
class Vehicle {
String color;
int speed;
int size;
void attributes() {
System.out.println("Color : " + color);
System.out.println("Speed : " + speed);
System.out.println("Size : " + size);
} }
class Car extends Vehicle {
int CC;
int gears;
void attributescar() {
System.out.println("Color of Car : " + color);
System.out.println("Speed of Car : " + speed);
System.out.println("Size of Car : " + size);
System.out.println("CC of Car : " + CC);
System.out.println("No of gears of Car : " + gears);
} }
public class Test {
public static void main(String args[]) {
Car b1 = new Car();
b1.color = "Blue";
b1.speed = 200 ;
b1.size = 22;
b1.CC = 1000;
b1.gears = 5;
b1.attributescar(); } }
10. Write a Java Program to show the use of a static variable
class Student{
int rollno;
String name;
static String college ="AFFINITY";
Student(int r,String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}

public static void main(String args[]){


Student s1 = new Student8 (1011,"RAM");
Student s2 = new Student8 (1022,"SHYAM");

s1.display();
s2.display(); } }

11. Write a Java Program to show the real use of Super keyword
class Person{
int id;
String name;
Person(int id,String name){
this.id=id;
this.name=name;
} }
class Emp extends Person{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
this.salary=salary;
}
void display(){System.out.println(id+" "+name+" "+salary); } }
class TestSuper5{
public static void main(String[] args){
Emp e1=new Emp(1,"ankit",45000f);
e1.display(); } }
12. Write a Java Program to for Runtime Polymorphism
class Animal{
void eat(){System.out.println("eating..."); }
}
class Dog extends Animal{
void eat(){System.out.println("eating bread..."); }
}
class Cat extends Animal{
void eat(){System.out.println("eating rat..."); }
}
class Lion extends Animal{
void eat(){System.out.println("eating meat..."); }
}
class TestPolymorphism3{
public static void main(String[] args) {
Animal a;
a=new Dog();
a.eat();
a=new Cat();
a.eat();
a=new Lion();
a.eat(); } }

13. Write a Java Program to illustrate the use of abstract keyword


abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); } }
14. Write a Java Program for abstract class and interface
Creating interface that has 4 methods
interface A{
void a();
void b();
void c();
void d();
}
abstract class B implements A{
public void c(){System.out.println("I am C");}
}
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d(); } }
15. Write a Java Program to calculate total Marks and percentage scored
by the student.
import java.lang.*;
import java.io.*;
class student
{
String name;
int roll_no;
int sub1,sub2;
int total;
float per;
void getdata() throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println ("Enter Name of Student");
name = br.readLine();
System.out.println ("Enter Roll No. of Student");
roll_no = Integer.parseInt(br.readLine());
System.out.println ("Enter marks out of 100 of 1st subject");
sub1 = Integer.parseInt(br.readLine());
System.out.println ("Enter marks out of 100 of 2nd subject");
sub2 = Integer.parseInt(br.readLine());
}
void show()
{
total=sub1+sub2;
per=(total*100)/200;
System.out.println ("Roll No. = "+roll_no);
System.out.println ("Name = "+name);
System.out.println ("Marks of 1st Subject = "+sub1);
System.out.println ("Marks of 2nd Subject = "+sub2);
System.out.println ("Total Marks = "+total);
System.out.println ("Percentage = "+per+"%");
} }
class q2Student
{
public static void main(String args[]) throws IOException
{
student s=new student();
s.getdata();
s.show();} }
16. Write a Java program depicting Multiple Inheritance Using Interface
interface vehicleone{
int speed=90;
public void distance();
}
interface vehicletwo{
int distance=100;
public void speed();
}
class Vehicle implements vehicleone,vehicletwo{
public void distance(){
int distance=speed*100;
System.out.println("distance travelled is "+distance);
}
public void speed(){
int speed=distance/100;
} }
class MultipleInheritanceUsingInterface{
public static void main(String args[]){
System.out.println("Vehicle");
obj.distance();
obj.speed();
} }

17. Write a Java Applet program that allows the user to draw lines,
rectangles and ovals

import java.awt.*;
import java.applet.*;
/*
<applet code="Name" width=200 height=200>
</applet>
*/
public class Sujith extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<=250;i++)
{
Color c1=new Color(35-i,55-i,110-i);
g.setColor(c1);
g.drawRect(250+i,250+i,100+i,100+i);
g.drawOval(100+i,100+i,50+i,50+i);
g.drawLine(50+i,20+i,10+i,10+i);
} } }

18. Write a Java Applet program that displays a simple message


import java.awt.*;
import java.applet.*;
/*
<applet code="sim" width=300 height=300>
</applet>
*/
public class sim extends Applet
{
String msg=" ";
public void init()
{
msg+="init()--->";
setBackground(Color.orange);
}
public void start()
{
msg+="start()--->";
setForeground(Color.blue);
}
public void paint(Graphics g)
{
msg+="paint()--->";
g.drawString(msg,200,50);
} }
19. Create the following form with given inputs using HTML

First Name *
Last Name *
Email Address *
Telephone Number
Comments *

Submit

<html>
<head>
<body>
<form name="htmlform" method="post" action="html_form_send.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25"
rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit"> ( <a
href="http://www.freecontactform.com/html_form.php">HTML Form</a> )
</td>
</tr>
</table>
</form>
</body>
</head>
</html>
20. Write a Java program to print the greatest no.
import java.io.*;
class gtn
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a,b,c;
System.out.println("Enter the values");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
if((a>b)&&(a>c))
{System.out.println("A is greatest");}
else if (b>c)
{
System.out.println("B is greatest");
}
else
{
System.out.println("C is greatest");}}}

21. Write a Java program to print the marksheet of a student


import java.io.*;
class marksheet
{ int m1,m2,m3,m4,m5,tot;
float avg;
void getdata()throws IOException
{BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the mark");
m1=Integer.parseInt(br.readLine());
m2=Integer.parseInt(br.readLine());
m3=Integer.parseInt(br.readLine());
m4=Integer.parseInt(br.readLine());
m5=Integer.parseInt(br.readLine());
tot=m1+m2+m3+m4+m5;avg=tot/5;}
void putdata()throws IOException
{BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Name : ");
System.out.println("Subject 1 marks : " + m1);
System.out.println("Subject 2 marks : " + m2);
System.out.println("Subject 3 marks : " + m3);
System.out.println("Subject 4 marks : " + m4);
System.out.println("Subject 5 marks : " + m5);
System.out.println("Total="+tot);
System.out.println("Average="+avg);
if((m1>=40)&&(m2>=40)&&(m3>=40)&&(m4>=40)&&(m5>=40))
{System.out.println("Result=Pass");
if (avg>=60)
{System.out.println("Class=First");
}
else if (avg>=50)
{System.out.println("Class=Second");
}
else
{System.out.println("Class=Third");
}}
20
else
{System.out.println("Result=Fail");}}}
class clsmain
{public static void main(String args[])throws IOException
{marksheet r1 = new marksheet();
r1.getdata();
r1.putdata();}}

22. Write a Java program to calculate area and volume using inheritance
of classes
import java.io.*;
class clsroom
{ int l;
int b;
clsroom(int x,int y)
{
l=x;
b=y;
}
int area()
{
return(l*b);
}}
class studyroom extends clsroom
{
int h;
studyroom(int x,int y,int z)
{
super(x,y);
h=z;
}
int volumn()
{
return(l*b*h);
}
}
class clshome
{
public static void main (String args[])
{
studyroom s1 = new studyroom(12,10,10);
System.out.println("Area = "+s1.area());
System.out.println("Volumn="+s1.volumn());
} }
23. Write a Java program to use applet function to draw a shape of the
house.
import java.awt.*;
import java.applet.*;
/*<applet code="appskel" width=300 height=100>
</applet>
*/
public class appskel extends Applet
{
public void paint(Graphics g)
{
g.drawLine(0,100,50,50);
g.drawLine(50,50,100,100);
g.drawLine(100,100,100,200);
g.drawLine(100,200,0,200);
g.drawLine(0,200,0,100);
g.drawLine(50,50,200,50);
g.setColor(Color.blue);
g.drawLine(200,50,250,100);
g.drawLine(250,100,250,200);
g.drawLine(250,200,100,200);
g.drawLine(250,100,0,100);
} }
24. Write a Java program to perform multithreading using multiple class
method.
import java.io.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("i="+i);
}
}
}
class B extends Thread
{
public void run()
{
for (int j=1;j<=5;j++)
{
System.out.println("j="+j);
}
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("k="+k);
}}}
class clsthread
{
public static void main(String args[])throws IOException
{
A a = new A();
42
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}}
25. To write a program to perform arithmetic operations using package.
PACKAGE:
package ARI;
public class clspackage
{
public int add(int a,int b)
{
return(a+b);
}
public int sub(int c, int d)
{
return(c-d);
}
public int mul(int e, int f)
{
return(e*f);
}
public int div(int g, int h)
{
return(g/h);
}
}
MAIN PROGRAM:
import java.io.*;
import ARI.*;
class clsmain
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
int a,b;
System.out.println("Enter the numbers:");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
clspackage r = new clspackage();
System.out.println(a + " + " + b + " = " +r.add(a,b));
37
System.out.println(a + " - " + b + " = " +r.sub(a,b));
System.out.println(a + " * " + b + " = " +r.mul(a,b));
System.out.println(a + " / " + b + " = " +r.div(a,b));
}
}

Das könnte Ihnen auch gefallen