Final Oops - 238
Final Oops - 238
Final Oops - 238
import java.io.*;
class Employee {
String name;
int age;
String designation;
double salary;
empOne.empAge(26);
empOne.empDesignation("Senior Software Engineer");
empOne.empSalary(1000);
empOne.printEmployee();
empTwo.empAge(21);
empTwo.empDesignation("Software Engineer");
empTwo.empSalary(500);
empTwo.printEmployee();
}
}
OUTPUT:
PROGRAM:
class Bicycle {
// derived class
class MountainBike extends Bicycle {
// driver class
public class Test {
public static void main(String args[])
{
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
F1.java
package pack1;
import pack2.*;
class F1{
public static void main(String[] args){
E obj=new E(12,4);
obj.fun();
}}
E.java
package pack2;
public class E{
int a;
int b;
public E(int a,int b){
this.a=a;
this.b=b;}
public void fun(){
System.out.println("Addition of 12 and 4 is:"+(a+b));
System.out.println("Subtraction of 12 and 4 is:"+(a-b));
System.out.println("Multiplication of 12 and 4 is:"+(a*b));
System.out.println("Division of 12 and 4 is:"+(a/b));
System.out.println("Remainder of 12 and 4 is:"+(a%b));
}
}
OUTPUT:
PROGRAM:
class Driver extends Employee{ private final int salary; public Driver(int t) {
salary = t;
}
int getSalary() {
return salary;
}
}
class help{
OUTPUT:
PROGRAM:
import java.util.Arrays;
import java.util.Scanner;
OUTPUT:
PROGRAM:
// main method
public static void main(String argvs[])
{
// creating an object of the class ArithmeticException
ARE obj = new ARE();
obj.divide(1, 0);
}
}
OUTPUT:
PROGRAM:
try {
br = new BufferedReader(new FileReader("myfile.txt"));
String data = null;
OUTPUT:
PROGRAM:
}
}
OUTPUT:
PROGRAM:
class helloq{
public static void main(String args[]){
String s1="this is index of example";
//passing substring
int index1=s1.indexOf("is");//returns the index of is substring
int index2=s1.indexOf("index");//returns the index of index substring
System.out.println("is - found at:"+index1+" "+"index - found at:"+index2);//2 8
OUTPUT:
PROGRAM:
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
System.out.println("delete :"+sb.delete(1,3));
System.out.println("replace :"+sb.replace(1,3,"Java"));
System.out.println("insert :"+sb.insert(1,"Java"));
System.out.println("length :"+sb.length());
System.out.println("reverse :"+sb.reverse());
}
}
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
import java.io.FileReader;
public class ReadFile {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("C:\\Users\\Senth\\OneDrive\\Desktop\\inputfile.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
File content:
OUTPUT:
PROGRAM:
import java.util.Scanner;
import java.io.*;
br.close();
try{
FileWriter fw = new FileWriter("C:\\Users\\Senth\\OneDrive\\Desktop\\"+destFile);
fw.write(content);
fw.close();
System.out.println("\nFile copied successfully!");
}
catch(IOException ioe)
{
System.out.println("\nSomething went wrong!");
System.out.println("Exception: " +ioe);
}}
catch(IOException ioe)
{
System.out.println("\nSomething went wrong!");
System.out.print("Exception: " +ioe);
}}}
OUTPUT:
COPIED FILE CONTENT:
PROGRAM:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class coll
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> age = new ArrayList<>();
int n = sc.nextInt();
for(int i=0;i<n;i++){
age.add(sc.nextInt());
}
Collections.sort(age);
System.out.println("sorted array list:");
for(Integer i : age){
System.out.println(i);
}}}
OUTPUT:
PROGRAM:
import java.util.*;
class helloqwe{
public static void main(String args[]) {
// Declaring a HashSet
HashSet<String>hashset = new HashSet<String>();
// Add elements to HashSet
hashset.add("Book");
hashset.add("Java");
hashset.add("Practice");
hashset.add("Practice");
hashset.add("Example");
// Get iterator
Iterator<String> it = hashset.iterator();
// Show HashSet elements
System.out.println("HashSet contains: ");
while(it.hasNext()) {
System.out.println(it.next());
}
}}
OUTPUT:
PROGRAM:
import java.util.*;
public class StackExample {
public static void main(String[] args)
{
//creating an instance of Stack class
Stack<Integer>stk= new Stack<>();
// checking stack is empty or not
boolean result = stk.empty();
System.out.println("Is the stack empty? " + result);
// pushing elements into stack
stk.push(789);
stk.push(1136);
stk.push(190);
stk.push(1200);
//prints elements of the stack
OUTPUT:
PROGRAM:
import java.util.*;
class MultithreadingDemo extends Thread {
public void run()
{
try {
System.out.println("Thread " + Thread.currentThread().getId()+ " is running");
}
catch (Exception e) {
System.out.println("Exception is caught");
}
}
}
OUTPUT:
PROGRAM:
import java.util.Arrays;
import java.util.List;
OUTPUT:
PROGRAM:
import java.util.Arrays;
}
}
OUTPUT:
PROGRAM:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CharCount extends JFrame implements ActionListener{
JLabel lb1,lb2;
JTextArea ta;
JButton b;
JButton pad,text;
CharCount(){
super("Char Word Count Tool ");
lb1=new JLabel("Characters: ");
lb1.setBounds(50,50,100,20);
lb2=new JLabel("Words: ");
lb2.setBounds(50,80,100,20);
ta=new JTextArea();
ta.setBounds(50,110,300,200);
b=new JButton("click");
b.setBounds(50,320, 80,30);//x,y,w,h
b.addActionListener(this);
add(lb1);add(lb2);add(ta);add(b);
setSize(400,400);
setLayout(null);//using no layout manager
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);//be like file chooser
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b){
String text=ta.getText();
lb1.setText("Characters: "+text.length());
String words[]=text.split("\\s");
lb2.setText("Words: "+words.length);
}}
public static void main(String[] args) {
new CharCount();
}}
OUTPUT:
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
SUBMIT=new JButton("SUBMIT");
import javax.swing.*;
import java.awt.*;
OUTPUT:
PROGRAM:
import javax.swing.*;
import java.awt.event.*;
// create class
class Calculator implements ActionListener
{
// f = Frame, T = TextField, btn = button
// i.e btn1 means Button 1
JFrame f;
JTextField t;
JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0;
JButton odiv,omul,osub,oadd,btneq,btndel,btnclr;
// initililze variables
Calculator()
{
f=new JFrame("Calculator");
t=new JTextField();
btn1=new JButton("1");
btn2=new JButton("2");
btn3=new JButton("3");
btn4=new JButton("4");
btn5=new JButton("5");
btn6=new JButton("6");
btn7=new JButton("7");
btn8=new JButton("8");
btn9=new JButton("9");
btn0=new JButton("0");
odiv=new JButton("/");
omul=new JButton("*");
osub=new JButton("-");
oadd=new JButton("+");
btneq=new JButton("=");
btndel=new JButton("Delete");
btnclr=new JButton("Clear");
t.setBounds(30,40,280,30);
btn7.setBounds(40,100,50,40);
btn8.setBounds(110,100,50,40);
btn9.setBounds(180,100,50,40);
odiv.setBounds(250,100,50,40);
btn4.setBounds(40,170,50,40);
btn5.setBounds(110,170,50,40);
btn6.setBounds(180,170,50,40);
omul.setBounds(250,170,50,40);
btn1.setBounds(40,240,50,40);
btn2.setBounds(110,240,50,40);
btn3.setBounds(180,240,50,40);
osub.setBounds(250,240,50,40);
btn0.setBounds(60,310,50,40);
btneq.setBounds(140,310,50,40);
oadd.setBounds(220,310,50,40);
btndel.setBounds(60,380,80,40);
btnclr.setBounds(170,380,80,40);
f.add(t);
f.add(btn7);
f.add(btn8);
f.add(btn9);
f.add(odiv);
f.add(btn4);
f.add(btn5);
f.add(btn6);
f.add(omul);
f.add(btn1);
f.add(btn2);
f.add(btn3);
f.add(osub);
f.add(btn0);
f.add(btneq);
f.add(oadd);
f.add(btndel);
f.add(btnclr);
f.setLayout(null);
f.setVisible(true);
f.setSize(350,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn0.addActionListener(this);
oadd.addActionListener(this);
odiv.addActionListener(this);
omul.addActionListener(this);
osub.addActionListener(this);
btneq.addActionListener(this);
btndel.addActionListener(this);
btnclr.addActionListener(this);
if(e.getSource()==btn2)
t.setText(t.getText().concat("2"));
if(e.getSource()==btn3)
t.setText(t.getText().concat("3"));
if(e.getSource()==btn4)
t.setText(t.getText().concat("4"));
if(e.getSource()==btn5)
t.setText(t.getText().concat("5"));
if(e.getSource()==btn6)
t.setText(t.getText().concat("6"));
if(e.getSource()==btn7)
t.setText(t.getText().concat("7"));
if(e.getSource()==btn8)
t.setText(t.getText().concat("8"));
if(e.getSource()==btn9)
t.setText(t.getText().concat("9"));
if(e.getSource()==btn0)
t.setText(t.getText().concat("0"));
if(e.getSource()==oadd)
{
num1=Double.parseDouble(t.getText());
operator=1;
t.setText("");
}
if(e.getSource()==osub)
{
num1=Double.parseDouble(t.getText());
operator=2;
t.setText("");
}
if(e.getSource()==omul)
{
num1=Double.parseDouble(t.getText());
operator=3;
t.setText("");
}
if(e.getSource()==odiv)
{
num1=Double.parseDouble(t.getText());
operator=4;
t.setText("");
}
if(e.getSource()==btneq)
{
num2=Double.parseDouble(t.getText());
// switch case check and perform operator according to input
switch(operator)
{
case 1: result=num1+num2;
break;
case 2: result=num1-num2;
break;
case 3: result=num1*num2;
break;
case 4: result=num1/num2;
break;
if(e.getSource()==btnclr)
t.setText("");
if(e.getSource()==btndel)
{
String s=t.getText();
t.setText("");
for(int i=0;i<s.length()-1;i++)
t.setText(t.getText()+s.charAt(i));
}
}
}
}
OUTPUT:
6*2