Final Oops - 238

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

PROGRAM:

import java.io.*;
class Employee {

String name;
int age;
String designation;
double salary;

public Employee(String name) {


this.name = name;
}

public void empAge(int empAge) {


age = empAge;
}

public void empDesignation(String empDesig) {


designation = empDesig;
}

public void empSalary(double empSalary) {


salary = empSalary;
}

public void printEmployee() {


System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
}
public class EmployeeTest {

public static void main(String args[]) {


Employee empOne = new Employee("James Smith");
Employee empTwo = new Employee("Mary Anne");

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 {

public int gear;


public int speed;

// the Bicycle class has one constructor


public Bicycle(int gear, int speed)
{
this.gear = gear;
this.speed = speed;
}
// the Bicycle class has three methods
public void applyBrake(int decrement)
{
speed -= decrement;
}

public void speedUp(int increment)


{
speed += increment;
}

// toString() method to print info of Bicycle


public String toString()
{
return ("No of gears are " + gear + "\n"
+ "speed of bicycle is " + speed);
}
}

// derived class
class MountainBike extends Bicycle {

// the MountainBike subclass adds one more field


public int seatHeight;

// the MountainBike subclass has one constructor


public MountainBike(int gear, int speed,
int startHeight)
{
// invoking base-class(Bicycle) constructor
super(gear, speed);
seatHeight = startHeight;
}

// the MountainBike subclass adds one more method


public void setHeight(int newValue)
{
seatHeight = newValue;
}
// overriding toString() method
// of Bicycle to print more info
@Override public String toString()
{
return (super.toString() + "\nseat height is "
+ seatHeight);
}
}

// driver class
public class Test {
public static void main(String args[])
{

MountainBike mb = new MountainBike(3, 100, 25);


System.out.println(mb.toString());
}
}

OUTPUT:
PROGRAM:

import java.util.Scanner; interface Polygon {


void getArea();
default void getSides() {
System.out.println("It has 4 sides");
}}
class Rectangle implements Polygon { public void getArea() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter lenghth and breadth :");
int length = sc.nextInt();
int breadth = sc.nextInt(); int area = length * breadth;
System.out.println("The area of the rectangle is " + area);
}
public void getSides() { System.out.println("It has 4 sides.");
}
}

class Square implements Polygon { public void getArea() {


Scanner sc = new Scanner (System.in);
System.out.println("Enter the side of a square"); int length = sc.nextInt();
int area = length * length;
System.out.println("The area of the square is " + area);
}
}
class Main {
public static void main(String[] args) { Rectangle r1 = new Rectangle(); r1.getArea();
r1.getSides();
Square s1 = new Square(); s1.getArea(); s1.getSides();
}
}

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:

abstract class Employee{


abstract int getSalary();
}
class Developer extends Employee{ private final int salary;
public Developer(int s) {
salary = s;
}
int getSalary() {
return salary;
}
}

class Driver extends Employee{ private final int salary; public Driver(int t) {
salary = t;

}
int getSalary() {
return salary;

}
}

class help{

public static void main(String[] args) {


Developer d1 = new Developer(7000);
Driver d2 = new Driver(2000);
int a, b;
a = d1.getSalary();
b = d2.getSalary();
System.out.println("Salary of developer :" + a);
System.out.println("Salary of Driver :" + b);
}}

OUTPUT:
PROGRAM:
import java.util.Arrays;
import java.util.Scanner;

public class ABC {


public static void main(String args[]) {
int[] myArray = {897, 56, 78, 90, 12, 123, 75};
System.out.println("Elements in the array are:: ");
System.out.println(Arrays.toString(myArray));
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index of the required element ::");
try {
int element = sc.nextInt();
System.out.println("Element in the given index is :: "+myArray[element]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println(e);
System.out.println();
}
}
}

OUTPUT:
PROGRAM:

public class ARE


{
void divide(int a, int b)
{
int res;
try
{
// performing divison and storing th result
res = a / b;
System.out.println("Division process has been done successfully.");
System.out.println("Result came after division is: " + res);
}
// handling the exception in the catch block
catch(java.lang.ArithmeticException ex)
{
System.out.println( ex);
}

// 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:

public class FileNotFoundExceptionExample {


public static void main(String args[]) {
BufferedReader br = null;

try {
br = new BufferedReader(new FileReader("myfile.txt"));
String data = null;

while ((data = br.readLine()) != null) {


System.out.println(data);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}

OUTPUT:
PROGRAM:

public class StringBufferCapacityExample1 {


public static void main(String[] args) {
StringBuffer sb = new StringBuffer("string buffer");

System.out.println("capacity: " + sb.capacity());


System.out.println("length: "+sb.length());

}
}

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

//passing substring with from index


int index3=s1.indexOf("is",4);//returns the index of is substring after 4th index
System.out.println("next index of is - "+index3);//5 i.e. the index of another is

//passing char value


int index4=s1.indexOf('s');//returns the index of s char value
System.out.println("index of char value 's'"+index4);//3
}}

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:

public class console


{
public static void main(String[] args)
{
System.out.println("Enter the input string");
String name = System.console().readLine();
System.out.println("You entered: " + name);
}
}

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.*;

public class FileExample


{
public static void main(String[] args)
{
String sourceFile, destFile, line, content="";
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Name of Source File: ");
sourceFile = scan.nextLine();
try{
FileReader fr =new FileReader("C:\\Users\\Senth\\OneDrive\\Desktop\\"+sourceFile);
BufferedReader br = new BufferedReader(fr);

for(line=br.readLine(); line!=null; line=br.readLine())


content = content + line + "\n";

br.close();

System.out.print("Enter the Name of Destination File: ");


destFile = scan.nextLine();

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

System.out.println("Elements in Stack: " + stk);


stk.pop();
System.out.println("Elements in Stack: " + stk);
stk.pop();
System.out.println("Elements in Stack: " + stk);
System.out.println("Top Element in Stack: " + stk.peek());
System.out.println("Elements in Stack: " + stk);
}
}

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");
}
}
}

public class Multithread {


public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n;
System.out.print("enter n:");
n = sc.nextInt(); //no.of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}
}
}

OUTPUT:
PROGRAM:

import java.util.Arrays;
import java.util.List;

public class GenericsTester {

public static double sum(List<? extends Number> numberlist) {


double sum = 0.0;
for (Number n : numberlist) sum += n.doubleValue();
return sum;
}

public static void main(String args[]) {


List<Integer> integerList = Arrays.asList(1, 2, 3);
System.out.println("sum = " + sum(integerList));

List<Double> doubleList = Arrays.asList(1.2, 2.3, 3.5);


System.out.println("sum = " + sum(doubleList));
}
}

OUTPUT:
PROGRAM:
import java.util.Arrays;

public class BubbleSortGeneric<T extends Comparable<? super T>> {


T[] array;
BubbleSortGeneric(T[] array){
this.array = array;
}

private T[] bubbleSort(){


for(int i = array.length; i > 1; i--){
for(int j = 0; j < i - 1; j++){
//if greater swap elements
if(array[j].compareTo(array[j+1]) > 0){
swapElements(j, array);
}
}
}
return array;
}
private void swapElements(int index, T[] arr){
T temp = arr[index];
arr[index] = arr[index+1];
arr[index+1] = temp;
}
public static void main(String[] args) {
Integer[] intArr = {47, 85, 62, 34, 7, 10, 92, 106, 2, 54};
System.out.println("input array- " + Arrays.toString(intArr));
BubbleSortGeneric<Integer> bsg1 = new BubbleSortGeneric<Integer>(intArr);
Integer[] sa1 = bsg1.bubbleSort();
System.out.println("Sorted array- " + Arrays.toString(sa1));

}
}

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.*;

class Login extends JFrame implements ActionListener


{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);

label2 = new JLabel();


label2.setText("Password:");
text2 = new JPasswordField(15);

SUBMIT=new JButton("SUBMIT");

panel=new JPanel(new GridLayout(3,1));


panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("OOPS-RECORD") && value2.equals("will smith")) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("LOGIN SUCCESSFUL");
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(600,200);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}

import javax.swing.*;
import java.awt.*;

class NextPage extends JFrame


{
NextPage()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}
}

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

static double num1=0,num2=0,result=0;


static int operator=0;

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);

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==btn1)
t.setText(t.getText().concat("1"));

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;

default: System.out.println("Enter valid operator");


}
// switch case end
t.setText(""+result);
}

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));
}
}

// main of the program where we call.


public static void main(String...s)
{
Calculator obj = new Calculator();

}
}

OUTPUT:

6*2

You might also like