Exam Paper Java Programs
Exam Paper Java Programs
97 3051 3051
Exam-Paper Core Java Programs
class OverloadDemo
{
void area(float x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
Write a java program to accept n integers from the user and store them
in on Arraylist collection. Display elements in reverse order
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
for(int i=0;i<num;i++)
{
System.out.println("Enter "+i+" Element of ArrayList :");
String elmt=sc.next();
alist.add(elmt);
}
sc.close();
System.out.println("Orignal list :"+alist);
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
Collections.reverse(alist);
System.out.println("Reverse of a ArrayList is :"+alist);
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileCharacterCount
{
public static void main(String[] args)
{
String fileName = "your_file.txt"; // Replace with the actual file path
int digitCount = 0;
int spaceCount = 0;
int characterCount = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))
{
int c;
while ((c = reader.read()) != -1) {
char ch = (char) c;
if (Character.isDigit(ch)) {
digitCount++;
} else if (Character.isWhitespace(ch)) {
spaceCount++;
}
characterCount++;
}
}
catch (IOException e) {
System.err.println("An error occurred while reading the file.");
e.printStackTrace();
}
System.out.println("Digits: " + digitCount);
System.out.println("Spaces: " + spaceCount);
System.out.println("Characters: " + characterCount);
}
}
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
class FibonacciExample1
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
}
}
import java.io.*;
class FileRev
{
public static void main(String[] args)throws Exception
{
int ch,n=0,i=0,b;
FileInputStream fin=new FileInputStream("abc.txt");
while((ch=fin.read())!=-1)
{
n=n+1;
//System.out.println((char)ch);
}
char str[]=new char[n];
fin.close();
FileInputStream fin1=new FileInputStream("abc.txt");
while((b=fin1.read())!=-1)
{
str[i]=(char)b;
i++;
}
for(i=n-1;i>=0;i--)
{
System.out.print(str[i]);
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
}
fin.close();
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyApp extends Applet
{
TextField name,pass;
Button b1,b2;
public void init()
{
Label n=new Label("Name:",Label.CENTER);
Label p=new Label("password:",Label.CENTER);
name=new TextField(20);
pass=new TextField(20);
pass.setEchoChar('$');
b1=new Button("submit");
b2=new Button("cancel");
add(n);
add(name);
add(p);
add(pass);
add(b1);
add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,130,90,60);
name.setBounds(280,100,90,20);
pass.setBounds(200,140,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
}
}
}
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
Write a java program to copy the data from one file into another file.
import java.io.*;
class FileCopyPro
{
public static void main(String args[ ])
{
try
{
FileInputStream fin=new FileInputStream("abc.txt");
FileOutputStream fout=new FileOutputStream("pqr.txt");
int i;
while((i=fin.read())!=-1)
{
fout.write(i);
}
System.out.println("File Copied Successfully");
fin.close();
fout.close();
}
catch(Exception e)
{
}
}
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
Write Java program which accepts string from user, if its length is less than five,
then throw user defined exception “Invalid String” otherwise display string in
uppercase.
import java.io.*;
class MyNameException extends Exception
{
MyNameException(String msg)
{
super(msg);
}
}
class UserExcp1
{
public static void main(String args[ ])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;
System.out.println("Enter Name");
s=br.readLine();
try
{
if(s.length()<6)
throw new MyNameException("Name is Invalid Exception");
else
System.out.println("Name is Valid "+s.toUpperCase());
}
catch(Exception e)
{
System.out.println("Error = "+e);
}
}
}
String name;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Student (int r,String n,int t1,int t2,int t3)
{
rn=r;
name =n;
m1=t1;
m2=t2;
m3=t3;
total=m1+m2+m3;
per=total/3;
}
}
public void display()
{
System.out.println("<========RESULT========>");
System.out.println("Roll No :"+rn);
System.out.println("Name :"+name);
System.out.println("Marks 1: "+m1);
System.out.println("Marks 2: "+m2);
System.out.println("Marks 3: "+m3);
System.out.println("Total Marks: "+total);
System.out.println("Percentage "+per+"%");
}
}
CustomerDetails() {
l1 = new Label("Customer ID:");
l1.setBounds(50, 50, 100, 30);
t1 = new TextField();
t1.setBounds(200, 50, 150, 30);
b = new Button("Submit");
b.setBounds(200, 200, 80, 30);
b.addActionListener(this);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
setSize(400, 300);
setLayout(null);
setVisible(true);
}
add(l1);
add(l2);
add(l3);
setSize(400, 300);
setLayout(null);
setVisible(true);
}
}
}
void area()
{
float a1=2*pi*red*height+2*pi*red*red;
System.out.println("Area of Cylinder-"+a1);
}
void volume()
{
float v1=pi*red*red*height;
System.out.println("Volume of Cylinder-"+v1);
}
}
class ShapeDemo1
{
public static void main(String arg[])
{
int r,s,h;
DataInputStream din=new DataInputStream(System.in);
try
{
System.out.println("Enter the values of Redius, Side , height-");
r=Integer.parseInt(din.readLine());
s=Integer.parseInt(din.readLine());
h=Integer.parseInt(din.readLine());
Cone c1=new Cone(r,s,h);
c1.area();
c1.volume();
Cylinder c2=new Cylinder(r,h);
c2.area();
c2.volume();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
import java.applet.*;
import java.awt.*;
/*<applet code ="Smiley" width=600 height=600>
</applet>*/
Write a java program which accepts student details (Sid, Sname, Saddr)
from user and display it on next frame. (Use AWT).
import java.awt.*;
import java.awt.event.*;
StudentDetails() {
l1 = new Label("Stud_Id");
l1.setBounds(50, 50, 100, 30);
l2 = new Label("Stud_Name");
l2.setBounds(50, 100, 100, 30);
l3 = new Label("Address");
l3.setBounds(50, 150, 100, 30);
t1 = new TextField();
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
b = new Button("Submit");
b.setBounds(100, 200, 100, 30);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new DisplayFrame(t1.getText(), t2.getText(), t3.getText());
}
});
add(l1);
add(l2);
add(l3);
add(t1);
add(t2);
add(t3);
add(b);
setSize(400, 400);
setLayout(null);
setVisible(true);
}
class DisplayFrame {
DisplayFrame(String sid, String sname, String saddr) {
Frame f = new Frame();
Label l1 = new Label("Sid: " + sid);
l1.setBounds(50, 50, 100, 30);
Label l2 = new Label("Sname: " + sname);
l2.setBounds(50, 100, 100, 30);
Label l3 = new Label("Saddr: " + saddr);
l3.setBounds(50, 150, 100, 30);
f.add(l1);
f.add(l2);
Saaish Academy, Canada Corner, Nashik. 97 3051 3051
Exam-Paper Core Java Programs
f.add(l3);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
try
{
reader = new BufferedReader(new FileReader("test.txt"));
String currentLine = reader.readLine();
while (currentLine != null)
{
lineCount++;
String[] words = currentLine.split(" ");
wordCount = wordCount + words.length;
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
class Teacher
{
int tid;
String tname;
String designation;
double salary;
String subject;
// Constructor
Teacher(int tid, String tname, String designation, double salary, String subject) {
this.tid = tid;
this.tname = tname;
this.designation = designation;
this.salary = salary;
this.subject = subject;
}
}
if (!found) {
System.out.println("No teachers found who teach Java.");
}
sc.close();
}
}