Java Practicals 60 1
Java Practicals 60 1
Submitted By - Roll No - 60
Name of the Student - Rane Shivam Ghanshyam
INDEX
Sr ProgramName Date Remark Sign
.
1 Write Java Program to accept two numbers.
Though Command & display addition of them.
2 write a Java program to accept a number through
Command line & find reserve of that number
16/(2*a - a * b)
class Student {
int rollNo;
String name, className;
Scanner scanner = new Scanner(System.in);
public class P3 {
public static void main(String[] args) {
var student1 = new Student();
var student2 = new Student();
var student3 = new Student();
student1.accept();
student2.accept();
student3.accept();
student1.display();
student2.display();
student3.display();
}
}
Q4 Write Java program For Student with data member will no. name &
class use parameterize Constructor to initialize three object on that
Class Write a display method to display detail's of the student
class Student {
int rollNo;
String name, className;
student1.display();
student2.display();
student3.display();
}
}
Q5 Write a program to define Class employee with data member name
& salary create an array object for employee Class which Store the
information from s employee & add display them accordingly
import java.util.Scanner;
class Employee {
int salary;
String name;
Scanner scanner = new Scanner(System.in);
public class P5 {
public static void main(String[] args) {
Employee[] emp = new Employee[5];
class Number {
int n;
public class P6 {
public static void main(String[] args) {
Number no1 = new Number();
no1.accept();
no1.display();
}
}
Employee() {}
Employee(int id, String name, int salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
Manager() {}
Manager(int id, String name, int salary) {
super(id, name, salary);
}
public void setBonus(float bonus) { this.bonus =
bonus; }
public float getBonus() { return bonus; }
m1.display();
m2.setId(2);
m2.setName("Style Bender");
m2.setSalary(999_999);
m2.display();
}
}
public class P8 {
public static void main(String[] args) {
Number no = new Number();
no.n = 6;
no.display();
no.display(3, "Hola Amigos");
no.display(4, "Hello", "Bienvenido");
}
}
π with value 3:14 create a class circle with. radius as data member
class calendar with radius height as data member this classes with
implement the value above created interface & calculate the aero and
Volume appropriately On object base on shape of on object
interface Operation{
float PI = 3.142f;
void Area();
void Volume();
}
public class P9 {
public static void main(String[] args) {
var c1 = new Circle();
c1.radius = 5;
c1.Area();
Q10 write Java program to handle exception two integer From user
and perform arithmetic operation like multiplication on division Find
the result of mathematical Expression
16/(2*a - a * b)
import java.util.InputMismatchException;
import java.util.Scanner;
try {
int result = division(a, b);
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: "
+ e.getMessage());
}
}
}
Q13 write a Java program to read n strings into array list & display it
in reverse order
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
3)Display 2nd list with color names white § black I insert least at given
position in the first list
import java.util.*;
colorList.add("red");
colorList.add("blue");
colorList.add("yellow");
colorList.add("orange");
secondList.add("white");
secondList.add("black");
ListIterator<String> i =
colorList.listIterator();
while (i.hasNext()) {
System.out.println(i.next());
}
while (i.hasPrevious()) {
System.out.println(i.previous());
}
Q16 Write a Java program to accept 'n' numbers from user & store
them in appropriately
Collection.
Condition:
Q17 Write a Java program to display current date & time using
various formats.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
System.out.println(java.time.Clock.systemUTC().instant
());
System.out.println(new java.util.Date());
DateTimeFormatter dff =
DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime dt = LocalDateTime.now();
System.out.println(dff.format(dt));
}
}
v1.addElement("A");
v1.addElement("B");
v1.addElement("C");
System.out.println(v1);
System.out.println(v1.get(1));
if (v1.contains("C")) {
System.out.println("Vector Contains C");
} else System.out.println("Vector doesn't
contains C");
System.out.println("Size of Vector is " +
v1.size());
v1.setSize(5);
System.out.println(v1);
v1.removeElement("C");
System.out.println(v1);
}
}
Q19 write a Java program to match the value entered value entered
by the user account against the problem.
1) mobile number
2) E mail
3) MCA Registration
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
if (parse("\\d{10}", phoneNo)) {
System.out.println("Phone number is
valid");
} else System.out.println("Invalid Phone
Number");
if (parse("^[A-Z0-9]+@[A-Z]+\\.[A-Z]{2,6}$",
email)) {
System.out.println("Email is valid");
} else System.out.println("Invalid Email");
if (parse("^[a-zA-Z]{3,10}", username)) {
System.out.println("Username is valid");
} else System.out.println("Invalid Username");
if (parse("^[a-zA-Z0-9.@$_]{3,12}", password))
{
System.out.println("Password is valid");
} else System.out.println("Invalid Password");
}
}
f1.setLayout(null);
f1.setVisible(true);
}
}
@Override
public void actionPerformed(ActionEvent
actionEvent) {
if
(actionEvent.getActionCommand().equals("red"))
setBackground(new Color(255, 0, 0));
if
(actionEvent.getActionCommand().equals("green"))
setBackground(new Color(0, 255, 0));
if
(actionEvent.getActionCommand().equals("blue"))
setBackground(new Color(0, 0, 255));
}
}
public class P21 {
public static void main(String[] args) {
frameObj.add(b1);
frameObj.add(b2);
frameObj.add(b3);
frameObj.add(b4);
frameObj.add(b5);
frameObj.add(b6);
frameObj.setLayout(new FlowLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
}
public class P25 {
public static void main(String[] args) {
new FlowLayoutExample();
}
}
Q26 Write a Java program using Card layout.
import java.awt.*;
import java.awt.event.*;
class CardLayoutExample extends Frame implements
ActionListener {
CardLayout card = new CardLayout(20, 20);
CardLayoutExample() {
setLayout(card);
Button btnFirst = new Button("First");
Button btnSecond = new Button("Second");
Button btnThird = new Button("Third");
add(btnFirst, "Card 1");
add(btnSecond, "Card 2");
add(btnThird, "Card 3");
btnFirst.addActionListener(this);
btnSecond.addActionListener(this);
btnThird.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent
actionEvent) {
card.next(this);
}
}
public class P26 {
public static void main(String[] args) {
CardLayoutExample frame = new
CardLayoutExample();
frame.setSize(220, 150);
frame.setResizable(false);
frame.setVisible(true);
frame.setTitle("Card Layout");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Q27 Write a Java program to create 3 threads will point different table
of numbers
class A extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("\tFrom thread A: i = "
+ i * 2);
}
System.out.println("exit from A");
}
}