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

Key-Test-1 18SC2009 Object Oriented Programming

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)
100 views25 pages

Key-Test-1 18SC2009 Object Oriented Programming

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
You are on page 1/ 25

Key-Test-1 18SC2009 Object Oriented Programming

1. import java.util.Scanner;

//Recursive or Iterative methods

class Series_Demo

public static void main(String[] args)

System.out.println("Enter any positive integer :");

int n=new Scanner(System.in).nextInt();

new Series_Demo().sum(n);

new Series_Demo().sum1(n);

System.out.print("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+=(Math.pow(i,2));

}
System.out.print("The Sum of Series for "+n+" eiements = "+sum);

long sum2(int n)

long sum=0;

if(n==1) return 1;

else return((long)(Math.pow(n,2))+sum2(n-1));

2. class Command_Demo

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

System.out.println("Student Id ="+id);

System.out.println("Student name ="+name);

System.out.println("Student branch ="+branch);

System.out.println("Student Section ="+sec);

public static void main(String[] args)

//long id= Long.parseLong(args[0]);

//String name=args[1];

//String branch=args[2];
//short sec=Short.parseShort(args[3]);

Command_Demo oc=new Command_Demo();

oc.display(Long.parseLong(args[0]),args[1],args[2],Short.parseShort(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 java.util.Scanner;

class String_swapp

public static void main(String[] args)

{ Scanner sc=new Scanner(System.in);

System.out.print("Enter the first string to be swapped :");

String s1=sc.next();

System.out.print("Enter the first string to be swapped :");

String s2=sc.next();

System.out.print("\nStrings before swapping are string1="+s1+" and string2="+s2);

String_swapp.swap(s1,s2);

System.out.print("\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)Math.pow(n1,n2));

static double powerDouble(double n3,int n4)

return Math.pow(n3,n4);

class Calculate_Demo

public static void main(String[] args)

System.out.print("For Integers "+Calculator.powerInt(10,2));

System.out.print("\nFor Double "+Calculator.powerDouble(2.22,2));

}
}

6. import java.awt.Color;

import java.awt.Font;

import java.awt.*;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

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

l1.setBounds(100, 30, 400, 30);

l2.setBounds(80, 70, 200, 30);

l3.setBounds(80, 110, 200, 30);

tf1.setBounds(300, 70, 200, 30);

p1.setBounds(300, 110, 200, 30);

btn1.setBounds(150, 160, 100, 30);

btn2.setBounds(300, 160, 150, 30);

frame.add(l1);

frame.add(l2);

frame.add(tf1);

frame.add(l3);

frame.add(p1);

frame.add(btn1);

frame.add(btn2);

frame.setSize(400, 400);

frame.setLayout(null);

frame.setVisible(true);

public static void main(String[] args) {

new LoginForm2();

}
}

7. class Company

int cid;

String cname;

void custDetails(int cid,String cname)

{this.cid=cid;this.cname=cname;}

class Service

void display()

System.out.print(" The customer with id "+cid +" and name "+cname +" have
come for servicing");

Service os=new Service();

class Purchase

void display()

System.out.print(" 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();

oc.custDetails(1111,"Jack");

oc.os.display();

Company oc1=new Company();

oc1.custDetails(2222,"Peter");

oc1.op.display();

8. abstract class Shapes

public abstract void edges();

public abstract void faces();

public abstract void vertices();

class Cube extends Shapes

public void edges()

{
System.out.print("edges-cube");

public void faces()

System.out.print("faces-cube");

public void vertices()

System.out.print("vertices-cube");

class Triangle extends Shapes

public void edges()

System.out.print("edges-Triangle");

public void faces()

System.out.print("faces-Triangle");

public void vertices()

System.out.print("vertices-Triangle");

}
}

class Shapes_Demo

public static void main(String[] args)

Shapes os=new Cube();

os.edges();

os.faces();

os.vertices();

9. import java.lang.*;

import java.util.*;

class Book

private String title;

private String authorName;

private double price;

private int yearOfPublication;

public String getTitle() {

return title;

public void setTitle(String title) {

this.title = title;
}

public String getAuthorName() {

return authorName;

public void setAuthorName(String authorName) {

this.authorName = authorName;

public double getPrice() {

return price;

public void setPrice(double price) {

this.price = price;

public int getYearOfPublication() {

return yearOfPublication;

public void setYearOfPublication(int yearOfPublication) {

this.yearOfPublication = yearOfPublication;

public class BookList

public static void main(String args[])


{

Scanner sc=new Scanner(System.in);

System.out.println("Enter how many book details you want to enter");

int n=sc.nextInt();

Book obj[]=new Book[n];

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

System.out.println("Enter Book" +i+"Details");

System.out.println("Enter Book Title");

obj[i].setTitle(sc.next());

System.out.println("Enter Book Author Name");

obj[i].setAuthorName(sc.next());

System.out.println("Enter Book Price");

obj[i].setPrice(sc.nextDouble());

System.out.println("Enter Year of Publication");

obj[i].setYearOfPublication(sc.nextInt());
}

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

System.out.println("Book Details are");

System.out.println("Book " + (i+1) +" Title: " +obj[i].getTitle());

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

System.out.println("Book " + (i+1) +" Price: " +obj[i].getPrice());

System.out.println("Book " + (i+1) +" Year of Publication: "


+obj[i].getYearOfPublication());

10. import java.util.Scanner;

public class Matrix {

Scanner sc;

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

int row, column;

Matrix() {

sc = new Scanner(System.in);
System.out.println("\nEnter number of rows & columns");

row = sc.nextInt();

column = sc.nextInt();

m1 = new int[row][column];

m2 = new int[row][column];

sum = new int[row][column];

System.out.println("Enter the data for first matrix :");

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

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

m1[i][j] = sc.nextInt();

System.out.println("Enter the data for second matrix :");

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

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


m2[i][j] = sc.nextInt();

void printMatrix() {

System.out.println("The First Matrix is :");

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

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

System.out.print("\t"+m1[i][j]);

System.out.println();

System.out.println("The Second Matrix is :");

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

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

System.out.print("\t"+m2[i][j]);
}

System.out.println();

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

System.out.println("The Sum is :");

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

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

System.out.print("\t"+sum[i][j]);

System.out.println();

}
}

public static void main(String args[]) {

Matrix obj = new Matrix();

obj.printMatrix();

obj.addMatrix();

11. class Person

String name;

String gender;

int age;

Person()

name="ABCD";

gender="male";

age=23;

void show()

System.out.println("name="+name);

System.out.println("gender="+gender);
System.out.println("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()

System.out.println("classofstudy="+classofstudy);

System.out.println("collegename="+collegename);

System.out.println("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()

System.out.println("designation="+designation);

System.out.println("compname="+compname);

System.out.println("yearofexp="+yearofexp);

class PersonDemo

public static void main(String args[])

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

s.show();

s.display();

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

e.disp();

}
12. import java.lang.*;

import java.util.*;

class Account

long accNumber;

String accHolderName;

double balance;

public Account() {

this.accNumber = 0;

this.accHolderName = "abc";

this.balance = 0;

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

this.accNumber = accNumber;

this.accHolderName = accHolderName;

this.balance = balance;

public long getAccNumber() {

return accNumber;

public void setAccNumber(long accNumber) {

this.accNumber = accNumber;

public String getAccHolderName() {


return accHolderName;

public void setAccHolderName(String accHolderName) {

this.accHolderName = accHolderName;

public double getBalance() {

return balance;

public void setBalance(double balance) {

this.balance = 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(System.in);


System.out.println("Enter Account Number");

sa_obj.setAccNumber(sc.nextLong());

System.out.println("Enter Account Holder Name");

sa_obj.setAccHolderName(sc.next()+sc.nextLine());

System.out.println("Enter Balance");

sa_obj.setBalance(sc.nextDouble());

while(true)

System.out.println("Choose Option");

System.out.println("1. Withdraw");

System.out.println("2. Deposite");

System.out.println("3. Show Balance");

System.out.println("4. Take Loan");

System.out.println("5. Exit");

int ch=sc.nextInt();

if(ch==1)

System.out.println("Enter Amount to Withdraw: ");

sa_obj.withdraw(sc.nextDouble());

else if(ch==2)

System.out.println("Enter Amount to Deposite: ");


sa_obj.deposit(sc.nextDouble());

else if(ch==3)

System.out.println("Your Balance is: " +sa_obj.balance());

else if(ch==4)

System.out.println("Enter Loan Amount: ");

double principle_amount=sc.nextDouble();

System.out.println("Enter No.of Months: ");

int no_of_months=sc.nextInt();

double monthly_intrest

monthly_intrest =la_obj.getMonthlyInterest(principle_amount,no_of_months);

System.out.println("Your Monthly Intrest is: "+ monthly_intrest);

else if(ch==5)

break;

You might also like