Академический Документы
Профессиональный Документы
Культура Документы
1(a):Write a Java program that takes a number as input and prints its multiplication table upto 10
Program
import java.util.Scanner;
Class Displaypattern
{
Public static void main(String ar[])
{
Int i,j;
For(i=5;i>=1;i--)
{
For(j=1;j<=1;j++)
{
System.out.print(“*”)
}
System.out.println();
}
}
}
Output:
*****
****
***
**
*
1(C):. Write a Java program to print the area and perimeter of a circle
Program
public class Areaperimeter
{
private static final double radius = 7.5;
public static void main(String[] args)
{
double perimeter = 2 * Math.PI * radius;
double area = Math.PI * radius * radius;
Output:
Perimeter is = 47.12388980384689
Area is = 176.71458676442586
Practical no 2
2.Use of Operators
2(a):Write a Java program to add two binary numbers.
Program
import java.util.Scanner;
public class Exercise17 {
public static void main(String[] args)
{
long binary1, binary2;
int i = 0, remainder = 0;
int[] sum = new int[20];
Scanner in = new Scanner(System.in);
Output:
110110111 in binary = 439 in decimal
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "puzzles";
byte [] result =
new byte [strAsByteArray.length];
System.out.println(new String(result));
}
}
Output:
Zzupesl
Practical no 3.
3.Java Data Types
3(a).Write a Java program to count the letters, spaces, numbers and other
characters of an input string.
Program
import java.util.Scanner;
public class Exercise38 {
}
public static void count(String x){
char[] ch = x.toCharArray();
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i = 0; i < x.length(); i++){
if(Character.isLetter(ch[i])){
letter ++ ;
}
else if(Character.isDigit(ch[i])){
num ++ ;
}
else if(Character.isSpaceChar(ch[i])){
space ++ ;
}
else{
other ++;
}
}
System.out.println("The string is : Aa kiu, I swd skieo 236587. GH
kiu: sieo?? 25.33");
System.out.println("letter: " + letter);
System.out.println("space: " + space);
System.out.println("number: " + num);
System.out.println("other: " + other);
}}
Output:
The string is : Aa kiu, I swd skieo 236587. GH kiu: sieo?? 25.33
letter: 23
space: 9
number: 10
other: 6
3(b).Implement a Java function that calculates the sum of digits for a given
char array consisting of the digits '0' to '9'. The function should return
the digit sum as a long value.
Program
Class A
{
Public static void main(String ar[])
{
String str=”67281”;
Char array[]=str.toCharArray();
Long s=SumOfDigits.sumOfDigits(array);
System.out.println(“sum of digits is:”+ s);
}
}
Class SumOfDigits
{
Public static long sumOfDigits(char arr[])
{
Long sum=0;
int n;
int i=0;
While(i<=arr.length)
{
n=(int)arr[i];
Sum =sum+n;
i++;
}
return sum;
}
}
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
System.out.print(“nSmallest Element:”+small);
System.out.println(“nLargest Element:”+large);
}
}
Output:
Enter no of elements: 6
Enter elements of array: 2 6 38 28 17 12
Smallest element: 2
Largest elements: 38
Practical no 4.
4.Methods and Constructors
4(a):Designed a class SortData that contains the method asec() and desc().
Program
import java.util.*;
class prac4A
{
Scanner input=new Scanner(System.in);
int num,i;
int arr[];
int temp=0;
public void getdata()
{
System.out.print("Enter the size of array: ");
num=input.nextInt();
arr=new int[num];
System.out.print("Enter the number: ");
for( i=0;i<num;i++)
{
arr[i]=input.nextInt();
}
}
void putdata()
{
System.out.print("Given numbers are: ");
for(i=0;i<num;i++)
{
System.out.println(arr[i]);
}
}
void asce()
{
for(i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.print("Ascending order of number are: ");
for(int i=0;i<num;i++)
{
System.out.println(arr[i]);
}
}
void desc()
{
for(i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.print("Descending order of number are: ");
for(int i=0;i<num;i++)
{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{
prac4A ob=new prac4A();
ob.getdata();
ob.putdata();
ob.asce();
ob.desc();
}
}
Output:
Enter array elements:
35
21
54
61
81
29
60
24
33
48
Program
import java.util.Scanner;
abstract class test
{
abstract void get();
}
class test1 extends test
{
void get()
{
int a,b;
Scanner ob=new Scanner(System.in);
System.out.print("Enter 1st Number: ");
a=ob.nextInt();
System.out.println("Enter 2st Number: ");
b=ob.nextInt();
System.out.println("Addition is: "+(a+b));
}
}
class prac4C
{
public static void main(String args[])
{
test1 obj=new test1();
obj.get();
}
}
Output:
This is a normal method of abstract class A
Abstract method is overridden in derived class
This is normal method of derived class B
Practical no 5.
5.Inheritance
5(a)Write a java program to implement single level.
Program
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}}
Output:
Main starts
This set() method of base class
This get() method of base class
This check() method of derived class
This demo() method of derived class
Main ends
5(b)Write a java program to implement method overriding inheritance.
Program:
class Parent
{
void show() { System.out.println("Parent's show()");
}
}
Output:
Parent's show()
Child's show()
5(c)Write a java program to implement multiple inheritance
Program
// First Parent class
class Parent1
{
void fun()
{
System.out.println("Parent1");
}
}
class Parent2
{
void fun()
{
System.out.println("Parent2");
}
}
class Test extends Parent1, Parent2
{
public static void main(String args[])
{
Test t = new Test();
t.fun();
}
}
Output:
Compiler Error
7.VECTORS AND MULTITHREADING
Program 7(a): Write a Java program to implement the vectors.
import java.util.*;
class Vector_demo {
arr.add(3);
arr.add("geeks");
arr.add("forgeeks");
arr.add(4);
v.addAll(arr);
OUTPUT:-
Program 7(b): Write a program to implement thread life cycle.
private Thread t;
threadName = name;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
if (t == null) {
R1.start();
R2.start();
Output:
:-
Program 7(c): Write a java program to implement multithreading.
try
Thread.currentThread().getId() +
"is running");
}catch(Exception e)
int n = 8;
object.start();
Output:
8 . File Handling
Program 8(a): Write a program to open a file and display the contents in the console window.
import java.util.Scanner;
import java.io.*;
String testwords=in.nextLine();
fw.write(testwords);
System.out.print(r);
fw.close();
}
Program 8(b): Write a java program to copy the contents from one file to other file.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
try {
String s;
fw.write(s);
fw.flush();
br.close();
fw.close();
System.out.println("file copied");
catch (IOException e)
e.printStackTrace();
}
}
Program 8(c):- Write a java program to read the student data from the user and store it in the
file.
import java.io.*;
int roll;
String name,city;
file.createNewFile();
roll=Integer.parseInt(dis.readLine());
name=dis.readLine();
city=dis.readLine();
writer.write("Roll number"+roll);
writer.write("Name is "+name);
writer.write("City is"+city);
writer.flush();
writer.close();
System.out.println("Student Data saved into the file");
import java.awt.*;
import java.awt.event.*;
TextField t1,t2;
int fact=1,m;
Button b1,b2,b3;
String msg;
Label l1,l2;
FactEvent e;
{e=this;
t1=new TextField(3);
t2=new TextField(10);
l2=new Label("RESULT");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
b1.addActionListener(this);
String str=t1.getText();
if(str!="")
int num=Integer.parseInt(str);
for(int i=num;i>0;i--)
fact=fact*i;
msg="" +fact;
t2.setText(msg);
fact=1;
OUTPUT:-
Program 9(b): Design an AWT program to perform various string operations like reverse string, string
concatenation etc.
import java.awt.*;
import java.applet.*;
TextField Ts1,Ts2;
add(Ts1);
add(Ts2);
Ts1.setText("");
Ts2.setText("");
}
String str1,str2;
str1=Ts1.getText();
str2=Ts2.getText();
g.setColor(Color.red);
g.drawString(str1+" "+str2,10,70);
showStatus("Concatination of 2 String");
repaint();
return true;
OUTPUT:-
Program 9(c): Write a java program to implement exception handling
class StringIndexOutOfBound_Demo
try {
System.out.println(c);
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
OUTPUT:-
10. GUI PROGRAMMING
Program 10(a): Design an AWT application that contains the interface to add student information
and display the same.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
</applet> */
String s1,s2,s3;
TextField t3,t4,t5,t6,t7;
Button tot,avg;
Checkbox c1,c2,c3,c4,m,f;
CheckboxGroup cbg;
Panel p1,p2,p3,p4;
s3=" ";
tot=new Button("Total");
avg=new Button("Average");
c1=new Checkbox("MCA",true);
c4=new Checkbox("MSIS");
cbg=new CheckboxGroup();
m=new Checkbox("Male",cbg,false);
f=new Checkbox("Female",cbg,true);
p1=new Panel();
p1.setLayout(new GridLayout(2,2));
p1.add(new TextField(5));
p1.add(new TextField(15));
add(p1);
p2.add(new Label("Gender"));
p2.add(m);
p2.add(f);
add(p2);
p3.add(new Label("Degree"));
add(p3);
p4.add(tot); p4.add(avg);
tot.addActionListener(this);
avg.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);
m.addItemListener(this);
f.addItemListener(this);
add(p4);
int no,m1,m2,m3,tot;
float avg=0.0f;
no=m1=m2=m3=tot=0;
try
m1=Integer.parseInt(t3.getText());
m2=Integer.parseInt(t4.getText());
m3=Integer.parseInt(t5.getText());
catch(Exception e)
tot=m1+m2+m3;
avg= tot/3;
s1=String.valueOf(tot);
s2=String.valueOf(avg);
repaint();
return true;
s3=e.getActionCommand();
if(s3.equals("Total"))
t6.setText(s1);
if(s3.equals("Average"))
t7.setText(s2);
repaint();
repaint();
Output:-
Program 10(b) : Design a calculator based on AWT application.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
TextField t1;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
String msg="",tmp;
int a, b;
setLayout(null);
t1=new TextField(20);
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
add=new Button("+");
sub=new Button("-");
div=new Button("/");
mul=new Button("*");
dot=new Button(".");
eql=new Button("=");
add(t1);
add(b7);
add(b8);
add(b9);
add(div);
add(b4);
add(b5);
add(b6);
add(mul);
add(b1);
add(b2);
add(b3);
add(sub);
add(dot);
add(b0);
add(eql);
add(add);
t1.setBounds(30,30,200,40);
b7.setBounds(30,80,44,44);
b8.setBounds(82,80,44,44);
b9.setBounds(134,80,44,44);
b4.setBounds(30,132,44,44);
b5.setBounds(82,132,44,44);
b6.setBounds(134,132,44,44);
b1.setBounds(30,184,44,44);
b2.setBounds(82,184,44,44);
b3.setBounds(134,184,44,44);
dot.setBounds(30,236,44,44);
b0.setBounds(82,236,44,44);
eql.setBounds(134,236,44,44);
add.setBounds(186,236,44,44);
sub.setBounds(186,184,44,44);
mul.setBounds(186,132,44,44);
div.setBounds(186,80,44,44);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
//b0.addActionListener(this);
//b0.addActionListener(this);
div.addActionListener(this);
mul.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
eql.addActionListener(this);
if (str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/"))
tmp=str;
a = Integer.parseInt(str1);
msg="";
else if(str.equals("="))
{
b = Integer.parseInt(str2);
int sum=0;
if(tmp=="+")
sum=a+b;
else if(tmp=="-")
sum=a-b;
else if(tmp=="*")
sum=a*b;
else if(tmp=="/")
sum=a/b;
String str1=String.valueOf(sum);
t1.setText(""+str1);
msg="";
else
//String ae.getActionCommand();
//str += ae.getActionCommand();
msg+=str;
t1.setText(""+msg);
}
public void paint(Graphics g)
g.setColor(Color.cyan);
g.fillRect(20,20,220,270);
OUTPUT :-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
</applet> */
String s1,s2,s3;
TextField t3,t4,t5,t6,t7;
Button tot,avg;
Checkbox c1,c2,c3,c4,m,f;
CheckboxGroup cbg;
Panel p1,p2,p3,p4;
s3=" ";
tot=new Button("Total");
avg=new Button("Average");
c1=new Checkbox("MCA",true);
c3=new Checkbox("MSIT");
c4=new Checkbox("MSIS");
cbg=new CheckboxGroup();
m=new Checkbox("Male",cbg,false);
f=new Checkbox("Female",cbg,true);
p1=new Panel();
p1.setLayout(new GridLayout(2,2));
p1.add(new TextField(5));
p1.add(new TextField(15));
add(p1);
p2.add(new Label("Gender"));
p2.add(m);
p2.add(f);
add(p2);
p3.add(new Label("Degree"));
add(p3);
p4.add(tot); p4.add(avg);
tot.addActionListener(this);
avg.addActionListener(this);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);
m.addItemListener(this);
f.addItemListener(this);
add(p4);
int no,m1,m2,m3,tot;
float avg=0.0f;
no=m1=m2=m3=tot=0;
try
m1=Integer.parseInt(t3.getText());
m2=Integer.parseInt(t4.getText());
m3=Integer.parseInt(t5.getText());
}
catch(Exception e)
tot=m1+m2+m3;
avg= tot/3;
s1=String.valueOf(tot);
s2=String.valueOf(avg);
repaint();
return true;
s3=e.getActionCommand();
if(s3.equals("Total"))
t6.setText(s1);
if(s3.equals("Average"))
t7.setText(s2);
repaint();
Output