0% found this document useful (0 votes)
171 views25 pages

Java Object-Oriented Programming Examples

The document contains 10 code examples demonstrating various object-oriented programming concepts in Java, including: 1. Defining recursive and iterative methods to calculate series sums. 2. Parsing command line arguments and passing them to a method. 3. Explanations of final classes, methods, and variables. 4. Swapping string values by passing them to a static method. 5. Defining static methods in one class and calling them from another class. 6. Creating a simple login form using Swing components. 7. Defining inner classes to represent different services provided by a company. 8. Implementing abstract methods in subclasses to represent shapes. 9. Defining a Book class with

Uploaded by

priya uppala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views25 pages

Java Object-Oriented Programming Examples

The document contains 10 code examples demonstrating various object-oriented programming concepts in Java, including: 1. Defining recursive and iterative methods to calculate series sums. 2. Parsing command line arguments and passing them to a method. 3. Explanations of final classes, methods, and variables. 4. Swapping string values by passing them to a static method. 5. Defining static methods in one class and calling them from another class. 6. Creating a simple login form using Swing components. 7. Defining inner classes to represent different services provided by a company. 8. Implementing abstract methods in subclasses to represent shapes. 9. Defining a Book class with

Uploaded by

priya uppala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Key-Test-1 18SC2009 Object Oriented Programming

1. import [Link];

//Recursive or Iterative methods

class Series_Demo

public static void main(String[] args)

[Link]("Enter any positive integer :");

int n=new Scanner([Link]).nextInt();

new Series_Demo().sum(n);

new Series_Demo().sum1(n);

[Link]("The Sum of Series for "+n+" eiements = "+new


Series_Demo().sum2(n));

void sum1(int n)

long sum=0;

for(int i=1;i<=n;i++)

sum+=([Link](i,2));

}
[Link]("The Sum of Series for "+n+" eiements = "+sum);

long sum2(int n)

long sum=0;

if(n==1) return 1;

else return((long)([Link](n,2))+sum2(n-1));

2. class Command_Demo

void display(long id,String name,String branch,short sec)

[Link]("Student Id ="+id);

[Link]("Student name ="+name);

[Link]("Student branch ="+branch);

[Link]("Student Section ="+sec);

public static void main(String[] args)

//long id= [Link](args[0]);

//String name=args[1];

//String branch=args[2];
//short sec=[Link](args[3]);

Command_Demo oc=new Command_Demo();

[Link]([Link](args[0]),args[1],args[2],[Link](args[3]));

3. If a class is final, it cannot be inherited. Methods become final but the instance Variables are not
final.

If a variable is final must be initialized at the time of declaration and cannot be modified.

If a method is final it cannot be overridden.

4. import [Link];

class String_swapp

public static void main(String[] args)

{ Scanner sc=new Scanner([Link]);

[Link]("Enter the first string to be swapped :");

String s1=[Link]();

[Link]("Enter the first string to be swapped :");

String s2=[Link]();

[Link]("\nStrings before swapping are string1="+s1+" and string2="+s2);

String_swapp.swap(s1,s2);

[Link]("\nStrings after swapping are string1="+s1+" and string2="+s2);

static void swap(String s1,String s2)


{

String temp=s1;

s1=s2;

s2=temp;

5.

class Calculator

static int powerInt(int n1,int n2)

return ((int)[Link](n1,n2));

static double powerDouble(double n3,int n4)

return [Link](n3,n4);

class Calculate_Demo

public static void main(String[] args)

[Link]("For Integers "+[Link](10,2));

[Link]("\nFor Double "+[Link](2.22,2));

}
}

6. import [Link];

import [Link];

import [Link].*;

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

class LoginForm2 extends JFrame {

JLabel l1, l2, l3;

JTextField tf1;

JButton btn1,btn2;

JPasswordField p1;

LoginForm2() {

JFrame frame = new JFrame("Employee Login Form");

l1 = new JLabel("");

l2 = new JLabel("Employee Id");

l3 = new JLabel("Password");

tf1 = new JTextField();

p1 = new JPasswordField();

btn1 = new JButton("Login");


btn2 = new JButton("Forgot Password");

[Link](100, 30, 400, 30);

[Link](80, 70, 200, 30);

[Link](80, 110, 200, 30);

[Link](300, 70, 200, 30);

[Link](300, 110, 200, 30);

[Link](150, 160, 100, 30);

[Link](300, 160, 150, 30);

[Link](l1);

[Link](l2);

[Link](tf1);

[Link](l3);

[Link](p1);

[Link](btn1);

[Link](btn2);

[Link](400, 400);

[Link](null);

[Link](true);

public static void main(String[] args) {

new LoginForm2();

}
}

7. class Company

int cid;

String cname;

void custDetails(int cid,String cname)

{[Link]=cid;[Link]=cname;}

class Service

void display()

[Link](" The customer with id "+cid +" and name "+cname +" have
come for servicing");

Service os=new Service();

class Purchase

void display()

[Link](" The customer with id "+cid +" and name "+cname +" have
come for purchase");

Purchase op=new Purchase();

}
class Company_Demo

public static void main(String[] args)

Company oc=new Company();

[Link](1111,"Jack");

[Link]();

Company oc1=new Company();

[Link](2222,"Peter");

[Link]();

8. abstract class Shapes

public abstract void edges();

public abstract void faces();

public abstract void vertices();

class Cube extends Shapes

public void edges()

{
[Link]("edges-cube");

public void faces()

[Link]("faces-cube");

public void vertices()

[Link]("vertices-cube");

class Triangle extends Shapes

public void edges()

[Link]("edges-Triangle");

public void faces()

[Link]("faces-Triangle");

public void vertices()

[Link]("vertices-Triangle");

}
}

class Shapes_Demo

public static void main(String[] args)

Shapes os=new Cube();

[Link]();

[Link]();

[Link]();

9. import [Link].*;

import [Link].*;

class Book

private String title;

private String authorName;

private double price;

private int yearOfPublication;

public String getTitle() {

return title;

public void setTitle(String title) {

[Link] = title;
}

public String getAuthorName() {

return authorName;

public void setAuthorName(String authorName) {

[Link] = authorName;

public double getPrice() {

return price;

public void setPrice(double price) {

[Link] = price;

public int getYearOfPublication() {

return yearOfPublication;

public void setYearOfPublication(int yearOfPublication) {

[Link] = yearOfPublication;

public class BookList

public static void main(String args[])


{

Scanner sc=new Scanner([Link]);

[Link]("Enter how many book details you want to enter");

int n=[Link]();

Book obj[]=new Book[n];

for(int i=0;i<n;i++)

[Link]("Enter Book" +i+"Details");

[Link]("Enter Book Title");

obj[i].setTitle([Link]());

[Link]("Enter Book Author Name");

obj[i].setAuthorName([Link]());

[Link]("Enter Book Price");

obj[i].setPrice([Link]());

[Link]("Enter Year of Publication");

obj[i].setYearOfPublication([Link]());
}

for(int i=0;i<n;i++)

[Link]("Book Details are");

[Link]("Book " + (i+1) +" Title: " +obj[i].getTitle());

[Link]("Book " + (i+1) +" Author Name: " +obj[i].getAuthorName());

[Link]("Book " + (i+1) +" Price: " +obj[i].getPrice());

[Link]("Book " + (i+1) +" Year of Publication: "


+obj[i].getYearOfPublication());

10. import [Link];

public class Matrix {

Scanner sc;

int m1[][], m2[][], sum[][];

int row, column;

Matrix() {

sc = new Scanner([Link]);
[Link]("\nEnter number of rows & columns");

row = [Link]();

column = [Link]();

m1 = new int[row][column];

m2 = new int[row][column];

sum = new int[row][column];

[Link]("Enter the data for first matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

m1[i][j] = [Link]();

[Link]("Enter the data for second matrix :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {


m2[i][j] = [Link]();

void printMatrix() {

[Link]("The First Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

[Link]("\t"+m1[i][j]);

[Link]();

[Link]("The Second Matrix is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

[Link]("\t"+m2[i][j]);
}

[Link]();

void addMatrix() {

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

sum[i][j] = m1[i][j] + m2[i][j];

[Link]("The Sum is :");

for(int i=0; i<row; i++) {

for(int j=0; j<column; j++) {

[Link]("\t"+sum[i][j]);

[Link]();

}
}

public static void main(String args[]) {

Matrix obj = new Matrix();

[Link]();

[Link]();

11. class Person

String name;

String gender;

int age;

Person()

name="ABCD";

gender="male";

age=23;

void show()

[Link]("name="+name);

[Link]("gender="+gender);
[Link]("age="+age);

class Student extends Person

String classofstudy;

String collegename;

int yearofstudy;

Student(String costudy,String colname,int yostudy)

classofstudy=costudy;

collegename=colname;

yearofstudy=yostudy;

void display()

[Link]("classofstudy="+classofstudy);

[Link]("collegename="+collegename);

[Link]("yearofstudy="+yearofstudy);

class Employee extends Person

String designation;

String compname;
int yearofexp;

Employee(String des,String coname,int yoexp)

designation=des;

compname=coname;

yearofexp=yoexp;

void disp()

[Link]("designation="+designation);

[Link]("compname="+compname);

[Link]("yearofexp="+yearofexp);

class PersonDemo

public static void main(String args[])

Student s=new Student("s1","klef",2);

[Link]();

[Link]();

Employee e=new Employee("tester","ibm",10);

[Link]();

}
12. import [Link].*;

import [Link].*;

class Account

long accNumber;

String accHolderName;

double balance;

public Account() {

[Link] = 0;

[Link] = "abc";

[Link] = 0;

public Account(long accNumber, String accHolderName, double balance) {

[Link] = accNumber;

[Link] = accHolderName;

[Link] = balance;

public long getAccNumber() {

return accNumber;

public void setAccNumber(long accNumber) {

[Link] = accNumber;

public String getAccHolderName() {


return accHolderName;

public void setAccHolderName(String accHolderName) {

[Link] = accHolderName;

public double getBalance() {

return balance;

public void setBalance(double balance) {

[Link] = balance;

class SavingsAccount extends Account

void withdraw(double amount){

balance=balance-amount;

void deposit(double amount){

balance=balance+amount;

double balance(){

return balance;

}
class LoanAccount extends Account

double annualInterestRate;

LoanAccount()

annualInterestRate=8.5;

double getMonthlyInterestRate(){

return annualInterestRate/12.00;

double getMonthlyInterest(double principle_amount,int no_of_months){

return ((principle_amount*no_of_months*getMonthlyInterestRate())/100.00);

public class BankMain

public static void main(String args[])

SavingsAccount sa_obj= new SavingsAccount();

LoanAccount la_obj=new LoanAccount();

Scanner sc=new Scanner([Link]);


[Link]("Enter Account Number");

sa_obj.setAccNumber([Link]());

[Link]("Enter Account Holder Name");

sa_obj.setAccHolderName([Link]()+[Link]());

[Link]("Enter Balance");

sa_obj.setBalance([Link]());

while(true)

[Link]("Choose Option");

[Link]("1. Withdraw");

[Link]("2. Deposite");

[Link]("3. Show Balance");

[Link]("4. Take Loan");

[Link]("5. Exit");

int ch=[Link]();

if(ch==1)

[Link]("Enter Amount to Withdraw: ");

sa_obj.withdraw([Link]());

else if(ch==2)

[Link]("Enter Amount to Deposite: ");


sa_obj.deposit([Link]());

else if(ch==3)

[Link]("Your Balance is: " +sa_obj.balance());

else if(ch==4)

[Link]("Enter Loan Amount: ");

double principle_amount=[Link]();

[Link]("Enter [Link] Months: ");

int no_of_months=[Link]();

double monthly_intrest

monthly_intrest =la_obj.getMonthlyInterest(principle_amount,no_of_months);

[Link]("Your Monthly Intrest is: "+ monthly_intrest);

else if(ch==5)

break;

You might also like