Beruflich Dokumente
Kultur Dokumente
IN ONE
PAGE ONLY ONE PROGRAM.
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);
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(); } }
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);
} } }
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");}}}
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));
}
}