COMPUTER WORKSHOP CS 306
[Link] a Program to add two numbers in JAVA….
import [Link];
class Sum
{
public static void main(String args[])
{
int x,y,z;
[Link]("Enter the value of a & b");
Scanner obj1= new Scanner([Link]);
x=[Link]();
y=[Link]();
z=x+y;
[Link]("Sum of the integer ="+z);
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
[Link] a Program to find Square root of a given number…
import [Link];
class Root
{
public static void main(String args[])
{
double x,y;
[Link]("Enter the value of x ");
Scanner sq= new Scanner([Link]);
x=[Link]();
y=[Link](x);
[Link]("Square root is ="+y);
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
3. Write a Program to sort array in increasing order…
import [Link];
public class InsertSort
{
public static void main (String [] args)
{
int [] array = {45,12,85,32,89,39,69,44,42,1,6,8};
int temp;
for (int i = 1; i < [Link]; i++)
{
for (int j = i; j > 0; j--)
{
if (array[j] < array [j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
NANDINI BHAVSAR CS03 BATCH 1
}
}
}
for (int i = 0; i < [Link]; i++)
{
[Link](array[i]);
}
} }
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
[Link] a Program to sort array in Decreasing order
import [Link];
public class InsertSort1
{
public static void main (String [] args)
{
int [] array = {45,12,85,32,89,39,69,44,42,1,6,8};
int temp;
for (int i = 1; i < [Link]; i++)
{
for (int j = i; j > 0; j--)
{
if (array[j] > array [j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
for (int i = 0; i < [Link]; i++)
{
[Link](array[i]);
}
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
[Link] a Program to compare the salary of two Employers
import [Link];
class compare
{
public static void main(String args[])
{
int x,y;
[Link]("Enter two integers for calculation");
Scanner obj1 = new Scanner([Link]);
x=[Link]();
y=[Link]();
if(x>y)
{
[Link]("The larger is x and x = " +x );
}
else
{
[Link]("The larger is y and y = "+ y );
}
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
6. Write a Program for multiplication in 2D Array
import [Link];
class Aarray
{
public static void main(String args[])
{
int myArray [] [] = new int [3] [4];
int i,j;
for (i=0;i<3;i++)
for(j=0;j<4;j++)
myArray[i][j] = i*j;
for (i=0;i<3;i++) {
for(j=0;j<4;j++)
[Link](myArray[i][j] + " ");
[Link]();
}
[Link]();
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
[Link] a Program for summation in 3D Array
import [Link];
class a3DArray
{
public static void main(String args[])
{
int my3DArray [] [] [] = new int [2] [3] [4];
int i,j,k;
for (i=0;i<2;i++)
for(j=0;j<3;j++)
for(k=0;k<4;k++)
my3DArray[i][j][k] = i+j+k;
for (i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
[Link](my3DArray[i][j][k] + " ");
[Link]();
}
[Link]();
}
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
8 Write a Program to using Static KeyWord…
import [Link];
class Emp
{
static double salary;
static String name = "Punit";
}
public class EmpDemo
{
public static void main(String args[])
{
[Link]=25000;
[Link]([Link]+"s average salary:"+[Link]);
}
}
9. Write a Program to find Ascii value
public class AsciiValue {
public static void main(String[] args) {
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
char ch = 'a';
int ascii = ch;
int convertAscii = (int) ch;
[Link]("The ASCII value of " + ch + " is: " + ascii);
[Link]("The ASCII value of " + ch + " is: " + convertAscii);
}
}
10. Write a Program to perform all binary operation
import [Link];
class calci
{
public static void main(String args[])
{
int a,b,di,s,m,mu;
[Link]("Enter two integers for calculation");
Scanner obj1 = new Scanner([Link]);
a=[Link]();
b=[Link]();
s=a+b;
m=a-b;
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
mu=a*b;
di=a/b;
[Link]("Sum of two number will be \n" + s);
[Link]("Subraction of two number will be \n" + m);
[Link]("Multipication of two number will be\n" + mu);
[Link]("Division of two number will be \n" + di); }}
11. Write a Program to compare the numbers
import [Link];
class compare
{
public static void main(String args[])
{
int x,y;
[Link]("Enter two integers for calculation");
Scanner obj1 = new Scanner([Link]);
x=[Link]();
y=[Link]();
if(x>y)
{
[Link]("The larger is x and x = " +x );
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
}
else
{
[Link]("The larger is y and y = "+ y );
}
}
}
12. Write a Program for Division of two number..
import [Link];
class div
{
public static void main(String args[])
{
int a,b,z;
[Link]("Enter two integers for division");
Scanner obj1 = new Scanner([Link]);
a=[Link]();
b=[Link]();
z=a/b;
[Link]("Division of two number will be " + z);
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
13. Write a Program to convert String to Integer
public class String_to_Integer {
public static void main(String args[])
{
String s="333";
int i=[Link](s);
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
[Link](i);
}
}
14. Write a Program to count the letters, spaces, numbers & other character of String
import [Link];
public class Counts_variable
{
public static void main(String[] args)
{
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
String test = "ajv ivbjsdbvi uvbskjb ?? sknvsi 3245 bsdnbio =vnei";
Count(test);
}
public static void Count(String x)
{
char[] ch = [Link]();
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i = 0; i < [Link](); i++){
if([Link](ch[i])){
letter ++ ;
}
else if([Link](ch[i])){
num ++ ;
}
else if([Link](ch[i])){
space ++ ;
}
else{
other ++;
}
}
[Link]("The string is : ajv ivbjsdbvi uvbskjb ?? sknvsi 3245
bsdnbio=vnei");
[Link]("letter: " + letter);
[Link]("space: " + space);
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
[Link]("number: " + num);
[Link]("other: " + other);
}
}
15. Write a Program for result of operation
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
import [Link];
class Operations
{
public static void main(String args[])
{
int a,b,c,d;
a=5*10-36;
b=(85-20)%9;
c=20+(-3*5)/8;
d=5+15/3*2-8%3;
[Link]("output of two number will be " + a);
[Link]("output of two number will be " + b);
[Link]("output of two number will be " + c);
[Link]("output of two number will be " + d);
}
}
16. Write a Program to print Hello World in applet
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
import [Link];
import [Link];
public class HelloWorld extends Applet
{
public void paint(Graphics g)
{
[Link]("Hello World!",150,150);
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
1. Complete the code segment to call the method print() of class Student first and then
call print() method of class School.
import [Link];
class School {
public void print() {
[Link]("Hi! This is class SCHOOL.");
}
}
class Student {
public void print() {
[Link]("Hi! This is class STUDENT");
}
}
public class Question212{
public static void main(String[] args) {
School write = new School();
[Link]();
Student show = new Student();
[Link]();
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
2. Complete the code segment to call print() method of class Question by creating a
method named ‘studentMethod()’.
import [Link];
public class Question213
{
public static void main(String[] args)
{
Question213 q = new Question213();
[Link]();
}
void studentMethod()
{
Question213 q = new Question213();
[Link]();
}
void print()
{
[Link]("Well Done!");
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
3. Complete the code segment to call default constructor first and then any
other constructor in the class Answer
import [Link]
public class Question214
{
public static void main(String[] args)
{
Answer a=new Answer();
Answer b = new Answer(10,"MCQ");
}
}
class Answer
{
Answer()
{
[Link]("You got nothing.");
}
Answer(int marks, String type)
{
[Link]("You got "+marks+" for an "+ type);
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
4. Complete the code segment to find the perimeter and area of a circle given a value
of [Link] should use [Link] constant in your [Link] radius is zero or less
than zero then print " please enter non zero positive number "
import [Link];
public class Exercise1_1
{
public static void main(String args[])
{
int a,b,c;
Scanner s=new Scanner([Link]);
double radius=[Link]();
double perimeter;
double area;
if(radius<=0)
{
[Link]("Please enter non zero positive number");
}
else
{
perimeter=2*[Link]*radius;
area=[Link]*radius*radius;
[Link](perimeter);
[Link](area);
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
5. Complete the code segment to find the largest among three numbers x, y, and
z. You should use if-then-else construct in Java.
import [Link];
public class Exercise1_2
{
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
int x=[Link]();
int y=[Link]();
int z=[Link]();
int result=0;
if(x>y && x>z)
{
[Link]("Biggest Number is"+" "+x);
}
else if(y>x && y>z)
{
[Link]("Biggest Number is"+" "+y);
}
else
{
[Link]("Biggest Number is"+" "+z);
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
6. Complete the code segment to check whether the number is an Armstrong
number or not.
import [Link];
public class Exercise1_3
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int sum=0;
int cal=0;
for(int i=0;;i+=2)
{
if((cal==n+1))
{
break;
}
if(i%3==0)
{
sum+=i;
}
cal+=1;
}
[Link](sum);
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
7. Complete the code segment to help Ram , find the highest mark and average mark
secured by him in "s" number of subjects.
import [Link];
import [Link];
public class Exercise1_4
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int n2=n;
int result=0;
int cal=0;
int n1=n;
int sum=0;
int re;
while(n1>0)
{
n1=n1/10;
cal++;
}
while(n>0)
{
re=n%10;
result+=[Link](re,cal);
n=n/10;
}
if(result==n2)
{
[Link]("Number is an Armstrong Number");
}
else
{
[Link]("Number is not an Armstrong Number");
}
}
}
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
8. Consider First n even numbers starting from zero(0).Complete the code segment to calculate
sum of all the numbers divisible by 3 from 0 to [Link] the sum.
import [Link];
public class Exercise1_5
{
public static void main(String args[])
{
Scanner input=new Scanner([Link]);
double mark_avg=0;
int result;
int i;
int s,high=0;
s=[Link]();
int[] arr=new int[s];
for(i=0;i<[Link];i++)
{
arr[i]=[Link]();
}
for(i=0;i<[Link];i++)
{
if(high<arr[i])
{
high=arr[i];
}
}
for(i=0;i<[Link];i++)
{
NANDINI BHAVSAR CS03 BATCH 1
COMPUTER WORKSHOP CS 306
mark_avg+=arr[i];
}
[Link](high);
[Link](mark_avg/[Link]);
}
}
NANDINI BHAVSAR CS03 BATCH 1