Java File 2021

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

LAB FILE

2021

JAVA
LAB FILE

Submitted By: Submitted To:

Name: - Saurav Mehto Prof.Ms. Ritu Bansal

Course: - MCA (LE)

Semester: - 3rd

WAP to Reverse of number and found its sum


import java.io.*;

import java.util.Scanner;

public class Sum {

public static void main(String[] args) {

int num, rem;

int rev = 0, sum = 0;

System.out.println("Enter an integer to reverse");

Scanner in = new Scanner(System.in);

num = in.nextInt();

do {

rem = num % 10;

rev = rev * 10 + rem;

sum = sum + rem;

num = num / 10;

while (num > 0);

System.out.println("Reverse of given number: " + rev);

System.out.println("Sum of digits of given number: " + sum);

WAP to convert Fahrenheit into Celsius


package javaSchool;
import java.util.Scanner;

public class Fahrenheit {

public static void main(String[] args) {

double a,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Fahrenheit temperature");
a=sc.nextDouble();
System.out.println("Celsius temperature is = "+celsius(a));

}
static double celsius(double f)
{
return (f-32)*5/9;
}

WAP to Perform Arithmetic operation


package javaSchool;
import java.util.Scanner;
public class Arithmetic {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);


while(true)
{
System.out.println("");
System.out.print("Enter the First number: ");
int x = s.nextInt();
System.out.print("Enter the Second number: ");
int y = s.nextInt();
System.out.println("Choose the operation you want to perform");
System.out.println("Choose 1 for ADDITION");
System.out.println("Choose 2 for SUBTRACTION");
System.out.println("Choose 3 for MULTIPLICATION");
System.out.println("Choose 4 for DIVISION");
System.out.println("Choose 5 for MOD");
System.out.println("Choose 6 for EXIT");
int n = s.nextInt();
switch(n)
{
case 1:
int add;
add = x + y;
System.out.println("ADDITION Result : "+add);
break;

case 2:
int sub;
sub = x - y;
System.out.println("SUBTRACTION Result : "+sub);
break;

case 3:
int mul;
mul = x * y;
System.out.println("MULTIPLICATION Result : "+mul);
break;

case 4:
float div;
div = (float) x / y;
System.out.print("DIVISION Result : "+div);
break;

case 5:
int mod;
mod = x % y;
System.out.println("MOD Result : "+mod);
break;

case 6:
System.out.println("Operation EXIT");
System.exit(0);
}
}
}

WAP to find Addition of Complex number


package javaSchool;

class ComplexNumber {
int real, image;
public ComplexNumber(int r, int i)
{
this.real = r;
this.image = i;
}

public void showC()


{
System.out.print(this.real + " +i" + this.image);
}
public static ComplexNumber add(ComplexNumber n1,
ComplexNumber n2)
{
ComplexNumber res = new ComplexNumber(0, 0);

res.real = n1.real + n2.real;


res.image = n1.image + n2.image;
return res;
}

public static void main(String arg[])


{

ComplexNumber c1 = new ComplexNumber(4, 5);


ComplexNumber c2 = new ComplexNumber(10, 5);
System.out.print("first Complex number: ");
c1.showC();
System.out.print("\nSecond Complex number: ");
c2.showC();
ComplexNumber res = add(c1, c2);
System.out.println("\nAddition is :");
res.showC();
}
}

WAP to find the number is palindrome or not


package javaSchool;
import java.util.Scanner;
public class plaindrome {

public static void main(String args[])


{
Scanner in = new Scanner(System.in);
System.out.print("Input a number: ");
int n = in.nextInt();
int sum = 0, r;
int temp = n;
while(n>0)
{
r = n % 10;
sum = (sum*10)+r;
n = n/10;
}
if(temp==sum)
System.out.println("It is a Palindrome number.");
else
System.out.println("Not a palindrome");
}
}

Count the number of Char and check it is vowel or Consonant


package javaSchool;
import java.util.Scanner;
public class Char {

public static void main(String[] args) {

int i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a character : ");
char ch=sc.next( ).charAt(0);

switch(ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :i++;
}
if(i==1)
System.out.println("Entered character "+ch+" is Vowel");
else
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
System.out.println("Entered character "+ch+" is
Consonent");
else
System.out.println("Not an alphabet");

WAP to find Sum of an average of given number


package javaSchool;
import java.util.Scanner;
public class Arthmetic {

public static void main(String[] args) {

int n, i, sum=0, armean;


int arr[] = new int[50];
Scanner scan = new Scanner(System.in);

System.out.print("How many Number you want to Enter ? ");


n = scan.nextInt();

System.out.print("Enter " +n+ " Numbers : ");


for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
sum = sum + arr[i];
}

armean = sum/n;

System.out.print("Arithmetic Mean = " +armean);


}

WAP to find to delete a file


package project2;
import java.io.File;
public class Filedelete {
public static void main(String[] args)
{
File myObj= new File("C:\\Users\\Saurav Mehto\\eclipse-
workspace\\javaSchool\\src\\javaSchool\\package-info.java");
if(myObj.delete())
{
System.out.println("Deleted the file:" + myObj.getName());
}
else
{
System.out.println("Failed Deleted the file:");
}
}

WAP to calculate the total marks of student and find the result using Swing.*
package javaSchool;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Simple extends JFrame implements ActionListener

JLabel lblPrinciple = new JLabel("Insert Name");

JLabel lblRate = new JLabel("Roll No");

JLabel lblTime = new JLabel("Section");

JLabel lblResult = new JLabel("Marks In HINDI");

JLabel lblResul = new JLabel("Marks In ENGLISH");

JLabel lblResu = new JLabel("Marks In MATHS");

JLabel lblRes = new JLabel("Total");

JLabel lblRe = new JLabel("Result");

JTextField txtPrinciple = new JTextField();

JTextField txtTime = new JTextField();

JTextField txtRate = new JTextField();

JTextField txtResult = new JTextField();

JTextField txtResul = new JTextField();

JTextField txtResu= new JTextField();

JTextField txtRes= new JTextField();

JTextField txtRe= new JTextField();

Simple()

setSize(400,500);

setTitle("STUDENT MARKS");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);
JButton btn = new JButton("PERCENTAGE");

btn.addActionListener(this);

lblPrinciple.setBounds(50,20,600,20);

lblTime.setBounds(50,50,60,20);

lblRate.setBounds(50,80,60,20);

lblResult.setBounds(50,110,600,20);

lblResul.setBounds(50,130,700,20);

lblResu.setBounds(50,150,800,20);

lblRes.setBounds(50,180,900,20);

lblRe.setBounds(50,200,900,20);

txtPrinciple.setBounds(150,20,100,20);

txtTime.setBounds(150,50,100,20);

txtRate.setBounds(150,80,100,20);

txtResult.setBounds(150,110,100,20);

txtResul.setBounds(150,130,100,20);

txtResu.setBounds(150,150,100,20);

txtRes.setBounds(150,180,100,20);

txtRe.setBounds(150,200,100,20);

btn.setBounds(50,230,200,20);

add(lblPrinciple);

add(lblTime);

add(lblRate);

add(lblResult);

add(lblResul);

add(lblResu);

add(lblRes);

add(lblRe);

add(txtPrinciple);
add(txtTime);

add(txtRate);

add(txtResult);

add(txtResul);

add(txtResu);

add(txtRes);

add(txtRe);

add(btn);

setVisible(true);

public void actionPerformed(ActionEvent ae)

int p,t,r;

float res;

p = Integer.parseInt(txtResult.getText());

t = Integer.parseInt(txtResul.getText());

r = Integer.parseInt(txtResu.getText());

res=(p+t+r)/3;

txtRes.setText(String.valueOf(res));

if(res>=50)

{ txtRe.setText(String.valueOf("Pass"));

else

txtRe.setText(String.valueOf("Fail"));

}}

public static void main(String args[])

{
new Simple();

WAP to find to make Calculator using awt.*


package javaSchool;
import java.awt.*;

import java.awt.event.*;

class Calculator implements ActionListener

Frame f=new Frame();

Label l1=new Label("First Number");

Label l2=new Label("Second Number");

Label l3=new Label("Result");

TextField t1=new TextField();

TextField t2=new TextField();

TextField t3=new TextField();

Button b1=new Button("Add");

Button b2=new Button("Sub");

Button b3=new Button("Mul");

Button b4=new Button("Div");

Calculator()

l1.setBounds(50,100,100,20);

l2.setBounds(50,140,100,20);

l3.setBounds(50,180,100,20);

t1.setBounds(200,100,100,20);

t2.setBounds(200,140,100,20);

t3.setBounds(200,180,100,20);

b1.setBounds(50,250,50,20);

b2.setBounds(110,250,50,20);

b3.setBounds(170,250,50,20);

b4.setBounds(230,250,50,20);

f.add(l1);
f.add(l2);

f.add(l3);

f.add(t1);

f.add(t2);

f.add(t3);

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

f.setLayout(null);

f.setVisible(true);

f.setSize(400,350);

public void actionPerformed(ActionEvent e)

float n1=Integer.parseInt(t1.getText());

float n2=Integer.parseInt(t2.getText());

if(e.getSource()==b1)

t3.setText(String.valueOf(n1+n2));

if(e.getSource()==b2)

{
t3.setText(String.valueOf(n1-n2));

if(e.getSource()==b3)

t3.setText(String.valueOf(n1*n2));

if(e.getSource()==b4)

t3.setText(String.valueOf(n1/n2));

}}

public static void main(String...s)

{new Calculator();}}

WAP to find to make Calculator using awt.*


package javaSchool;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Simple extends JFrame implements ActionListener
{

JLabel lblPrinciple = new JLabel("Principle");


JLabel lblRate = new JLabel("Rate");
JLabel lblTime = new JLabel("Time");
JLabel lblResult = new JLabel("Result");

JTextField txtPrinciple = new JTextField();


JTextField txtTime = new JTextField();
JTextField txtRate = new JTextField();
JTextField txtResult = new JTextField();

Simple()
{
setSize(400,500);
setTitle("Simple Intrest");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);

JButton btn = new JButton("Run");


btn.addActionListener(this);

lblPrinciple.setBounds(50,20,60,20);
lblTime.setBounds(50,50,60,20);
lblRate.setBounds(50,80,60,20);
lblResult.setBounds(50,110,60,20);

txtPrinciple.setBounds(150,20,100,20);
txtTime.setBounds(150,50,100,20);
txtRate.setBounds(150,80,100,20);
txtResult.setBounds(150,110,100,20);

btn.setBounds(150,140,100,20);

add(lblPrinciple);
add(lblTime);
add(lblRate);
add(lblResult);

add(txtPrinciple);
add(txtTime);
add(txtRate);
add(txtResult);

add(btn);

setVisible(true);
}

public void actionPerformed(ActionEvent ae)


{
int p,t,r;
float res;
p = Integer.parseInt(txtPrinciple.getText());
t = Integer.parseInt(txtTime.getText());
r = Integer.parseInt(txtRate.getText());
res=(p*t*r)/100;
txtResult.setText(String.valueOf(res));
}

public static void main(String args[])


{
new Simple();
}
}

You might also like