Download 1. (a) Write a simple Java program to print a line of text. (b) Write a

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1. (a) Write a simple Java program to print a line of text.
(b) Write a simple Java Program to Add, Subtract, Multiply and divide two integers.
2. (a) Write a Java program to convert the temperature from the command line in Celsius to Fahrenheit.
(b) Write a simple Java program to find if a number from the command line is even or not.
3. (a) Write a Java program to print numbers from 1 to n using the do … while structure.
(b) Program to print sum of n natural Number
4. (a) Write a Java program to print the multiplication table of a given number n using the while structure.
(b) Find if a given number is prime or not using the ‘ for’ statement.
5. (a) Write a program that prints the first 20 Fibonacci numbers.
(b) Create an array of integers and print its values, sum of values and average of the values
6. (a) Find the maximum value from the given array of numbers.
(b) Arrange the given array into ascending order.
7. (a) Create a string array, write values into it and print its values.
(b) Write a program for addition of two matrices.
8. (a) Write a program to Multiplication two matrix
(b) Write a program to find whether given number is perfect or not.
9. (a) Write a program to find the maximum value among three values.
(b) Write a program to find roots of a quadratic equation in all cases.
10. (a) Write a program to find Given number is palindrome or not
(b) Write a Program to find the factorial of given number
11. (a) Create a class called test with private data x and y.
(b) Write all necessary constructors and methods to accept data from user,display user data and return the sum
and product of these variable to theuser.
14. (a) Develop an applet to display a simple message “ hello”.
(b) Display any image on the applet
15. (a) Develop an applet that contains two text fields and a button. Write programso that the
second text field displays the factorial of the number in thefirst field when the button is clicked
(b) Display a simple message “FACTORIAL”
1. (A) AIM:-Write a simple Java program to print a line of text.
// prints a line of text
class sample
{
public static void main(String args[])
{
System.out.println("welcome to CSE students");
}
}
Output
welcome to CSE students
integers.
1. (B) AIM:-Write a program to add , subtract , multiply and divide two integers
classArithmeticOperations
{
public static void main(String args[])
{
int a , b ,x1,x2,x3,x4 ;
a = 10 ;
b=6;
x1 = a+b;
x2 = a-b;
x3 = a*b;
x4 = a/b;
System.out.println("a = "+ a + " b= " + b);
System.out.println("a + b = "+ x1);
System.out.println("a - b = "+ x2);
System.out.println("a * b = "+ x3);
System.out.println("a / b = "+ x4);
}
}
Output
a = 10 b= 6
a + b = 16
a-b=4
a * b = 60
a/b=1
2. (A) AIM:-Write a Java program to convert the temperature from the command line in
Celsius to Fahrenheit.
// converts temperature from celsius to fahrenheit
importjava.util.*;
classCelsiusToFahrenheit
{ public static void main(String args[])
{ floatc,f ;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperature in Celsius");
c = in.nextInt();
f = 9*c/5 + 32;
System.out.println("Temperature in Celsius = " + c);
System.out.println("Temperature in Fahrenheit = " + f);
}
}
input
Enter temperature in Celsius 0
output
Temperature in Celsius = 0.0
Temperature in Fahrenheit = 32.0
input
Enter temperature in Celsius 100
output
Temperature in Celsius = 100.0
Temperature in Fahrenheit = 212.0
input
Enter temperature in Celsius 34
output
Temperature in Celsius = 34.0
Temperature in Fahrenheit = 93.2
input
Enter temperature in Celsius -40
output
Temperature in Celsius = -40.0
Temperature in Fahrenheit = -40.0
2. (B) AIM:-Write a simple Java program to find if a number from the command line is
even or not.
This java program finds if a number is odd or even. If the number is divisible by 2 then it is even,
otherwise it is odd. We use modulus operator to find remainder in our program.
// finds the given number is either odd or even
importjava.util.Scanner;
classOddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check either odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if ( x % 2 == 0 )
System.out.println(x+" is an even number.");
else
System.out.println(x+" is an odd number.");
}
}
input
Enter an integer to check either odd or even 3
output
3 is an odd number.
input
Enter an integer to check either odd or even 4
output
4 is an even number.
3. (A) AIM:-Write a Java program to print numbers from 1 to n using the do … while
structure.
importjava.util.Scanner;
class numbers
{ public static void main(String args[])
{ intn,i;
System.out.println("enter an integer");
Scanner in = new Scanner(System.in);
n= in.nextInt();
i=1;
do {
System.out.println(i);
i=i+1;
}
while(i<=n);
}
}
Input
enter an integer 6
output
1
2
3
4
5
6
3. (B) AIM:-Program to print sum of n natural Number
import java.io.*;
classSumNnum
{
public static void main(String[] args) throws IOException
{
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
int s=0, n, m, k=0;
System.out.println ("Enter the value :");
n=Integer.parseInt(br.readLine());
for(int i=0;i<=n;i++)
{
s = s+i;
}
System.out.println("The sum of N numbers is :"+s);
}
}
Input
enter an integer :5
output
Sum of N natural numbers is : 15
4. (A) AIM:-Write a Java program to print the multiplication table of a given number
n using the while structure.
// prints multiplication table of n
importjava.util.Scanner;
classMultiplicationTable
{
public static void main(String args[])
{
int n, i;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :");
for ( i = 1 ; i <= 10 ; i++ )
System.out.println( n+"*"+i+" = "+(n*i));
}
}
input
Enter an integer to print it's multiplication table 5
output
Multiplication table of 5 is :
5*1 = 5
5*2 = 10
5*3 = 15
5*4 = 20
5*5 = 25
5*6 = 30
5*7 = 35
5*8 = 40
5*9 = 45
5*10 = 50
4. (B) AIM:-Find if a given number is prime or not using the ‘ for’ statement.
// Prime number: A number which is only divisible by one and itself is called a // prime number. Divisible
means remainder is zero. 7 is a prime number because // it is only divisible by one and seven. 8 is not a prime
number because it is // divisible by one, two, four and eight.
// program finds a number is prime or not
importjava.util.Scanner;
classPrimeNumber {
public static void main(String args[])
{
int n, j ,status = 1 ;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to find whether prime or not");
n = in.nextInt();
for ( j = 2 ; j <= Math.sqrt(n) ; j++ )
{
if ( n % j == 0 )
{
status = 0;
System.out.println(n+" is not a prime number");
break;
}
}
if ( status == 1 )
{
System.out.println(n+" is a prime number ");
}
}
}
input
Enter a number to find whether prime or not 7
output
7 is a prime number
input
Enter a number to find whether prime or not 8
output
8 is not a prime number
5. (A) AIM:-Write a program that prints the first 20 Fibonacci numbers.
// First few numbers of Fibonacci series are 0, 1, 1, 2, 3, 5, 8 ,etc
// Except first two terms in sequence every other term is the sum of two previous //terms, For example 8 = 3 +
5 (addition of 3, 5). This sequence has many applications //in mathematics and Computer Science.
// prints first 20 Fibonacci numbers
class Fibonacci
{
public static void main(String args[])
{
int f1, f2, f3, c;
f1 = 0;
f2 = 1 ;
System.out.println("First 20 Fibonacci numbers:");
for(c=1;c<=20;c++)
{
System.out.println(f1);
f3=f1+f2;
f1=f2;
f2=f3;
}
}
}
output
First 20 Fibonacci numbers:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
5. (B) AIM:-Create an array of integers and print its values, sum of values andaverage of the values.
// creates an array of integers and print its values, sum of values and
// average of the values
importjava.util.Scanner;
classArrayExample{
public static void main(String args[]){
inti,n,s=0,a[] ;
float average;
System.out.println("How many values in an array");
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new int[n];
System.out.println("enter"+n+"values line by line");
for(i=0;i<n;++i){
System.out.print("enter a value");
a[i] = in.nextInt();
s+=a[i];}
average = (float)s/n ;
System.out.println("Given Array");
for(i=0;i<n;++i){
System.out.println(a[i]);}
System.out.println("sum="+s);
System.out.println("Average="+average);
}}
input
How many values in an array 5
Enter 5 values line by line
enter a value 12
enter a value 3
enter a value 24
enter a value 32
enter a value 5
output
Given Array
12
3
24
32
5
sum=76
Average=15.2
6. (A) AIM:-Find the maximum value from the given array of numbers.
// finds maximum value in a given array of integers
importjava.util.Scanner;
classArrayMax{
public static void main(String args[]){
inti,n,max,a[] ;
System.out.println("How many values in an array");
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new int[n];
System.out.println("enter "+n+" values line by line");
for(i=0;i<n;++i){
System.out.print("enter a value: ");
a[i] = in.nextInt();}
//process to find maximum value
max = a[0];
for(i=1;i<n;++i){
if(max < a[i]) max = a[i]; }
System.out.println("Given Array");
for(i=0;i<n;++i){
System.out.println(a[i]);}
System.out.println("Maximum value = "+max);
}
}
input
How many values in an array 5
enter 5 values line by line
enter a value: 12
enter a value: 23
enter a value: 45
enter a value: 32
enter a value: 18
output
Given Array
12
23
45
32
18
Maximum value = 45
6. (B) AIM:-Arrange the given array into ascending order.
// Sorting an Array of integers
importjava.util.*;
classArraySort{
public static void main(String args[]){
inti,n,a[] ;
System.out.print("How many values in an array: ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
a = new int[n]; //specifies array size
System.out.println("enter "+n+" values line by line");
for(i=0;i<n;++i){
System.out.print("enter a value: ");
a[i] = in.nextInt();}
System.out.println("Given Array");
for(i=0;i<n;++i){
System.out.println(a[i]);}
// for array sorting
Arrays.sort(a);
System.out.println("Sorted Array in ascending order");
for(i=0;i<n;++i){
System.out.println(a[i]);}
}
}
input
How many values in an array: 5
enter 5 values line by line
enter a value: 24
enter a value: 13
enter a value: 6
enter a value: 43
enter a value: 11
output
Given Array
24
13
6
43
11
Sorted Array in ascending order
6
11
13
24
43
7. (A) AIM:-Create a string array, write values into it and print its values.
// Array with strings
importjava.util.*;
classArrayString{
public static void main(String args[]){
inti,n;
String[] a = new String[]{"Govardhan","Bheemeswar","Ramarao",
"Rajareddy", "Basavraj"};
n = a.length;
for(i=0;i<n;++i){
System.out.println(a[i]);}
}
}
Output
Govardhan
Bheemeswar
Ramarao
Rajareddy
Basavraj
7. (B) AIM:-Write a program for addition of two matrices.
// Matrix Addition
importjava.util.Scanner;
classMatrixAddition
{
public static void main(String args[])
{
intm,n,c,d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of a matrix)");
m = in.nextInt();
n =in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Enter "+m*n+" 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 "+m*n+" elements of second matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt();
// process of addition
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
// printing first matrix
System.out.println("First Matrix is:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(first[c][d]+"\t");
System.out.println();
}
// printing second matrix
System.out.println("Second Matrix is:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(second[c][d]+"\t");
System.out.println();
}
System.out.println("Sum of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
}
input
Enter the number of rows and columns of a matrix
2
2
Enter 4 elements of first matrix
1
2
3
4
Enter 4 elements of second matrix
5
6
7
8
output
First Matrix is:1
2
3
4
Second Matrix is:5
6
7
8
Sum of entered matrices:6
8
10
12
8. (A) AIM:-Write a program to Multiplication two matrix
import java.io.*;
classMatrixMultiplication
{
public static void main(String args[]) throws IOException
{
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
inti,j,k;
int a[][] = new int[10][10];
int b[][] = new int[10][10];
int c[][] = new int[10][10];
System.out.println("Enter the A matrix :");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
a[i][j]=Integer.parseInt(br.readLine());
System.out.println("Enter the B matrix :");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
b[i][j]=Integer.parseInt(br.readLine());
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
System.out.println("Multiplication of A & B matrix is :");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
System.out.println(c[i][j]);
}
}
input
Enter the number of rows and columns of a matrix
2
2
Enter 4 elements of first matrix
1
2
3
4
Enter 4 elements of second matrix
5
6
7
8
output
First Matrix is:1
2
3
4
Second Matrix is:1
0
0
1
Sum of entered matrices:1
2
3
4
8. (B) AIM:-Write a program to find whether given number is perfect or not.
/*DECRIPTION :- A perfect number is a positive integer that is equal to the sumof its proper positive
divisors, that is, the sum of its positivedivisors excluding the number itself. Equivalently, a perfect
numberis a number that is half the sum of all of its positive divisors.The first perfect number is 6,
because 1, 2 and 3 are its properpositive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6
is equal to half the sum of all its positive divisors:
( 1 + 2 + 3 + 6 ) / 2 = 6. */
importjava.util.Scanner;
public class PerfectNumber
{
voidcal(int number)
{
int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number){
System.out.println("It is a perfect number");
} else {
System.out.println("It is not a perfect number");
}
}
public static void main(String args[]) throws Exception
{
int n;
PerfectNumberpn = new PerfectNumber();
Scanner in = new Scanner(System.in);
System.out.println("Enter the Number :");
n = in.nextInt();
pn.cal(n);
}
}
Input
Enter the Number: 28
output
It is a perfect number
9. (A) AIM:-Write a program to find the maximum value among three values.
importjava.util.Scanner;
classBigNumber
{
public static void main(String[] args)
{
inta,b,c,big;
Scanner in = new Scanner(System.in);
System.out.println("Enter the three Numbers :");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
if(a<b)
{
if(b<c)
System.out.println("c is Big Number :"+c);
else
System.out.println("b is Big Number :"+b);
}
else
{
if(a<c)
System.out.println("c is Big Number :"+c);
else
System.out.println("a is Big Number :"+a);
}
}
}
Input:Enter the three numbers : 1 2 3
Output :c is Big Number : 3
9. (B)AIM:-Write a program to find roots of a quadratic equation in all cases.
importjava.util.Scanner;
importjava.lang.Math;
public class Qequation
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a,b,c pressing ENTER after each... ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double root = ((b*b)-(4*a*c));
if(root == 0)
{
double r1 = -(((-b)+ Math.sqrt(root))/(2*a) );
System.out.println(" The Equation has the single root: " +r1);
}
if(root > 0)
{
double r1 = -(((-b)+ Math.sqrt(root))/(2*a) );
double r2 = -(((-b)- Math.sqrt(root))/(2*a) );
System.out.println("The Equation has the two roots: " +r1 + " and " +r2);
}
if(root < 0)
{
System.out.println("\n The Equation is an identity - any value is a root.");
}
}
}
Input:Enter a,b,c pressing ENTER after each...: 1 5 6
Output :The Equation has the two roots: 2.0 and 3.0
10. (A) AIM:-Write a program to find Given number is palindrome or not
import java.io.*;
class Palindrome
{
public static void main(String[] args) throws IOException
{
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
intn,m,s=0;
System.out.println ("Enter the value :");
n=Integer.parseInt(br.readLine());
m=n;
while (n!=0)
{
s=s*10+n%10;
n=n/10;
}
if(s==m)
System.out.println("palindrome");
else
System.out.println("Not pliandrome");
}
}
Input
Enter the Value:151
output
palindrome
10. (B)AIM:- Write a Program to find the factorial of given number by using recursion method
import java.io.*;
class Factorial
{
staticint fact(int n)
{
int f;
if(n==1)
return n;
f=n*fact(n-1);
return f;
}
public static void main(String[] args) throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
int n, f;
System.out.println("enter the range of factorial of number :");
n=Integer.parseInt(br.readLine());
f = fact(n);
System.out.println("Factorial value:"+f );
}
}
Input
enter the range of factorial of number:5
output
Factorial value: 120
11. (A) AIM:-Create a class called test with private data x and y
class Test
{
privateintx,y;
void display()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter two Numbers :");
x = in.nextInt();
y = in.nextInt();
System.out.println("Sum of two Numbers :"+(x+y));
}
public static void main(String args[])
{
Test t=new Test();
t.display();
}
}
Input
Enter two Numbers :10 20
output
Sum of two Numbers: 30
11. (B) AIM:- Write all necessary constructors and methods to accept data from user, display
user data and return the sum and product of these variable to the
user.
classMathcal
{
inta,b;
Mathcal(int x, int y)
{
System.out.println("calling constructor");
a=x;b=y;
}
float multi()
{
return a*b;
}
float sum()
{
returna+b;
}
}
classConstructorDemo
{
public static void main(String args[])
{
Mathcalobj= new Mathcal(10,20);
float c= obj.sum();
float d= obj.multi();
System.out.println("Sum of Two Numbers is: "+c);
System.out.println("product of Two Numbers is: "+d);
}
}
OUTPUT:-
Calling constructor
Sum of Two Numbers is: 30
Product of Two Numbers is: 200
14. (A)AIM:-Develop an applet to display a simple message “ hello”.
//develop an Applet to display a simple message “hello”
Her we are giving step wise procedure to create and run an applet
First create a java program hellojava.java
importjava.awt.*;
importjava.applet.*;
public class hellojava extends Applet
{public void paint (Graphics g)
{
g.drawString("hello",10,100);
}}
When this above program is error free , automatically it creates hellojava.class and then create an html
tag hellojava.html as follows. The <applet> tag is the basis for embedding an applet in an HTML file.
Below is an example that invokes an applet:
<HTML>
<BODY>
<APPLET CODE= hellojava.class
WIDTH=400
HEIGHT=200>
</APPLET>
</BODY>
</HTML>
An applet may be invoked by embedding directives in an HTML file and viewing the file through an
applet viewer or Java-enabled browser.
If we use appletviewer
appletviewer hellojava.html causes to display as follows.
14. (b) AIM:- Display any image on the applet
Her we are giving step wise procedure to create and run an applet
First create a java program appletImage.java
importjava.applet.*;
importjava.awt.*;
public class appletImage extends Applet{
Image img;
MediaTrackertr;
public void paint(Graphics g) {
tr = new MediaTracker(this);
img = getImage(getCodeBase(), "demoimg.gif");
tr.addImage(img,0);
g.drawImage(img, 0, 0, this);
}
}
When this above program is error free, automatically it creates appletImage.class and then create an
html tag appletImage.html as follows. The <applet> tag is the basis for embedding an applet in an HTML
file. Below is an example that invokes an applet:
<HTML>
<BODY>
<APPLET CODE= appletImage.class
WIDTH=400
HEIGHT=200>
</APPLET>
</BODY>
</HTML>
An applet may be invoked by embedding directives in an HTML file and viewing the file through an
applet viewer or Java-enabled browser.
OUTPUT:If we use appletviewer
appletviewerappletImage.html causes to display as follows.
15. (a) AIM:- Develop an applet that contains two text fields and a button. Write program
so that the second text field displays the factorial of the number in the
first field when the button is clicked
//ComputeFactorialApplet.java
importjava.applet.Applet;
importjava.awt.*;
importjava.awt.event.*;
public class ComputeFactorialApplet extends Applet implements ActionListener
{
TextField t1,t2;
Label l1,l2;
Button b1;
public void init()
{
l1=new Label("Enter a number:");
add(l1);
t1=new TextField(4);
add(t1);
b1=new Button("Compute");
add(b1);
b1.addActionListener(this);
l2=new Label("The factorial value:");
add(l2);
t2=new TextField(8);
add(t2);
}
public void actionPerformed(ActionEvent e)
{
int n;
if(e.getSource()==b1)
{
int f=1;
n=Integer.parseInt(t1.getText());
for(int i=1;i<=n;i++)
{
f=f*i;
}
t2.setText(String.valueOf(f));
}
}
}
//ComputeFactorialApplet.html
<html>
<applet code="ComputeFactorialApplet"width=250 height = 150>
</applet>
</html>
OUTPUT:-
15. (b)AIM:- Display a simple message “FACTORIAL”
//develop an Applet to display a simple message “FACTORIAL”
Her we are giving step wise procedure to create and run an applet
First create a javaprogram factorialjava.java
importjava.awt.*;
importjava.applet.*;
public class factorialjava extends Applet
{public void paint (Graphics g)
{
g.drawString("FACTORIAL",10,100);
}}
When this above program is error free , automatically it creates factorialjava.class and then create an
html tag factorialjava.html as follows. The <applet> tag is the basis for embedding an applet in an HTML
file. Below is an example that invokes an applet:
<HTML>
<BODY>
<APPLET CODE= factorialjava.class
WIDTH=400
HEIGHT=200>
</APPLET>
</BODY>
</HTML>
An applet may be invoked by embedding directives in an HTML file and viewing the file through an
applet viewer or Java-enabled browser.
OUTPUT:-