Java 12
Java 12
of
JAVA PROGRAMMING
(Code: CSEB3501T)
Submitted by:
Submitted to:
Er. Karandeep Singh
Er. Priyanka Jarial
2
1)WAP to perform basic calculator operations.
import java.io.*;
import java.lang.*;
import java.lang.Math;
import java.util.Scanner;
public class BasicCalculator {
public static void main(String[] args)
{
double num1, num2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the numbers:");
num1 = sc.nextDouble();
num2 = sc.nextDouble();
System.out.println("Enter the operator (+,-,*,/):");
char op = sc.next().charAt(0);
double o = 0;
switch (op) {
case '+':
o = num1 + num2;
break;
case '-':
o = num1 - num2;
break;
case '*':
o = num1 * num2;
break;
case '/':
o = num1 / num2;
3
break;
default:
System.out.println("You enter wrong input");
}
System.out.println("The final result:");
System.out.println();
System.out.println(num1 + " " + op + " " + num2
+ " = " + o);
}
}
Output:
Enter the numbers:
2
2
Enter the operator (+,-,*,/)
+
The final result:
2.0 + 2.0 = 4.0
4
2) WAP to calculate a factorial of a number using recursion.
class Test {
static int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
public static void main(String[] args)
{ int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(5));
}
}
Output:
Factorial of 5 is 120
5
3) WAP to prints first 15 numbers of fibonacci series.
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
}}
Output:
0 1 1 2 3 5 8 13 21 34
6
4) WAP to check weather a given number and string is palindrome or
not.
class Main {
public static void main(String[] args) {
String str = "Radar", reverseStr = "";
int strLength = str.length();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + str.charAt(i);
}
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
Output:
Radar is a Palindrome String.
7
5) WAP to check weather a given number is armstrong or not.
public class Armstrong {
public static void main(String[] args) {
int number = 371, originalNumber, remainder, result = 0;
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}
Output:
371 is an Armstrong number.
8
6) WAP to concatenate two strings.
class TestStringConcatenation1{
public static void main(String args[]){
String s="Sachin"+" Tendulkar";
System.out.println(s);//Sachin Tendulkar
}
}
Output:
Sachin Tendulkar
9
7) WAP to implement basic concepts of control statements using class.
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
10
8) WAP to check whether the given array is mirror inverse or not.
public class GFG {
static boolean isMirrorInverse(int arr[])
{ for (int i = 0; i < arr.length; i++) {
if (arr[arr[i]] != i)
return false;
}
return true;
}
public static void main(String[] args)
{ int arr[] = { 1, 2, 3, 0 };
if (isMirrorInverse(arr))
System.out.println("Yes");
else
System.out.println("No");
}
}
Output:
No
11
9) WAP to implement constructors and overloading.
public class Student {
int id;
String name;
Student(){
System.out.println("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
Student s = new Student();
System.out.println("\nDefault Constructor values: \n");
System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);
System.out.println("\nParameterized Constructor values: \n");
Student student = new Student(10, "David");
System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
}
}
Output:
this a default constructor
Default Constructor values:
Student Id : 0
Student Name : null
Parameterized Constructor values:
Student Id : 10
Student Name : David
12
10) WAP to implement recursion, functions and arrays.
class GFG {
int fact(int n)
{ int result;
if (n == 1)
return 1;
result = fact(n - 1) * n;
return result;
}
}
class Recursion {
public static void main(String[] args)
{
GFG f = new GFG();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}
Output:
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
13
11) WAP to perform binary search.
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
Output:
Element is found at index: 2
14
12) WAP to implement Inheritance, interfaces and packages.
import java.io.*;
interface intfA {
void m1();
}
interface intfB {
void m2();
}
class sample implements intfA, intfB {
@Override public void m1()
{
System.out.println("Welcome: inside the method m1");
}
@Override public void m2()
{
System.out.println("Welcome: inside the method m2");
}
}
class GFG {
public static void main(String[] args)
{
sample ob1 = new sample()
ob1.m1();
ob1.m2();
}
}
Output:
Welcome: inside the method m1
Welcome: inside the method m2
15
13) WAP which will explain the concept of try, catch and throw.
class ThrowExcep {
static void help()
{
try {
throw new NullPointerException("error_unknown");
}
catch (NullPointerException e) {
System.out.println("Caught inside help().");
throw e;
}
}
public static void main(String args[])
{
try {
help();
}
catch (NullPointerException e) {
System.out.println( "Caught in main error name given below:");
System.out.println(e);
}
}
}
Output:
Caught inside help().
Caught in main error name given below:
java.lang.NullPointerException: error_unknown
16
14) WAP to demonstrate the use of appelets.
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet
{
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}
Output:
appletviewer HelloWorld
17
15) WAP to demonstrate threads and animations.
import java.awt.*;
import java.applet.*;
public class AnimationExample extends Applet {
Image picture;
try{Thread.sleep(100);}catch(Exception e){}
}
}
}
18
16) WAP to explain I/O streams and files and socket programming.
import java.io.File;
public class ReadDir {
public static void main(String[] args) {
File file = null;
String[] paths;
try {
file = new File("/tmp");
paths = file.list();
for(String path:paths) {
System.out.println(path);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
test1.txt
test2.txt
ReadDir.java
ReadDir.class
19
17) WAP to implements Applets and use of it on internet.
import java.awt.*;
import java.applet.*;
public class GfgApplet extends Applet
{
String msg="";
public void init()
{
msg="Hello Geeks";
}
public void start()
{
msg=msg+",Welcome to GeeksForGeeks";
}
public void paint(Graphics g)
{
g.drawString(msg,20,20);
}
}
Output:
20
18) WAP to describe AWT Class, Frames, Panels and Drawing.
import java.awt.*;
class AWTExample2 {
AWTExample2() {
Frame f = new Frame();
Label l = new Label("Employee id:");
Button b = new Button("Submit");
TextField t = new TextField();
l.setBounds(20, 80, 80, 30);
t.setBounds(20, 100, 80, 30);
b.setBounds(100, 100, 80, 30);
f.add(b);
f.add(l);
f.add(t);
f.setSize(400,300);
f.setTitle("Employee info");
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]) {
AWTExample2 awt_obj = new AWTExample2();
}
}
Output:
21
19) WAP to demonstrate JDBC and build an application.
import java.io.*;
import java.sql.*;
class GFG {
public static void main(String[] args) throws Exception
{ String url = "jdbc:mysql://localhost:3306/table_name";
String username = "rootgfg"; // MySQL credentials
String password = "gfg123";
String query= "select *from students";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection( url, username, password);
System.out.println("Connection Established successfully");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query); // Execute query
rs.next();
String name = rs.getString("name"); // Retrieve name from db
System.out.println(name); // Print result on console
st.close(); // close statement
con.close(); // close connection
System.out.println("Connection Closed....");
}
}
Output:
22
20) WAP to implements use of JSP.
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%
out.println("<h1>Hello World.</h1>");
out.println("<h1>This is my first JSP Program.</h1>");
%>
</body>
</html>
Output:
Hello World.
This is my first JSP Program.
23